mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
[CST-4039] Patch Add entire array with virtual values does not work
Created integration test
This commit is contained in:
@@ -25,6 +25,8 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.dspace.app.rest.matcher.MetadataMatcher;
|
||||
import org.dspace.app.rest.model.MetadataValueRest;
|
||||
@@ -49,6 +51,7 @@ import org.dspace.content.service.EntityTypeService;
|
||||
import org.dspace.content.service.ItemService;
|
||||
import org.dspace.content.service.RelationshipTypeService;
|
||||
import org.dspace.content.service.WorkspaceItemService;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -81,6 +84,8 @@ public class PatchMetadataIT extends AbstractEntityIntegrationTest {
|
||||
|
||||
private List<String> authorsOriginalOrder;
|
||||
|
||||
private List<MetadataValue> authorsMetadataOriginalOrder;
|
||||
|
||||
private AtomicReference<Integer> idRef1;
|
||||
private AtomicReference<Integer> idRef2;
|
||||
|
||||
@@ -202,19 +207,19 @@ public class PatchMetadataIT extends AbstractEntityIntegrationTest {
|
||||
.andDo(result -> idRef2.set(read(result.getResponse().getContentAsString(), "$.id")));
|
||||
|
||||
publication = workspaceItemService.find(context, publicationItem.getID());
|
||||
List<MetadataValue> publicationAuthorList =
|
||||
authorsMetadataOriginalOrder =
|
||||
itemService.getMetadata(publication.getItem(), "dc", "contributor", "author", Item.ANY);
|
||||
assertEquals(publicationAuthorList.size(), 5);
|
||||
assertThat(publicationAuthorList.get(0).getValue(), equalTo(authorsOriginalOrder.get(0)));
|
||||
assertThat(publicationAuthorList.get(0).getAuthority(), not(startsWith("virtual::")));
|
||||
assertThat(publicationAuthorList.get(1).getValue(), equalTo(authorsOriginalOrder.get(1)));
|
||||
assertThat(publicationAuthorList.get(1).getAuthority(), startsWith("virtual::"));
|
||||
assertThat(publicationAuthorList.get(2).getValue(), equalTo(authorsOriginalOrder.get(2)));
|
||||
assertThat(publicationAuthorList.get(2).getAuthority(), not(startsWith("virtual::")));
|
||||
assertThat(publicationAuthorList.get(3).getValue(), equalTo(authorsOriginalOrder.get(3)));
|
||||
assertThat(publicationAuthorList.get(3).getAuthority(), not(startsWith("virtual::")));
|
||||
assertThat(publicationAuthorList.get(4).getValue(), equalTo(authorsOriginalOrder.get(4)));
|
||||
assertThat(publicationAuthorList.get(4).getAuthority(), startsWith("virtual::"));
|
||||
assertEquals(authorsMetadataOriginalOrder.size(), 5);
|
||||
assertThat(authorsMetadataOriginalOrder.get(0).getValue(), equalTo(authorsOriginalOrder.get(0)));
|
||||
assertThat(authorsMetadataOriginalOrder.get(0).getAuthority(), not(startsWith("virtual::")));
|
||||
assertThat(authorsMetadataOriginalOrder.get(1).getValue(), equalTo(authorsOriginalOrder.get(1)));
|
||||
assertThat(authorsMetadataOriginalOrder.get(1).getAuthority(), startsWith("virtual::"));
|
||||
assertThat(authorsMetadataOriginalOrder.get(2).getValue(), equalTo(authorsOriginalOrder.get(2)));
|
||||
assertThat(authorsMetadataOriginalOrder.get(2).getAuthority(), not(startsWith("virtual::")));
|
||||
assertThat(authorsMetadataOriginalOrder.get(3).getValue(), equalTo(authorsOriginalOrder.get(3)));
|
||||
assertThat(authorsMetadataOriginalOrder.get(3).getAuthority(), not(startsWith("virtual::")));
|
||||
assertThat(authorsMetadataOriginalOrder.get(4).getValue(), equalTo(authorsOriginalOrder.get(4)));
|
||||
assertThat(authorsMetadataOriginalOrder.get(4).getAuthority(), startsWith("virtual::"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1162,6 +1167,45 @@ public class PatchMetadataIT extends AbstractEntityIntegrationTest {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This test will overwrite all authors (dc.contributor.author) of a workspace publication's "traditionalpageone"
|
||||
* section using a PATCH add with the entire array values.
|
||||
* It makes sure that virtual values are correctly reordered or deleted.
|
||||
*/
|
||||
@Test
|
||||
public void patchAddAllAuthorsOnTraditionalPageTest() throws Exception {
|
||||
|
||||
// "Whyte, William"
|
||||
// "Dahlen, Sarah" (virtual)
|
||||
// "Peterson, Karrie"
|
||||
// "Perotti, Enrico"
|
||||
// "Linton, Oliver" (virtual)
|
||||
initPersonPublicationWorkspace();
|
||||
|
||||
// "Linton, Oliver" (virtual)
|
||||
// "Perotti, Enrico"
|
||||
// "Peterson, Karrie"
|
||||
// "Dahlen, Sarah" (virtual)
|
||||
// "Whyte, William"
|
||||
List<MetadataValue> reverse = new ArrayList<MetadataValue>();
|
||||
reverse.add(this.authorsMetadataOriginalOrder.get(4));
|
||||
reverse.add(this.authorsMetadataOriginalOrder.get(3));
|
||||
reverse.add(this.authorsMetadataOriginalOrder.get(2));
|
||||
reverse.add(this.authorsMetadataOriginalOrder.get(1));
|
||||
reverse.add(this.authorsMetadataOriginalOrder.get(0));
|
||||
patchAddEntireArray(reverse);
|
||||
|
||||
// "Peterson, Karrie"
|
||||
// "Linton, Oliver" (virtual)
|
||||
// "Whyte, William"
|
||||
List<MetadataValue> deletionAndReorder = new ArrayList<MetadataValue>();
|
||||
deletionAndReorder.add(this.authorsMetadataOriginalOrder.get(2));
|
||||
deletionAndReorder.add(this.authorsMetadataOriginalOrder.get(4));
|
||||
deletionAndReorder.add(this.authorsMetadataOriginalOrder.get(0));
|
||||
patchAddEntireArray(deletionAndReorder);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1305,6 +1349,50 @@ public class PatchMetadataIT extends AbstractEntityIntegrationTest {
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method set the entire authors list (dc.contributor.author) within a workspace
|
||||
* publication's "traditionalpageone" section
|
||||
* @param metadataValues The metadata list of all the metadata values
|
||||
*/
|
||||
private void patchAddEntireArray(List<MetadataValue> metadataValues) throws Exception {
|
||||
List<Operation> ops = new ArrayList<Operation>();
|
||||
List<MetadataValueRest> value = new ArrayList<MetadataValueRest>();
|
||||
|
||||
// generates the MetadataValueRest list
|
||||
metadataValues.stream().forEach(mv -> {
|
||||
MetadataValueRest mrv = new MetadataValueRest();
|
||||
value.add(mrv);
|
||||
mrv.setValue(mv.getValue());
|
||||
if (mv.getAuthority() != null && mv.getAuthority().startsWith("virtual::")) {
|
||||
mrv.setAuthority(mv.getAuthority());
|
||||
mrv.setConfidence(mv.getConfidence());
|
||||
}
|
||||
});
|
||||
|
||||
AddOperation add = new AddOperation("/sections/traditionalpageone/dc.contributor.author", value);
|
||||
ops.add(add);
|
||||
String patchBody = getPatchContent(ops);
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(patch("/api/submission/workspaceitems/" + publicationItem.getID())
|
||||
.content(patchBody)
|
||||
.contentType(javax.ws.rs.core.MediaType.APPLICATION_JSON_PATCH_JSON))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
final String authorField = "dc.contributor.author";
|
||||
final List<Matcher<? super Object>> matchers = new ArrayList<>();
|
||||
IntStream.range(0, metadataValues.size()).forEach((i) -> {
|
||||
matchers.add(Matchers.is(MetadataMatcher.matchMetadata(authorField, metadataValues.get(i).getValue(), 0)));
|
||||
});
|
||||
|
||||
|
||||
getClient(token).perform(get("/api/submission/workspaceitems/" + publicationItem.getID()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(contentType))
|
||||
.andExpect(jsonPath("$.sections.traditionalpageone", Matchers.allOf(matchers)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a move operation on a workspace item's "traditionalpageone" section for
|
||||
* metadata field "dc.contributor.author".
|
||||
|
Reference in New Issue
Block a user