diff --git a/dspace-spring-rest/src/test/java/org/dspace/app/rest/BitstreamRestRepositoryIT.java b/dspace-spring-rest/src/test/java/org/dspace/app/rest/BitstreamRestRepositoryIT.java new file mode 100644 index 0000000000..178c5e4ac1 --- /dev/null +++ b/dspace-spring-rest/src/test/java/org/dspace/app/rest/BitstreamRestRepositoryIT.java @@ -0,0 +1,208 @@ +/** + * 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 org.apache.commons.codec.CharEncoding; +import org.apache.commons.io.IOUtils; +import org.dspace.app.rest.builder.BitstreamBuilder; +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.test.AbstractControllerIntegrationTest; +import org.dspace.content.Bitstream; +import org.dspace.content.Collection; +import org.dspace.content.Community; +import org.dspace.content.Item; +import org.hamcrest.Matchers; +import org.junit.Test; + +import java.io.InputStream; + +import static org.hamcrest.Matchers.not; +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; + +public class BitstreamRestRepositoryIT extends AbstractControllerIntegrationTest { + + @Test + public void findAllTest() throws Exception{ + //We turn off the authorization system in order to create the structure as defined below + context.turnOffAuthorisationSystem(); + + //** GIVEN ** + //1. A community-collection structure with one parent community with sub-community and one collection. + parentCommunity = new CommunityBuilder().createCommunity(context) + .withName("Parent Community") + .build(); + Community child1 = new CommunityBuilder().createSubCommunity(context, parentCommunity) + .withName("Sub Community") + .build(); + Collection col1 = new CollectionBuilder().createCollection(context, child1).withName("Collection 1").build(); + + //2. One public items that is readable by Anonymous + Item publicItem1 = new ItemBuilder().createItem(context, col1) + .withTitle("Test") + .withIssueDate("2010-10-17") + .withAuthor("Smith, Donald") + .withSubject("ExtraEntry") + .build(); + + String bitstreamContent = "ThisIsSomeDummyText"; + //Add a bitstream to an item + Bitstream bitstream = null; + try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) { + bitstream = BitstreamBuilder. + createBitstream(context, publicItem1, is) + .withName("Bitstream") + .withMimeType("text/plain") + .build(); + } + + //Add a bitstream to an item + Bitstream bitstream1 = null; + try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) { + bitstream1 = BitstreamBuilder. + createBitstream(context, publicItem1, is) + .withName("Bitstream1") + .withMimeType("text/plain") + .build(); + } + + getClient().perform(get("/api/core/bitstreams/")) + .andExpect(status().isOk()) + .andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$._embedded.bitstreams", Matchers.containsInAnyOrder( + BitstreamMatcher.matchBitstreamEntry(bitstream.getName(), bitstream.getID()), + BitstreamMatcher.matchBitstreamEntry(bitstream1.getName(), bitstream1.getID()) + ))) + + ; + } + + @Test + public void findOneBitstreamTest() throws Exception { + + //We turn off the authorization system in order to create the structure as defined below + context.turnOffAuthorisationSystem(); + + //** GIVEN ** + //1. A community-collection structure with one parent community with sub-community and one collection. + parentCommunity = new CommunityBuilder().createCommunity(context) + .withName("Parent Community") + .build(); + Community child1 = new CommunityBuilder().createSubCommunity(context, parentCommunity) + .withName("Sub Community") + .build(); + Collection col1 = new CollectionBuilder().createCollection(context, child1).withName("Collection 1").build(); + + //2. One public items that is readable by Anonymous + Item publicItem1 = new ItemBuilder().createItem(context, col1) + .withTitle("Test") + .withIssueDate("2010-10-17") + .withAuthor("Smith, Donald") + .withSubject("ExtraEntry") + .build(); + + String bitstreamContent = "ThisIsSomeDummyText"; + //Add a bitstream to an item + Bitstream bitstream = null; + try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) { + bitstream = BitstreamBuilder. + createBitstream(context, publicItem1, is) + .withName("Bitstream") + .withMimeType("text/plain") + .build(); + } + + //Add a bitstream to an item + Bitstream bitstream1 = null; + try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) { + bitstream1 = BitstreamBuilder. + createBitstream(context, publicItem1, is) + .withName("Bitstream1") + .withMimeType("text/plain") + .build(); + } + + getClient().perform(get("/api/core/bitstreams/"+bitstream.getID())) + .andExpect(status().isOk()) + .andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$", BitstreamMatcher.matchBitstreamEntry(bitstream.getName(), bitstream.getID()))) + .andExpect(jsonPath("$", not(BitstreamMatcher.matchBitstreamEntry(bitstream1.getName(), bitstream1.getID())))) + ; + + } + + @Test + public void findOneBitstreamRelsTest() throws Exception { + + //We turn off the authorization system in order to create the structure as defined below + context.turnOffAuthorisationSystem(); + + //** GIVEN ** + //1. A community-collection structure with one parent community with sub-community and one collection. + parentCommunity = new CommunityBuilder().createCommunity(context) + .withName("Parent Community") + .build(); + Community child1 = new CommunityBuilder().createSubCommunity(context, parentCommunity) + .withName("Sub Community") + .build(); + Collection col1 = new CollectionBuilder().createCollection(context, child1).withName("Collection 1").build(); + + //2. One public items that is readable by Anonymous + Item publicItem1 = new ItemBuilder().createItem(context, col1) + .withTitle("Test") + .withIssueDate("2010-10-17") + .withAuthor("Smith, Donald") + .withSubject("ExtraEntry") + .build(); + + String bitstreamContent = "ThisIsSomeDummyText"; + //Add a bitstream to an item + Bitstream bitstream = null; + try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) { + bitstream = BitstreamBuilder. + createBitstream(context, publicItem1, is) + .withName("Bitstream") + .withMimeType("text/plain") + .build(); + } + + //Add a bitstream to an item + Bitstream bitstream1 = null; + try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) { + bitstream1 = BitstreamBuilder. + createBitstream(context, publicItem1, is) + .withName("Bitstream1") + .withMimeType("text/plain") + .build(); + } + + getClient().perform(get("/api/core/bitstreams/"+bitstream.getID()+"/format")) + .andExpect(status().isOk()) + .andExpect(content().contentType(contentType)) + ; + + + getClient().perform(get("/api/core/bitstreams/"+bitstream.getID()+"/self")) + .andExpect(status().isOk()) + ; + + + //TODO This test fails in the current code. Authorization error +// getClient().perform(get("/api/core/bitstreams/"+bitstream.getID()+"/content")) +// .andExpect(status().isOk()) +// ; + + } + + //TODO /api/core/bitstreams/search does not yet exist (404 error) +} diff --git a/dspace-spring-rest/src/test/java/org/dspace/app/rest/builder/AbstractBuilder.java b/dspace-spring-rest/src/test/java/org/dspace/app/rest/builder/AbstractBuilder.java index ac8fb847dd..61e630ff45 100644 --- a/dspace-spring-rest/src/test/java/org/dspace/app/rest/builder/AbstractBuilder.java +++ b/dspace-spring-rest/src/test/java/org/dspace/app/rest/builder/AbstractBuilder.java @@ -22,7 +22,15 @@ import org.dspace.authorize.service.ResourcePolicyService; import org.dspace.content.DSpaceObject; import org.dspace.content.Item; import org.dspace.content.factory.ContentServiceFactory; -import org.dspace.content.service.*; +import org.dspace.content.service.BitstreamFormatService; +import org.dspace.content.service.BitstreamService; +import org.dspace.content.service.BundleService; +import org.dspace.content.service.CollectionService; +import org.dspace.content.service.CommunityService; +import org.dspace.content.service.DSpaceObjectService; +import org.dspace.content.service.InstallItemService; +import org.dspace.content.service.ItemService; +import org.dspace.content.service.WorkspaceItemService; import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.discovery.IndexingService; @@ -60,6 +68,7 @@ public abstract class AbstractBuilder { static AuthorizeService authorizeService; static ResourcePolicyService resourcePolicyService; static IndexingService indexingService; + static BitstreamFormatService bitstreamFormatService; protected Context context; @@ -86,6 +95,7 @@ public abstract class AbstractBuilder { authorizeService = AuthorizeServiceFactory.getInstance().getAuthorizeService(); resourcePolicyService = AuthorizeServiceFactory.getInstance().getResourcePolicyService(); indexingService = DSpaceServicesFactory.getInstance().getServiceManager().getServiceByName(IndexingService.class.getName(),IndexingService.class); + bitstreamFormatService = ContentServiceFactory.getInstance().getBitstreamFormatService(); } public static void destroy() { @@ -101,6 +111,7 @@ public abstract class AbstractBuilder { authorizeService = null; resourcePolicyService = null; indexingService = null; + bitstreamFormatService = null; } public static void cleanupObjects() throws Exception { diff --git a/dspace-spring-rest/src/test/java/org/dspace/app/rest/builder/BitstreamBuilder.java b/dspace-spring-rest/src/test/java/org/dspace/app/rest/builder/BitstreamBuilder.java index ae16d37a6c..5eb6c9ee15 100644 --- a/dspace-spring-rest/src/test/java/org/dspace/app/rest/builder/BitstreamBuilder.java +++ b/dspace-spring-rest/src/test/java/org/dspace/app/rest/builder/BitstreamBuilder.java @@ -23,9 +23,6 @@ import org.dspace.eperson.Group; /** * Builder class to build bitstreams in test cases - * - * @author Tom Desair (tom dot desair at atmire dot com) - * @author Raf Ponsaerts (raf dot ponsaerts at atmire dot com) */ public class BitstreamBuilder extends AbstractBuilder{ @@ -127,4 +124,4 @@ public class BitstreamBuilder extends AbstractBuilder{ protected DSpaceObjectService getDsoService() { return bitstreamService; } -} +} \ No newline at end of file diff --git a/dspace-spring-rest/src/test/java/org/dspace/app/rest/matcher/BitstreamMatcher.java b/dspace-spring-rest/src/test/java/org/dspace/app/rest/matcher/BitstreamMatcher.java new file mode 100644 index 0000000000..592d4d3e39 --- /dev/null +++ b/dspace-spring-rest/src/test/java/org/dspace/app/rest/matcher/BitstreamMatcher.java @@ -0,0 +1,40 @@ +/** + * 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.matcher; + +import org.hamcrest.Matcher; + +import java.util.UUID; + +import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; + +public class BitstreamMatcher { + + public static Matcher matchBitstreamEntry(String name, UUID uuid) { + return allOf( + //Check core metadata (the JSON Path expression evaluates to a collection so we have to use contains) + hasJsonPath("$.uuid", is(uuid.toString())), + hasJsonPath("$.name", is(name)), + //Check links + matchBitstreamLinks(uuid) + ); + } + + private static Matcher matchBitstreamLinks(UUID uuid) { + return allOf( + hasJsonPath("$._links.format.href", containsString("/api/core/bitstreams/" + uuid + "/format")), + hasJsonPath("$._links.self.href", containsString("/api/core/bitstreams/"+uuid)), + hasJsonPath("$._links.content.href", containsString("/api/core/bitstreams/"+uuid+"/content")) + ); + } + + +}