Commits

Anonymous committed 3366eff

Extract DeleteHandler from DefaultChempound

Comments (0)

Files changed (5)

chempound-app/src/main/java/net/chempound/DefaultChempound.java

 package net.chempound;
 
-import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.Resource;
 import net.chempound.bean.ItemInfo;
 import net.chempound.config.ContentUri;
 import net.chempound.content.DepositRequest;
 import net.chempound.content.DepositResponse;
 import net.chempound.datastore.TripleStore;
+import net.chempound.deposit.DeleteHandler;
 import net.chempound.deposit.DepositHandler;
 import net.chempound.plugin.RepositoryListener;
 import net.chempound.plugin.RepositoryListenerRegistry;
-import net.chempound.rdf.chempound.Item;
-import net.chempound.rdf.ore.AggregatedResource;
-import net.chempound.rdf.ore.Aggregation;
 import net.chempound.storage.LocalResource;
 import net.chempound.storage.ResourceStore;
 
 import javax.inject.Inject;
+import javax.inject.Singleton;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
-import java.util.ArrayList;
 import java.util.List;
 
 /**
  * @author Sam Adams
  */
+@Singleton
 public class DefaultChempound implements Chempound {
 
     private final ResourceStore resourceStore;
     private final RepositoryListenerRegistry listenerRegistry;
 
     private final DepositHandler depositHandler;
+    private final DeleteHandler deleteHandler;
 
     private final URI contentRoot;
 
     @Inject
-    public DefaultChempound(final ResourceStore resourceStore, final TripleStore dataStore, final DepositHandler depositHandler, final RepositoryListenerRegistry listenerRegistry, @ContentUri final URI contentRoot) throws Exception {
+    public DefaultChempound(final ResourceStore resourceStore, final TripleStore dataStore, final DepositHandler depositHandler, final RepositoryListenerRegistry listenerRegistry, final DeleteHandler deleteHandler, @ContentUri final URI contentRoot) throws Exception {
         this.tripleStore = dataStore;
         this.resourceStore = resourceStore;
         this.depositHandler = depositHandler;
         this.listenerRegistry = listenerRegistry;
+        this.deleteHandler = deleteHandler;
         this.contentRoot = contentRoot;
 
         init();
 
     @Override
     public void deleteEntity(final URI uri) throws IOException {
-        final Model model = tripleStore.getModel(uri);
-        final Item item = model.getResource(uri.toString()).as(Item.class);
-        removeFromCollections(item.as(AggregatedResource.class));
-
-        final List<URI> models = new ArrayList<URI>();
-        models.add(uri);
-        models.add(uri.resolve("#rdf"));
-
-        final List<String> resources = new ArrayList<String>();
-        for (final AggregatedResource resource : item.getAggregatedResources()) {
-            final URI resourceURI = contentRoot.relativize(resource.getUri());
-            resources.add(resourceURI.toString());
-        }
-
-        resourceStore.deleteItems(resources);
-        tripleStore.deleteModels(models);
+        deleteHandler.deleteEntity(uri);
     }
 
     @Override
         final LocalResource resource = resourceStore.retrieveItem(imagePath.toString());
         return resource.openInputStream();
     }
-
-    private void removeFromCollections(final AggregatedResource item) {
-        for (final Aggregation aggregation : item.getAggregatingResources()) {
-            final Model model = tripleStore.getModel(aggregation.getUri());
-            model.getResource(aggregation.getURI())
-                .as(Aggregation.class)
-                .removeAggregatedResource(item);
-        }
-    }
-
 }

chempound-app/src/main/java/net/chempound/deposit/DeleteHandler.java

+package net.chempound.deposit;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import net.chempound.config.ContentUri;
+import net.chempound.datastore.TripleStore;
+import net.chempound.rdf.chempound.Item;
+import net.chempound.rdf.ore.AggregatedResource;
+import net.chempound.rdf.ore.Aggregation;
+import net.chempound.storage.ResourceStore;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+
+@Singleton
+public class DeleteHandler {
+
+    private final TripleStore tripleStore;
+    private final ResourceStore resourceStore;
+    private final URI contentRoot;
+
+    @Inject
+    public DeleteHandler(final TripleStore tripleStore, final ResourceStore resourceStore, final @ContentUri URI contentRoot) {
+        this.tripleStore = tripleStore;
+        this.resourceStore = resourceStore;
+        this.contentRoot = contentRoot;
+    }
+
+    public void deleteEntity(final URI uri) throws IOException {
+        final Model model = tripleStore.getModel(uri);
+        final Item item = model.getResource(uri.toString()).as(Item.class);
+        removeFromCollections(item.as(AggregatedResource.class));
+
+        final List<URI> models = getModelList(uri);
+        final List<String> resources = getResourceList(item);
+
+        resourceStore.deleteItems(resources);
+        tripleStore.deleteModels(models);
+    }
+
+    private List<URI> getModelList(final URI uri) {
+        final List<URI> models = new ArrayList<URI>();
+        models.add(uri);
+        models.add(uri.resolve("#rdf"));
+        return models;
+    }
+
+    private List<String> getResourceList(final Item item) {
+        final List<String> resources = new ArrayList<String>();
+        for (final AggregatedResource resource : item.getAggregatedResources()) {
+            final URI resourceURI = contentRoot.relativize(resource.getUri());
+            resources.add(resourceURI.toString());
+        }
+        return resources;
+    }
+
+    private void removeFromCollections(final AggregatedResource item) {
+        for (final Aggregation aggregation : item.getAggregatingResources()) {
+            removeItemFromParent(item, aggregation);
+        }
+    }
+
+    private void removeItemFromParent(final AggregatedResource item, final Aggregation aggregation) {
+        final Model model = tripleStore.getModel(aggregation.getUri());
+        model.getResource(aggregation.getURI())
+                .as(Aggregation.class)
+                .removeAggregatedResource(item);
+        tripleStore.saveModel(aggregation.getUri(), model);
+    }
+}

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

 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;
         URI contentRoot = baseUri.resolve("content/");
         final DepositHandler depositHandler = new DepositHandler(new EntryUriGenerator(dataStore, contentRoot),
                 dataStore, resourceStore, new IndexManager(), new ResourceMapGenerator(baseUri), contentRoot);
-        chempound = new DefaultChempound(resourceStore, dataStore, depositHandler, new DefaultRepositoryListenerRegistry(), contentRoot);
+        final DeleteHandler deleteHandler = new DeleteHandler(dataStore, resourceStore, contentRoot);
+        chempound = new DefaultChempound(resourceStore, dataStore, depositHandler, new DefaultRepositoryListenerRegistry(), deleteHandler, contentRoot);
     }
 
     @After

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

 package net.chempound;
 
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
-import com.hp.hpl.jena.rdf.model.ResourceFactory;
 import net.chempound.content.DefaultDepositRequest;
 import net.chempound.content.DepositRequest;
 import net.chempound.content.DepositResponse;
 import net.chempound.datastore.InMemoryJenaTripleStore;
 import net.chempound.datastore.JenaTripleStore;
