71214: TopCities & TopCountries usage report tests

This commit is contained in:
Marie Verdonck
2020-06-11 12:50:02 +02:00
parent 2b0a3664ca
commit 1bf46bca89
2 changed files with 235 additions and 52 deletions

View File

@@ -16,6 +16,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse; import com.maxmind.geoip2.model.CityResponse;
@@ -66,8 +67,10 @@ public class MockSolrLoggerServiceImpl
* @return faked CityResponse * @return faked CityResponse
*/ */
private CityResponse mockCityResponse() { private CityResponse mockCityResponse() {
List<String> cityNames = new ArrayList<>(Collections.singleton("New York")); List<String> cityLocales = new ArrayList<String>(Collections.singleton("en"));
City city = new City(cityNames, 1, 1, new HashMap()); Map<String, String> cityNames = new HashMap<>();
cityNames.put("en", "New York");
City city = new City(cityLocales, 1, 1, cityNames);
List<String> countryNames = new ArrayList<>(Collections.singleton("United States")); List<String> countryNames = new ArrayList<>(Collections.singleton("United States"));
Country country = new Country(countryNames, 1, 1, "US", new HashMap()); Country country = new Country(countryNames, 1, 1, "US", new HashMap());

View File

@@ -28,6 +28,8 @@ import java.util.UUID;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.matcher.UsageReportMatcher; import org.dspace.app.rest.matcher.UsageReportMatcher;
import org.dspace.app.rest.model.UsageReportPointCityRest;
import org.dspace.app.rest.model.UsageReportPointCountryRest;
import org.dspace.app.rest.model.UsageReportPointDateRest; import org.dspace.app.rest.model.UsageReportPointDateRest;
import org.dspace.app.rest.model.UsageReportPointDsoTotalVisitsRest; import org.dspace.app.rest.model.UsageReportPointDsoTotalVisitsRest;
import org.dspace.app.rest.model.UsageReportPointRest; import org.dspace.app.rest.model.UsageReportPointRest;
@@ -46,7 +48,7 @@ import org.junit.Test;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
/** /**
* Integration test to test the /api/statistics/usagereports/ endpoints of {@link UsageReportRestRepository} * Integration test to test the /api/statistics/usagereports/ endpoints, see {@link UsageReportRestRepository}
* *
* @author Maria Verdonck (Atmire) on 10/06/2020 * @author Maria Verdonck (Atmire) on 10/06/2020
*/ */
@@ -65,6 +67,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
private static final String TOTAL_VISITS_REPORT_ID = "TotalVisits"; private static final String TOTAL_VISITS_REPORT_ID = "TotalVisits";
private static final String TOTAL_VISITS_PER_MONTH_REPORT_ID = "TotalVisitsPerMonth"; private static final String TOTAL_VISITS_PER_MONTH_REPORT_ID = "TotalVisitsPerMonth";
private static final String TOTAL_DOWNLOADS_REPORT_ID = "TotalDownloads"; private static final String TOTAL_DOWNLOADS_REPORT_ID = "TotalDownloads";
private static final String TOP_COUNTRIES_REPORT_ID = "TopCountries";
private static final String TOP_CITIES_REPORT_ID = "TopCities";
@BeforeClass @BeforeClass
public static void clearStatistics() throws Exception { public static void clearStatistics() throws Exception {
@@ -129,8 +133,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsReport_Community_Visited() throws Exception { public void totalVisitsReport_Community_Visited() throws Exception {
//** WHEN ** // ** WHEN **
//We visit the community // We visit the community
ViewEventRest viewEventRest = new ViewEventRest(); ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("community"); viewEventRest.setTargetType("community");
viewEventRest.setTargetId(communityVisited.getID()); viewEventRest.setTargetId(communityVisited.getID());
@@ -150,9 +154,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that community's TotalVisits stat report // And request that community's TotalVisits stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + communityVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID)) get("/api/statistics/usagereports/" + communityVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
.matchUsageReport(communityVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID, .matchUsageReport(communityVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -162,8 +165,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsReport_Community_NotVisited() throws Exception { public void totalVisitsReport_Community_NotVisited() throws Exception {
//** WHEN ** // ** WHEN **
//Community is never visited // Community is never visited
UsageReportPointDsoTotalVisitsRest expectedPoint = new UsageReportPointDsoTotalVisitsRest(); UsageReportPointDsoTotalVisitsRest expectedPoint = new UsageReportPointDsoTotalVisitsRest();
expectedPoint.addValue("views", 0); expectedPoint.addValue("views", 0);
expectedPoint.setType("community"); expectedPoint.setType("community");
@@ -172,9 +175,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that community's TotalVisits stat report // And request that community's TotalVisits stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + communityNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID)) get("/api/statistics/usagereports/" + communityNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
.matchUsageReport(communityNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID, .matchUsageReport(communityNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -184,8 +186,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsReport_Collection_Visited() throws Exception { public void totalVisitsReport_Collection_Visited() throws Exception {
//** WHEN ** // ** WHEN **
//We visit the collection twice // We visit the collection twice
ViewEventRest viewEventRest = new ViewEventRest(); ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("collection"); viewEventRest.setTargetType("collection");
viewEventRest.setTargetId(collectionVisited.getID()); viewEventRest.setTargetId(collectionVisited.getID());
@@ -210,9 +212,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that collection's TotalVisits stat report // And request that collection's TotalVisits stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + collectionVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID)) get("/api/statistics/usagereports/" + collectionVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
.matchUsageReport(collectionVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID, .matchUsageReport(collectionVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -222,8 +223,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsReport_Collection_NotVisited() throws Exception { public void totalVisitsReport_Collection_NotVisited() throws Exception {
//** WHEN ** // ** WHEN **
//Collection is never visited // Collection is never visited
UsageReportPointDsoTotalVisitsRest expectedPoint = new UsageReportPointDsoTotalVisitsRest(); UsageReportPointDsoTotalVisitsRest expectedPoint = new UsageReportPointDsoTotalVisitsRest();
expectedPoint.addValue("views", 0); expectedPoint.addValue("views", 0);
expectedPoint.setType("collection"); expectedPoint.setType("collection");
@@ -232,9 +233,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that collection's TotalVisits stat report // And request that collection's TotalVisits stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + collectionNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID)) get("/api/statistics/usagereports/" + collectionNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
.matchUsageReport(collectionNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID, .matchUsageReport(collectionNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -244,8 +244,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsReport_Item_Visited() throws Exception { public void totalVisitsReport_Item_Visited() throws Exception {
//** WHEN ** // ** WHEN **
//We visit an Item // We visit an Item
ViewEventRest viewEventRest = new ViewEventRest(); ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("item"); viewEventRest.setTargetType("item");
viewEventRest.setTargetId(itemVisited.getID()); viewEventRest.setTargetId(itemVisited.getID());
@@ -265,9 +265,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that collection's TotalVisits stat report // And request that collection's TotalVisits stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + itemVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID)) get("/api/statistics/usagereports/" + itemVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
.matchUsageReport(itemVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID, .matchUsageReport(itemVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -277,7 +276,7 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsReport_Item_NotVisited() throws Exception { public void totalVisitsReport_Item_NotVisited() throws Exception {
//** WHEN ** // ** WHEN **
//Item is never visited //Item is never visited
UsageReportPointDsoTotalVisitsRest expectedPoint = new UsageReportPointDsoTotalVisitsRest(); UsageReportPointDsoTotalVisitsRest expectedPoint = new UsageReportPointDsoTotalVisitsRest();
expectedPoint.addValue("views", 0); expectedPoint.addValue("views", 0);
@@ -287,9 +286,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that item's TotalVisits stat report // And request that item's TotalVisits stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" + TOTAL_VISITS_REPORT_ID)) get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" + TOTAL_VISITS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
.matchUsageReport(itemNotVisitedWithBitstreams.getID() + "_" + TOTAL_VISITS_REPORT_ID, .matchUsageReport(itemNotVisitedWithBitstreams.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -299,8 +297,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsReport_Bitstream_Visited() throws Exception { public void totalVisitsReport_Bitstream_Visited() throws Exception {
//** WHEN ** // ** WHEN **
//We visit a Bitstream // We visit a Bitstream
ViewEventRest viewEventRest = new ViewEventRest(); ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("bitstream"); viewEventRest.setTargetType("bitstream");
viewEventRest.setTargetId(bitstreamVisited.getID()); viewEventRest.setTargetId(bitstreamVisited.getID());
@@ -320,9 +318,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that bitstream's TotalVisits stat report // And request that bitstream's TotalVisits stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + bitstreamVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID)) get("/api/statistics/usagereports/" + bitstreamVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
.matchUsageReport(bitstreamVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID, .matchUsageReport(bitstreamVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -332,8 +329,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsReport_Bitstream_NotVisited() throws Exception { public void totalVisitsReport_Bitstream_NotVisited() throws Exception {
//** WHEN ** // ** WHEN **
//Bitstream is never visited // Bitstream is never visited
UsageReportPointDsoTotalVisitsRest expectedPoint = new UsageReportPointDsoTotalVisitsRest(); UsageReportPointDsoTotalVisitsRest expectedPoint = new UsageReportPointDsoTotalVisitsRest();
expectedPoint.addValue("views", 0); expectedPoint.addValue("views", 0);
expectedPoint.setType("bitstream"); expectedPoint.setType("bitstream");
@@ -342,9 +339,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that bitstream's TotalVisits stat report // And request that bitstream's TotalVisits stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + bitstreamNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID)) get("/api/statistics/usagereports/" + bitstreamNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
.matchUsageReport(bitstreamNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID, .matchUsageReport(bitstreamNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -354,8 +350,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsPerMonthReport_Item_Visited() throws Exception { public void totalVisitsPerMonthReport_Item_Visited() throws Exception {
//** WHEN ** // ** WHEN **
//We visit an Item // We visit an Item
ViewEventRest viewEventRest = new ViewEventRest(); ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("item"); viewEventRest.setTargetType("item");
viewEventRest.setTargetId(itemVisited.getID()); viewEventRest.setTargetId(itemVisited.getID());
@@ -372,7 +368,7 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that item's TotalVisitsPerMonth stat report // And request that item's TotalVisitsPerMonth stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + itemVisited.getID() + "_" + TOTAL_VISITS_PER_MONTH_REPORT_ID)) get("/api/statistics/usagereports/" + itemVisited.getID() + "_" + TOTAL_VISITS_PER_MONTH_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
@@ -382,15 +378,15 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsPerMonthReport_Item_NotVisited() throws Exception { public void totalVisitsPerMonthReport_Item_NotVisited() throws Exception {
//** WHEN ** // ** WHEN **
//Item is not visited // Item is not visited
List<UsageReportPointRest> expectedPoints = this.getListOfVisitsPerMonthsPoints(0); List<UsageReportPointRest> expectedPoints = this.getListOfVisitsPerMonthsPoints(0);
// And request that item's TotalVisitsPerMonth stat report // And request that item's TotalVisitsPerMonth stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" + get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" +
TOTAL_VISITS_PER_MONTH_REPORT_ID)) TOTAL_VISITS_PER_MONTH_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
@@ -401,8 +397,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void totalVisitsPerMonthReport_Collection_Visited() throws Exception { public void totalVisitsPerMonthReport_Collection_Visited() throws Exception {
//** WHEN ** // ** WHEN **
//We visit a Collection twice // We visit a Collection twice
ViewEventRest viewEventRest = new ViewEventRest(); ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("collection"); viewEventRest.setTargetType("collection");
viewEventRest.setTargetId(collectionVisited.getID()); viewEventRest.setTargetId(collectionVisited.getID());
@@ -424,7 +420,7 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that collection's TotalVisitsPerMonth stat report // And request that collection's TotalVisitsPerMonth stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + collectionVisited.getID() + "_" + TOTAL_VISITS_PER_MONTH_REPORT_ID)) get("/api/statistics/usagereports/" + collectionVisited.getID() + "_" + TOTAL_VISITS_PER_MONTH_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
@@ -434,8 +430,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void TotalDownloadsReport_Bitstream() throws Exception { public void TotalDownloadsReport_Bitstream() throws Exception {
//** WHEN ** // ** WHEN **
//We visit a Bitstream // We visit a Bitstream
ViewEventRest viewEventRest = new ViewEventRest(); ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("bitstream"); viewEventRest.setTargetType("bitstream");
viewEventRest.setTargetId(bitstreamVisited.getID()); viewEventRest.setTargetId(bitstreamVisited.getID());
@@ -455,7 +451,7 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
// And request that bitstreams's TotalDownloads stat report // And request that bitstreams's TotalDownloads stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + bitstreamVisited.getID() + "_" + TOTAL_DOWNLOADS_REPORT_ID)) get("/api/statistics/usagereports/" + bitstreamVisited.getID() + "_" + TOTAL_DOWNLOADS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
@@ -465,8 +461,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void TotalDownloadsReport_Item() throws Exception { public void TotalDownloadsReport_Item() throws Exception {
//** WHEN ** // ** WHEN **
//We visit an Item's bitstream // We visit an Item's bitstream
ViewEventRest viewEventRest = new ViewEventRest(); ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("bitstream"); viewEventRest.setTargetType("bitstream");
viewEventRest.setTargetId(bitstreamVisited.getID()); viewEventRest.setTargetId(bitstreamVisited.getID());
@@ -483,12 +479,11 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
expectedPoint.setType("bitstream"); expectedPoint.setType("bitstream");
expectedPoint.setId("Bitstream"); expectedPoint.setId("Bitstream");
// And request that item's TotalDownloads stat report // And request that item's TotalDownloads stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" + get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" +
TOTAL_DOWNLOADS_REPORT_ID)) TOTAL_DOWNLOADS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
@@ -498,13 +493,13 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
@Test @Test
public void TotalDownloadsReport_Item_NotVisited() throws Exception { public void TotalDownloadsReport_Item_NotVisited() throws Exception {
//** WHEN ** // ** WHEN **
// You don't visit an item's bitstreams // You don't visit an item's bitstreams
// And request that item's TotalDownloads stat report // And request that item's TotalDownloads stat report
getClient().perform( getClient().perform(
get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" + get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" +
TOTAL_DOWNLOADS_REPORT_ID)) TOTAL_DOWNLOADS_REPORT_ID))
//** THEN ** // ** THEN **
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is( .andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher UsageReportMatcher
@@ -519,6 +514,191 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
.andExpect(status().is(HttpStatus.BAD_REQUEST.value())); .andExpect(status().is(HttpStatus.BAD_REQUEST.value()));
} }
/**
* Note: Geolite response mocked in {@link org.dspace.statistics.MockSolrLoggerServiceImpl}
*/
@Test
public void topCountriesReport_Collection_Visited() throws Exception {
// ** WHEN **
// We visit a Collection
ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("collection");
viewEventRest.setTargetId(collectionVisited.getID());
ObjectMapper mapper = new ObjectMapper();
getClient(loggedInToken).perform(post("/api/statistics/viewevents")
.content(mapper.writeValueAsBytes(viewEventRest))
.contentType(contentType))
.andExpect(status().isCreated());
UsageReportPointCountryRest expectedPoint = new UsageReportPointCountryRest();
expectedPoint.addValue("views", 1);
expectedPoint.setId("US");
expectedPoint.setLabel("United States");
// And request that collection's TopCountries report
getClient().perform(
get("/api/statistics/usagereports/" + collectionVisited.getID() + "_" + TOP_COUNTRIES_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(collectionVisited.getID() + "_" + TOP_COUNTRIES_REPORT_ID,
TOP_COUNTRIES_REPORT_ID, Arrays.asList(expectedPoint)))));
}
/**
* Note: Geolite response mocked in {@link org.dspace.statistics.MockSolrLoggerServiceImpl}
*/
@Test
public void topCountriesReport_Community_Visited() throws Exception {
// ** WHEN **
// We visit a Community twice
ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("community");
viewEventRest.setTargetId(communityVisited.getID());
ObjectMapper mapper = new ObjectMapper();
getClient(loggedInToken).perform(post("/api/statistics/viewevents")
.content(mapper.writeValueAsBytes(viewEventRest))
.contentType(contentType))
.andExpect(status().isCreated());
getClient(loggedInToken).perform(post("/api/statistics/viewevents")
.content(mapper.writeValueAsBytes(viewEventRest))
.contentType(contentType))
.andExpect(status().isCreated());
UsageReportPointCountryRest expectedPoint = new UsageReportPointCountryRest();
expectedPoint.addValue("views", 2);
expectedPoint.setId("US");
expectedPoint.setLabel("United States");
// And request that collection's TopCountries report
getClient().perform(
get("/api/statistics/usagereports/" + communityVisited.getID() + "_" + TOP_COUNTRIES_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(communityVisited.getID() + "_" + TOP_COUNTRIES_REPORT_ID,
TOP_COUNTRIES_REPORT_ID, Arrays.asList(expectedPoint)))));
}
/**
* Note: Geolite response mocked in {@link org.dspace.statistics.MockSolrLoggerServiceImpl}
*/
@Test
public void topCountriesReport_Item_NotVisited() throws Exception {
// ** WHEN **
// Item is not visited
// And request that item's TopCountries report
getClient().perform(
get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" + TOP_COUNTRIES_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(itemNotVisitedWithBitstreams.getID() + "_" + TOP_COUNTRIES_REPORT_ID,
TOP_COUNTRIES_REPORT_ID, new ArrayList<>()))));
}
/**
* Note: Geolite response mocked in {@link org.dspace.statistics.MockSolrLoggerServiceImpl}
*/
@Test
public void topCitiesReport_Item_Visited() throws Exception {
// ** WHEN **
// We visit an Item
ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("item");
viewEventRest.setTargetId(itemVisited.getID());
ObjectMapper mapper = new ObjectMapper();
getClient(loggedInToken).perform(post("/api/statistics/viewevents")
.content(mapper.writeValueAsBytes(viewEventRest))
.contentType(contentType))
.andExpect(status().isCreated());
UsageReportPointCityRest expectedPoint = new UsageReportPointCityRest();
expectedPoint.addValue("views", 1);
expectedPoint.setId("New York");
// And request that item's TopCities report
getClient().perform(
get("/api/statistics/usagereports/" + itemVisited.getID() + "_" + TOP_CITIES_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(itemVisited.getID() + "_" + TOP_CITIES_REPORT_ID,
TOP_CITIES_REPORT_ID, Arrays.asList(expectedPoint)))));
}
/**
* Note: Geolite response mocked in {@link org.dspace.statistics.MockSolrLoggerServiceImpl}
*/
@Test
public void topCitiesReport_Community_Visited() throws Exception {
// ** WHEN **
// We visit a Community thrice
ViewEventRest viewEventRest = new ViewEventRest();
viewEventRest.setTargetType("community");
viewEventRest.setTargetId(communityVisited.getID());
ObjectMapper mapper = new ObjectMapper();
getClient(loggedInToken).perform(post("/api/statistics/viewevents")
.content(mapper.writeValueAsBytes(viewEventRest))
.contentType(contentType))
.andExpect(status().isCreated());
getClient(loggedInToken).perform(post("/api/statistics/viewevents")
.content(mapper.writeValueAsBytes(viewEventRest))
.contentType(contentType))
.andExpect(status().isCreated());
getClient(loggedInToken).perform(post("/api/statistics/viewevents")
.content(mapper.writeValueAsBytes(viewEventRest))
.contentType(contentType))
.andExpect(status().isCreated());
UsageReportPointCityRest expectedPoint = new UsageReportPointCityRest();
expectedPoint.addValue("views", 3);
expectedPoint.setId("New York");
// And request that community's TopCities report
getClient().perform(
get("/api/statistics/usagereports/" + communityVisited.getID() + "_" + TOP_CITIES_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(communityVisited.getID() + "_" + TOP_CITIES_REPORT_ID,
TOP_CITIES_REPORT_ID, Arrays.asList(expectedPoint)))));
}
/**
* Note: Geolite response mocked in {@link org.dspace.statistics.MockSolrLoggerServiceImpl}
*/
@Test
public void topCitiesReport_Collection_NotVisited() throws Exception {
// ** WHEN **
// Collection is not visited
// And request that collection's TopCountries report
getClient().perform(
get("/api/statistics/usagereports/" + collectionNotVisited.getID() + "_" + TOP_CITIES_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(collectionNotVisited.getID() + "_" + TOP_CITIES_REPORT_ID,
TOP_CITIES_REPORT_ID, new ArrayList<>()))));
}
// Create expected points from -6 months to now, with given number of views in current month // Create expected points from -6 months to now, with given number of views in current month
private List<UsageReportPointRest> getListOfVisitsPerMonthsPoints(int viewsLastMonth) { private List<UsageReportPointRest> getListOfVisitsPerMonthsPoints(int viewsLastMonth) {
List<UsageReportPointRest> expectedPoints = new ArrayList<>(); List<UsageReportPointRest> expectedPoints = new ArrayList<>();