Commits

Sam Adams  committed 5625594

More work on Pingback ATs

  • Participants
  • Parent commits 7d7bcc7

Comments (0)

Files changed (4)

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

+package net.chempound.acceptance.dsl;
+
+import org.restlet.*;
+import org.restlet.data.MediaType;
+import org.restlet.data.Protocol;
+import org.restlet.data.Status;
+import org.restlet.representation.StringRepresentation;
+
+import java.net.URI;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Sam Adams
+ */
+public class StubBlogServer {
+
+    private static final int PORT = 23456;
+
+    private final Component component;
+    private final ConcurrentMap<String, URI> posts = new ConcurrentHashMap<String, URI>();
+    private final ConcurrentMap<URI, AtomicInteger> visits = new ConcurrentHashMap<URI, AtomicInteger>();
+
+    public StubBlogServer() {
+        component = new Component();
+        component.getServers().add(Protocol.HTTP, PORT);
+        component.getDefaultHost().attach("/blog", new BlogApplication(component.getContext().createChildContext()));
+    }
+
+    public void start() throws Exception {
+        component.start();
+    }
+
+    public void stop() throws Exception {
+        component.stop();
+    }
+
+    public URI addPost(final URI uri) {
+        final String id = Long.toString(System.nanoTime());
+        posts.put(id, uri);
+        final URI blogUrl = URI.create("http://localhost:23456/blog/post/" + id);
+        visits.put(blogUrl, new AtomicInteger());
+        return blogUrl;
+    }
+
+    public void checkPageLoads(final URI blogUrl, final int count) {
+        assertEquals(count, visits.get(blogUrl).get());
+    }
+
+    private class BlogApplication extends Application {
+
+        public BlogApplication(final Context context) {
+            super(context);
+        }
+
+        @Override
+        public void handle(final Request request, final Response response) {
+            final String path = request.getResourceRef().getRemainingPart();
+            if (path.startsWith("/post/")) {
+                final String id = path.substring(6);
+                if (posts.containsKey(id)) {
+                    visits.get(request.getResourceRef().toUri()).incrementAndGet();
+
+                    final String s = "<html>" +
+                            "<head><title>Blog Post</title></head>" +
+                            "<body>" +
+                            "<p><a href='" + posts.get(id) + "'>link to item</a></p>" +
+                            "</body>" +
+                            "</html>";
+
+                    response.setEntity(new StringRepresentation(s, MediaType.TEXT_HTML));
+                    return;
+                }
+            }
+            response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
+        }
+
+    }
+
+}

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 net.chempound.webapp.pingback.PingbackFault;
+import nu.xom.Builder;
 import nu.xom.Document;
+import nu.xom.Nodes;
 import org.apache.http.Header;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import java.io.IOException;
 import java.net.URI;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 /**
  * @author Sam Adams
  */
         this.context = context;
     }
 