+import net.chempound.deposit.DeleteHandler;
 import net.chempound.deposit.DepositHandler;
 import net.chempound.plugin.RepositoryListener;
-import net.chempound.rdf.ORE;
-import net.chempound.rdf.ore.AggregatedResource;
-import net.chempound.rdf.ore.Aggregation;
-import net.chempound.rdf.ore.OREFactory;
-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 static java.util.Arrays.asList;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.*;
 
  */
 public class DefaultChempoundTest {
 
-    private static final URI ROOT_URI = URI.create("http://repo.example.net/path/content/");
-    private static final URI ITEM_URI = URI.create("http://repo.example.net/path/content/item/");
-
     private final JenaTripleStore tripleStore = spy(new InMemoryJenaTripleStore());
     private final ResourceStore resourceStore = spy(new InMemoryResourceStore());
     
         DepositHandler depositHandler = mock(DepositHandler.class);
         DepositResponse response = mock(DepositResponse.class);
         when(depositHandler.loadContent(any(DepositRequest.class))).thenReturn(response);
+        DeleteHandler deleteHandler = mock(DeleteHandler.class);
 
-        chempound = new DefaultChempound(resourceStore, tripleStore, depositHandler, listenerRegistry, contentUri);
+        chempound = new DefaultChempound(resourceStore, tripleStore, depositHandler, listenerRegistry, deleteHandler, contentUri);
     }
 
     @Test
         assertEquals(2, listener.getCount());
     }
 
-    @Test
-    public void testDeleteItemRemovesModelsFromTripleStore() throws Exception {
-        Model itemModel = ModelFactory.createDefaultModel();
-        OREFactory.createAggregatedResource(itemModel, ITEM_URI.toString());
-        tripleStore.saveModel(ITEM_URI, itemModel);
-        
-        chempound.deleteEntity(ITEM_URI);
-        
-        verify(tripleStore).deleteModels(asList(ITEM_URI, ITEM_URI.resolve("#rdf")));
-    }
 
