Commits

Sam Adams  committed 2d96f19

Fix deposit receipt title testing

  • Participants
  • Parent commits 4bcc73b

Comments (0)

Files changed (12)

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

     protected AbstractAcceptanceTest() {
         this.testContext = new TestContext(rootUri);
         this.driverFactory = new SeleniumDriverFactory(testContext);
+        TestContext.setCurrent(testContext);
     }
 
     @BeforeClass

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

  */
 public class TestContext {
 
+    private static final ThreadLocal<TestContext> CURRENT = new ThreadLocal<TestContext>();
+
     private final Map<String, String> actuals = new HashMap<String, String>();
     private final URI rootUri;
 
+    public static TestContext getCurrent() {
+        return CURRENT.get();
+    }
+
+    public static void setCurrent(final TestContext testContext) {
+        CURRENT.set(testContext);
+    }
+
     public TestContext(final URI rootUri) {
         this.rootUri = rootUri;
     }

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

 package net.chempound.acceptance.dsl.driver;
 
+import net.chempound.acceptance.dsl.TestContext;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeMatcher;
         private final String slug;
 
         public ItemSlugMatcher(final String slug) {
-            this.slug = slug;
+            final TestContext testContext = TestContext.getCurrent();
+            this.slug = testContext.actual(slug);
         }
 
         @Override
         private final String title;
 
         public ItemTitleMatcher(final String title) {
-            this.title = title;
+            final TestContext testContext = TestContext.getCurrent();
+            this.title = testContext.actual(title);
         }
 
         @Override

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

 import net.chempound.storage.InMemoryResource;
 import net.chempound.storage.LocalResource;
 import net.chempound.util.MimeType;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+import java.net.URI;
+
+import static org.junit.Assert.assertEquals;
 
 /**
  * @author Sam Adams
 public class APIDriver {
 
     private final ChempoundClient client;
+    private final HttpClient httpClient;
     private final TestContext context;
 
     public APIDriver(final TestContext context) {
         this.context = context;
         this.client = new DefaultChempoundClient();
+        this.httpClient = new DefaultHttpClient();
     }
 
     public Collection createCollection(final CollectionSpecification... specifications) {
     private static LocalResource createResource(final Resource file) {
         return new InMemoryResource(file.getPath(), new MimeType(file.getMimeType()), file.getContent());
     }
+
+    public void verifyResourceDoesNotExist(final URI uri) throws IOException {
+        final HttpUriRequest request = new HttpHead(uri);
+        final HttpResponse response = httpClient.execute(request);
+        try {
+            assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusLine().getStatusCode());
+        } finally {
+            EntityUtils.consume(response.getEntity());
+        }
+    }
 }

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

 package net.chempound.acceptance.dsl.driver.api;
 
+import net.chempound.acceptance.dsl.TestContext;
 import net.chempound.acceptance.dsl.driver.ItemView;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Link;
         return uri;
     }
 
+    public URI getEditUri() {
+        return URI.create(editLink.getHref().toString());
+    }
+
+    public URI getEditMediaUri() {
+        return URI.create(editMediaLink.getHref().toString());
+    }
+
     public Link getEditMediaLink() {
         return editMediaLink;
     }
         return null;
     }
 
+
     public IRI getDerivedResources() {
         Assert.fail();
         // ttp://purl.org.net/sword/terms/derivedResource
         return null;
     }
 
-
     @Override
     public boolean hasTitle(final String title) {
-        return title.equals(title);
+        return title.equals(this.title);
     }
 
     @Override
         throw new UnsupportedOperationException();
     }
 
+    @Override
+    public String toString() {
+        return "DepositReceipt{" +
+                "title='" + title + '\'' +
+                ", uri=" + uri +
+                ", editLink=" + editLink +
+                ", editMediaLink=" + editMediaLink +
+                ", derivedResources=" + derivedResources +
+                '}';
+    }
 }

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

         swordClient.getStatement(statementUrl, new OreStatement(), null);
     }
 
-    public void deleteItem(final String deleteUrl) throws Exception {
-        swordClient.delete(deleteUrl, null);
+    public SwordResponse deleteItem(final String deleteUrl) throws Exception {
+        return swordClient.delete(deleteUrl, null);
     }
 
     public void deleteFileFromItem(final String deleteUrl) throws Exception {
 
     private Deposit createDeposit(final DepositInstruction instruction) {
         final Deposit deposit = new Deposit();
-        deposit.setSlug(instruction.slug);
+        deposit.setSlug(context.actual(instruction.slug));
 
         final EntryPart entryPart = new EntryPart();
         entryPart.getEntry().setTitle(context.actual(instruction.title));

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

+package net.chempound.acceptance.dsl.driver.sword;
+
+import net.chempound.acceptance.dsl.TestContext;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+import java.net.URI;
+
+public class SwordCollectionMatchers {
+
+    public static Matcher<SwordCollection> hasEntry(final Matcher<SwordEntry>... matchers) {
+        return new TypeSafeMatcher<SwordCollection>() {
+            @Override
+            protected boolean matchesSafely(final SwordCollection swordCollection) {
+                for (final SwordEntry entry : swordCollection.getEntries()) {
+                    boolean match = true;
+                    for (final Matcher<SwordEntry> matcher : matchers) {
+                        if (!matcher.matches(entry)) {
+                            match = false;
+                            break;
+                        }
+                    }
+                    if (match) {
+                        return true;
+                    }
+                }
+                return false;
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("has entry");
+            }
+        };
+    }
+
+    public static Matcher<SwordCollection> doesNotHaveEntry(final Matcher<SwordEntry>... matchers) {
+        return new TypeSafeMatcher<SwordCollection>() {
+            @Override
+            protected boolean matchesSafely(final SwordCollection swordCollection) {
+                for (final SwordEntry entry : swordCollection.getEntries()) {
+                    boolean match = true;
+                    for (final Matcher<SwordEntry> matcher : matchers) {
+                        if (!matcher.matches(entry)) {
+                            match = false;
+                            break;
+                        }
+                    }
+                    if (match) {
+                        return false;
+                    }
+                }
+                return true;
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("does not have entry");
+            }
+        };
+    }
+
+    public static Matcher<SwordEntry> hasEditUri(final URI editUri) {
+        return new TypeSafeMatcher<SwordEntry>() {
+            @Override
+            protected boolean matchesSafely(final SwordEntry swordEntry) {
+                return editUri.equals(swordEntry.getEditUri());
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("editUri matches '" + editUri + "'");
+            }
+        };
+    }
+
+    public static Matcher<SwordEntry> hasId(final URI id) {
+        return new TypeSafeMatcher<SwordEntry>() {
+            @Override
+            protected boolean matchesSafely(final SwordEntry swordEntry) {
+                return id.equals(swordEntry.getId());
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("id matches '" + id + "'");
+            }
+        };
+    }
+
+    public static Matcher<SwordEntry> hasTitle(final String title, final TestContext testContext) {
+        return new TypeSafeMatcher<SwordEntry>() {
+            @Override
+            protected boolean matchesSafely(final SwordEntry swordEntry) {
+                return testContext.actual(title).equals(swordEntry.getTitle());
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("title matches '" + title + "'");
+            }
+        };
+    }
+}

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

 
     @Override
     public boolean hasSlug(final String slug) {
-        return depositReceipt.getContentSrc().toString().contains(slug);
+        return depositReceipt.getSplashPageIRI().toString().contains(slug);
     }
 
     @Override

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

 package net.chempound.acceptance.api;
 
 import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
+import net.chempound.acceptance.dsl.driver.Resource;
 import net.chempound.acceptance.dsl.driver.api.APIDriver;
 import net.chempound.acceptance.dsl.driver.api.DepositReceipt;
-import net.chempound.acceptance.dsl.driver.Resource;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;

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

 package net.chempound.acceptance.sword;
 
 import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
+import net.chempound.acceptance.dsl.driver.Resource;
 import net.chempound.acceptance.dsl.driver.api.APIDriver;
+import net.chempound.acceptance.dsl.driver.api.DepositReceipt;
 import net.chempound.acceptance.dsl.driver.sword.SwordClientDriver;
+import net.chempound.acceptance.dsl.driver.sword.SwordCollection;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
+import org.swordapp.client.SwordResponse;
 
+import static net.chempound.acceptance.dsl.driver.DepositSpecifications.withFile;
+import static net.chempound.acceptance.dsl.driver.sword.SwordCollectionMatchers.doesNotHaveEntry;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
 public class DeleteAcceptanceTest extends AbstractAcceptanceTest {
     private APIDriver apiDriver;
     private SwordClientDriver swordDriver;
 
+    private Resource file;
+    private DepositReceipt item;
+
     @Before
     public void setUp() throws Exception {
         apiDriver = driverFactory.createAPIDriver();
         swordDriver = driverFactory.createSwordDriver();
+
+        file = newResource("file");
+        item = apiDriver.depositItem(withFile(file));
     }
 
     @Test
-    @Ignore("work in progress")
     public void shouldDeleteItem() throws Exception {
-        fail();
+        final SwordResponse response = swordDriver.deleteItem(item.getEditUri().toString());
+
+        assertEquals(204, response.getStatusCode());
+        apiDriver.verifyResourceDoesNotExist(item.getUri());
+        apiDriver.verifyResourceDoesNotExist(item.getUri().resolve(file.getPath()));
     }
 
     @Test
+//    @Ignore("work in progress")
+    public void shouldRemoveDeletedItemFromParentCollection() throws Exception {
+        final SwordResponse response = swordDriver.deleteItem(item.getEditUri().toString());
+
+        assertEquals(204, response.getStatusCode());
+        final SwordCollection collection = swordDriver.getCollection(testContext.getRootUri().resolve("sword/collection/"));
+        verify(collection, doesNotHaveEntry());
+    }
+
+
+    @Test
     @Ignore("work in progress")
     public void shouldDeleteFileFromItem() throws Exception {
-        fail();
+        final SwordResponse response = swordDriver.deleteItem(item.getEditMediaUri().resolve(file.getPath()).toString());
+        assertEquals(204, response.getStatusCode());
+
+        apiDriver.verifyResourceDoesNotExist(item.getUri().resolve(file.getPath()));
     }
 
     @Test
     @Ignore("work in progress")
     public void shouldNotDeleteUnknownItem() throws Exception {
-        fail();
+        final SwordResponse response = swordDriver.deleteItem(testContext.getRootUri().resolve("sword/edit/foo/").toString());
+        assertEquals(404, response.getStatusCode());
     }
 
     @Test
     @Ignore("work in progress")
     public void shouldNotDeleteUnknownFile() throws Exception {
-        fail();
+        final SwordResponse response = swordDriver.deleteItem(testContext.getRootUri().resolve("sword/edit-media/foo/").toString());
+        assertEquals(404, response.getStatusCode());
     }
 }

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

     }
 
     @Test
-    @Ignore("work in progress")
+//    @Ignore("work in progress")
     public void shouldSupportMetaDataOnlyDeposit() throws Exception {
         final SwordDepositReceipt depositReceipt = swordDriver.depositItem(withSlug("slug"), withTitle("title"));
         verify(depositReceipt, hasTitle("title"), hasSlug("slug"));

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

 import net.chempound.acceptance.dsl.driver.api.DepositReceipt;
 import net.chempound.acceptance.dsl.driver.sword.SwordClientDriver;
 import net.chempound.acceptance.dsl.driver.sword.SwordCollection;
-import net.chempound.acceptance.dsl.driver.sword.SwordEntry;
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-import org.hamcrest.TypeSafeMatcher;
+import net.chempound.acceptance.dsl.driver.sword.SwordCollectionMatchers;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
-import java.net.URI;
-
 import static net.chempound.acceptance.dsl.driver.DepositSpecifications.withSlug;
 import static net.chempound.acceptance.dsl.driver.DepositSpecifications.withTitle;
 
 
         final SwordCollection collection = swordDriver.getCollection(testContext.getRootUri().resolve("sword/collection/"));
 
-        verify(collection, hasEntry(hasId(depositReceipt.getUri()), hasTitle("title"), hasEditUri(testContext.getRootUri())));
+        verify(collection, SwordCollectionMatchers.hasEntry(SwordCollectionMatchers.hasId(depositReceipt.getUri()), SwordCollectionMatchers.hasTitle("title", testContext), SwordCollectionMatchers.hasEditUri(testContext.getRootUri())));
     }
 
-    private Matcher<SwordEntry> hasId(final URI id) {
-        return new TypeSafeMatcher<SwordEntry>() {
-            @Override
-            protected boolean matchesSafely(final SwordEntry swordEntry) {
-                return id.equals(swordEntry.getId());
-            }
-
-            @Override
-            public void describeTo(final Description description) {
-                description.appendText("id matches '" + id + "'");
-            }
-        };
-    }
-
-    private Matcher<SwordEntry> hasTitle(final String title) {
-        return new TypeSafeMatcher<SwordEntry>() {
-            @Override
-            protected boolean matchesSafely(final SwordEntry swordEntry) {
-                return testContext.actual(title).equals(swordEntry.getTitle());
-            }
-
-            @Override
-            public void describeTo(final Description description) {
-                description.appendText("title matches '" + title + "'");
-            }
-        };
-    }
-
-    private Matcher<SwordEntry> hasEditUri(final URI editUri) {
-        return new TypeSafeMatcher<SwordEntry>() {
-            @Override
-            protected boolean matchesSafely(final SwordEntry swordEntry) {
-                return editUri.equals(swordEntry.getEditUri());
-            }
-
-            @Override
-            public void describeTo(final Description description) {
-                description.appendText("editUri matches '" + editUri + "'");
-            }
-        };
-    }
-
-    private static Matcher<SwordCollection> hasEntry(final Matcher<SwordEntry>... matchers) {
-        return new TypeSafeMatcher<SwordCollection>() {
-            @Override
-            protected boolean matchesSafely(final SwordCollection swordCollection) {
-                for (final SwordEntry entry : swordCollection.getEntries()) {
-                    boolean match = true;
-                    for (final Matcher<SwordEntry> matcher : matchers) {
-                        if (!matcher.matches(entry)) {
-                            match = false;
-                            break;
-                        }
-                    }
-                    if (match) {
-                        return true;
-                    }
-                }
-                return false;
-            }
-
-            @Override
-            public void describeTo(final Description description) {
-                description.appendText("has entry");
-            }
-        };
-    }
 }