Sam Adams avatar Sam Adams committed c311e02

Implemented pingback checks in web driver

Comments (0)

Files changed (8)

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

 
     @BeforeClass
     public static void startChempound() throws Exception {
-        rootUri = chempoundServer.startChempoundInstance();
+        rootUri = chempoundServer.startInstance();
     }
 
     @AfterClass
     public static void stopChempound() throws Exception {
-        chempoundServer.stopChempoundInstance(rootUri);
+        chempoundServer.stopInstance(rootUri);
     }
 
     @After
         driverFactory.shutdown();
     }
 
+    protected Resource newResource(final String path) {
+        return new Resource(testContext.actual(path), "mime/type", generateContent());
+    }
+
     protected static <T, U extends T> void check(final U target, final Matcher<T>... matchers) {
         for (final Matcher<T> matcher : matchers) {
             assertThat(target, matcher);
         }
     }
 
-    protected Resource newResource(final String path) {
-        return new Resource(testContext.actual(path), "mime/type", generateContent());
-    }
-
     private static byte[] generateContent() {
         final byte[] bytes = new byte[1024];
         final Random random = new Random();

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

     public boolean hasSlug(final String slug) {
         return uri.toString().contains(slug);
     }
+
+    @Override
+    public boolean hasPingbacks(final URI... pingbacks) {
+        throw new UnsupportedOperationException();
+    }
+
 }

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

 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeMatcher;
 
+import java.net.URI;
 import java.util.Arrays;
 
 /**
         return new ItemFileLinkMatcher(files);
     }
 
+    public static Matcher<ItemView> hasPingbacks(final URI... pingbacks) {
+        return new PingbackMatcher(pingbacks);
+    }
+
     private static class ItemSlugMatcher extends TypeSafeMatcher<ItemView> {
         private final String slug;
 
 
         @Override
         public void describeTo(final Description description) {
-            description.appendText("slug matches '" + slug + "'");
+            description.appendText("slug matches: '" + slug + "'");
         }
     }
 
 
         @Override
         public void describeTo(final Description description) {
-            description.appendText("title matches '" + title + "'");
+            description.appendText("title matches: '" + title + "'");
         }
     }
 
 
         @Override
         public void describeTo(final Description description) {
-            description.appendText("contains file '" + Arrays.toString(files) + "'");
+            description.appendText("has files: " + Arrays.toString(files));
+        }
+    }
+
+    private static class PingbackMatcher extends TypeSafeMatcher<ItemView> {
+
+        private final URI[] pingbacks;
+
+        public PingbackMatcher(final URI[] pingbacks) {
+            this.pingbacks = pingbacks;
+        }
+
+        @Override
+        protected boolean matchesSafely(final ItemView item) {
+            return item.hasPingbacks(pingbacks);
+        }
+
+        @Override
+        public void describeTo(final Description description) {
+            description.appendText("has pingbacks: " + Arrays.toString(pingbacks));
         }
     }
 }

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

 import org.openqa.selenium.WebElement;
 
 import java.net.URI;
+import java.util.List;
+
+import static java.lang.String.format;
 
 /**
  * @author Sam Adams
  */
 public class ItemPage implements ItemView {
 
+    private static final String XPATH_FILE = "//a[@href='%s']";
+    private static final String XPATH_PINGBACK = "//li[contains(@class, 'pingback')]/a[@href='%s']";
+
     private final WebDriver webDriver;
     private final TestContext context;
 
     public boolean hasFiles(final String... files) {
         for (final String file : files) {
             final URI uri = URI.create(webDriver.getCurrentUrl()).resolve(file);
-            final WebElement element = webDriver.findElement(By.xpath("//a[@href='" + uri + "']"));
-            if (element == null) {
+            final List<WebElement> elements = webDriver.findElements(By.xpath(format(XPATH_FILE, uri)));
+            if (elements.isEmpty()) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public boolean hasPingbacks(final URI... pingbacks) {
+        for (final URI pingback : pingbacks) {
+            final List<WebElement> elements = webDriver.findElements(By.xpath(format(XPATH_PINGBACK, pingback)));
+            if (elements.isEmpty()) {
                 return false;
             }
         }

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

 package net.chempound.acceptance.dsl.driver;
 
+import java.net.URI;
+
 /**
  * @author Sam Adams
  */
     boolean hasFiles(String... file);
 
     boolean hasSlug(String slug);
+
+    boolean hasPingbacks(URI... pingbacks);
+
 }

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

     private ChempoundServer() {
         component = new Component();
         final Server server = component.getServers().add(Protocol.HTTP, PORT);
-        server.getContext().getParameters().add("maxThreads", "256");
+        server.getContext().getParameters().add("maxThreads", "512");
     }
 
-    public URI startChempoundInstance() throws Exception {
+    public URI startInstance() throws Exception {
         if (component.isStopped()) {
             startComponent();
         }
         return injector.getInstance(Application.class);
     }
 
-    public void stopChempoundInstance(final URI rootUri) throws Exception {
+    public void stopInstance(final URI rootUri) throws Exception {
         final Application instance = instances.remove(rootUri);
         component.getDefaultHost().detach(instance);
         instance.stop();

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

                      .append("<head><title>Blog Post</title></head>")
                      .append("<body>");
                     for (final URI item : posts.get(id)) {
-                        html.append("<p><a href='" + item + "'>link to item</a></p>");
+                        html.append("<p><a href='").append(item).append("'>link to item</a></p>");
                     }
                     html
                      .append("</body>")

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

 
 import java.net.URI;
 
+import static net.chempound.acceptance.dsl.driver.ItemMatchers.hasPingbacks;
+
 /**
  * @author Sam Adams
  */
         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, testContext.getPingbackUrl());
     }
 
     @Test
     public void shouldRejectPingbackWithBadSourceUrl() throws Exception {
         URI blog = blogServer.addPost(item.getUri());
+
         pingbackDriver.pingback(item.getUri(), blog.resolve("--does-not-exist"), PingbackFault.SOURCE_URI_DOES_NOT_EXIST);
     }
 
     @Test
-    @Ignore
+    @SuppressWarnings("unchecked")
     public void shouldDisplayPingbackOnItemPage() throws Exception {
         URI blog = blogServer.addPost(item.getUri());
+
         pingbackDriver.pingback(item.getUri(), blog);
 
-        // webUIDriver.check(item, hasPingback());
-
-        Assert.fail("work in progress");
+        webUIDriver.check(item, hasPingbacks(blog));
     }
 
     @Test
-    @Ignore
-    public void shouldUpdatePingbacksOnItemPageWhenNewPingbackRecieved() throws Exception {
+    @SuppressWarnings("unchecked")
+    public void shouldUpdatePingbacksOnItemPageWhenNewPingbackReceived() throws Exception {
         URI blog1 = blogServer.addPost(item.getUri());
         URI blog2 = blogServer.addPost(item.getUri());
 
         pingbackDriver.pingback(item.getUri(), blog1);
 
-        // webUIDriver.check(item, hasPingbacks());
+        webUIDriver.check(item, hasPingbacks(blog1));
 
         pingbackDriver.pingback(item.getUri(), blog2);
 
-        // webUIDriver.check(item, hasPingbacks());
-
-        Assert.fail("work in progress");
+        webUIDriver.check(item, hasPingbacks(blog1, blog2));
     }
-
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.