Commits

Anonymous committed e08f521

Working on SWORD ATs

Comments (0)

Files changed (13)

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

 package net.chempound.acceptance.dsl.driver;
 
+import net.chempound.acceptance.dsl.driver.sword.SwordServiceDocumentCollection;
 import net.chempound.acceptance.dsl.driver.sword.SwordServiceDocument;
 import net.chempound.acceptance.dsl.driver.sword.SwordWorkspace;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeMatcher;
 
+import java.net.URI;
+
 public class ServiceDocumentMatchers {
 
     public static Matcher<SwordServiceDocument> hasVersion(final String version) {
         };
     }
 
+    public static Matcher<SwordWorkspace> hasCollection(final Matcher<SwordServiceDocumentCollection>... matchers) {
+        return new TypeSafeMatcher<SwordWorkspace>() {
+            @Override
+            protected boolean matchesSafely(final SwordWorkspace swordWorkspace) {
+                for (final SwordServiceDocumentCollection collection : swordWorkspace.getCollections()) {
+                    boolean match = true;
+                    for (final Matcher<SwordServiceDocumentCollection> matcher : matchers) {
+                        if (!matcher.matches(collection)) {
+                            match = false;
+                            break;
+                        }
+                    }
+                    if (match) {
+                        return true;
+                    }
+                }
+                return false;
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("collection matches");
+            }
+        };
+    }
+
+    public static Matcher<SwordServiceDocumentCollection> hasTitle(final String title) {
+        return new TypeSafeMatcher<SwordServiceDocumentCollection>() {
+            @Override
+            protected boolean matchesSafely(final SwordServiceDocumentCollection swordCollection) {
+                return title.equals(swordCollection.getTitle());
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("title matches '" + title + "'");
+            }
+        };
+    }
+
+    public static Matcher<SwordServiceDocumentCollection> hasHref(final URI href) {
+        return new TypeSafeMatcher<SwordServiceDocumentCollection>() {
+            @Override
+            protected boolean matchesSafely(final SwordServiceDocumentCollection swordCollection) {
+                return href.equals(swordCollection.getHref());
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("href matches '" + href + "'");
+            }
+        };
+    }
+
 }

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

 package net.chempound.acceptance.dsl.driver.sword;
 
 import net.chempound.acceptance.dsl.TestContext;
-import org.swordapp.client.SWORDClient;
-import org.swordapp.client.ServiceDocument;
+import net.chempound.acceptance.dsl.driver.DepositInstruction;
+import net.chempound.acceptance.dsl.driver.DepositSpecification;
+import net.chempound.acceptance.dsl.driver.api.*;
+import org.swordapp.client.*;
+import org.swordapp.client.DepositReceipt;
+
+import java.net.URI;
 
 public class SwordClientDriver {
 
         return new SwordServiceDocument(serviceDocument);
     }
 
+    public SwordCollection getCollection(final URI url) throws Exception {
+        final CollectionEntries collectionEntries = swordClient.listCollection(url.toString());
+        return new SwordCollection(collectionEntries);
+    }
+
+    public SwordDepositReceipt depositItem(final DepositSpecification... specifications) throws Exception {
+        final DepositInstruction instruction = new DepositInstruction();
+        for (final DepositSpecification specification : specifications) {
+            specification.apply(instruction);
+        }
+        final URI collection = instruction.parent != null ? instruction.parent : context.getContentRoot();
+        final DepositReceipt depositReceipt = swordClient.deposit(collection.toString(), createDeposit(instruction));
+        return new SwordDepositReceipt(depositReceipt);
+    }
+
+    private Deposit createDeposit(final DepositInstruction instruction) {
+        final Deposit deposit = new Deposit();
+        deposit.setSlug(instruction.slug);
+
+        final EntryPart entryPart = new EntryPart();
+        entryPart.getEntry().setTitle(context.actual(instruction.title));
+        deposit.setEntryPart(entryPart);
+
+        return deposit;
+    }
+
+    public void completeDeposit(final String editUrl) throws Exception {
+        swordClient.complete(editUrl, null);
+    }
+
+    public SwordDepositReceipt getDepositReceipt(final String editUrl) throws Exception {
+        return new SwordDepositReceipt(swordClient.getDepositReceipt(editUrl, null));
+    }
+
+    public void getAtomStatement(final String statementUrl) throws Exception {
+        swordClient.getStatement(statementUrl, new AtomStatement(), null);
+    }
+
+    public void getOreStatement(final String statementUrl) throws Exception {
+        swordClient.getStatement(statementUrl, new OreStatement(), null);
+    }
+
+    public void deleteItem(final String deleteUrl) throws Exception {
+        swordClient.delete(deleteUrl, null);
+    }
+
+    public void deleteFileFromItem(final String deleteUrl) throws Exception {
+        swordClient.delete(deleteUrl, null);
+    }
 
 }

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

