Author: bdelacretaz
Date: Wed Feb  3 15:31:52 2016
New Revision: 1728331

URL: http://svn.apache.org/viewvc?rev=1728331&view=rev
Log:
SLING-5452 - verify that webconsole is ready before installing bundle

Added:
    
sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClientTest.java
Modified:
    sling/trunk/testing/junit/teleporter/pom.xml
    
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java
    
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java

Modified: sling/trunk/testing/junit/teleporter/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/pom.xml?rev=1728331&r1=1728330&r2=1728331&view=diff
==============================================================================
--- sling/trunk/testing/junit/teleporter/pom.xml (original)
+++ sling/trunk/testing/junit/teleporter/pom.xml Wed Feb  3 15:31:52 2016
@@ -68,8 +68,14 @@
       <artifactId>slf4j-simple</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.github.tomakehurst</groupId>
+      <artifactId>wiremock</artifactId>
+      <version>1.57</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
-
+  
   <scm>
     
<connection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/junit/teleporter</connection>
     
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/junit/teleporter</developerConnection>

Modified: 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java?rev=1728331&r1=1728330&r2=1728331&view=diff
==============================================================================
--- 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java
 (original)
+++ 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/ClientSideTeleporter.java
 Wed Feb  3 15:31:52 2016
@@ -47,6 +47,7 @@ public class ClientSideTeleporter extend
     public static final String DEFAULT_TEST_SERVLET_PATH = 
"system/sling/junit";
     private DependencyAnalyzer dependencyAnalyzer;
     private int testReadyTimeoutSeconds = 5;
+    private int webConsoleReadyTimeoutSeconds = 30;
     private String baseUrl;
     private String serverCredentials;
     private String testServletPath = DEFAULT_TEST_SERVLET_PATH;
@@ -104,6 +105,11 @@ public class ClientSideTeleporter extend
         testReadyTimeoutSeconds = tm;
     }
     
+    /** Define how long to wait for the webconsole to be ready, before 
installing the test bundle */
+    public void setWebConsoleReadyTimeoutSeconds (int tm) {
+        webConsoleReadyTimeoutSeconds = tm;
+    }
+    
     /** Set the credentials to use to install our test bundle on the server */
     public void setServerCredentials(String username, String password) {
         serverCredentials = username + ":" + password;
@@ -154,7 +160,7 @@ public class ClientSideTeleporter extend
         final SimpleDateFormat fmt = new SimpleDateFormat("HH-mm-ss-");
         final String bundleSymbolicName = getClass().getSimpleName() + "." + 
fmt.format(new Date()) + "." + UUID.randomUUID();
         final InputStream bundle = buildTestBundle(classUnderTest, 
embeddedClasses, bundleSymbolicName);
-        httpClient.installBundle(bundle, bundleSymbolicName);
+        httpClient.installBundle(bundle, bundleSymbolicName, 
webConsoleReadyTimeoutSeconds);
         return bundleSymbolicName;
     }
     

Modified: 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java?rev=1728331&r1=1728330&r2=1728331&view=diff
==============================================================================
--- 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java
 (original)
+++ 
sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java
 Wed Feb  3 15:31:52 2016
@@ -26,7 +26,9 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import javax.xml.bind.DatatypeConverter;
 
@@ -62,14 +64,34 @@ class TeleporterHttpClient {
             c.setRequestProperty ("Authorization", basicAuth);
         }
     }
+
+    /** Wait until specified URL returns specified status */
+    public void waitForStatus(String url, int expectedStatus, int timeoutMsec) 
throws IOException {
+        final long end = System.currentTimeMillis() + timeoutMsec;
+        final Set<Integer> statusSet = new HashSet<Integer>();
+        while(System.currentTimeMillis() < end) {
+            try {
+                final int status = getHttpGetStatus(url);
+                statusSet.add(status);
+                if(status == expectedStatus) {
+                    return;
+                }
+                Thread.sleep(50);
+            } catch(Exception ignore) {
+            }
+        }
+        throw new IOException("Did not get status " + expectedStatus + " at " 
+ url + " after " + timeoutMsec + " msec, got " + statusSet);
+    }
     
-    void installBundle(InputStream bundle, String bundleSymbolicName) throws 
MalformedURLException, IOException {
+    void installBundle(InputStream bundle, String bundleSymbolicName, int 
webConsoleReadyTimeoutSeconds) throws MalformedURLException, IOException {
         // Equivalent of
         // curl -u admin:admin -F action=install -Fbundlestart=1 
[email protected] http://localhost:8080/system/console/bundles
         final String url = baseUrl + "/system/console/bundles";
         final String contentType = "application/octet-stream";
         final HttpURLConnection c = (HttpURLConnection)new 
URL(url).openConnection();
         
+        waitForStatus(url, 200, webConsoleReadyTimeoutSeconds * 1000);
+        
         try {
             setConnectionCredentials(c);
             new MultipartAdapter(c, CHARSET)
@@ -106,7 +128,7 @@ class TeleporterHttpClient {
         }
     }
     
-    private int getHttpGetStatus(String url) throws MalformedURLException, 
IOException {
+    public int getHttpGetStatus(String url) throws MalformedURLException, 
IOException {
         final HttpURLConnection c = (HttpURLConnection)new 
URL(url).openConnection();
         setConnectionCredentials(c);
         c.setUseCaches(false);

Added: 
sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClientTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClientTest.java?rev=1728331&view=auto
==============================================================================
--- 
sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClientTest.java
 (added)
+++ 
sling/trunk/testing/junit/teleporter/src/test/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClientTest.java
 Wed Feb  3 15:31:52 2016
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.testing.teleporter.client;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.givenThat;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
+
+public class TeleporterHttpClientTest {
+    private static final int PORT = 1234;
+    private static final String baseUrl = "http://127.0.0.1:"; + PORT;
+    private static final String TEST_PATH = "/foo";
+    
+    @Rule
+    public WireMockRule http = new WireMockRule(PORT);
+    
+    private void activateLater(final String path, long delayMsec) {
+        TimerTask t = new TimerTask() {
+            public void run() {
+                
givenThat(get(urlEqualTo(path)).willReturn(aResponse().withStatus(200)));
+            }
+        };
+        
+        new Timer(true).schedule(t, delayMsec);
+    }
+    
+    @Test
+    public void waitForStatusWithLongTimeout() throws MalformedURLException, 
IOException {
+        final TeleporterHttpClient client = new TeleporterHttpClient(baseUrl, 
TEST_PATH);
+        final String testUrl = baseUrl + TEST_PATH;
+        
+        assertEquals(404, client.getHttpGetStatus(baseUrl + TEST_PATH));
+        activateLater(TEST_PATH, 1000);
+        client.waitForStatus(testUrl, 200, 2000);
+        assertEquals(200, client.getHttpGetStatus(baseUrl + TEST_PATH));
+    }
+    
+    @Test
+    public void waitForStatusWithShortTimeout() throws MalformedURLException, 
IOException {
+        final TeleporterHttpClient client = new TeleporterHttpClient(baseUrl, 
TEST_PATH);
+        final String testUrl = baseUrl + TEST_PATH;
+        
+        assertEquals(404, client.getHttpGetStatus(baseUrl + TEST_PATH));
+        activateLater(TEST_PATH, 1000);
+        
+        try {
+            client.waitForStatus(testUrl, 200, 100);
+            fail("Expected waitForStatus to timeout");
+        } catch(IOException expected) {
+        }
+    }
+}
\ No newline at end of file


Reply via email to