Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into
Browse files Browse the repository at this point in the history
EA-3823-improve-profile-param
  • Loading branch information
GordeaS authored and GordeaS committed Jun 28, 2024
2 parents 35258da + 8c6f5e9 commit 0a1a26f
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 97 deletions.
2 changes: 1 addition & 1 deletion set-client/src/test/resources/content/userset-open.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "Collection",
"type": "DynamicCollection",
"title": {
"en": "Cinema of attraction"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ public void setIsDefinedBy(String isDefinedBy) {
}

public boolean isOpenSet() {
if (getIsDefinedBy() != null) {
return true;
}
return false;
if(UserSetTypes.DYNAMICCOLLECTION.getJsonValue().equals(type)) {
return true;
}
return false;
}

@Override
Expand Down
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 @@ -10,7 +10,7 @@
*/
public enum UserSetTypes implements JsonKeyword {

COLLECTION("Collection"), BOOKMARKSFOLDER("BookmarkFolder"), ENTITYBESTITEMSSET("EntityBestItemsSet");
COLLECTION("Collection"), DYNAMICCOLLECTION("DynamicCollection"), BOOKMARKSFOLDER("BookmarkFolder"), ENTITYBESTITEMSSET("EntityBestItemsSet");

private String jsonValue;

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 @@ -12,6 +12,7 @@
import java.util.Arrays;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -27,8 +28,10 @@
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;
import eu.europeana.set.web.exception.request.ItemValidationException;
import eu.europeana.set.web.model.WebUserSetImpl;
import eu.europeana.set.web.search.UserSetQueryBuilder;
Expand Down Expand Up @@ -81,6 +84,34 @@ public void create_UserSet_201Created() throws Exception {
addToCreatedSets(identifier);
}

@Test
public void create_UserSet_DynamicCollecton_without_isDefinedBy() throws Exception {
String requestJson = getJsonStringInput(USER_SET_OPEN);
JSONObject withoutIsDefinedBy = new JSONObject(requestJson);
withoutIsDefinedBy.remove(WebUserSetModelFields.IS_DEFINED_BY);

mockMvc
.perform(
post(BASE_URL).param(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.MINIMAL.name())
.content(withoutIsDefinedBy.toString()).header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}

@Test
public void create_UserSet_Collection_with_isDefinedBy() throws Exception {
String requestJson = getJsonStringInput(USER_SET_REGULAR);
JSONObject withIsDefinedBy = new JSONObject(requestJson);
withIsDefinedBy.append(WebUserSetModelFields.IS_DEFINED_BY, "https://api.europeana.eu/api/v2/search.json?query=%28europeana_collectionName%3A08641%2A%20AND%20NOT%28proxy_dc_identifier%3A%5C%221037479000000477087%5C%22%29%29");

mockMvc
.perform(
post(BASE_URL).param(CommonApiConstants.QUERY_PARAM_PROFILE, LdProfiles.MINIMAL.name())
.content(withIsDefinedBy.toString()).header(HttpHeaders.AUTHORIZATION, regularUserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}

@Test
public void create_UserSet_401_bad_request_InvalidInput() throws Exception {
mockMvc
Expand Down Expand Up @@ -162,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
Loading

0 comments on commit 0a1a26f

Please sign in to comment.