Author: rombert Date: Thu Aug 17 14:07:43 2017 New Revision: 1805287 URL: http://svn.apache.org/viewvc?rev=1805287&view=rev Log: SLING-7060 - Allow running a test only if a certain bundle is present at a certain version
Modified: sling/trunk/bundles/commons/testing/pom.xml sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java Modified: sling/trunk/bundles/commons/testing/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/pom.xml?rev=1805287&r1=1805286&r2=1805287&view=diff ============================================================================== --- sling/trunk/bundles/commons/testing/pom.xml (original) +++ sling/trunk/bundles/commons/testing/pom.xml Thu Aug 17 14:07:43 2017 @@ -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> Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java?rev=1805287&r1=1805286&r2=1805287&view=diff ============================================================================== --- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java (original) +++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java Thu Aug 17 14:07:43 2017 @@ -26,6 +26,9 @@ import java.util.List; 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.aut 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 @@ public class HttpTestBase extends TestCa 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