Skip to content

Commit

Permalink
Merge pull request #251 from europeana/EA-3872-fix-pagination-and-con…
Browse files Browse the repository at this point in the history
…sole-allignment

1) added type for Agents in the serialization; 2) added .jon get method;
  • Loading branch information
gsergiu authored Jun 28, 2024
2 parents 76930ed + 289581b commit 8c6f5e9
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class UserSetUtils {

public static final String EUROPEANA_ID_FIELD_REGEX = "^[a-zA-Z0-9_]*$";
public static final Pattern EUROPEANA_ID = Pattern.compile("^/[a-zA-Z0-9_]*/[a-zA-Z0-9_]*$");
public static final int DEFAULT_PAGE = 1;
/**
* This method converts string value to Map<String,String> values for given key - language.
*
Expand Down Expand Up @@ -84,14 +85,14 @@ public UserSet updatePagination(UserSet userSet, UserSetConfiguration config) {
userSet.setTotal(total);
//NOTE: the first and last properties are not used now and might be deprecated, they should not be stored in the database
if (total > 0) {
int first = 0;
int first = UserSetUtils.DEFAULT_PAGE;
String firstPageStr = fillPage(userSet, config, first, UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
userSet.setFirst(firstPageStr);
int last = (int) Math.ceil( (double)total / UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
if(last > 0) {
last = last - 1; // we start counting by 0
}
String lastPageStr = fillPage(userSet, config, last, UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
final int totalPages = (int) Math.floor( (double) total / UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
int pageIndexOfset = UserSetUtils.DEFAULT_PAGE -1;
//the index of last page depends on the start index. i.e. 2 pages [0,1] vs. [1,2]
int lastPageIndex = totalPages + pageIndexOfset;
String lastPageStr = fillPage(userSet, config, lastPageIndex, UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE);
userSet.setLast(lastPageStr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ protected WebUserSetImpl createTestUserSet(String testFile, String token) throws
Authentication authentication = getAuthentication(token);
WebUserSetImpl createdSet =
(WebUserSetImpl) getUserSetService().storeUserSet(set, authentication);
//keep the list of created sets to delete in the end
createdUserSets.add(createdSet);
return createdSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,10 @@ public void searchItemsInSet() throws Exception {
String[] qf = new String[] {"item:/08641/1037479000000476467",
"item:/08641/1037479000000476875", "item:/11654/_Botany_U_1419207", "item:/2048128/618580",
"item:/2048128/618580", "item:/2048128/notexisting", "item:/2048128/notexisting1"};
String result = callSearchItemsInSet(setIdentifier, qf, "1", "2", null);
final String secondPageIndex = String.valueOf(UserSetUtils.DEFAULT_PAGE + 1);
//using pagesize 2, we get two pages of results (only 4 items found in set)
//retrieve last page
String result = callSearchItemsInSet(setIdentifier, qf, secondPageIndex, "2", null);
// check ids
String searchUri = "/set/" + setIdentifier + "/search";
assertTrue(StringUtils.contains(result, searchUri));
Expand All @@ -609,7 +612,8 @@ public void searchItemsInSet() throws Exception {
// last page no next
assertTrue(!containsKeyOrValue(result, WebUserSetFields.NEXT));

result = callSearchItemsInSet(setIdentifier, qf, "0", "2", null);
//retrieve fist page of results
result = callSearchItemsInSet(setIdentifier, qf, String.valueOf(UserSetUtils.DEFAULT_PAGE), "2", null);
// check ids
assertTrue(StringUtils.contains(result, searchUri));
assertTrue(containsKeyOrValue(result, WebUserSetFields.TOTAL));
Expand All @@ -620,9 +624,6 @@ public void searchItemsInSet() throws Exception {
// first page no prev
assertTrue(!containsKeyOrValue(result, WebUserSetFields.PREV));
assertTrue(containsKeyOrValue(result, WebUserSetFields.NEXT));

// delete item created by test
// getUserSetService().deleteUserSet(setIdentifier);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public void getCloseUserSet_ItemDescriptionsWithPageValues() throws Exception {
WebUserSetImpl userSet = createTestUserSet(USER_SET_LARGE, regularUserToken);

// get the identifier
final int secondPageIndex = UserSetUtils.DEFAULT_PAGE + 1;
MvcResult response = mockMvc.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, String.valueOf(secondPageIndex))
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "100")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)).andReturn();
Expand All @@ -130,11 +131,11 @@ public void getCloseUserSet_ItemDescriptionsWithPageValues() throws Exception {
.replaceFirst(getConfiguration().getApiBasePath(), "");
String requestedPage = baseUrl + response.getRequest().getPathInfo();
// int pageSize = 100;
// int page = 1;
// int page = 2;
final String collectionUrl = getUserSetService().buildResultsPageUrl(requestedPage,
response.getRequest().getQueryString(), null);
final String resultPageId =
getUserSetService().buildPageUrl(collectionUrl, 1, 100, LdProfiles.ITEMDESCRIPTIONS);
getUserSetService().buildPageUrl(collectionUrl, secondPageIndex, 100, LdProfiles.ITEMDESCRIPTIONS);
assertTrue(containsKeyOrValue(result, resultPageId));

// check part of ID
Expand Down Expand Up @@ -173,10 +174,11 @@ public void getCloseUserSet_ItemDescriptionsWithLastPage() throws Exception {
WebUserSetImpl userSet = createTestUserSet(USER_SET_LARGE, regularUserToken);

// set size = 249 items, request for more than lastPage
final String firstIndexAfterLastPage = "4";
MockHttpServletResponse response =
mockMvc.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "3")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, firstIndexAfterLastPage)
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "100")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)).andReturn().getResponse();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import eu.europeana.api.commons.definitions.vocabulary.CommonLdConstants;
import eu.europeana.api.set.integration.BaseUserSetTestUtils;
import eu.europeana.set.definitions.config.UserSetConfigurationImpl;
import eu.europeana.set.definitions.model.utils.UserSetUtils;
import eu.europeana.set.definitions.model.vocabulary.LdProfiles;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
import eu.europeana.set.web.model.WebUserSetImpl;
Expand Down Expand Up @@ -62,42 +63,43 @@ public void getUserSetPagination() throws Exception {
WebUserSetImpl userSet = createTestUserSet(USER_SET_LARGE, regularUserToken);

// get the identifier
final String secondPageIndex = "2";
MockHttpServletResponse response = mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.STANDARD.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, secondPageIndex)
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "5")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();

//
String result = response.getContentAsString();
assertNotNull(result);
String secondPageJson = response.getContentAsString();
assertNotNull(secondPageJson);
assertEquals(HttpStatus.OK.value(), response.getStatus());
// build collection uri?
// String collectionUrl = buildCollectionUrl(null, request.getRequestURL().toString(),
// request.getQueryString());
// assertTrue(constainsKey(result, collectionUrl));

assertTrue(containsKeyOrValue(result, WebUserSetFields.PART_OF));
assertTrue(containsKeyOrValue(result, CommonLdConstants.COLLECTION));
assertTrue(containsKeyOrValue(result, CommonLdConstants.COLLECTION_PAGE));
assertTrue(containsKeyOrValue(result, WebUserSetFields.START_INDEX));
assertTrue(containsKeyOrValue(result, WebUserSetFields.FIRST));
assertTrue(containsKeyOrValue(result, WebUserSetFields.LAST));
assertTrue(containsKeyOrValue(result, WebUserSetFields.PREV));
assertTrue(containsKeyOrValue(result, WebUserSetFields.NEXT));
assertTrue(containsKeyOrValue(result, WebUserSetFields.ITEMS));
assertTrue(containsKeyOrValue(secondPageJson, WebUserSetFields.PART_OF));
assertTrue(containsKeyOrValue(secondPageJson, CommonLdConstants.COLLECTION));
assertTrue(containsKeyOrValue(secondPageJson, CommonLdConstants.COLLECTION_PAGE));
assertTrue(containsKeyOrValue(secondPageJson, WebUserSetFields.START_INDEX));
assertTrue(containsKeyOrValue(secondPageJson, WebUserSetFields.FIRST));
assertTrue(containsKeyOrValue(secondPageJson, WebUserSetFields.LAST));
assertTrue(containsKeyOrValue(secondPageJson, WebUserSetFields.PREV));
assertTrue(containsKeyOrValue(secondPageJson, WebUserSetFields.NEXT));
assertTrue(containsKeyOrValue(secondPageJson, WebUserSetFields.ITEMS));
// verify that the ids are not escaped
assertTrue(containsKeyOrValue(result, "http://data.europeana.eu/item/11648/_Botany_L_1444437"));
assertTrue(containsKeyOrValue(secondPageJson, "http://data.europeana.eu/item/11648/_Botany_L_1444437"));
// assertTrue(constainsKeyOrValue(result, WebUserSetFields.ITEMS));

int idCount = StringUtils.countMatches(result, "\"id\"");
int idCount = StringUtils.countMatches(secondPageJson, "\"id\"");
// 1 id part of and one for collection page
assertEquals(2, idCount);

int total = StringUtils.countMatches(result, "\"total\"");
int total = StringUtils.countMatches(secondPageJson, "\"total\"");
// 1 id part of and one for collection page
assertEquals(2, total);
}
Expand Down Expand Up @@ -145,7 +147,7 @@ public void getPageForEmptyUserSet() throws Exception {
MockHttpServletResponse response = mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.STANDARD.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "0")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, String.valueOf(UserSetUtils.DEFAULT_PAGE))
// .queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "5")
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
Expand Down Expand Up @@ -186,10 +188,11 @@ public void getUserSetPaginationDefaultPageSize() throws Exception {
WebUserSetImpl userSet = createTestUserSet(USER_SET_LARGE, regularUserToken);

// get the identifier
final String secondPageIdex = "2";
MockHttpServletResponse response = mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.STANDARD.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, secondPageIdex)
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();
Expand All @@ -211,25 +214,26 @@ public void getUserSetPaginationItemDescriptions() throws Exception {
WebUserSetImpl userSet = createTestUserSet(USER_SET_LARGE, regularUserToken);

// get the identifier
final String secondPageIndex = "2";
MockHttpServletResponse response =
mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE,
LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, secondPageIndex)
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();

//
String result = response.getContentAsString();
assertNotNull(result);
String secondPageContent = response.getContentAsString();
assertNotNull(secondPageContent);
assertEquals(HttpStatus.OK.value(), response.getStatus());
// verify that ids are not escaped, use one item from second page
assertTrue(containsKeyOrValue(result, "\\/11647\\/_Botany_AMD_87140"));
assertTrue(containsKeyOrValue(secondPageContent, "\\/11647\\/_Botany_AMD_87140"));

int defaultPageSize = UserSetConfigurationImpl.DEFAULT_ITEMS_PER_PAGE;
int pageSize = StringUtils.countMatches(result, "\\/item\\/");
int pageSize = StringUtils.countMatches(secondPageContent, "\\/item\\/");
assertEquals(defaultPageSize, pageSize);
}

Expand All @@ -240,13 +244,15 @@ public void getUserSetSecondPageItemDescriptions() throws Exception {
WebUserSetImpl userSet = createTestUserSet(USER_SET_LARGE2, regularUserToken);

// get the identifier
final String secondPageContent = "2";
final String requestedPageSize = "100";
MockHttpServletResponse response =
mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE,
LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "1")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "100")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, secondPageContent)
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, requestedPageSize)
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();
Expand All @@ -270,13 +276,14 @@ public void getUserSetPaginationItemDescriptionsOrder() throws Exception {
WebUserSetImpl userSet = createTestUserSet(USER_SET_TATTOOS, regularUserToken);

// get the identifier
final String requestedPageSize = "10";
MockHttpServletResponse response =
mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE,
LdProfiles.ITEMDESCRIPTIONS.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "0")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "10")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, String.valueOf(UserSetUtils.DEFAULT_PAGE))
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, requestedPageSize)
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();
Expand Down Expand Up @@ -355,11 +362,12 @@ public void getUserSetPaginationPageSizeExceeded() throws Exception {
WebUserSetImpl userSet = createTestUserSet(USER_SET_LARGE, regularUserToken);

// get the identifier
final String requestedPageSize = "200";
MockHttpServletResponse response = mockMvc
.perform(get(BASE_URL + "{identifier}", userSet.getIdentifier())
.queryParam(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.STANDARD.name())
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, "0")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, "200")
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE, String.valueOf(UserSetUtils.DEFAULT_PAGE))
.queryParam(CommonApiConstants.QUERY_PARAM_PAGE_SIZE, requestedPageSize)
.header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andReturn().getResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import eu.europeana.set.definitions.model.UserSet;
import eu.europeana.set.definitions.model.search.UserSetQuery;
import eu.europeana.set.definitions.model.utils.UserSetUtils;
import eu.europeana.set.definitions.model.vocabulary.AgentTypes;
import eu.europeana.set.definitions.model.vocabulary.LdProfiles;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetModelFields;
Expand Down Expand Up @@ -192,6 +193,7 @@ public void getUserSet_Success() throws Exception {
// without page in request, it is not a collection page
assertFalse(containsKeyOrValue(result, CommonLdConstants.COLLECTION_PAGE));
assertFalse(containsKeyOrValue(result, WebUserSetFields.PART_OF));
assertEquals(((JSONObject)(new JSONObject(result)).get("creator")).getString("type"),AgentTypes.PERSON.name());
}

// Update user set Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import eu.europeana.set.definitions.model.UserSet;
import eu.europeana.set.definitions.model.search.UserSetFacetQuery;
import eu.europeana.set.definitions.model.search.UserSetQuery;
import eu.europeana.set.definitions.model.utils.UserSetUtils;
import eu.europeana.set.definitions.model.vocabulary.UserSetTypes;
import eu.europeana.set.definitions.model.vocabulary.VisibilityTypes;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
Expand Down Expand Up @@ -408,7 +409,9 @@ public ResultSet<PersistentUserSet> find(UserSetQuery query) {

@SuppressWarnings("deprecation")
private void setPaginationOptions(Query<PersistentUserSet> mongoQuery, UserSetQuery query) {
mongoQuery.offset(query.getPageNr() * query.getPageSize());
//first mongoPage has index 0
final int mongoPageIndex = query.getPageNr() - UserSetUtils.DEFAULT_PAGE;
mongoQuery.offset(mongoPageIndex * query.getPageSize());
mongoQuery.limit(query.getPageSize());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package eu.europeana.set.web.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import eu.europeana.set.definitions.model.agent.impl.Person;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetModelFields;

Expand All @@ -15,7 +13,7 @@ public WebUser() {
}

@Override
@JsonIgnore
@JsonProperty(WebUserSetModelFields.TYPE)
public String getType() {
return super.getType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import eu.europeana.set.definitions.config.UserSetConfiguration;
import eu.europeana.set.definitions.exception.UserSetProfileValidationException;
import eu.europeana.set.definitions.model.UserSet;
import eu.europeana.set.definitions.model.utils.UserSetUtils;
import eu.europeana.set.definitions.model.vocabulary.LdProfiles;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
import eu.europeana.set.stats.service.UsageStatsService;
Expand Down Expand Up @@ -295,7 +296,7 @@ public String getApiVersion() {

protected ResponseEntity<String> buildGetResponse(UserSet userSet, LdProfiles profile, Integer pageNr, int pageSize, HttpServletRequest request) throws IOException, HttpException {
String jsonBody = "";
if(pageNr == null || pageNr < 0) {
if(pageNr == null || pageNr < UserSetUtils.DEFAULT_PAGE) {
jsonBody = serializeUserSet(profile, userSet);
}else {
CollectionPage itemPage = getUserSetService().buildCollectionPage(userSet, profile, pageNr, pageSize, request);
Expand Down
Loading

0 comments on commit 8c6f5e9

Please sign in to comment.