Commits

Anonymous committed cf762af

Refactoring create collection DSL

Comments (0)

Files changed (5)

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

         this.context = context;
         this.client = new DefaultChempoundClient();
     }
-    
-    public CreateCollectionInstruction createCollection() {
-        return new CreateCollectionInstruction(this);
+
+    public Collection createCollection(final CollectionSpecification... specifications) {
+        final CreateCollectionInstruction instruction = new CreateCollectionInstruction();
+        for (final CollectionSpecification specification : specifications) {
+            specification.apply(instruction);
+        }
+        return createCollection(instruction);
     }
 
     Collection createCollection(final CreateCollectionInstruction instruction) {

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

+package net.chempound.acceptance.dsl.driver;
+
+public class CollectionSpecification {
+
+    public static CollectionSpecification withSlug(final String slug) {
+        return new CollectionSpecification() {
+            @Override
+            protected void apply(final CreateCollectionInstruction instruction) {
+                instruction.slug = slug;
+            }
+        };
+    }
+
+    public static CollectionSpecification withTitle(final String title) {
+        return new CollectionSpecification() {
+            @Override
+            protected void apply(final CreateCollectionInstruction instruction) {
+                instruction.title = title;
+            }
+        };
+    }
+
+    public static CollectionSpecification withParent(final Collection parent) {
+        return new CollectionSpecification() {
+            @Override
+            protected void apply(final CreateCollectionInstruction instruction) {
+                instruction.parent = parent.getUri();
+            }
+        };
+    }
+
+    protected void apply(final CreateCollectionInstruction instruction) {
+        throw new UnsupportedOperationException();
+    }
+}

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

  */
 public class CreateCollectionInstruction {
 
-    private final APIDriver driver;
-
     URI parent;
     String title;
     String slug;
 
-    CreateCollectionInstruction(final APIDriver driver) {
-        this.driver = driver;
-    }
-
-    public CreateCollectionInstruction withParent(final Collection parent) {
-        this.parent = parent.getUri();
-        return this;
-    }
-
-    public CreateCollectionInstruction withTitle(final String title) {
-        this.title = title;
-        return this;
-    }
-
-    public CreateCollectionInstruction withSlug(final String slug) {
-        this.slug = slug;
-        return this;
-    }
-
-    public Collection execute() {
-        return driver.createCollection(this);
-    }
-
 }

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

     }
 
     @SuppressWarnings("unchecked")
-    public void check(final Collection collection, final Matcher<CollectionView> matcher) {
+    public void verify(final Collection collection, final Matcher<CollectionView> matcher) {
         check(collection, asList(matcher));
     }
 
     @SuppressWarnings("unchecked")
-    public void check(final Collection collection, final Matcher<CollectionView> matcher1, final Matcher<CollectionView> matcher2) {
+    public void verify(final Collection collection, final Matcher<CollectionView> matcher1, final Matcher<CollectionView> matcher2) {
         check(collection, asList(matcher1, matcher2));
     }
 

acceptance-tests/src/test/java/net/chempound/acceptance/ui/CollectionAcceptanceTest.java

 import org.junit.Test;
 
 import static net.chempound.acceptance.dsl.driver.CollectionMatchers.*;
+import static net.chempound.acceptance.dsl.driver.CollectionSpecification.withParent;
+import static net.chempound.acceptance.dsl.driver.CollectionSpecification.withSlug;
+import static net.chempound.acceptance.dsl.driver.CollectionSpecification.withTitle;
 
 /**
  * @author Sam Adams
 
     @Test
     public void shouldCreateCollectionWithTitle() throws Exception {
-        Collection collection = apiDriver.createCollection()
-            .withTitle("title")
-            .execute();
+        Collection collection = apiDriver.createCollection(withTitle("title"));
 
-        webDriver.check(collection, hasTitle("title"));
+        webDriver.verify(collection, hasTitle("title"));
     }
 
     @Test
     public void shouldCreateCollectionWithSlug() throws Exception {
-        Collection collection = apiDriver.createCollection()
-            .withSlug("slug")
-            .withTitle("title")
-            .execute();
+        Collection collection = apiDriver.createCollection(withSlug("slug"), withTitle("title"));
 
-        webDriver.check(collection, hasTitle("title"), hasSlug("slug"));
+        webDriver.verify(collection, hasTitle("title"), hasSlug("slug"));
     }
 
     @Test
     public void shouldLinkFromParentToChildCollection() throws Exception {
-        Collection parent = apiDriver.createCollection()
-            .execute();
-        
-        Collection child = apiDriver.createCollection()
-            .withParent(parent)
-            .withTitle("child-title")
-            .execute();
+        Collection parent = apiDriver.createCollection(withTitle("parent"));
 
-        webDriver.check(parent, containsItem(child));
+        Collection child = apiDriver.createCollection(withParent(parent), withTitle("child"));
+
+        webDriver.verify(parent, containsItem(child));
     }
 
 }