Commits

Anonymous committed a464cf3

Extract ChempoundIntegrationTest

Comments (0)

Files changed (2)

chempound-app/src/test/java/net/chempound/ChempoundApplicationTest.java

-package net.chempound;
-
-import com.hp.hpl.jena.query.Dataset;
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.Resource;
-import com.hp.hpl.jena.rdf.model.Seq;
-import com.hp.hpl.jena.tdb.TDBFactory;
-import net.chempound.content.DefaultDepositRequest;
-import net.chempound.content.DepositRequest;
-import net.chempound.content.DepositResponse;
-import net.chempound.datastore.JenaTripleStore;
-import net.chempound.deposit.DeleteHandler;
-import net.chempound.deposit.DepositHandler;
-import net.chempound.deposit.ResourceMapGenerator;
-import net.chempound.indexes.IndexManager;
-import net.chempound.rdf.Bibo;
-import net.chempound.rdf.ORE;
-import net.chempound.rdf.chempound.ChempoundAggregation;
-import net.chempound.rdf.dc.DCUtils;
-import net.chempound.rdf.foaf.FoafUtil;
-import net.chempound.rdf.foaf.Person;
-import net.chempound.storage.InMemoryResource;
-import net.chempound.storage.InMemoryResourceStore;
-import net.chempound.storage.ResourceStore;
-import net.chempound.util.MimeType;
-import org.apache.commons.io.FileUtils;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.URI;
-import java.util.Date;
-import java.util.List;
-import java.util.Set;
-import java.util.UUID;
-
-import static org.junit.Assert.*;
-
-/**
- * @author Sam Adams
- */
-public class ChempoundApplicationTest {
-
-    private File workspace;
-    private Dataset dataSet;
-    private Chempound chempound;
-
-    @Before
-    public void setupWorkspace() throws Exception {
-        this.workspace = createWorkspace();
-
-        dataSet = TDBFactory.createDataset();
-        JenaTripleStore dataStore = new JenaTripleStore(dataSet);
-        ResourceStore resourceStore = new InMemoryResourceStore();
-        URI baseUri = URI.create("http://chempound.example.com/repo/");
-        URI contentRoot = baseUri.resolve("content/");
-        final DepositHandler depositHandler = new DepositHandler(new EntryUriGenerator(dataStore, contentRoot),
-                dataStore, resourceStore, new IndexManager(), new ResourceMapGenerator(baseUri), contentRoot);
-        final DeleteHandler deleteHandler = new DeleteHandler(dataStore, resourceStore, contentRoot);
-        chempound = new DefaultChempound(resourceStore, dataStore, depositHandler, new DefaultRepositoryListenerRegistry(), deleteHandler, contentRoot);
-    }
-
-    @After
-    public void cleanWorkspace() throws IOException {
-        FileUtils.deleteDirectory(workspace);
-    }
-
-    private static File createWorkspace() throws IOException {
-        File workspace;
-        do {
-            workspace = new File("target/tmp/-"+ UUID.randomUUID());
-        } while (workspace.exists());
-        FileUtils.forceMkdir(workspace);
-        return workspace;
-    }
-
-    @Test
-    public void testDepositEmptyItem() throws Exception {
-        DepositRequest request = new DefaultDepositRequest();
-        DepositResponse response = chempound.loadContent(request);
-        assertNotNull(response);
-        URI uri = response.getUri();
-        assertNotNull(uri);
-
-        Model model = dataSet.getNamedModel(uri.toString());
-        try {
-            assertNotNull(model);
-            assertFalse(model.isEmpty());
-        } finally {
-            model.close();
-        }
-    }
-
-    @Test
-    public void testDepositItem() throws Exception {
-        DepositRequest request = new DefaultDepositRequest();
-        request.addResource(new InMemoryResource("foo.txt", "wibble wobble"));
-        request.getMetadataModel().getResource("foo.txt");
-        DepositResponse response = chempound.loadContent(request);
-        assertNotNull(response);
-        URI uri = response.getUri();
-        assertNotNull(uri);
-        assertTrue(uri.toString().startsWith("http://chempound.example.com/repo/content"));
-        Model model = dataSet.getNamedModel(uri.toString());
-        try {
-            assertNotNull(model);
-            assertFalse(model.isEmpty());
-        } finally {
-            model.close();
-        }
-    }
-
-    @Test
-    public void testDepositWithSlug() throws Exception {
-        DepositRequest request = new DefaultDepositRequest();
-        request.setSlug("foobar");
-        request.addResource(new InMemoryResource("foo.txt", "wibble wobble"));
-        request.getMetadataModel().getResource("foo.txt");
-        DepositResponse response = chempound.loadContent(request);
-        assertNotNull(response);
-        URI uri = response.getUri();
-        assertEquals(URI.create("http://chempound.example.com/repo/content/foobar/"), uri);
-
-        Model model = dataSet.getNamedModel(uri.toString());
-        try {
-            assertNotNull(model);
-            assertFalse(model.isEmpty());
-        } finally {
-            model.close();
-        }
-    }
-
-    @Test
-    public void testDepositSetsCreatedDate() throws Exception {
-        DepositRequest request = new DefaultDepositRequest();
-        DepositResponse response = chempound.loadContent(request);
-        URI uri = response.getUri();
-        Model model = response.getMetadataModel();
-        try {
-            Date created = DCUtils.getCreated(model.getResource(uri.toString()));
-            assertNotNull(created);
-        } finally {
-            model.close();
-        }
-    }
-
-    @Test
-    public void testDepositSetsModifiedDate() throws Exception {
-        DepositRequest request = new DefaultDepositRequest();
-        DepositResponse response = chempound.loadContent(request);
-        URI uri = response.getUri();
-        Model model = response.getMetadataModel();
-        try {
-            Date modified = DCUtils.getModified(model.getResource(uri.toString()));
-            assertNotNull(modified);
-        } finally {
-            model.close();
-        }
-    }
-
-    @Test
-    public void testDepositAcceptsAuthorList() throws IOException {
-        DepositRequest request = new DefaultDepositRequest();
-        addAuthors(request);
-        DepositResponse response = chempound.loadContent(request);
-
-        String uri = response.getUri().toString();
-        Model model = dataSet.getNamedModel(uri);
-        try {
-            Seq authorList = model.getResource(uri).getRequiredProperty(Bibo.authorList).getSeq();
-            assertEquals(2, authorList.size());
-            assertEquals("John Smith", authorList.getResource(1).as(Person.class).getName());
-            assertEquals("Joe Bloggs", authorList.getResource(2).as(Person.class).getName());
-        } finally {
-            model.close();
-        }
-    }
-
-    @Test
-    public void testDepositGeneratesAuthorUris() throws IOException {
-        DepositRequest request = new DefaultDepositRequest();
-        addAuthors(request);
-
-        DepositResponse response = chempound.loadContent(request);
-        String uri = response.getUri().toString();
-        Model model = dataSet.getNamedModel(uri);
-        try {
-            Seq authorList = model.getResource(uri).getRequiredProperty(Bibo.authorList).getSeq();
-            assertEquals(2, authorList.size());
-            assertNotNull(authorList.getResource(1).getURI());
-            assertNotNull(authorList.getResource(2).getURI());
-        } finally {
-            model.close();
-        }
-    }
-
-    private void addAuthors(DepositRequest request) {
-        ChempoundAggregation item = request.getResourceMetadata();
-
-        Person p1 = FoafUtil.createPerson(request.getMetadataModel());
-        p1.setName("John Smith");
-        item.addAuthor(p1);
-
-        Person p2 = FoafUtil.createPerson(request.getMetadataModel());
-        p2.setName("Joe Bloggs");
-        item.addAuthor(p2);
-    }
-
-
-    @Test
-    public void testDepositMapsBiboAuthorListDctContibutors() throws IOException {
-        DepositRequest request = new DefaultDepositRequest();
-        addAuthors(request);
-        DepositResponse response = chempound.loadContent(request);
-
-        String uri = response.getUri().toString();
-        Model model = dataSet.getNamedModel(uri);
-        try {
-            ChempoundAggregation aggregation = model.getResource(uri).as(ChempoundAggregation.class);
-            List<Person> authorList = aggregation.getAuthors();
-            assertEquals(2, authorList.size());
-
-            Set<Person> contributors = aggregation.getContributors();
-            assertEquals(authorList.size(), contributors.size());
-            assertTrue(contributors.containsAll(authorList));
-
-        } finally {
-            model.close();
-        }
-    }
-
-    @Test
-    public void testDepositCreatesResourceMaps() throws IOException {
-        DepositRequest request = new DefaultDepositRequest();
-        request.setSlug("foo");
-
-        DepositResponse response = chempound.loadContent(request);
-        assertNotNull(response);
-        URI uri = response.getUri();
-        assertNotNull(uri);
-
-        Model model = dataSet.getNamedModel(uri.toString());
-        try {
-            Resource item = model.getResource("http://chempound.example.com/repo/content/foo/");
-            Resource html = model.getResource("http://chempound.example.com/repo/content/foo/index.html");
-            Resource rdf = model.getResource("http://chempound.example.com/repo/content/foo/index.rdf");
-            Resource n3 = model.getResource("http://chempound.example.com/repo/content/foo/index.n3");
-            Resource ttl = model.getResource("http://chempound.example.com/repo/content/foo/index.ttl");
-
-            assertTrue(model.contains(item, ORE.isDescribedBy, html));
-            assertTrue(model.contains(item, ORE.isDescribedBy, rdf));
-            assertTrue(model.contains(item, ORE.isDescribedBy, n3));
-            assertTrue(model.contains(item, ORE.isDescribedBy, ttl));
-            assertTrue(model.contains(html, ORE.describes, item));
-            assertTrue(model.contains(rdf, ORE.describes, item));
-            assertTrue(model.contains(n3, ORE.describes, item));
-            assertTrue(model.contains(ttl, ORE.describes, item));
-
-            assertEquals(new MimeType("text/html"), DCUtils.getMimeType(html));
-            assertEquals(new MimeType("application/rdf+xml"), DCUtils.getMimeType(rdf));
-            assertEquals(new MimeType("text/n3"), DCUtils.getMimeType(n3));
-            assertEquals(new MimeType("application/x-turtle"), DCUtils.getMimeType(ttl));
-
-        } finally {
-            model.close();
-        }
-    }
-
-    @Test
-    public void testOreAggregation() throws IOException {
-        DepositRequest request = new DefaultDepositRequest();
-        request.addResource(new InMemoryResource("foo.txt", "foo"));
-        request.addResource(new InMemoryResource("bar.txt", "bar"));
-        request.setSlug("foo");
-
-        chempound.loadContent(request);
-
-        Model model = dataSet.getNamedModel("http://chempound.example.com/repo/content/foo/");
-        Resource aggr = model.getResource("http://chempound.example.com/repo/content/foo/");
-        Resource foo = model.getResource("http://chempound.example.com/repo/content/foo/foo.txt");
-        Resource bar = model.getResource("http://chempound.example.com/repo/content/foo/bar.txt");
-
-        assertTrue(aggr.hasProperty(ORE.aggregates, foo));
-        assertTrue(aggr.hasProperty(ORE.aggregates, bar));
-        assertTrue(foo.hasProperty(ORE.isAggregatedBy, aggr));
-        assertTrue(bar.hasProperty(ORE.isAggregatedBy, aggr));
-
-    }
-
-}

