Skip to content

Commit

Permalink
Merge pull request #266 from europeana/EA-3957-insert-multiple-items
Browse files Browse the repository at this point in the history
insert multiple items
  • Loading branch information
gsergiu authored Nov 8, 2024
2 parents 766ea97 + eae216e commit 074858a
Show file tree
Hide file tree
Showing 11 changed files with 640 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.containers.output.WaitingConsumer;
import eu.europeana.api.commons.exception.ApiKeyExtractionException;
import eu.europeana.api.commons.exception.AuthorizationExtractionException;
import eu.europeana.api.commons.oauth2.utils.OAuthUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONArray;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -453,6 +456,97 @@ void insertPinnedItems_EntityUserSets_withEditorUser() throws Exception {
// getUserSetService().deleteUserSet(identifier);
}

// test insert multiple items
@Test
void insertMultipleItems_EntityUserSets_withEditorUser() throws Exception {
WebUserSetImpl userSet = createTestUserSet(ENTITY_USER_SET_REGULAR, editorUserToken);
String identifier = userSet.getIdentifier();

List<String> newItems=new ArrayList<>();
String item1="http://data.europeana.eu/item/01/123_pinnedItem";
String item2="/02/123_pinnedItem";
JSONArray newItemsJson = new JSONArray();
newItemsJson.put(item1);
newItemsJson.put(item2);

String result = mockMvc
.perform(
put(BASE_URL + "{identifier}/items", identifier)
.content(newItemsJson.toString())
.queryParam(WebUserSetFields.PATH_PARAM_POSITION,
WebUserSetModelFields.PINNED_POSITION)
.header(HttpHeaders.AUTHORIZATION, editor2UserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().is(HttpStatus.OK.value()))
.andReturn().getResponse()
.getContentAsString();

assertTrue(containsKeyOrValue(result, userSet.getId()));

UserSet existingUserSet = getUserSetService().getUserSetById(userSet.getIdentifier());
//check for the new items
assertEquals(0, existingUserSet.getItems().indexOf(item1));
assertEquals(1, existingUserSet.getItems().indexOf(item2));
assertEquals(2, existingUserSet.getPinned());
assertEquals(4, existingUserSet.getItems().size());

// insert pinned items, some of which already exist (and can be pinned or not-pinned in the set)
newItems.clear();
String item3="/03/123_pinnedItem";
newItems.add(item2);
String item4Existing=existingUserSet.getItems().get(2);
newItems.add(item4Existing);
newItems.add(item3);
getUserSetService().insertMultipleItems(newItems, WebUserSetModelFields.PINNED_POSITION, -1, existingUserSet);
//check the new items
assertEquals(0, existingUserSet.getItems().indexOf(item2));
assertEquals(1, existingUserSet.getItems().indexOf(item4Existing));
assertEquals(2, existingUserSet.getItems().indexOf(item3));
assertEquals(3, existingUserSet.getItems().indexOf(item1));
assertEquals(4, existingUserSet.getPinned());
assertEquals(5, existingUserSet.getItems().size());

//insert un-pinned items, some of which may exist (and can be pinned or not-pinned in the set)
newItems.clear();
//adding an existing pinned item in the request of un-pinned items
newItems.add(item1);
String item5="/05/123_unPinnedItem";
newItems.add(item5);
String item6Existing=existingUserSet.getItems().get(4);
newItems.add(item6Existing);
getUserSetService().insertMultipleItems(newItems, "4", 4, existingUserSet);
//check the new items
assertEquals(0, existingUserSet.getItems().indexOf(item2));
assertEquals(1, existingUserSet.getItems().indexOf(item4Existing));
assertEquals(2, existingUserSet.getItems().indexOf(item3));
assertEquals(3, existingUserSet.getItems().indexOf(item1));
assertEquals(4, existingUserSet.getPinned());
assertEquals(4, existingUserSet.getItems().indexOf(item5));
assertEquals(5, existingUserSet.getItems().indexOf(item6Existing));
assertEquals(4, existingUserSet.getPinned());
assertEquals(6, existingUserSet.getItems().size());

//insert item to the last position
newItems.clear();
String item7="/07/123_unPinnedItem";
newItems.add(item7);
getUserSetService().insertMultipleItems(newItems, null, -1, existingUserSet);
//check the new items
assertEquals(6, existingUserSet.getItems().indexOf(item7));
assertEquals(4, existingUserSet.getPinned());
assertEquals(7, existingUserSet.getItems().size());
//insert item to the position grater than the total size of items
newItems.clear();
String item8="/08/123_unPinnedItem";
newItems.add(item8);
getUserSetService().insertMultipleItems(newItems, "100", 100, existingUserSet);
//check the new items
assertEquals(7, existingUserSet.getItems().indexOf(item8));
assertEquals(4, existingUserSet.getPinned());
assertEquals(8, existingUserSet.getItems().size());

}

// test conversion of pinned -> normal item
@Test
void insertAlreadyExistingPinnedItemAsNormalItem_EntityUserSets_withEditorUser()
Expand Down Expand Up @@ -606,6 +700,43 @@ void deletePinnedItems_EntityUserSets_withEditorUser() throws Exception {

}

@Test
void deleteMultipleItems_EntityUserSets_withEditorUser() throws Exception {
WebUserSetImpl userSet = createTestUserSet(ENTITY_USER_SET_REGULAR, editorUserToken);

List<String> newItems=new ArrayList<>();
String item1="/01/123_pinnedItem";
String item2="/02/123_pinnedItem";
newItems.add(item1);
newItems.add(item2);
getUserSetService().insertMultipleItems(newItems, WebUserSetModelFields.PINNED_POSITION, 0, userSet);

assertEquals(2, userSet.getPinned());
String identifier = userSet.getIdentifier();

JSONArray newItemsJson = new JSONArray();
newItemsJson.put(item2);
//this item is from the input json (ENTITY_USER_SET_REGULAR), so please check it exists there
newItemsJson.put("http://data.europeana.eu/item/08641/1037479000000476591");
//this is not existing item
newItemsJson.put("/03/123_non_existing_item");

mockMvc
.perform(
delete(BASE_URL + "{identifier}/items", identifier)
.content(newItemsJson.toString())
.header(HttpHeaders.AUTHORIZATION, editor2UserToken)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().is(HttpStatus.OK.value())).andReturn().getResponse();

UserSet userSet1 = getUserSetService().getUserSetById(userSet.getIdentifier());
assertEquals(1, userSet1.getPinned());
assertEquals(2, userSet1.getItems().size());

// getUserSetService().deleteUserSet(identifier);

}

private void checkItemCountAndPosition(UserSet existingUserSet, String newItem,
int expectedTotalItems, int expectedPinnedItems, int expectedPositionOfItem) {
assertEquals(expectedPinnedItems, existingUserSet.getPinned());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import javax.annotation.Resource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.Authentication;
Expand All @@ -15,7 +16,6 @@
import eu.europeana.set.definitions.config.UserSetConfiguration;
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.LdProfiles;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetFields;
import eu.europeana.set.definitions.model.vocabulary.WebUserSetModelFields;
Expand Down Expand Up @@ -60,6 +60,7 @@ static void setProperties(DynamicPropertyRegistry registry) {
* Generate isShownBy field for all sets in the (local) db
*/
@Test
@Disabled
public void generateGalleriesWithDepiction() throws Exception {
// createTestUserSet(USER_SET_REGULAR, regularUserToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private UserSetI18nConstants() {
public static final String INVALID_HEADER_FORMAT = "error.userset_invalid_header_format";
public static final String INVALID_HEADER_VALUE = "error.userset_invalid_header_value";
public static final String INVALID_SUBJECT_VALUE = "error.userset_subject_invalid_value";
public static final String INVALID_UNPINNED_ITEMS_POSITION = "error.userset_unpinned_items_position_invalid";

public static final String USERSET_VALIDATION = "error.userset_validation";
public static final String USERSET_DUPLICATION = "error.userset_duplication";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ private SwaggerConstants() {
public static final String SAMPLES_JSONLD = "Please find JSON-LD samples for user set in <a href=\"../jsp/template/jsonld.jsp\" target=\"_blank\">templates</a>. ";
public static final String SEARCH_HELP_NOTE = "Identifier is a number.";
public static final String INSERT_ITEM_NOTE = "Please create your insert item request using selected parameters.";
public static final String INSERT_MULTIPLE_ITEM_NOTE = "Please create your insert multiple items request using selected parameters.";
public static final String PUBLISH_SET_NOTE = "Please create the request for publishing the set using the provided parameters.";
public static final String CHECK_ITEM_NOTE = "Check if item is already in a user set";
public static final String DELETE_ITEM_NOTE = "Delete a item from the set ";
public static final String DELETE_ITEM_NOTE = "Delete a item from the set";
public static final String DELETE_MULTIPLE_ITEMS_NOTE = "Delete multiple items from the set";
public static final String SEARCH = "Searching user sets. The criteria for filtering results is provided in form of <field>:<value> pairs, using the query and qf request parameters. Currently supported fields are: type, visibility, creator, item, provider and lang (title language).";
public static final String SEARCH_ITEMS_IN_SET = "Searching items within a set indicated by identifier parameter. Use the use * for query and item:<itemId> filter in qf parameter. (Multiple item filters are build an OR search criteria)";
public static final String UPDATE_SAMPLES_JSONLD = SAMPLES_JSONLD + "Please create your JSON update request using selected fields you are going " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ UserSet fetchItems(UserSet storedUserSet, String sort, String sortOrder, int pag
UserSet insertItem(String datasetId, String localId, String position, UserSet existingUserSet)
throws ApplicationAuthenticationException, ItemValidationException;

/**
* This method updates existing item list
*
* @param existingUserSet
* @return updated user set
*/
UserSet updateItemList(UserSet existingUserSet);
public UserSet insertMultipleItems(List<String> items, String position, int itemsPosition, UserSet existingUserSet)
throws ItemValidationException;

UserSet deleteItem(String item, UserSet existingUserSet);

UserSet deleteMultipleItems(List<String> items, UserSet existingUserSet);

/**
* search user sets using the given query and profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import eu.europeana.api.commons.web.exception.ApplicationAuthenticationException;
import eu.europeana.api.commons.web.exception.HttpException;
import eu.europeana.api.commons.web.exception.ParamValidationException;
import eu.europeana.api.commons.web.http.HttpHeaders;
import eu.europeana.api.commons.web.model.vocabulary.Operations;
import eu.europeana.set.definitions.config.UserSetConfiguration;
import eu.europeana.set.definitions.exception.UserSetProfileValidationException;
Expand Down
Loading

0 comments on commit 074858a

Please sign in to comment.