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.HashMap;
import java.util.List;
import java.util.Map;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
@@ -66,8 +67,10 @@ public class MockSolrLoggerServiceImpl
* @return faked CityResponse
*/
private CityResponse mockCityResponse() {
List<String> cityNames = new ArrayList<>(Collections.singleton("New York"));
City city = new City(cityNames, 1, 1, new HashMap());
List<String> cityLocales = new ArrayList<String>(Collections.singleton("en"));
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"));
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 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.UsageReportPointDsoTotalVisitsRest;
import org.dspace.app.rest.model.UsageReportPointRest;
@@ -46,7 +48,7 @@ import org.junit.Test;
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
*/
@@ -65,6 +67,8 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
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_DOWNLOADS_REPORT_ID = "TotalDownloads";
private static final String TOP_COUNTRIES_REPORT_ID = "TopCountries";
private static final String TOP_CITIES_REPORT_ID = "TopCities";
@BeforeClass
public static void clearStatistics() throws Exception {
@@ -152,7 +156,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
get("/api/statistics/usagereports/" + communityVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(communityVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -174,7 +177,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
get("/api/statistics/usagereports/" + communityNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(communityNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -212,7 +214,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
get("/api/statistics/usagereports/" + collectionVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(collectionVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -234,7 +235,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
get("/api/statistics/usagereports/" + collectionNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(collectionNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -267,7 +267,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
get("/api/statistics/usagereports/" + itemVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(itemVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -289,7 +288,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" + TOTAL_VISITS_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(itemNotVisitedWithBitstreams.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -322,7 +320,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
get("/api/statistics/usagereports/" + bitstreamVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(bitstreamVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -344,7 +341,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
get("/api/statistics/usagereports/" + bitstreamNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID))
// ** THEN **
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
UsageReportMatcher
.matchUsageReport(bitstreamNotVisited.getID() + "_" + TOTAL_VISITS_REPORT_ID,
@@ -483,7 +479,6 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
expectedPoint.setType("bitstream");
expectedPoint.setId("Bitstream");
// And request that item's TotalDownloads stat report
getClient().perform(
get("/api/statistics/usagereports/" + itemNotVisitedWithBitstreams.getID() + "_" +
@@ -519,6 +514,191 @@ public class UsageReportRestRepositoryIT extends AbstractControllerIntegrationTe
.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
private List<UsageReportPointRest> getListOfVisitsPerMonthsPoints(int viewsLastMonth) {
List<UsageReportPointRest> expectedPoints = new ArrayList<>();