-    public void checkPingbackUrl(final URI targetUrl) throws IOException {
-        findPingbackUrl(targetUrl);
+    public void pingback(final URI targetUrl, final URI sourceUrl) throws Exception {
+        pingback(targetUrl, sourceUrl, null);
     }
 
-    public void pingback(final URI targetUrl) throws IOException {
+    public void pingback(final URI targetUrl, final URI sourceUrl, final PingbackFault expectedFault) throws Exception {
         final URI pingbackUrl = findPingbackUrl(targetUrl);
-        final URI sourceUrl = URI.create("http://example.com/test");
-        final Document response = sendPingback(pingbackUrl, targetUrl);
+        doPingback(targetUrl, sourceUrl, expectedFault, pingbackUrl);
     }
 
-    private Document sendPingback(final URI pingbackUrl, final URI targetUrl) throws IOException {
+    public void doPingback(final URI targetUrl, final URI sourceUrl, final PingbackFault expectedFault, final URI pingbackUrl) throws Exception {
+        final Document response = sendPingback(pingbackUrl, sourceUrl, targetUrl);
+        if (expectedFault == null) {
+            checkSucccess(response);
+        } else {
+            checkFault(response, expectedFault);
+        }
+    }
+
+    private void checkSucccess(final Document response) {
+        assertTrue(response.toXML(), response.query("/methodResponse/params").size() != 0);
+    }
+
+    private void checkFault(final Document response, final PingbackFault expectedFault) {
+        final Nodes query = response.query("/methodResponse/fault/value/struct/member/value/int");
+        assertEquals(response.toXML(), 1, query.size());
+        final String faultCode = query.get(0).getValue();
+        assertEquals(response.toXML(), expectedFault.getCode(), Integer.parseInt(faultCode));
+    }
+
+    private Document sendPingback(final URI pingbackUrl, final URI sourceUrl, final URI targetUrl) throws Exception {
         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>" +
                 throw new RuntimeException("HTTP request returned status: " + response.getStatusLine());
             }
 
-            response.getEntity().writeTo(System.out);
-
-            return null;
+            return new Builder().build(response.getEntity().getContent());
         } finally {
             EntityUtils.consume(response.getEntity());
         }
     }
 
-    private URI findPingbackUrl(final URI targetUrl) throws IOException {
+    public URI findPingbackUrl(final URI targetUrl) throws IOException {
         final HttpResponse response = httpClient.execute(new HttpGet(targetUrl));
         try {
             if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

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.StubBlogServer;
 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;
+import net.chempound.webapp.pingback.PingbackFault;
+import org.junit.*;
+
+import java.net.URI;
 
 /**
  * @author Sam Adams
  */
 public class PingbackAcceptanceTest extends AbstractAcceptanceTest {
 
+    private static StubBlogServer blogServer;
+
     private APIDriver apiDriver;
     private WebUIDriver webUIDriver;
     private PingbackDriver pingbackDriver;
                 .execute();
     }
 
-    @Test
-    public void shouldDetectPingbackUrlForItem() throws Exception {
-        pingbackDriver.checkPingbackUrl(item.getUri());
+    @BeforeClass
+    public static void startBlogServer() throws Exception {
+        blogServer = new StubBlogServer();
+        blogServer.start();
+    }
+
+    @AfterClass
+    public static void stopBlogServer() throws Exception {
+        blogServer.stop();
+        blogServer = null;
     }
 
     @Test
-    @Ignore
-    public void shouldAcceptPingbackToItem() throws Exception {
-        pingbackDriver.pingback(item.getUri());
+    public void shouldDetectPingbackUrlForItem() throws Exception {
+        pingbackDriver.findPingbackUrl(item.getUri());
     }
 
     @Test
-    @Ignore
-    public void shouldAcceptMultiplePingbacksToItem() throws Exception {
-        pingbackDriver.pingback(item.getUri());
-        pingbackDriver.pingback(item.getUri());
+    public void shouldAcceptPingbackToItem() throws Exception {
+        URI blog = blogServer.addPost(item.getUri());
+
+        pingbackDriver.pingback(item.getUri(), blog);
+
+        blogServer.checkPageLoads(blog, 1);
     }
 
     @Test
-    @Ignore
-    public void shouldRejectDuplicatePingbackToItem() throws Exception {
-        pingbackDriver.pingback(item.getUri());
-        pingbackDriver.pingback(item.getUri());
+    public void shouldAcceptMultiplePingbacksToItem() throws Exception {
+        URI blog1 = blogServer.addPost(item.getUri());
+        URI blog2 = blogServer.addPost(item.getUri());
 
-        Assert.fail("work in progress");
-    }
+        pingbackDriver.pingback(item.getUri(), blog1);
+        pingbackDriver.pingback(item.getUri(), blog2);
 
-
-    @Test
-    @Ignore
-    public void shouldRejectPingbackToNonExistentItem() throws Exception {
-
-        Assert.fail("work in progress");
+        blogServer.checkPageLoads(blog1, 1);
+        blogServer.checkPageLoads(blog2, 1);
     }
 
     @Test
-    @Ignore
+    public void shouldRejectDuplicatePingbackToItem() throws Exception {
+        URI blog = blogServer.addPost(item.getUri());
+
+        pingbackDriver.pingback(item.getUri(), blog);
+        pingbackDriver.pingback(item.getUri(), blog, PingbackFault.ALREADY_REGISTERED);
+    }
+
+    @Test
+    public void shouldRejectPingbackToNonExistentItem() throws Exception {
+        final URI itemUri = item.getUri().resolve("--does-not-exist");
+        URI blog = blogServer.addPost(itemUri);
+        pingbackDriver.doPingback(itemUri, blog, PingbackFault.TARGET_URI_INVALID, URI.create("http://localhost:12345/chempound/pingback"));
+    }
+
+    @Test
     public void shouldRejectPingbackWithBadSourceUrl() throws Exception {
-
-        Assert.fail("work in progress");
+        URI blog = blogServer.addPost(item.getUri());
+        pingbackDriver.pingback(item.getUri(), blog.resolve("--does-not-exist"), PingbackFault.SOURCE_URI_DOES_NOT_EXIST);
     }
 
     @Test
     @Ignore
     public void shouldDisplayPingbackOnItemPage() throws Exception {
-        pingbackDriver.pingback(item.getUri());
+        URI blog = blogServer.addPost(item.getUri());
+        pingbackDriver.pingback(item.getUri(), blog);
 
         // webUIDriver.check(item, hasPingback());
 
     @Test
     @Ignore
     public void shouldUpdatePingbacksOnItemPageWhenNewPingbackRecieved() throws Exception {
-        pingbackDriver.pingback(item.getUri());
+        URI blog1 = blogServer.addPost(item.getUri());
+        URI blog2 = blogServer.addPost(item.getUri());
+
+        pingbackDriver.pingback(item.getUri(), blog1);
 
         // webUIDriver.check(item, hasPingbacks());
 
-        pingbackDriver.pingback(item.getUri());
+        pingbackDriver.pingback(item.getUri(), blog2);
 
         // webUIDriver.check(item, hasPingbacks());
 
             <dependency>
                 <groupId>org.swordapp</groupId>
                 <artifactId>server</artifactId>
-                <version>2.0-r339</version>
+                <version>${sword.version}</version>
             </dependency>
             <dependency>
                 <groupId>org.swordapp</groupId>
                 <artifactId>sword-client</artifactId>
-                <version>2.0-r339</version>
+                <version>${sword.version}</version>
             </dependency>
 
             <dependency>
                 <groupId>org.freemarker</groupId>
                 <artifactId>freemarker</artifactId>
-                <version>2.3.18</version>
+                <version>${freemarker.version}</version>
             </dependency>
 
             <dependency>