SLING-4728 - refactor/cleanup tests

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1685057 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 2b11d6f..2907179 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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>
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java b/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
index e6bfc1a..80e2b16 100644
--- a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
+++ b/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
@@ -3,6 +3,7 @@
 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.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 @@
 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 @@
     @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 @@
             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 @@
             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 @@
                 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 @@
             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();
+        // 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"));
             }
-            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"));
-                }
-            }
-            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 @@
             final String content = IOUtils.toString(response.getEntity().getContent(), encoding);
             assertEquals(expectedContent, content);
         } finally {
-            C.closeConnection(response);
+            U.closeConnection(response);
         }
     }
     
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java b/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
index 814ef16..f687e4c 100644
--- a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
+++ b/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
@@ -11,13 +11,9 @@
 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 @@
         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 @@
         });
     }
     
-    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();
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/U.java b/src/test/java/org/apache/sling/crankstart/launcher/U.java
new file mode 100644
index 0000000..d57fde8
--- /dev/null
+++ b/src/test/java/org/apache/sling/crankstart/launcher/U.java
@@ -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