Add integration tests for bundle GET endpoint

This commit is contained in:
Jelle Pelgrims
2019-08-21 08:01:56 +02:00
parent 591f32bfe3
commit 5daf5be97d
9 changed files with 279 additions and 3 deletions

View File

@@ -1,3 +1,10 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.converter;
import java.util.stream.Collectors;

View File

@@ -1,3 +1,10 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.model;
import java.util.List;
@@ -32,7 +39,6 @@ public class BundleRest extends DSpaceObjectRest {
return NAME;
}
@JsonIgnore
@LinkRest(linkClass = Bitstream.class)
public BitstreamRest getPrimaryBitstream() {

View File

@@ -1,3 +1,10 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.model.hateoas;
import org.dspace.app.rest.model.BundleRest;

View File

@@ -1,3 +1,10 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.repository;
import java.sql.SQLException;

View File

@@ -0,0 +1,111 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.endsWith;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.InputStream;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.io.IOUtils;
import org.dspace.app.rest.builder.BitstreamBuilder;
import org.dspace.app.rest.builder.BundleBuilder;
import org.dspace.app.rest.builder.CollectionBuilder;
import org.dspace.app.rest.builder.CommunityBuilder;
import org.dspace.app.rest.builder.ItemBuilder;
import org.dspace.app.rest.matcher.BitstreamMatcher;
import org.dspace.app.rest.matcher.BundleMatcher;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
public class BundleRestRepositoryIT extends AbstractControllerIntegrationTest {
private Collection collection;
private Item item;
private Bundle bundle1;
private Bundle bundle2;
private Bitstream bitstream1;
@Before
public void setUp() throws Exception {
super.setUp();
System.out.println("test");
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
collection = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
item = ItemBuilder.createItem(context, collection)
.withTitle("Public item 1")
.withIssueDate("2017-10-17")
.withAuthor("Smith, Donald").withAuthor("Doe, John")
.withSubject("ExtraEntry")
.build();
String bitstreamContent = "Dummy content";
try (InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) {
bitstream1 = BitstreamBuilder.createBitstream(context, item, is)
.withName("Bitstream")
.withMimeType("text/plain")
.build();
}
bundle1 = BundleBuilder.createBundle(context, item)
.withName("testname")
.withBitstream(bitstream1)
.build();
bundle2 = BundleBuilder.createBundle(context, item).withName("test2").build();
context.restoreAuthSystemState();
}
@Test
public void GetSingleBundle() throws Exception {
getClient().perform(get("/api/core/bundles/" + bundle1.getID()))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$", BundleMatcher.matchBundle(bundle1)))
.andExpect(jsonPath("$._embedded.bitstreams._embedded.bitstreams", containsInAnyOrder(
BitstreamMatcher.matchBitstreamEntry(bitstream1.getID(), bitstream1.getSizeBytes())))
)
;
}
@Test
public void getItemBundles() throws Exception {
getClient().perform(get("/api/core/items/" + item.getID() + "/bundles"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.bundles", Matchers.hasItems(
BundleMatcher.matchBundle(bundle1),
BundleMatcher.matchBundle(bundle2)
)))
.andExpect(jsonPath("$._links.self.href", endsWith("/api/core/items/" + item.getID() + "/bundles")))
;
}
}

View File

@@ -7,11 +7,13 @@
*/
package org.dspace.app.rest.builder;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.builder.util.AbstractBuilderCleanupUtil;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.authorize.service.ResourcePolicyService;
@@ -190,7 +192,7 @@ public abstract class AbstractBuilder<T, S> {
*/
public abstract void cleanup() throws Exception;
public abstract T build();
public abstract T build() throws SQLException, AuthorizeException;
public abstract void delete(T dso) throws Exception;

View File

@@ -7,9 +7,11 @@
*/
package org.dspace.app.rest.builder;
import java.sql.SQLException;
import java.util.Date;
import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
@@ -216,7 +218,7 @@ public abstract class AbstractDSpaceObjectBuilder<T extends DSpaceObject>
return (B) this;
}
public abstract T build();
public abstract T build() throws SQLException, AuthorizeException;
public void delete(T dso) throws Exception {

View File

@@ -0,0 +1,70 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.builder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Item;
import org.dspace.content.service.DSpaceObjectService;
import org.dspace.core.Context;
public class BundleBuilder extends AbstractDSpaceObjectBuilder<Bundle> {
private Bundle bundle;
private Item item;
private String name;
private List<Bitstream> bitstreams = new ArrayList<>();
protected BundleBuilder(Context context) {
super(context);
}
public static BundleBuilder createBundle(final Context context, final Item item) {
BundleBuilder builder = new BundleBuilder(context);
return builder.create(context, item);
}
private BundleBuilder create(Context context, Item item) {
this.context = context;
this.item = item;
return this;
}
public BundleBuilder withName(String name) {
this.name = name;
return this;
}
public BundleBuilder withBitstream(Bitstream bitstream) {
this.bitstreams.add(bitstream);
return this;
}
public void cleanup() throws Exception {
delete(bundle);
}
protected DSpaceObjectService<Bundle> getService() {
return bundleService;
}
public Bundle build() throws SQLException, AuthorizeException {
bundle = bundleService.create(context, item, name);
for (Bitstream bitstream: bitstreams) {
bundleService.addBitstream(context, bundle, bitstream);
}
return bundle;
}
}

View File

@@ -0,0 +1,64 @@
package org.dspace.app.rest.matcher;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.core.Constants;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
public class BundleMatcher {
private BundleMatcher() { }
public static Matcher<? super Object> matchBundle(Bundle bundle) {
return matchBundle(
bundle.getName(),
bundle.getID(),
bundle.getHandle(),
bundle.getType(),
bundle.getBitstreams()
);
}
public static Matcher<? super Object> matchBundle(String name,
UUID uuid,
String handle,
int type,
List<Bitstream> bitstreams) {
return allOf(
hasJsonPath("$.uuid", is(uuid.toString())),
hasJsonPath("$.name", is(name)),
hasJsonPath("$.handle", is(handle)),
hasJsonPath("$.type", is(Constants.typeText[type].toLowerCase())),
hasJsonPath("$.metadata", Matchers.allOf(
MetadataMatcher.matchMetadata("dc.title", name)
)),
hasJsonPath("$._embedded.bitstreams._embedded.bitstreams", Matchers.containsInAnyOrder(
bitstreams
.stream()
.map(x -> BitstreamMatcher.matchBitstreamEntry(x.getID(), x.getSizeBytes()))
.collect(Collectors.toList())
)),
matchLinks(uuid)
);
}
private static Matcher<? super Object> matchLinks(UUID uuid) {
return allOf(
hasJsonPath("$._links.primaryBitstream.href", endsWith("/api/core/bundles/" + uuid + "/primaryBitstream")),
hasJsonPath("$._links.bitstreams.href", endsWith("/api/core/bundles/" + uuid + "/bitstreams")),
hasJsonPath("$._links.self.href", endsWith("/api/core/bundles/" + uuid))
);
}
}