SLING-7060 - Allow running a test only if a certain bundle is present at
a certain version

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1805287 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 1408b82..88bcf90 100644
--- a/pom.xml
+++ b/pom.xml
@@ -141,6 +141,13 @@
                 </exclusion>
             </exclusions>
         </dependency>
+        <dependency>
+          <groupId>org.apache.sling</groupId>
+          <artifactId>org.apache.sling.commons.johnzon</artifactId>
+          <version>1.0.0</version>
+          <scope>copmile</scope>
+        </dependency>
+
         
         <!-- Logging for tests -->
         <dependency>
diff --git a/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java b/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
index 03f75af..9cb772c 100644
--- a/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
+++ b/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
@@ -26,6 +26,9 @@
 import java.util.Map;
 import java.util.regex.Pattern;
 
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
 import javax.servlet.http.HttpServletResponse;
 
 import junit.framework.TestCase;
@@ -42,6 +45,7 @@
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.sling.commons.testing.util.JavascriptEngine;
+import org.osgi.framework.Version;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.MDC;
@@ -573,4 +577,32 @@
         readinessCheckExtension = extension;
         readinessCheckContentTypePrefix = contentTypePrefix;
     }
+    
+    /**
+     * Returns true if the bundle version deployed on the Sling instance is present and at least of the specified version
+     * 
+     * @param bundleSymbolicName the bundle name to check
+     * @param version the minimal version of the bundle that should be deployed
+     * @return true if the bundle is deployed with the specified version, false if it is not deployed or the version it too old
+     * @throws IOException 
+     */
+    public boolean isBundleVersionAtLeast(String bundleSymbolicName, String version) throws IOException {
+
+        GetMethod method = new GetMethod(HTTP_BASE_URL + "/system/console/bundles/" + bundleSymbolicName + ".json");
+        int result = httpClient.executeMethod(method);
+        if ( result != HttpServletResponse.SC_OK) {
+            return false;
+        }
+        
+        try ( JsonReader jsonReader = Json.createReader(method.getResponseBodyAsStream()) ) {
+            JsonObject bundleInfo = jsonReader.readObject();
+            String bundleVersion = bundleInfo.getJsonArray("data").getJsonObject(0).getString("version");
+            Version bundleVer = new Version(bundleVersion);
+            if ( bundleVer.compareTo(new Version(version)) < 0 )  {
+                return false;
+            }
+        }
+        
+        return true;
+    }
 }
\ No newline at end of file