+package net.chempound.acceptance.dsl.driver.sword;
+
+import org.apache.abdera.model.Entry;
+import org.swordapp.client.CollectionEntries;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class SwordCollection {
+
+    private final CollectionEntries collectionEntries;
+
+    public SwordCollection(final CollectionEntries collectionEntries) {
+        this.collectionEntries = collectionEntries;
+    }
+
+    public List<SwordEntry> getEntries() {
+        final List<SwordEntry> entries = new ArrayList<SwordEntry>();
+        for (final Entry entry : collectionEntries.getEntries()) {
+            entries.add(new SwordEntry(entry));
+        }
+        return entries;
+    }
+}

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

+package net.chempound.acceptance.dsl.driver.sword;
+
+import net.chempound.acceptance.dsl.driver.ItemView;
+import org.swordapp.client.DepositReceipt;
+
+import java.net.URI;
+
+public class SwordDepositReceipt implements ItemView {
+
+    private final DepositReceipt depositReceipt;
+
+    public SwordDepositReceipt(final DepositReceipt depositReceipt) {
+        this.depositReceipt = depositReceipt;
+    }
+
+    @Override
+    public boolean hasSlug(final String slug) {
+        return depositReceipt.getContentSrc().toString().contains(slug);
+    }
+
+    @Override
+    public boolean hasTitle(final String title) {
+        return title.equals(depositReceipt.getEntry().getTitle());
+    }
+
+    @Override
+    public boolean hasFiles(final String... file) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean hasPingbacks(final URI... pingbacks) {
+        return false;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public boolean isInProgress(final boolean inProgress) {
+        return false;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    public String getEditUri() {
+        return depositReceipt.getEditLink().getHref().toString();
+    }
+}

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

+package net.chempound.acceptance.dsl.driver.sword;
+
+import org.apache.abdera.i18n.iri.IRI;
+import org.apache.abdera.model.Entry;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+public class SwordEntry {
+
+    private final Entry entry;
+
+    public SwordEntry(final Entry entry) {
+        this.entry = entry;
+    }
+
+    public URI getId() {
+        return toUri(entry.getId());
+    }
+
+    public String getTitle() {
+        return entry.getTitle();
+    }
+
+    public URI getEditUri() {
+        return toUri(entry.getEditLinkResolvedHref());
+    }
+
+    private static URI toUri(final IRI iri) {
+        try {
+            return iri == null ? null : iri.toURI();
+        } catch (URISyntaxException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

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

+package net.chempound.acceptance.dsl.driver.sword;
+
+import org.swordapp.client.SWORDCollection;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+public class SwordServiceDocumentCollection {
+
+    private final SWORDCollection collection;
+
+    public SwordServiceDocumentCollection(final SWORDCollection collection) {
+        this.collection = collection;
+    }
+
+    public String getTitle() {
+        return collection.getTitle();
+    }
+
+    public URI getHref() {
+        try {
+            return collection.getHref().toURI();
+        } catch (URISyntaxException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

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

 package net.chempound.acceptance.dsl.driver.sword;
 
+import org.swordapp.client.SWORDCollection;
 import org.swordapp.client.SWORDWorkspace;
 
+import java.util.ArrayList;
+import java.util.List;
+
 public class SwordWorkspace {
 
     private final SWORDWorkspace workspace;
         this.workspace = workspace;
     }
 
+    public List<SwordServiceDocumentCollection> getCollections() {
+        final List<SwordServiceDocumentCollection> collections = new ArrayList<SwordServiceDocumentCollection>();
+        for (final SWORDCollection collection : workspace.getCollections()) {
+            collections.add(new SwordServiceDocumentCollection(collection));
+        }
+        return collections;
+    }
+
 }

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.api.APIDriver;
+import net.chempound.acceptance.dsl.driver.sword.SwordClientDriver;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static org.junit.Assert.fail;
+
+public class DeleteAcceptanceTest extends AbstractAcceptanceTest {
+
+    private APIDriver apiDriver;
+    private SwordClientDriver swordDriver;
+
+    @Before
+    public void setUp() throws Exception {
+        apiDriver = driverFactory.createAPIDriver();
+        swordDriver = driverFactory.createSwordDriver();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldDeleteItem() throws Exception {
+        fail();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldDeleteFileFromItem() throws Exception {
+        fail();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldNotDeleteUnknownItem() throws Exception {
+        fail();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldNotDeleteUnknownFile() throws Exception {
+        fail();
+    }
+}

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

+package net.chempound.acceptance.sword;
+
+import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
+import net.chempound.acceptance.dsl.driver.api.APIDriver;
+import net.chempound.acceptance.dsl.driver.sword.SwordClientDriver;
+import net.chempound.acceptance.dsl.driver.sword.SwordDepositReceipt;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static net.chempound.acceptance.dsl.driver.DepositSpecifications.withSlug;
+import static net.chempound.acceptance.dsl.driver.DepositSpecifications.withTitle;
+import static net.chempound.acceptance.dsl.driver.ItemMatchers.hasSlug;
+import static net.chempound.acceptance.dsl.driver.ItemMatchers.hasTitle;
+import static org.junit.Assert.fail;
+
+public class DepositAcceptanceTest extends AbstractAcceptanceTest {
+
+    private APIDriver apiDriver;
+    private SwordClientDriver swordDriver;
+
+    @Before
+    public void setUp() throws Exception {
+        apiDriver = driverFactory.createAPIDriver();
+        swordDriver = driverFactory.createSwordDriver();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldSupportMetaDataOnlyDeposit() throws Exception {
+        final SwordDepositReceipt depositReceipt = swordDriver.depositItem(withSlug("slug"), withTitle("title"));
+        verify(depositReceipt, hasTitle("title"), hasSlug("slug"));
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldSupportBinaryDeposit() throws Exception {
+        fail();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldSupportMultipartDeposit() throws Exception {
+        fail();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldSupportInProgressDeposit() throws Exception {
+        fail();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldCompleteInProgressDeposit() throws Exception {
+        fail();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldProvideDepositReceipt() throws Exception {
+        final SwordDepositReceipt depositReceipt = swordDriver.depositItem(withSlug("slug"));
+
+        final SwordDepositReceipt copyOfDepositReceipt = swordDriver.getDepositReceipt(depositReceipt.getEditUri());
+
+        verify(copyOfDepositReceipt, hasSlug("slug"));
+    }
+}

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

+package net.chempound.acceptance.sword;
+
+import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
+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 net.chempound.acceptance.dsl.driver.sword.SwordEntry;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+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;
+
+public class ListCollectionAcceptanceTest extends AbstractAcceptanceTest {
+
+    private APIDriver apiDriver;
+    private SwordClientDriver swordDriver;
+
+    @Before
+    public void setUp() throws Exception {
+        apiDriver = driverFactory.createAPIDriver();
+        swordDriver = driverFactory.createSwordDriver();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldListCollectionContents() throws Exception {
+        final DepositReceipt depositReceipt = apiDriver.depositItem(withSlug("slug"), withTitle("title"));
+
+        final SwordCollection collection = swordDriver.getCollection(testContext.getRootUri().resolve("sword/collection/"));
+
+        verify(collection, hasEntry(hasId(depositReceipt.getUri()), hasTitle("title"), 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");
+            }
+        };
+    }
+}

acceptance-tests/src/test/java/net/chempound/acceptance/sword/ServiceDocumentAcceptanceTest.java

+package net.chempound.acceptance.sword;
+
+import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
+import net.chempound.acceptance.dsl.driver.sword.SwordClientDriver;
+import net.chempound.acceptance.dsl.driver.sword.SwordServiceDocument;
+import org.junit.Before;
+import org.junit.Test;
+
+import static net.chempound.acceptance.dsl.driver.ServiceDocumentMatchers.hasMaxUploadSize;
+import static net.chempound.acceptance.dsl.driver.ServiceDocumentMatchers.hasVersion;
+
+/**
+ * @author Sam Adams
+ */
+public class ServiceDocumentAcceptanceTest extends AbstractAcceptanceTest {
+
+    private SwordClientDriver swordDriver;
+
+    @Before
+    public void setUp() throws Exception {
+        swordDriver = driverFactory.createSwordDriver();
+    }
+
+    @Test
+    public void shouldProvideSwordStatement() throws Exception {
+        final SwordServiceDocument serviceDocument = swordDriver.getServiceDocument();
+        verify(serviceDocument, hasVersion("2.0"), hasMaxUploadSize("-1"));
+    }
+}

acceptance-tests/src/test/java/net/chempound/acceptance/sword/StatementAcceptanceTest.java

+package net.chempound.acceptance.sword;
+
+import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
+import net.chempound.acceptance.dsl.driver.api.APIDriver;
+import net.chempound.acceptance.dsl.driver.sword.SwordClientDriver;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static org.junit.Assert.fail;
+
+public class StatementAcceptanceTest extends AbstractAcceptanceTest {
+
+    private APIDriver apiDriver;
+    private SwordClientDriver swordDriver;
+
+    @Before
+    public void setUp() throws Exception {
+        apiDriver = driverFactory.createAPIDriver();
+        swordDriver = driverFactory.createSwordDriver();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldProvideAtomFormatStatement() throws Exception {
+        fail();
+    }
+
+    @Test
+    @Ignore("work in progress")
+    public void shouldProvideOreFormatStatement() throws Exception {
+        fail();
+    }
+}

acceptance-tests/src/test/java/net/chempound/acceptance/sword/SwordStatementAcceptanceTest.java

-package net.chempound.acceptance.sword;
-
-import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
-import net.chempound.acceptance.dsl.driver.sword.SwordClientDriver;
-import net.chempound.acceptance.dsl.driver.sword.SwordServiceDocument;
-import org.junit.Before;
-import org.junit.Test;
-
-import static net.chempound.acceptance.dsl.driver.ServiceDocumentMatchers.hasMaxUploadSize;
-import static net.chempound.acceptance.dsl.driver.ServiceDocumentMatchers.hasVersion;
-
-/**
- * @author Sam Adams
- */
-public class SwordStatementAcceptanceTest extends AbstractAcceptanceTest {
-
-    private SwordClientDriver swordDriver;
-
-    @Before
-    public void setUp() throws Exception {
-        swordDriver = driverFactory.createSwordDriver();
-    }
-
-    @Test
-    public void shouldProvideSwordStatement() throws Exception {
-        final SwordServiceDocument serviceDocument = swordDriver.getServiceDocument();
-        verify(serviceDocument, hasVersion("2.0"), hasMaxUploadSize("-1"));
-    }
-}