-    @Test
-    public void testDeleteItemRemovesResourcesFromResourceStore() throws Exception {
-        InMemoryResource file = new InMemoryResource("file.txt", MimeType.TEXT_PLAIN, "foo");
-        resourceStore.storeItem("item/", file);
-
-        Model itemModel = ModelFactory.createDefaultModel();
-        Aggregation item = OREFactory.createAggregation(itemModel, ITEM_URI.toString());
-        AggregatedResource resource = OREFactory.createAggregatedResource(itemModel, ITEM_URI.resolve("file.txt").toString());
-        item.addAggregatedResource(resource);
-        tripleStore.saveModel(ITEM_URI, itemModel);
-
-        chempound.deleteEntity(ITEM_URI);
-
-        verify(tripleStore).deleteModels(asList(ITEM_URI, ITEM_URI.resolve("#rdf")));
-        verify(resourceStore).deleteItems(asList("item/file.txt"));
-    }
-
-    @Test
-    public void testDeleteItemUpdatesParentCollection() throws Exception {
-        Model itemModel = ModelFactory.createDefaultModel();
-        AggregatedResource item = OREFactory.createAggregatedResource(itemModel, ITEM_URI.toString());
-        
-        Model rootModel = ModelFactory.createDefaultModel();
-        Aggregation aggregation = OREFactory.createAggregation(rootModel, ROOT_URI.toString());
-
-        aggregation.addAggregatedResource(item);
-
-        tripleStore.saveModel(ITEM_URI, itemModel);
-        tripleStore.saveModel(ROOT_URI, rootModel);
-
-        chempound.deleteEntity(ITEM_URI);
-
-        rootModel = tripleStore.getModel(ROOT_URI);
-        assertFalse(rootModel.contains(
-            ResourceFactory.createResource(ROOT_URI.toString()),
-            ORE.aggregates,
-            ResourceFactory.createResource(ROOT_URI.toString())
-        ));
-
-        verify(tripleStore).deleteModels(asList(ITEM_URI, ITEM_URI.resolve("#rdf")));
-    }
-    
     private static class CountingRepositoryListener implements RepositoryListener {
 
         private int count = 0;

chempound-app/src/test/java/net/chempound/deposit/DeleteHandlerTest.java

+package net.chempound.deposit;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.ResourceFactory;
+import net.chempound.datastore.InMemoryJenaTripleStore;
+import net.chempound.datastore.JenaTripleStore;
+import net.chempound.rdf.ORE;
+import net.chempound.rdf.ore.AggregatedResource;
+import net.chempound.rdf.ore.Aggregation;
+import net.chempound.rdf.ore.OREFactory;
+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.net.URI;
+
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertFalse;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+public class DeleteHandlerTest {
+
+    private static final URI ROOT_URI = URI.create("http://repo.example.net/path/content/");
+    private static final URI ITEM_URI = URI.create("http://repo.example.net/path/content/item/");
+
+    private final JenaTripleStore tripleStore = spy(new InMemoryJenaTripleStore());
+    private final ResourceStore resourceStore = spy(new InMemoryResourceStore());
+
+    private DeleteHandler deleteHandler;
+
+    @Before
+    public void setUp() throws Exception {
+        URI contentUri = URI.create("http://repo.example.net/path/content/");
+
+        this.deleteHandler = new DeleteHandler(tripleStore, resourceStore, contentUri);
+    }
+
+    @Test
+    public void testDeleteItemRemovesModelsFromTripleStore() throws Exception {
+        Model itemModel = ModelFactory.createDefaultModel();
+        OREFactory.createAggregatedResource(itemModel, ITEM_URI.toString());
+        tripleStore.saveModel(ITEM_URI, itemModel);
+
+        deleteHandler.deleteEntity(ITEM_URI);
+
+        verify(tripleStore).deleteModels(asList(ITEM_URI, ITEM_URI.resolve("#rdf")));
+    }
+
+    @Test
+    public void testDeleteItemRemovesResourcesFromResourceStore() throws Exception {
+        InMemoryResource file = new InMemoryResource("file.txt", MimeType.TEXT_PLAIN, "foo");
+        resourceStore.storeItem("item/", file);
+
+        Model itemModel = ModelFactory.createDefaultModel();
+        Aggregation item = OREFactory.createAggregation(itemModel, ITEM_URI.toString());
+        AggregatedResource resource = OREFactory.createAggregatedResource(itemModel, ITEM_URI.resolve("file.txt").toString());
+        item.addAggregatedResource(resource);
+        tripleStore.saveModel(ITEM_URI, itemModel);
+
+        deleteHandler.deleteEntity(ITEM_URI);
+
+        verify(tripleStore).deleteModels(asList(ITEM_URI, ITEM_URI.resolve("#rdf")));
+        verify(resourceStore).deleteItems(asList("item/file.txt"));
+    }
+
+    @Test
+    public void testDeleteItemUpdatesParentCollection() throws Exception {
+        Model itemModel = ModelFactory.createDefaultModel();
+        AggregatedResource item = OREFactory.createAggregatedResource(itemModel, ITEM_URI.toString());
+
+        Model rootModel = ModelFactory.createDefaultModel();
+        Aggregation aggregation = OREFactory.createAggregation(rootModel, ROOT_URI.toString());
+
+        aggregation.addAggregatedResource(item);
+
+        tripleStore.saveModel(ITEM_URI, itemModel);
+        tripleStore.saveModel(ROOT_URI, rootModel);
+
+        deleteHandler.deleteEntity(ITEM_URI);
+
+        rootModel = tripleStore.getModel(ROOT_URI);
+        assertFalse(rootModel.contains(
+                ResourceFactory.createResource(ROOT_URI.toString()),
+                ORE.aggregates,
+                ResourceFactory.createResource(ROOT_URI.toString())
+        ));
+
+        verify(tripleStore).deleteModels(asList(ITEM_URI, ITEM_URI.resolve("#rdf")));
+    }
+
+
+}