Skip to content

Commit 64d84a1

Browse files
committed
added metadata item and bitstream tests
1 parent 0b3c0d7 commit 64d84a1

2 files changed

Lines changed: 172 additions & 15 deletions

File tree

dspace-server-webapp/src/test/java/org/dspace/app/rest/ProvenanceServiceIT.java

Lines changed: 166 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,29 @@
1111
import com.fasterxml.jackson.databind.JsonNode;
1212
import com.fasterxml.jackson.databind.ObjectMapper;
1313
import com.fasterxml.jackson.databind.node.ObjectNode;
14+
import org.apache.lucene.util.Bits;
15+
import org.checkerframework.checker.units.qual.A;
1416
import org.dspace.app.rest.matcher.CollectionMatcher;
1517
import org.dspace.app.rest.matcher.ItemMatcher;
18+
import org.dspace.app.rest.model.patch.AddOperation;
1619
import org.dspace.app.rest.model.patch.Operation;
20+
import org.dspace.app.rest.model.patch.RemoveOperation;
1721
import org.dspace.app.rest.model.patch.ReplaceOperation;
1822
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
23+
import org.dspace.authorize.AuthorizeException;
24+
import org.dspace.builder.BitstreamBuilder;
25+
import org.dspace.builder.BundleBuilder;
1926
import org.dspace.builder.CollectionBuilder;
2027
import org.dspace.builder.CommunityBuilder;
2128
import org.dspace.builder.ItemBuilder;
29+
import org.dspace.content.Bitstream;
30+
import org.dspace.content.Bundle;
2231
import org.dspace.content.Collection;
2332
import org.dspace.content.Community;
33+
import org.dspace.content.DSpaceObject;
2434
import org.dspace.content.Item;
2535
import org.dspace.content.MetadataValue;
36+
import org.dspace.content.service.BitstreamService;
2637
import org.dspace.content.service.ItemService;
2738
import org.hamcrest.Matchers;
2839
import org.hamcrest.core.StringContains;
@@ -31,18 +42,25 @@
3142
import org.junit.Test;
3243
import org.springframework.beans.factory.annotation.Autowired;
3344
import org.springframework.test.web.servlet.MvcResult;
45+
import org.springframework.test.web.servlet.ResultActions;
3446

3547
import javax.ws.rs.core.MediaType;
48+
import java.io.IOException;
49+
import java.sql.SQLException;
3650
import java.util.ArrayList;
51+
import java.util.HashMap;
3752
import java.util.LinkedHashMap;
3853
import java.util.List;
3954
import java.util.Map;
4055
import java.util.Objects;
4156
import java.util.regex.Matcher;
4257
import java.util.regex.Pattern;
4358

