Author: bdelacretaz
Date: Fri Jun 12 10:18:06 2015
New Revision: 1685057

URL: http://svn.apache.org/r1685057
Log:
SLING-4728 - refactor/cleanup tests

Added:
    
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java
Modified:
    sling/trunk/contrib/crankstart/launcher/pom.xml
    
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
    
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java

Modified: sling/trunk/contrib/crankstart/launcher/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/pom.xml?rev=1685057&r1=1685056&r2=1685057&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/pom.xml (original)
+++ sling/trunk/contrib/crankstart/launcher/pom.xml Fri Jun 12 10:18:06 2015
@@ -160,6 +160,12 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.tools</artifactId>
+            <version>1.0.10</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.json</artifactId>
             <version>2.0.6</version>
             <scope>test</scope>

Modified: 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java?rev=1685057&r1=1685056&r2=1685057&view=diff
==============================================================================
--- 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
 (original)
+++ 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
 Fri Jun 12 10:18:06 2015
@@ -3,6 +3,7 @@ package org.apache.sling.crankstart.laun
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
@@ -15,6 +16,7 @@ import org.apache.sling.commons.json.JSO
 import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.commons.testing.junit.Retry;
 import org.apache.sling.commons.testing.junit.RetryRule;
+import org.apache.sling.testing.tools.osgi.WebconsoleClient;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -25,9 +27,11 @@ import org.junit.Test;
 public class CrankstartBootstrapTest {
     
     private static final CrankstartSetup C = new CrankstartSetup();
-    public static final int LONG_TIMEOUT = 10000;
+    public static final int LONG_TIMEOUT_SECONDS = 10;
+    public static final int LONG_TIMEOUT_MSEC = LONG_TIMEOUT_SECONDS * 1000;
     public static final int STD_INTERVAL = 250;
     private DefaultHttpClient client;
+    private WebconsoleClient osgiConsole;
     
     @Rule
     public final RetryRule retryRule = new RetryRule();
@@ -35,11 +39,12 @@ public class CrankstartBootstrapTest {
     @Before
     public void setupHttpClient() throws IOException {
         C.setup();
-        client = new DefaultHttpClient(); 
+        client = new DefaultHttpClient();
+        osgiConsole = new WebconsoleClient(C.getBaseUrl(), "admin", "admin");
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
     public void testHttpRoot() throws Exception {
         final HttpUriRequest get = new HttpGet(C.getBaseUrl());
         HttpResponse response = null;
@@ -47,12 +52,12 @@ public class CrankstartBootstrapTest {
             response = client.execute(get);
             assertEquals("Expecting page not found at " + get.getURI(), 404, 
response.getStatusLine().getStatusCode());
         } finally {
-            C.closeConnection(response);
+            U.closeConnection(response);
         }
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
     public void testSingleConfigServlet() throws Exception {
         final HttpUriRequest get = new HttpGet(C.getBaseUrl() + "/single");
         HttpResponse response = null;
@@ -60,12 +65,12 @@ public class CrankstartBootstrapTest {
             response = client.execute(get);
             assertEquals("Expecting success for " + get.getURI(), 200, 
response.getStatusLine().getStatusCode());
         } finally {
-            C.closeConnection(response);
+            U.closeConnection(response);
         }
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
     public void testConfigFactoryServlet() throws Exception {
         final String [] paths = { "/foo", "/bar/test" };
         for(String path : paths) {
@@ -75,13 +80,13 @@ public class CrankstartBootstrapTest {
                 response = client.execute(get);
                 assertEquals("Expecting success for " + get.getURI(), 200, 
response.getStatusLine().getStatusCode());
             } finally {
-                C.closeConnection(response);
+                U.closeConnection(response);
             }
         }
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
     public void testJUnitServlet() throws Exception {
         final String path = "/system/sling/junit";
         final HttpUriRequest get = new HttpGet(C.getBaseUrl() + path);
@@ -90,83 +95,65 @@ public class CrankstartBootstrapTest {
             response = client.execute(get);
             assertEquals("Expecting JUnit servlet to be installed via sling 
extension command, at " + get.getURI(), 200, 
response.getStatusLine().getStatusCode());
         } finally {
-            C.closeConnection(response);
+            U.closeConnection(response);
         }
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
     public void testAdditionalBundles() throws Exception {
-        C.setAdminCredentials(client);
-        final String basePath = "/system/console/bundles/";
         final String [] addBundles = {
                 "org.apache.sling.commons.mime",
                 "org.apache.sling.settings"
         };
         
         for(String name : addBundles) {
-            final String path = basePath + name;
-            final HttpUriRequest get = new HttpGet(C.getBaseUrl() + path);
-            HttpResponse response = null;
             try {
-                response = client.execute(get);
-                assertEquals("Expecting additional bundle to be present at " + 
get.getURI(), 200, response.getStatusLine().getStatusCode());
-            } finally {
-                C.closeConnection(response);
+                osgiConsole.checkBundleInstalled(name, 
CrankstartBootstrapTest.LONG_TIMEOUT_SECONDS);
+            } catch(AssertionError ae) {
+                fail("Expected bundle to be present:" + name);
             }
         }
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
     public void testSpecificStartLevel() throws Exception {
-        // Verify that this bundle is only installed, as it's set to start 
level 99
-        C.setAdminCredentials(client);
-        final String path = 
"/system/console/bundles/org.apache.commons.collections.json";
-        final HttpUriRequest get = new HttpGet(C.getBaseUrl() + path);
-        HttpResponse response = null;
-        try {
-            response = client.execute(get);
-            assertEquals("Expecting bundle status to be available at " + 
get.getURI(), 200, response.getStatusLine().getStatusCode());
-            assertNotNull("Expecting response entity", response.getEntity());
-            String encoding = "UTF-8";
-            if(response.getEntity().getContentEncoding() != null) {
-                encoding = 
response.getEntity().getContentEncoding().getValue();
-            }
-            final String content = 
IOUtils.toString(response.getEntity().getContent(), encoding);
-            
-            // Start level is in the props array, with key="Start Level"
-            final JSONObject status = new JSONObject(content);
-            final JSONArray props = 
status.getJSONArray("data").getJSONObject(0).getJSONArray("props");
-            final String KEY = "key";
-            final String SL = "Start Level";
-            boolean found = false;
-            for(int i=0; i < props.length(); i++) {
-                final JSONObject o = props.getJSONObject(i);
-                if(o.has(KEY) && SL.equals(o.getString(KEY))) {
-                    found = true;
-                    assertEquals("Expecting the start level that we set", 
"99", o.getString("value"));
-                }
+        // This bundle should only be installed, as it's set to start level 99
+        final String symbolicName = "org.apache.commons.collections";
+        
+        assertEquals("Expecting bundle " + symbolicName + " to be installed", 
+                "Installed", 
+                osgiConsole.getBundleState(symbolicName));
+        
+        // Start level is in the props array, with key="Start Level"
+        final JSONObject status = U.getBundleData(C, client, symbolicName);
+        final JSONArray props = 
status.getJSONArray("data").getJSONObject(0).getJSONArray("props");
+        final String KEY = "key";
+        final String SL = "Start Level";
+        boolean found = false;
+        for(int i=0; i < props.length(); i++) {
+            final JSONObject o = props.getJSONObject(i);
+            if(o.has(KEY) && SL.equals(o.getString(KEY))) {
+                found = true;
+                assertEquals("Expecting the start level that's set in 
provisioning model", "99", o.getString("value"));
             }
-            assertTrue("Expecting start level to be found in JSON output", 
found);
-        } finally {
-            C.closeConnection(response);
         }
+        assertTrue("Expecting start level to be found in JSON output", found);
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
     public void testEmptyConfig() throws Exception {
-        C.setAdminCredentials(client);
+        U.setAdminCredentials(client);
         assertHttpGet(
             "/test/config/empty.config.should.work", 
             
"empty.config.should.work#service.pid=(String)empty.config.should.work##EOC#");
     }
         
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
     public void testFelixFormatConfig() throws Exception {
-        C.setAdminCredentials(client);
+        U.setAdminCredentials(client);
         assertHttpGet(
                 "/test/config/felix.format.test", 
                 "felix.format.test#array=(String[])[foo, 
bar.from.launcher.test]#mongouri=(String)mongodb://localhost:27017#service.pid=(String)felix.format.test#service.ranking.launcher.test=(Integer)54321##EOC#");
@@ -186,7 +173,7 @@ public class CrankstartBootstrapTest {
             final String content = 
IOUtils.toString(response.getEntity().getContent(), encoding);
             assertEquals(expectedContent, content);
         } finally {
-            C.closeConnection(response);
+            U.closeConnection(response);
         }
     }
     

Modified: 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java?rev=1685057&r1=1685056&r2=1685057&view=diff
==============================================================================
--- 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
 (original)
+++ 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
 Fri Jun 12 10:18:06 2015
@@ -11,13 +11,9 @@ import java.io.Reader;
 import java.net.ServerSocket;
 import java.util.Random;
 
-import org.apache.http.HttpResponse;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.util.EntityUtils;
 
 /** Setup a Crankstart-launched instance for our tests */ 
 public class CrankstartSetup {
@@ -52,13 +48,6 @@ public class CrankstartSetup {
         return result;
     }
     
-    void setAdminCredentials(DefaultHttpClient c) {
-        c.getCredentialsProvider().setCredentials(
-                AuthScope.ANY, 
-                new UsernamePasswordCredentials("admin", "admin"));
-        c.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0);
-    }
-    
     private static void mergeModelResource(Launcher launcher, String path) 
throws IOException {
         final InputStream is = CrankstartSetup.class.getResourceAsStream(path);
         assertNotNull("Expecting test resource to be found:" + path, is);
@@ -119,12 +108,6 @@ public class CrankstartSetup {
         });
     }
     
-    void closeConnection(HttpResponse r) throws IOException {
-        if(r != null && r.getEntity() != null) {
-            EntityUtils.consume(r.getEntity());
-        }
-    }
-    
     private static String getOsgiStoragePath() {
         final File tmpRoot = new File(System.getProperty("java.io.tmpdir"));
         final Random random = new Random();

Added: 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java?rev=1685057&view=auto
==============================================================================
--- 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java
 (added)
+++ 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java
 Fri Jun 12 10:18:06 2015
@@ -0,0 +1,45 @@
+package org.apache.sling.crankstart.launcher;
+
+import java.io.IOException;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONObject;
+import org.apache.sling.testing.tools.http.RequestBuilder;
+import org.apache.sling.testing.tools.http.RequestExecutor;
+
+/** General testing utilities */ 
+public class U {
+    
+    public static final String ADMIN = "admin";
+    
+    static void setAdminCredentials(DefaultHttpClient c) {
+        c.getCredentialsProvider().setCredentials(
+                AuthScope.ANY, 
+                new UsernamePasswordCredentials(ADMIN, ADMIN));
+        c.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0);
+    }
+    
+    static void closeConnection(HttpResponse r) throws IOException {
+        if(r != null && r.getEntity() != null) {
+            EntityUtils.consume(r.getEntity());
+        }
+    }
+
+    /** Get JSON bundle data from webconsole */ 
+    static JSONObject getBundleData(CrankstartSetup C, DefaultHttpClient 
client, String symbolicName) 
+            throws ClientProtocolException, IOException, JSONException {
+        final RequestBuilder b = new RequestBuilder(C.getBaseUrl());
+        final RequestExecutor e = new RequestExecutor(client);
+        return new JSONObject(e.execute(
+                b.buildGetRequest("/system/console/bundles/" + symbolicName + 
".json")
+                .withCredentials(U.ADMIN, U.ADMIN)
+        ).assertStatus(200)
+        .getContent());
+    }
+}
\ No newline at end of file


Reply via email to