Commits

Sam Adams committed 7d7bcc7

Work on Pingback ATs

  • Participants
  • Parent commits 707dd0e

Comments (0)

Files changed (7)

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

 package net.chempound.acceptance.dsl;
 
 import net.chempound.acceptance.dsl.driver.APIDriver;
+import net.chempound.acceptance.dsl.driver.PingbackDriver;
 import net.chempound.acceptance.dsl.driver.WebUIDriver;
 
 /**
 
     WebUIDriver createWebUIDriver();
 
+    PingbackDriver createPingbackDriver();
+
     void shutdown();
+
 }

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

 
 import com.gargoylesoftware.htmlunit.BrowserVersion;
 import net.chempound.acceptance.dsl.driver.APIDriver;
+import net.chempound.acceptance.dsl.driver.PingbackDriver;
 import net.chempound.acceptance.dsl.driver.WebUIDriver;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver;
     }
 
     @Override
+    public PingbackDriver createPingbackDriver() {
+        return new PingbackDriver(context);
+    }
+
+    @Override
     public void shutdown() {
         webDriver.close();
     }

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

 import net.chempound.storage.InMemoryResource;
 import net.chempound.storage.LocalResource;
 import net.chempound.util.MimeType;
-import org.hamcrest.Matcher;
 
 import java.net.URI;
 
-import static org.junit.Assert.assertThat;
-
 /**
  * @author Sam Adams
  */

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

     }
 
     public IRI getAlternateResources() {
-        Assert.fail();;
+        Assert.fail();
         // http://purl.org/net/sword/terms/originalDeposit
         return null;
     }

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

+package net.chempound.acceptance.dsl.driver;
+
+import net.chempound.acceptance.dsl.TestContext;
+import nu.xom.Document;
+import org.apache.http.Header;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+import java.net.URI;
+
+/**
+ * @author Sam Adams
+ */
+public class PingbackDriver {
+
+    private final HttpClient httpClient = new DefaultHttpClient();
+
+    private final TestContext context;
+
+    public PingbackDriver(final TestContext context) {
+        this.context = context;
+    }
+
+    public void checkPingbackUrl(final URI targetUrl) throws IOException {
+        findPingbackUrl(targetUrl);
+    }
+
+    public void pingback(final URI targetUrl) throws IOException {
+        final URI pingbackUrl = findPingbackUrl(targetUrl);
+        final URI sourceUrl = URI.create("http://example.com/test");
+        final Document response = sendPingback(pingbackUrl, targetUrl);
+    }
+
+    private Document sendPingback(final URI pingbackUrl, final URI targetUrl) throws IOException {
+        final HttpPost request = new HttpPost(pingbackUrl);
+        final URI sourceUrl = URI.create("http://www.example.com/blog/123");
+        request.setEntity(new StringEntity("<?xml version='1.0'?>" +
+                                            "<methodCall>" +
+                                            "  <methodName>pingback.ping</methodName>" +
+                                            "  <params>" +
+                                            "    <param><value><string>" + sourceUrl + "</string></value></param>" +
+                                            "    <param><value><string>" + targetUrl + "</string></value></param>" +
+                                            "  </params>" +
+                                            "</methodCall>", "text/xml", "UTF-8"));
+
+        final HttpResponse response = httpClient.execute(request);
+        try {
+            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
+                throw new RuntimeException("HTTP request returned status: " + response.getStatusLine());
+            }
+
+            response.getEntity().writeTo(System.out);
+
+            return null;
+        } finally {
+            EntityUtils.consume(response.getEntity());
+        }
+    }
+
+    private URI findPingbackUrl(final URI targetUrl) throws IOException {
+        final HttpResponse response = httpClient.execute(new HttpGet(targetUrl));
+        try {
+            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
+                throw new RuntimeException("HTTP request returned status: " + response.getStatusLine());
+            }
+            final Header header = response.getFirstHeader("X-Pingback");
+            if (header == null) {
+                throw new RuntimeException("X-Pingback header not found");
+            }
+            return URI.create(header.getValue());
+        } finally {
+            EntityUtils.consume(response.getEntity());
+        }
+    }
+
+}

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

+package net.chempound.acceptance;
+
+import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
+import net.chempound.acceptance.dsl.driver.APIDriver;
+import net.chempound.acceptance.dsl.driver.DepositReceipt;
+import net.chempound.acceptance.dsl.driver.PingbackDriver;
+import net.chempound.acceptance.dsl.driver.WebUIDriver;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * @author Sam Adams
+ */
+public class PingbackAcceptanceTest extends AbstractAcceptanceTest {
+
+    private APIDriver apiDriver;
+    private WebUIDriver webUIDriver;
+    private PingbackDriver pingbackDriver;
+
+    private DepositReceipt item;
+
+    @Before
+    public void setUp() throws Exception {
+        apiDriver = driverFactory.createAPIDriver();
+        webUIDriver = driverFactory.createWebUIDriver();
+        pingbackDriver = driverFactory.createPingbackDriver();
+
+        item = apiDriver.depositItem()
+                .withTitle("title")
+                .execute();
+    }
+
+    @Test
+    public void shouldDetectPingbackUrlForItem() throws Exception {
+        pingbackDriver.checkPingbackUrl(item.getUri());
+    }
+
+    @Test
+    @Ignore
+    public void shouldAcceptPingbackToItem() throws Exception {
+        pingbackDriver.pingback(item.getUri());
+    }
+
+    @Test
+    @Ignore
+    public void shouldAcceptMultiplePingbacksToItem() throws Exception {
+        pingbackDriver.pingback(item.getUri());
+        pingbackDriver.pingback(item.getUri());
+    }
+
+    @Test
+    @Ignore
+    public void shouldRejectDuplicatePingbackToItem() throws Exception {
+        pingbackDriver.pingback(item.getUri());
+        pingbackDriver.pingback(item.getUri());
+
+        Assert.fail("work in progress");
+    }
+
+
+    @Test
+    @Ignore
+    public void shouldRejectPingbackToNonExistentItem() throws Exception {
+
+        Assert.fail("work in progress");
+    }
+
+    @Test
+    @Ignore
+    public void shouldRejectPingbackWithBadSourceUrl() throws Exception {
+
+        Assert.fail("work in progress");
+    }
+
+    @Test
+    @Ignore
+    public void shouldDisplayPingbackOnItemPage() throws Exception {
+        pingbackDriver.pingback(item.getUri());
+
+        // webUIDriver.check(item, hasPingback());
+
+        Assert.fail("work in progress");
+    }
+
+    @Test
+    @Ignore
+    public void shouldUpdatePingbacksOnItemPageWhenNewPingbackRecieved() throws Exception {
+        pingbackDriver.pingback(item.getUri());
+
+        // webUIDriver.check(item, hasPingbacks());
+
+        pingbackDriver.pingback(item.getUri());
+
+        // webUIDriver.check(item, hasPingbacks());
+
+        Assert.fail("work in progress");
+    }
+
+}

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

 import org.junit.Ignore;
 import org.junit.Test;
 
-import java.util.Random;
-
 import static net.chempound.acceptance.dsl.driver.ItemMatchers.*;
 
 /**