chempound-app/src/test/java/net/chempound/integration/ChempoundIntegrationTest.java

+package net.chempound.integration;
+
+import com.hp.hpl.jena.query.Dataset;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Seq;
+import com.hp.hpl.jena.tdb.TDBFactory;
+import net.chempound.Chempound;
+import net.chempound.DefaultChempound;
+import net.chempound.DefaultRepositoryListenerRegistry;
+import net.chempound.EntryUriGenerator;
+import net.chempound.content.DefaultDepositRequest;
+import net.chempound.content.DepositRequest;
+import net.chempound.content.DepositResponse;
+import net.chempound.datastore.JenaTripleStore;
+import net.chempound.deposit.DeleteHandler;
+import net.chempound.deposit.DepositHandler;
+import net.chempound.deposit.ResourceMapGenerator;
+import net.chempound.indexes.IndexManager;
+import net.chempound.rdf.Bibo;
+import net.chempound.rdf.ORE;
+import net.chempound.rdf.chempound.ChempoundAggregation;
+import net.chempound.rdf.dc.DCUtils;
+import net.chempound.rdf.foaf.FoafUtil;
+import net.chempound.rdf.foaf.Person;
+import net.chempound.storage.InMemoryResource;
+import net.chempound.storage.InMemoryResourceStore;
+import net.chempound.storage.ResourceStore;
+import net.chempound.util.MimeType;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.Date;
+import java.util.List;
+import java.util.Set;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author Sam Adams
+ */
+public class ChempoundIntegrationTest {
+
+    private static final URI BASE_URI = URI.create("http://chempound.example.com/repo/");
+    private static final URI CONTENT_ROOT = BASE_URI.resolve("content/");
+
+    private Dataset dataSet;
+    private Chempound chempound;
+
+    @Before
+    public void setUp() throws Exception {
+        dataSet = TDBFactory.createDataset();
+
+        final JenaTripleStore dataStore = new JenaTripleStore(dataSet);
+        final ResourceStore resourceStore = new InMemoryResourceStore();
+        final DepositHandler depositHandler = new DepositHandler(new EntryUriGenerator(dataStore, CONTENT_ROOT), dataStore, resourceStore, new IndexManager(), new ResourceMapGenerator(BASE_URI), CONTENT_ROOT);
+        final DeleteHandler deleteHandler = new DeleteHandler(dataStore, resourceStore, CONTENT_ROOT);
+
+        chempound = new DefaultChempound(resourceStore, dataStore, depositHandler, new DefaultRepositoryListenerRegistry(), deleteHandler, CONTENT_ROOT);
+    }
+
+    @Test
+    public void testDepositEmptyItem() throws Exception {
+        DepositRequest request = new DefaultDepositRequest();
+        DepositResponse response = chempound.loadContent(request);
+        assertNotNull(response);
+        URI uri = response.getUri();
+        assertNotNull(uri);
+
+        Model model = dataSet.getNamedModel(uri.toString());
+        try {
+            assertNotNull(model);
+            assertFalse(model.isEmpty());
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    @Test
+    public void testDepositItem() throws Exception {
+        DepositRequest request = new DefaultDepositRequest();
+        request.addResource(new InMemoryResource("foo.txt", "wibble wobble"));
+        request.getMetadataModel().getResource("foo.txt");
+        DepositResponse response = chempound.loadContent(request);
+        assertNotNull(response);
+        URI uri = response.getUri();
+        assertNotNull(uri);
+        assertTrue(uri.toString().startsWith("http://chempound.example.com/repo/content"));
+        Model model = dataSet.getNamedModel(uri.toString());
+        try {
+            assertNotNull(model);
+            assertFalse(model.isEmpty());
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    @Test
+    public void testDepositWithSlug() throws Exception {
+        DepositRequest request = new DefaultDepositRequest();
+        request.setSlug("foobar");
+        request.addResource(new InMemoryResource("foo.txt", "wibble wobble"));
+        request.getMetadataModel().getResource("foo.txt");
+        DepositResponse response = chempound.loadContent(request);
+        assertNotNull(response);
+        URI uri = response.getUri();
+        assertEquals(URI.create("http://chempound.example.com/repo/content/foobar/"), uri);
+
+        Model model = dataSet.getNamedModel(uri.toString());
+        try {
+            assertNotNull(model);
+            assertFalse(model.isEmpty());
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    @Test
+    public void testDepositSetsCreatedDate() throws Exception {
+        DepositRequest request = new DefaultDepositRequest();
+        DepositResponse response = chempound.loadContent(request);
+        URI uri = response.getUri();
+        Model model = response.getMetadataModel();
+        try {
+            Date created = DCUtils.getCreated(model.getResource(uri.toString()));
+            assertNotNull(created);
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    @Test
+    public void testDepositSetsModifiedDate() throws Exception {
+        DepositRequest request = new DefaultDepositRequest();
+        DepositResponse response = chempound.loadContent(request);
+        URI uri = response.getUri();
+        Model model = response.getMetadataModel();
+        try {
+            Date modified = DCUtils.getModified(model.getResource(uri.toString()));
+            assertNotNull(modified);
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    @Test
+    public void testDepositAcceptsAuthorList() throws IOException {
+        DepositRequest request = new DefaultDepositRequest();
+        addAuthors(request);
+        DepositResponse response = chempound.loadContent(request);
+
+        String uri = response.getUri().toString();
+        Model model = dataSet.getNamedModel(uri);
+        try {
+            Seq authorList = model.getResource(uri).getRequiredProperty(Bibo.authorList).getSeq();
+            assertEquals(2, authorList.size());
+            assertEquals("John Smith", authorList.getResource(1).as(Person.class).getName());
+            assertEquals("Joe Bloggs", authorList.getResource(2).as(Person.class).getName());
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    @Test
+    public void testDepositGeneratesAuthorUris() throws IOException {
+        DepositRequest request = new DefaultDepositRequest();
+        addAuthors(request);
+
+        DepositResponse response = chempound.loadContent(request);
+        String uri = response.getUri().toString();
+        Model model = dataSet.getNamedModel(uri);
+        try {
+            Seq authorList = model.getResource(uri).getRequiredProperty(Bibo.authorList).getSeq();
+            assertEquals(2, authorList.size());
+            assertNotNull(authorList.getResource(1).getURI());
+            assertNotNull(authorList.getResource(2).getURI());
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    private void addAuthors(DepositRequest request) {
+        ChempoundAggregation item = request.getResourceMetadata();
+
+        Person p1 = FoafUtil.createPerson(request.getMetadataModel());
+        p1.setName("John Smith");
+        item.addAuthor(p1);
+
+        Person p2 = FoafUtil.createPerson(request.getMetadataModel());
+        p2.setName("Joe Bloggs");
+        item.addAuthor(p2);
+    }
+
+
+    @Test
+    public void testDepositMapsBiboAuthorListDctContibutors() throws IOException {
+        DepositRequest request = new DefaultDepositRequest();
+        addAuthors(request);
+        DepositResponse response = chempound.loadContent(request);
+
+        String uri = response.getUri().toString();
+        Model model = dataSet.getNamedModel(uri);
+        try {
+            ChempoundAggregation aggregation = model.getResource(uri).as(ChempoundAggregation.class);
+            List<Person> authorList = aggregation.getAuthors();
+            assertEquals(2, authorList.size());
+
+            Set<Person> contributors = aggregation.getContributors();
+            assertEquals(authorList.size(), contributors.size());
+            assertTrue(contributors.containsAll(authorList));
+
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    @Test
+    public void testDepositCreatesResourceMaps() throws IOException {
+        DepositRequest request = new DefaultDepositRequest();
+        request.setSlug("foo");
+
+        DepositResponse response = chempound.loadContent(request);
+        assertNotNull(response);
+        URI uri = response.getUri();
+        assertNotNull(uri);
+
+        Model model = dataSet.getNamedModel(uri.toString());
+        try {
+            Resource item = model.getResource("http://chempound.example.com/repo/content/foo/");
+            Resource html = model.getResource("http://chempound.example.com/repo/content/foo/index.html");
+            Resource rdf = model.getResource("http://chempound.example.com/repo/content/foo/index.rdf");
+            Resource n3 = model.getResource("http://chempound.example.com/repo/content/foo/index.n3");
+            Resource ttl = model.getResource("http://chempound.example.com/repo/content/foo/index.ttl");
+
+            assertTrue(model.contains(item, ORE.isDescribedBy, html));
+            assertTrue(model.contains(item, ORE.isDescribedBy, rdf));
+            assertTrue(model.contains(item, ORE.isDescribedBy, n3));
+            assertTrue(model.contains(item, ORE.isDescribedBy, ttl));
+            assertTrue(model.contains(html, ORE.describes, item));
+            assertTrue(model.contains(rdf, ORE.describes, item));
+            assertTrue(model.contains(n3, ORE.describes, item));
+            assertTrue(model.contains(ttl, ORE.describes, item));
+
+            assertEquals(new MimeType("text/html"), DCUtils.getMimeType(html));
+            assertEquals(new MimeType("application/rdf+xml"), DCUtils.getMimeType(rdf));
+            assertEquals(new MimeType("text/n3"), DCUtils.getMimeType(n3));
+            assertEquals(new MimeType("application/x-turtle"), DCUtils.getMimeType(ttl));
+
+        } finally {
+            closeQuietly(model);
+        }
+    }
+
+    @Test
+    public void testOreAggregation() throws IOException {
+        DepositRequest request = new DefaultDepositRequest();
+        request.addResource(new InMemoryResource("foo.txt", "foo"));
+        request.addResource(new InMemoryResource("bar.txt", "bar"));
+        request.setSlug("foo");
+
+        chempound.loadContent(request);
+
+        Model model = dataSet.getNamedModel("http://chempound.example.com/repo/content/foo/");
+        Resource aggr = model.getResource("http://chempound.example.com/repo/content/foo/");
+        Resource foo = model.getResource("http://chempound.example.com/repo/content/foo/foo.txt");
+        Resource bar = model.getResource("http://chempound.example.com/repo/content/foo/bar.txt");
+
+        assertTrue(aggr.hasProperty(ORE.aggregates, foo));
+        assertTrue(aggr.hasProperty(ORE.aggregates, bar));
+        assertTrue(foo.hasProperty(ORE.isAggregatedBy, aggr));
+        assertTrue(bar.hasProperty(ORE.isAggregatedBy, aggr));
+
+    }
+
+    private void closeQuietly(final Model model) {
+        if (model != null) {
+            model.close();
+        }
+    }
+}