59+
import static java.nio.charset.Charset.defaultCharset;
60+
import static org.apache.commons.io.IOUtils.toInputStream;
4461
import static org.dspace.app.rest.matcher.MetadataMatcher.matchMetadata;
4562
import static org.dspace.app.rest.matcher.MetadataMatcher.matchMetadataDoesNotExist;
63+
import static org.hamcrest.Matchers.is;
4664
import static org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST_VALUE;
4765
import static org.springframework.http.MediaType.parseMediaType;
4866
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@@ -57,6 +75,8 @@ public class ProvenanceServiceIT extends AbstractControllerIntegrationTest {
5775

5876
@Autowired
5977
private ItemService itemService;
78+
@Autowired
79+
private BitstreamService bitstreamService;
6080

6181
@Before
6282
public void setup() throws Exception {
@@ -68,6 +88,19 @@ public void setup() throws Exception {
6888
collection = CollectionBuilder.createCollection(context, parentCommunity).build();
6989
}
7090

91+
private String provenanceMetadataModified(String metadata) {
92+
// Regex to match the date pattern
93+
String datePattern = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z";
94+
Pattern pattern = Pattern.compile(datePattern);
95+
Matcher matcher = pattern.matcher(metadata);
96+
String rspModifiedProvenance = metadata;
97+
while (matcher.find()) {
98+
String dateString = matcher.group(0);
99+
rspModifiedProvenance = rspModifiedProvenance.replaceAll(dateString, "");
100+
}
101+
return rspModifiedProvenance;
102+
}
103+
71104
private JsonNode preprocessingProvenance(String responseBody) throws JsonProcessingException {
72105
//String responseBody = resultActions.andReturn().getResponse().getContentAsString();
73106
JsonNode responseJson = objectMapper.readTree(responseBody);
@@ -106,6 +139,15 @@ private Item createItem() {
106139
return item;
107140
}
108141

142+
private Bitstream createBitstream(Item item) throws SQLException, AuthorizeException, IOException {
143+
context.turnOffAuthorisationSystem();
144+
Bundle bundle = BundleBuilder.createBundle(context, item).withName("test").build();
145+
Bitstream bitstream = BitstreamBuilder.createBitstream(context, bundle,
146+
toInputStream("Test Content", defaultCharset())).build();
147+
context.restoreAuthSystemState();
148+
return bitstream;
149+
}
150+
109151
private void responseCheck(JsonNode response, String respKey) {
110152
JsonNode expectedSubStr = suite.get(respKey);
111153
JsonNode responseMetadata = response.get("metadata").get("dc.description.provenance");
@@ -123,15 +165,15 @@ private void responseCheck(JsonNode response, String respKey) {
123165
}
124166
}
125167

126-
private void itemCheck(Item item, String respKey) {
168+
private void objectCheck(DSpaceObject obj, String respKey) {
127169
String expectedSubStr = suite.get(respKey).asText();
128-
List<MetadataValue> metadata = item.getMetadata();
170+
List<MetadataValue> metadata = obj.getMetadata();
129171
boolean contain = false;
130172
for (MetadataValue value : metadata) {
131173
if (!Objects.equals(value.getMetadataField().toString(), "dc_description_provenance")) {
132174
continue;
133175
}
134-
if (value.getValue().contains(expectedSubStr)) {
176+
if (provenanceMetadataModified(value.getValue()).contains(expectedSubStr)) {
135177
contain = true;
136178
break;
137179
}
@@ -142,25 +184,25 @@ private void itemCheck(Item item, String respKey) {
142184
}
143185

144186
@Test
145-
public void makeDsicoverableTest() throws Exception {
187+
public void makeDiscoverableTest() throws Exception {
146188
String token = getAuthToken(admin.getEmail(), password);
147189
Item item = createItem();
148-
// List<Operation> ops = new ArrayList<Operation>();
149-
// ReplaceOperation replaceOperation = new ReplaceOperation("/discoverable", true);
150-
// ops.add(replaceOperation);
151-
// String patchBody = getPatchContent(ops);
190+
List<Operation> ops = new ArrayList<>();
191+
ReplaceOperation replaceOperation = new ReplaceOperation("/discoverable", true);
192+
ops.add(replaceOperation);
193+
String patchBody = getPatchContent(ops);
152194

153195
// make discoverable
154-
MvcResult mvcResult = getClient(token).perform(patch("/api/core/items/" + item.getID()
155-
+ "/discoverable")
156-
// .content(patchBody)
196+
getClient(token).perform(patch("/api/core/items/" + item.getID())
197+
.content(patchBody)
157198
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
158199
.andExpect(status().isOk())
159200
.andExpect(jsonPath("$.uuid", Matchers.is(item.getID().toString())))
160201
.andExpect(jsonPath("$.discoverable", Matchers.is(true)))
161202
.andReturn();
162-
responseCheck(Objects.requireNonNull(preprocessingProvenance(mvcResult.getResponse().getContentAsString())),
163-
"discoverable");
203+
objectCheck(itemService.find(context, item.getID()), "discoverable");
204+
//responseCheck(Objects.requireNonNull(preprocessingProvenance(mvcResult.getResponse().getContentAsString())),
205+
// "discoverable");
164206
}
165207

166208
@Test
@@ -187,6 +229,116 @@ public void mappedCollection() throws Exception {
187229
)))
188230
.andExpect(jsonPath("$._links.self.href", Matchers.containsString("/api/core/items")))
189231
;
190-
itemCheck(itemService.find(context, item.getID()), "mapped");
232+
objectCheck(itemService.find(context, item.getID()), "mapped");
233+
}
234+
235+
@Test
236+
public void addMetadata() throws Exception {
237+
Item item = createItem();
238+
239+
String adminToken = getAuthToken(admin.getEmail(), password);
240+
241+
// Modify the entityType and verify the response already contains this modification
242+
List<Operation> ops = new ArrayList<>();
243+
AddOperation addOperation = new AddOperation("/metadata/dc.title", "Test");
244+
ops.add(addOperation);
245+
String patchBody = getPatchContent(ops);
246+
getClient(adminToken).perform(patch("/api/core/items/" + item.getID())
247+
.content(patchBody)
248+
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
249+
.andExpect(status().isOk());
250+
251+
objectCheck(itemService.find(context, item.getID()), "addMetadata");
252+
}
253+
254+
@Test
255+
public void replaceMetadata() throws Exception {
256+
Item item = createItem();
257+
258+
String adminToken = getAuthToken(admin.getEmail(), password);
259+
int index = 0;
260+
// Modify the entityType and verify the response already contains this modification
261+
List<Operation> ops = new ArrayList<>();
262+
ReplaceOperation replaceOperation = new ReplaceOperation("/metadata/dc.title/" + index, "Test");
263+
ops.add(replaceOperation);
264+
String patchBody = getPatchContent(ops);
265+
getClient(adminToken).perform(patch("/api/core/items/" + item.getID())
266+
.content(patchBody)
267+
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
268+
.andExpect(status().isOk());
269+
270+
objectCheck(itemService.find(context, item.getID()), "replaceMetadata");
271+
}
272+
273+
@Test
274+
public void removeMetadata() throws Exception {
275+
Item item = createItem();
276+
277+
String adminToken = getAuthToken(admin.getEmail(), password);
278+
int index = 0;
279+
// Modify the entityType and verify the response already contains this modification
280+
List<Operation> ops = new ArrayList<>();
281+
RemoveOperation removeOperation = new RemoveOperation("/metadata/dc.title" + index);
282+
ops.add(removeOperation);
283+
String patchBody = getPatchContent(ops);
284+
getClient(adminToken).perform(patch("/api/core/items/" + item.getID())
285+
.content(patchBody)
286+
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
287+
.andExpect(status().isOk());
288+
289+
objectCheck(itemService.find(context, item.getID()), "removeMetadata");
290+
}
291+
292+
@Test
293+
public void removeMetadataBitstream() throws Exception {
294+
Item item = createItem();
295+
Bitstream bitstream = createBitstream(item);
296+
String adminToken = getAuthToken(admin.getEmail(), password);
297+
int index = 0;
298+
// Modify the entityType and verify the response already contains this modification
299+
List<Operation> ops = new ArrayList<>();
300+
AddOperation addOperation = new AddOperation("/metadata/dc.description", "test");
301+
ops.add(addOperation);
302+
String patchBody = getPatchContent(ops);
303+
getClient(adminToken).perform(patch("/api/core/bitstreams/" + bitstream.getID())
304+
.content(patchBody)
305+
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
306+
.andExpect(status().isOk());
307+
objectCheck(itemService.find(context, item.getID()), "removeBitstreamMtd");
308+
}
309+
310+
@Test
311+
public void addMetadataBitstream() throws Exception {
312+
Item item = createItem();
313+
Bitstream bitstream = createBitstream(item);
314+
String adminToken = getAuthToken(admin.getEmail(), password);
315+
// Modify the entityType and verify the response already contains this modification
316+
List<Operation> ops = new ArrayList<>();
317+
AddOperation addOperation = new AddOperation("/metadata/dc.description", "test");
318+
ops.add(addOperation);
319+
String patchBody = getPatchContent(ops);
320+
getClient(adminToken).perform(patch("/api/core/bitstreams/" + bitstream.getID())
321+
.content(patchBody)
322+
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
323+
.andExpect(status().isOk());
324+
objectCheck(itemService.find(context, item.getID()), "removeBitstreamMtd");
325+
}
326+
327+
@Test
328+
public void updateMetadataBitstream() throws Exception {
329+
Item item = createItem();
330+
Bitstream bitstream = createBitstream(item);
331+
String adminToken = getAuthToken(admin.getEmail(), password);
332+
int index = 0;
333+
// Modify the entityType and verify the response already contains this modification
334+
List<Operation> ops = new ArrayList<>();
335+
ReplaceOperation replaceOperation = new ReplaceOperation("/metadata/dc.title" + index, "test 1");
336+
ops.add(replaceOperation);
337+
String patchBody = getPatchContent(ops);
338+
getClient(adminToken).perform(patch("/api/core/bitstreams/" + bitstream.getID())
339+
.content(patchBody)
340+
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
341+
.andExpect(status().isOk());
342+
objectCheck(itemService.find(context, item.getID()), "replaceBitstreamMtd");
191343
}
192344
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"discoverable": "Item was made discoverable by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 0\nItem was in collections:\n",
3-
"mapped": "was mapped to collection"
3+
"mapped": "was mapped to collection",
4+
"addMetadata": "Item metadata (dc.title) was added by first (admin) last (admin) (admin@email.com) on",
5+
"replaceMetadata": "Item metadata (dc.title: Public item 1) was updated by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 0",
6+
"removeMetadata": "Item metadata (dc.title: Public item 1) was removed by first (admin) last (admin) (admin@email.com) on \nNo. of bitstreams: 0",
7+
"removeBitstreamMtd": "Item metadata (dc.description) was added by bitstream",
8+
"replaceBitstreamMtd": "Item metadata (dc.description: test) was updated by bitstream"
49
}
510

0 commit comments

Comments
 (0)