Commits

Anonymous committed 2ef5a30

Refactoring deposit item DSL

  • Participants
  • Parent commits 27667d3

Comments (0)

Files changed (14)

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/AbstractAcceptanceTest.java

     }
 
     @SuppressWarnings("unchecked")
-    protected static <T, U extends T> void check(final U target, final Matcher<T> matcher) {
-        check(target, asList(matcher));
+    protected static <T, U extends T> void verify(final U target, final Matcher<T> matcher) {
+        verify(target, asList(matcher));
     }
 
     @SuppressWarnings("unchecked")
-    protected static <T, U extends T> void check(final U target, final Matcher<T> matcher1, final Matcher<T> matcher2) {
-        check(target, asList(matcher1, matcher2));
+    protected static <T, U extends T> void verify(final U target, final Matcher<T> matcher1, final Matcher<T> matcher2) {
+        verify(target, asList(matcher1, matcher2));
     }
 
-    protected static <T, U extends T> void check(final U target, final Iterable<Matcher<T>> matchers) {
+    protected static <T, U extends T> void verify(final U target, final Iterable<Matcher<T>> matchers) {
         for (final Matcher<T> matcher : matchers) {
             assertThat(target, matcher);
         }

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/driver/APIDriver.java

         return new CreateCollectionInstruction(this);
     }
 
-    Collection createCollection(final CreateCollectionInstruction config) {
+    Collection createCollection(final CreateCollectionInstruction instruction) {
         final CreateCollectionRequest request = new CreateCollectionRequest()
-            .withUri(config.parent != null ? config.parent : context.getContentRoot())
-            .withTitle(context.actual(config.title))
-            .withSlug(context.actual(config.slug));
+            .withUri(instruction.parent != null ? instruction.parent : context.getContentRoot())
+            .withTitle(context.actual(instruction.title))
+            .withSlug(context.actual(instruction.slug));
 
         final CreateCollectionResponse response;
         try {
         return new Collection(response.getUri(), response.getTitle());
     }
 
-    public DepositInstruction depositItem() {
-        return new DepositInstruction(this);
+    public DepositReceipt depositItem(final DepositSpecification... specifications) {
+        final DepositInstruction instruction = new DepositInstruction();
+        for (final DepositSpecification specification : specifications) {
+            specification.apply(instruction);
+        }
+        return depositItem(instruction);
     }
 
-    DepositReceipt depositItem(final DepositInstruction config) {
+    private DepositReceipt depositItem(final DepositInstruction instruction) {
         final DepositItemRequest request = new DepositItemRequest()
-            .withUri(config.parent != null ? config.parent : context.getContentRoot())
-            .withTitle(context.actual(config.title))
-            .withSlug(context.actual(config.slug))
-            .withMetadata(config.metadata);
+            .withUri(instruction.parent != null ? instruction.parent : context.getContentRoot())
+            .withTitle(context.actual(instruction.title))
+            .withSlug(context.actual(instruction.slug))
+            .withMetadata(instruction.metadata);
 
-        if (config.files != null) {
-            for (final Resource file : config.files) {
+        if (instruction.files != null) {
+            for (final Resource file : instruction.files) {
                 request.withFile(createResource(file));
             }
         }

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/driver/DepositInstruction.java

  */
 public class DepositInstruction {
 
-    private final APIDriver driver;
-
     URI parent;
     String title;
     String slug;
     boolean multipart;
-    List<Resource> files;
+    List<Resource> files = new ArrayList<Resource>();
     boolean inProgress;
     Model metadata;
 
-    DepositInstruction(final APIDriver driver) {
-        this.driver = driver;
-    }
-
-    public DepositInstruction withParent(final Collection parent) {
-        this.parent = parent.getUri();
-        return this;
-    }
-
-    public DepositInstruction withTitle(final String title) {
-        this.title = title;
-        return this;
-    }
-
-    public DepositInstruction withSlug(final String slug) {
-        this.slug = slug;
-        return this;
-    }
-
-    public DepositInstruction multipart(final boolean multipart) {
-        this.multipart = multipart;
-        return this;
-    }
-
-    public DepositInstruction withFile(final Resource file) {
-        if (this.files == null) {
-            this.files = new ArrayList<Resource>();
-        }
-        this.files.add(file);
-        return this;
-    }
-
-    public DepositInstruction withInProgress(final boolean inProgress) {
-        this.inProgress = inProgress;
-        return this;
-    }
-
-    public DepositReceipt execute() {
-        return driver.depositItem(this);
-    }
-
-    public DepositInstruction withMetadata(final Model metadata) {
-        this.metadata = metadata;
-        return this;
-    }
 }

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/driver/DepositReceipt.java

         throw new UnsupportedOperationException();
     }
 
+    @Override
+    public boolean isInProgress(final boolean inProgress) {
+        throw new UnsupportedOperationException();
+    }
+
 }

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/driver/DepositSpecification.java

+package net.chempound.acceptance.dsl.driver;
+
+import com.hp.hpl.jena.rdf.model.Model;
+
+import static java.util.Arrays.asList;
+
+public class DepositSpecification {
+
+    public static DepositSpecification withSlug(final String slug) {
+        return new DepositSpecification() {
+            @Override
+            protected void apply(final DepositInstruction instruction) {
+                instruction.slug = slug;
+            }
+        };
+    }
+
+    public static DepositSpecification withTitle(final String title) {
+        return new DepositSpecification() {
+            @Override
+            protected void apply(final DepositInstruction instruction) {
+                instruction.title = title;
+            }
+        };
+    }
+
+    public static DepositSpecification withFile(final Resource file) {
+        return new DepositSpecification() {
+            @Override
+            protected void apply(final DepositInstruction instruction) {
+                instruction.files.add(file);
+            }
+        };
+    }
+
+    public static DepositSpecification withFiles(final Resource... files) {
+        return withFiles(asList(files));
+    }
+
+    public static DepositSpecification withFiles(final java.util.Collection<Resource> files) {
+        return new DepositSpecification() {
+            @Override
+            protected void apply(final DepositInstruction instruction) {
+                instruction.files.addAll(files);
+            }
+        };
+    }
+
+    public static DepositSpecification withMetadata(final Model metadata) {
+        return new DepositSpecification() {
+            @Override
+            protected void apply(final DepositInstruction instruction) {
+                instruction.metadata = metadata;
+            }
+        };
+    }
+
+    public static DepositSpecification asMultipart(final boolean multipart) {
+        return new DepositSpecification() {
+            @Override
+            protected void apply(final DepositInstruction instruction) {
+                instruction.multipart = multipart;
+            }
+        };
+    }
+
+    public static DepositSpecification inProgress(final boolean inProgress) {
+        return new DepositSpecification() {
+            @Override
+            protected void apply(final DepositInstruction instruction) {
+                instruction.inProgress = inProgress;
+            }
+        };
+    }
+
+    protected void apply(final DepositInstruction instruction) {
+        throw new UnsupportedOperationException();
+    }
+}

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/driver/ItemMatchers.java

         return new PingbackMatcher(pingbacks);
     }
 
+    public static Matcher<ItemView> isInProgress(final boolean inProgress) {
+        return new ItemInProgressMatcher(inProgress);
+    }
+
     private static class ItemSlugMatcher extends TypeSafeMatcher<ItemView> {
         private final String slug;
 
         }
     }
 
+    private static class ItemInProgressMatcher extends TypeSafeMatcher<ItemView> {
+
+        private final boolean inProgress;
+
+        public ItemInProgressMatcher(final boolean inProgress) {
+            this.inProgress = inProgress;
+        }
+
+        @Override
+        protected boolean matchesSafely(final ItemView item) {
+            return item.isInProgress(inProgress);
+        }
+
+        @Override
+        public void describeTo(final Description description) {
+            description.appendText("title in progress: '" + inProgress + "'");
+        }
+    }
+
     private static class PingbackMatcher extends TypeSafeMatcher<ItemView> {
 
         private final URI[] pingbacks;

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/driver/ItemPage.java

     }
 
     @Override
+    public boolean isInProgress(final boolean inProgress) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
     public boolean hasSlug(final String slug) {
         return webDriver.getCurrentUrl().contains('/' + context.actual(slug) + '/');
     }

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/driver/ItemView.java

 
     boolean hasPingbacks(URI... pingbacks);
 
+    boolean isInProgress(boolean inProgress);
 }

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/server/StubBlogServer.java

         return blogUrl;
     }
 
-    public void checkVisitorCount(final URI blogUrl, final int count) {
+    public void verifyVisitorCount(final URI blogUrl, final int count) {
         assertEquals(count, visits.get(blogUrl).get());
     }
 

File acceptance-tests/src/test/java/net/chempound/acceptance/PingbackAcceptanceTest.java

 
 import java.net.URI;
 
+import static net.chempound.acceptance.dsl.driver.DepositSpecification.withTitle;
 import static net.chempound.acceptance.dsl.driver.ItemMatchers.hasPingbacks;
 import static net.chempound.webapp.pingback.PingbackFault.*;
 
         webUIDriver = driverFactory.createWebUIDriver();
         pingbackDriver = driverFactory.createPingbackDriver();
 
-        item = apiDriver.depositItem()
-                .withTitle("title")
-                .execute();
+        item = apiDriver.depositItem(withTitle("title"));
     }
 
     @BeforeClass
 
         pingbackDriver.pingback(item.getUri(), blog);
 
-        blogServer.checkVisitorCount(blog, 1);
+        blogServer.verifyVisitorCount(blog, 1);
     }
 
     @Test
         pingbackDriver.pingback(item.getUri(), blog1);
         pingbackDriver.pingback(item.getUri(), blog2);
 
-        blogServer.checkVisitorCount(blog1, 1);
-        blogServer.checkVisitorCount(blog2, 1);
+        blogServer.verifyVisitorCount(blog1, 1);
+        blogServer.verifyVisitorCount(blog2, 1);
     }
 
     @Test
     public void shouldAcceptPingbackToMultipleItems() throws Exception {
-        DepositReceipt otherItem = apiDriver.depositItem().execute();
+        DepositReceipt otherItem = apiDriver.depositItem(withTitle("title"));
 
         URI blog = blogServer.addPost(item.getUri(), otherItem.getUri());
 
         pingbackDriver.pingback(item.getUri(), blog);
         pingbackDriver.pingback(otherItem.getUri(), blog);
 
-        blogServer.checkVisitorCount(blog, 2);
+        blogServer.verifyVisitorCount(blog, 2);
     }
 
     @Test

File acceptance-tests/src/test/java/net/chempound/acceptance/chemistry/GaussianLogAcceptanceTest.java

 
 import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
 import net.chempound.acceptance.dsl.driver.APIDriver;
-import net.chempound.acceptance.dsl.driver.DepositInstruction;
 import net.chempound.acceptance.dsl.driver.Resource;
 import net.chempound.compchem.gaussian.GaussianLogImporter;
 import net.chempound.content.DepositRequest;
 import net.chempound.storage.DepositResource;
-import nu.xom.Document;
 import org.apache.commons.io.IOUtils;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
-import org.xmlcml.cml.base.CMLBuilder;
 
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import static net.chempound.acceptance.dsl.driver.DepositSpecification.*;
 
 /**
  * @author Sam Adams
             IOUtils.closeQuietly(in);
         }
 
-        final DepositInstruction depositInstruction = apiDriver.depositItem()
-                                                       .withSlug("slug")
-                                                       .withTitle("title")
-                                                       .withMetadata(depositRequest.getMetadataModel());
+        final List<Resource> files = new ArrayList<Resource>();
         for (DepositResource resource : depositRequest.getAggregatedResources()) {
-            depositInstruction.withFile(new Resource(resource.getURI(), resource.getMimeType().getName(), IOUtils.toByteArray(resource.openInputStream())));
+            files.add(new Resource(resource.getURI(), resource.getMimeType().getName(), IOUtils.toByteArray(resource.openInputStream())));
         }
-        depositInstruction.execute();
+
+        apiDriver.depositItem(withSlug("slug"), withTitle("title"), withMetadata(depositRequest.getMetadataModel()), withFiles(files));
 
         Assert.fail("work in progress");
     }

File acceptance-tests/src/test/java/net/chempound/acceptance/chemistry/NwChemLogAcceptanceTest.java

 
 import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
 import net.chempound.acceptance.dsl.driver.APIDriver;
-import net.chempound.acceptance.dsl.driver.DepositInstruction;
 import net.chempound.acceptance.dsl.driver.Resource;
-import net.chempound.compchem.gaussian.GaussianLogImporter;
 import net.chempound.compchem.nwchem.NWChemLogImporter;
 import net.chempound.content.DepositRequest;
 import net.chempound.storage.DepositResource;
 import org.junit.Test;
 
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import static net.chempound.acceptance.dsl.driver.DepositSpecification.*;
 
 /**
  * @author Sam Adams
             IOUtils.closeQuietly(in);
         }
 
-        final DepositInstruction depositInstruction = apiDriver.depositItem()
-                                                       .withSlug("slug")
-                                                       .withTitle("title")
-                                                       .withMetadata(depositRequest.getMetadataModel());
+
+        final List<Resource> files = new ArrayList<Resource>();
         for (DepositResource resource : depositRequest.getAggregatedResources()) {
-            depositInstruction.withFile(new Resource(resource.getURI(), resource.getMimeType().getName(), IOUtils.toByteArray(resource.openInputStream())));
+            files.add(new Resource(resource.getURI(), resource.getMimeType().getName(), IOUtils.toByteArray(resource.openInputStream())));
         }
-        depositInstruction.execute();
+
+        apiDriver.depositItem(withSlug("slug"), withTitle("title"), withMetadata(depositRequest.getMetadataModel()), withFiles(files));
 
         Assert.fail("work in progress");
     }

File acceptance-tests/src/test/java/net/chempound/acceptance/sword/DepositAcceptanceTest.java

 import net.chempound.acceptance.dsl.driver.APIDriver;
 import net.chempound.acceptance.dsl.driver.DepositReceipt;
 import net.chempound.acceptance.dsl.driver.Resource;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import static net.chempound.acceptance.dsl.driver.DepositSpecification.*;
 import static net.chempound.acceptance.dsl.driver.ItemMatchers.*;
 
 /**
     }
 
     @Test
-    public void shouldCreateItemOnMetadataOnlDeposit() {
-        DepositReceipt item = apiDriver.depositItem()
-            .withSlug("slug")
-            .withTitle("title")
-            .execute();
+    public void shouldCreateItemOnMetadataOnlyDeposit() {
+        DepositReceipt item = apiDriver.depositItem(withSlug("slug"), withTitle("title"));
 
-        check(item, hasTitle("title"), hasSlug("slug"));
+        verify(item, hasTitle("title"), hasSlug("slug"));
     }
 
     @Test
     public void shouldCreateItemOnBinaryDeposit() {
         Resource file = newResource("file");
         
-        DepositReceipt item = apiDriver.depositItem()
-            .withFile(file)
-            .execute();
+        DepositReceipt item = apiDriver.depositItem(withFile(file));
 
-        check(item, hasFiles(file.getPath()));
+        verify(item, hasFiles(file.getPath()));
     }
 
     @Test
         Resource file2 = newResource("file2");
         Resource file3 = newResource("file3");
 
-        DepositReceipt item = apiDriver.depositItem()
-                               .withFile(file1)
-                               .withFile(file2)
-                               .withFile(file3)
-                               .execute();
+        DepositReceipt item = apiDriver.depositItem(withFiles(file1, file2, file3));
 
-        check(item, hasFiles(file1.getPath(), file2.getPath(), file3.getPath()));
+        verify(item, hasFiles(file1.getPath(), file2.getPath(), file3.getPath()));
     }
 
     @Test
     public void shouldCreateItemOnMultipartDeposit() {
         Resource file = newResource("file");
 
-        DepositReceipt item = apiDriver.depositItem()
-                     .withTitle("title")
-                     .withFile(file)
-                     .multipart(true)
-                     .execute();
+        DepositReceipt item = apiDriver.depositItem(withTitle("title"), withFile(file), asMultipart(true));
 
-        check(item, hasTitle("title"), hasFiles(file.getPath()));
+        verify(item, hasTitle("title"), hasFiles(file.getPath()));
     }
 
     @Test
         Resource file2 = newResource("file2");
         Resource file3 = newResource("file3");
 
-        DepositReceipt item = apiDriver.depositItem()
-                               .withTitle("title")
-                               .withFile(file1)
-                               .withFile(file2)
-                               .withFile(file3)
-                               .multipart(true)
-                               .execute();
+        DepositReceipt item = apiDriver.depositItem(withTitle("title"), withFiles(file1, file2, file3), asMultipart(true));
 
-        check(item, hasTitle("title"), hasFiles(file1.getPath(), file2.getPath(), file3.getPath()));
+        verify(item, hasTitle("title"), hasFiles(file1.getPath(), file2.getPath(), file3.getPath()));
     }
 
     @Test
     @Ignore
     public void shouldSupportInProgressDeposit() {
-        DepositReceipt item = apiDriver.depositItem()
-                     .withTitle("title")
-                     .withInProgress(true)
-                     .execute();
+        DepositReceipt item = apiDriver.depositItem(withTitle("title"), inProgress(true));
 
-        Assert.fail("work in progress");
+        verify(item, hasTitle("title"), isInProgress(true));
     }
 
 }

File acceptance-tests/src/test/java/net/chempound/acceptance/ui/WebUIAcceptanceTest.java

 
 import java.util.Random;
 
+import static net.chempound.acceptance.dsl.driver.DepositSpecification.*;
 import static net.chempound.acceptance.dsl.driver.ItemMatchers.*;
 
 /**
 
     @Test
     public void shouldCreateItemOnMetadataDeposit() {
-        DepositReceipt item = apiDriver.depositItem()
-            .withSlug("slug")
-            .withTitle("title")
-            .execute();
+        DepositReceipt item = apiDriver.depositItem(withSlug("slug"), withTitle("title"));
 
         webDriver.check(item, hasSlug("slug"), hasTitle("title"));
     }
     public void shouldCreateItemOnBinaryDeposit() {
         Resource file = newResource();
         
-        DepositReceipt item = apiDriver.depositItem()
-            .withFile(file)
-            .execute();
+        DepositReceipt item = apiDriver.depositItem(withFile(file));
 
         webDriver.check(item, hasFiles(file.getPath()));
     }
     public void shouldCreateItemOnMultipartDeposit() {
         Resource file = newResource();
 
-        DepositReceipt item = apiDriver.depositItem()
-                     .withTitle("title")
-                     .withFile(file)
-                     .multipart(true)
-                     .execute();
+        DepositReceipt item = apiDriver.depositItem(withTitle("title"), withFile(file), asMultipart(true));
 
         webDriver.check(item, hasTitle("title"), hasFiles(file.getPath()));
     }