This is an automated email from the ASF dual-hosted git repository. radu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-bundle-tracker-it.git
commit 84e289c2dde3b7837126066aeadb620984ca09b0 Author: Radu Cotescu <r...@apache.org> AuthorDate: Fri Sep 28 16:29:08 2018 +0200 refactor * extracted IT in separate module (need to re-add jacoco) * made sure BRU dependencies are obtained with the correct bundle context --- README.md | 18 ++ pom.xml | 295 +++++++++++++++++++++ .../junit/teleporter/customizers/ITCustomizer.java | 35 +++ .../resolver/internal/AbstractEndpointIT.java | 148 +++++++++++ .../resolver/internal/BundledScriptTrackerIT.java | 64 +++++ .../scripting/resolver/internal/EndpointIT.java | 90 +++++++ .../scripting/resolver/internal/ScriptMatchIT.java | 153 +++++++++++ src/test/provisioning/it-model.txt | 35 +++ 8 files changed, 838 insertions(+) diff --git a/README.md b/README.md new file mode 100644 index 0000000..91fd661 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +Apache Sling Scripting Resolver (WIP, name not final) +==== + +## Integration Tests + +To run the integration tests do: + +``` +mvn clean verify -Pit +``` + +## Example + +To play around with a sling instance on localhost port 8080 (override with -Dhttp.port=<port>) that has the [examples](../examples) installed run: + +``` +mvn clean verify -Pexample +``` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6e36856 --- /dev/null +++ b/pom.xml @@ -0,0 +1,295 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ 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. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.resolver.reactor</artifactId> + <version>0.0.1-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>org.apache.sling.scripting.resolver.it</artifactId> + <version>0.0.1-SNAPSHOT</version> + <packaging>slingstart</packaging> + + <properties> + <sling.java.version>8</sling.java.version> + <jacoco.maven.plugin.version>0.8.1</jacoco.maven.plugin.version> + </properties> + + <build> + <plugins> + <plugin> + <!-- the Sling instance is provisioned from the model in src/test/provisioning/it-model.txt --> + <groupId>org.apache.sling</groupId> + <artifactId>slingstart-maven-plugin</artifactId> + <version>1.7.16</version> + <extensions>true</extensions> + <configuration> + <usePomDependencies>true</usePomDependencies> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.resolver</artifactId> + <version>0.0.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.sightly</artifactId> + <version>1.0.55-1.4.0-exp-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.examplebundle</artifactId> + <version>0.0.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.examplebundle.precompiled</artifactId> + <version>0.0.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.examplebundle.classic</artifactId> + <version>0.0.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.scripting.examplebundle.hi</artifactId> + <version>0.0.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <!-- TESTING --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.junit.teleporter</artifactId> + <version>1.0.16</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.clients</artifactId> + <version>1.1.12</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.jsoup</groupId> + <artifactId>jsoup</artifactId> + <version>1.7.3</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.rules</artifactId> + <version>1.0.6</version> + <scope>test</scope> + </dependency> + </dependencies> + + <profiles> + <profile> + <id>it</id> + <properties> + <http.host>localhost</http.host> + <sling.vm.options>-Xmx1024m -XX:MaxPermSize=256m -Djava.awt.headless=true</sling.vm.options> + </properties> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <executions> + <execution> + <id>reserve-network-port</id> + <goals> + <!-- pre-integration-test is too late --> + <goal>reserve-network-port</goal> + </goals> + <phase>process-resources</phase> + <configuration> + <portNames> + <portName>http.port.testing</portName> + </portNames> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <argLine>@{argLine}</argLine> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <executions> + <execution> + <id>integration-test</id> + <phase>integration-test</phase> + <goals> + <goal>integration-test</goal> + </goals> + </execution> + <execution> + <id>verify</id> + <phase>integration-test</phase> + <goals> + <goal>verify</goal> + </goals> + </execution> + </executions> + <configuration> + <systemPropertyVariables> + <launchpad.http.server.url>http://${http.host}:${http.port.testing}/</launchpad.http.server.url> + </systemPropertyVariables> + </configuration> + </plugin> + <plugin> + <!-- the Sling instance is provisioned from the model in src/test/provisioning/it-model.txt --> + <groupId>org.apache.sling</groupId> + <artifactId>slingstart-maven-plugin</artifactId> + <extensions>true</extensions> + <executions> + <execution> + <id>prepare-launchpad-package</id> + <phase>pre-integration-test</phase> + <goals> + <goal>prepare-package</goal> + </goals> + </execution> + <execution> + <id>build-launchpad-package</id> + <phase>pre-integration-test</phase> + <goals> + <goal>package</goal> + </goals> + </execution> + <execution> + <id>start-container-before-IT</id> + <goals> + <goal>start</goal> + </goals> + </execution> + <execution> + <id>stop-container-after-IT</id> + <goals> + <goal>stop</goal> + </goals> + </execution> + </executions> + <configuration> + <usePomDependencies>true</usePomDependencies> + <attachArtifact>false</attachArtifact> + <!-- <shouldBlockUntilKeyIsPressed>true</shouldBlockUntilKeyIsPressed> --> + <servers> + <server> + <id>testinstance</id> + <port>${http.port.testing}</port> + <vmOpts>${sling.vm.options}</vmOpts> + <debug>${jacoco.agent}</debug> + </server> + </servers> + </configuration> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>example</id> + <properties> + <http.host>localhost</http.host> + <http.port>8080</http.port> + <sling.vm.options>-Xmx1024m -XX:MaxPermSize=256m -Djava.awt.headless=true -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000</sling.vm.options> + </properties> + <build> + <plugins> + <plugin> + <!-- the Sling instance is provisioned from the model in src/test/provisioning/it-model.txt --> + <groupId>org.apache.sling</groupId> + <artifactId>slingstart-maven-plugin</artifactId> + <version>1.7.16</version> + <extensions>true</extensions> + <executions> + <execution> + <id>prepare-launchpad-package</id> + <phase>pre-integration-test</phase> + <goals> + <goal>prepare-package</goal> + </goals> + </execution> + <execution> + <id>build-launchpad-package</id> + <phase>pre-integration-test</phase> + <goals> + <goal>package</goal> + </goals> + </execution> + <execution> + <id>start-container-before-IT</id> + <goals> + <goal>start</goal> + </goals> + </execution> + <execution> + <goals> + <goal>stop</goal> + </goals> + </execution> + </executions> + <configuration> + <usePomDependencies>true</usePomDependencies> + <attachArtifact>false</attachArtifact> + <shouldBlockUntilKeyIsPressed>true</shouldBlockUntilKeyIsPressed> + <servers> + <server> + <id>exampleinstance</id> + <port>${http.port}</port> + <vmOpts>${sling.vm.options}</vmOpts> + </server> + </servers> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> + +</project> diff --git a/src/test/java/org/apache/sling/junit/teleporter/customizers/ITCustomizer.java b/src/test/java/org/apache/sling/junit/teleporter/customizers/ITCustomizer.java new file mode 100644 index 0000000..e14e36f --- /dev/null +++ b/src/test/java/org/apache/sling/junit/teleporter/customizers/ITCustomizer.java @@ -0,0 +1,35 @@ +/* + * 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.junit.teleporter.customizers; + +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.testing.teleporter.client.ClientSideTeleporter; + +public class ITCustomizer implements TeleporterRule.Customizer { + + public static final String BASE_URL_PROP = "launchpad.http.server.url"; + + @Override + public void customize(TeleporterRule t, String options) { + final ClientSideTeleporter cst = (ClientSideTeleporter)t; + cst.setBaseUrl(System.getProperty(BASE_URL_PROP, BASE_URL_PROP + "_IS_NOT_SET")); + cst.setServerCredentials("admin", "admin"); + cst.includeDependencyPrefix("org.apache.sling.scripting.resolver.internal"); + cst.includeDependencyPrefix("org.apache.sling.testing"); + cst.setTestReadyTimeoutSeconds(20); + } +} diff --git a/src/test/java/org/apache/sling/scripting/resolver/internal/AbstractEndpointIT.java b/src/test/java/org/apache/sling/scripting/resolver/internal/AbstractEndpointIT.java new file mode 100644 index 0000000..aad08f4 --- /dev/null +++ b/src/test/java/org/apache/sling/scripting/resolver/internal/AbstractEndpointIT.java @@ -0,0 +1,148 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ 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.scripting.resolver.internal; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpOptions; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.client.methods.HttpTrace; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.sling.junit.teleporter.customizers.ITCustomizer; +import org.apache.sling.testing.junit.rules.SlingInstanceRule; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.ClassRule; + + +public abstract class AbstractEndpointIT { + + protected int contentFindTimeout = 20000; + protected int contentFindRetryDelay = 1000; + private static CloseableHttpClient httpClient; + + @ClassRule + public static final SlingInstanceRule SLING_INSTANCE_RULE = new SlingInstanceRule(); + + private Map<String, Document> documentMap = new ConcurrentHashMap<>(); + + @BeforeClass + public static void setUp() { + httpClient = HttpClientBuilder.create().build(); + } + + @AfterClass + public static void tearDown() throws IOException { + httpClient.close(); + } + + /** + * Retrieves a jsoup Document from the passed {@code url}. The URL can contain selectors and extensions, but it has to identify a Sling + * content {@link org.apache.sling.api.resource.Resource}. + * + * @param url the URL from which to retrieve the {@link Document} + * @return the Document + * @throws Exception if the resource was not found before the timeout elapsed + */ + protected Document getDocument(String url) throws Exception { + return getDocument(url, HttpGet.METHOD_NAME); + } + + protected Document getDocument(String url, String httpMethod, NameValuePair... parameters) throws Exception { + URIBuilder uriBuilder = new URIBuilder(url); + uriBuilder.setParameters(parameters); + URI uri = uriBuilder.build(); + Document document = documentMap.get(httpMethod + ":" + uri.toString()); + if (document == null) { + HttpResponse response = getResponse(httpMethod, url, 200); + document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), + System.getProperty(ITCustomizer.BASE_URL_PROP, ITCustomizer.BASE_URL_PROP + + "_IS_NOT_SET")); + documentMap.put(httpMethod + ":" + uri, document); + } + return document; + } + + protected HttpResponse getResponse(String method, String url, int statusCode, NameValuePair... parameters) throws Exception { + String resourcePath = url.substring(0, url.indexOf('.')); + SLING_INSTANCE_RULE.getAdminClient().waitExists(resourcePath, contentFindTimeout, contentFindRetryDelay); + HttpUriRequest request = prepareRequest(method, url, parameters); + HttpResponse response = httpClient.execute(request); + Assert.assertNotNull(response); + Assert.assertEquals("URL " + url + " did not return a " + statusCode + " status code.", statusCode, + response.getStatusLine().getStatusCode + ()); + return response; + } + + protected HttpUriRequest prepareRequest(String method, String url, NameValuePair... parameters) throws URISyntaxException { + HttpRequestBase request = null; + URIBuilder uriBuilder = + new URIBuilder(System.getProperty(ITCustomizer.BASE_URL_PROP, ITCustomizer.BASE_URL_PROP + "_IS_NOT_SET") + url); + uriBuilder.setParameters(parameters); + switch (method) { + case HttpGet.METHOD_NAME: + request = new HttpGet(uriBuilder.build()); + break; + case HttpHead.METHOD_NAME: + request = new HttpHead(uriBuilder.build()); + break; + case HttpOptions.METHOD_NAME: + request = new HttpOptions(uriBuilder.build()); + break; + case HttpPost.METHOD_NAME: + request = new HttpPost(uriBuilder.build()); + break; + case HttpPut.METHOD_NAME: + request = new HttpPut(uriBuilder.build()); + break; + case HttpPatch.METHOD_NAME: + request = new HttpPatch(uriBuilder.build()); + break; + case HttpTrace.METHOD_NAME: + request = new HttpTrace(uriBuilder.build()); + break; + case HttpDelete.METHOD_NAME: + request = new HttpDelete(uriBuilder.build()); + break; + } + return request; + } + +} diff --git a/src/test/java/org/apache/sling/scripting/resolver/internal/BundledScriptTrackerIT.java b/src/test/java/org/apache/sling/scripting/resolver/internal/BundledScriptTrackerIT.java new file mode 100644 index 0000000..0ca3578 --- /dev/null +++ b/src/test/java/org/apache/sling/scripting/resolver/internal/BundledScriptTrackerIT.java @@ -0,0 +1,64 @@ +/* + * 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.scripting.resolver.internal; + +import javax.servlet.Servlet; + +import org.apache.sling.junit.rules.TeleporterRule; +import org.apache.sling.testing.clients.util.poller.Polling; +import org.junit.Rule; +import org.junit.Test; + +public class BundledScriptTrackerIT { + + @Rule + public TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "IT"); + + @Test + public void testSlingServletForResourceTypeProvided() throws Exception { + waitForService(Servlet.class, String.format("(%s=%s)", "sling.servlet.resourceTypes", "org.apache.sling.scripting" + + ".examplebundle.hello/1.0.0"), 20000, 1000); + waitForService(Servlet.class, String.format("(%s=%s)", "sling.servlet.resourceTypes", "org.apache.sling.scripting" + + ".examplebundle.hello/2.0.0"), 20000, 1000); + waitForService(Servlet.class, String.format("(%s=%s)", "sling.servlet.resourceTypes", "org.apache.sling.scripting" + + ".examplebundle.hello"), 20000, 1000); + waitForService(Servlet.class, String.format("(%s=%s)", "sling.servlet.resourceTypes", "org.apache.sling.scripting" + + ".examplebundle.hi/1.0.0"), 20000, 1000); + waitForService(Servlet.class, String.format("(%s=%s)", "sling.servlet.resourceTypes", "org.apache.sling.scripting" + + ".examplebundle.hi"), 20000, 1000); + waitForService(Servlet.class, String.format("(%s=%s)", "sling.servlet.resourceTypes", "org.apache.sling.scripting" + + ".examplebundle.scriptmatching/1.0.0"), 20000, 1000); + waitForService(Servlet.class, String.format("(%s=%s)", "sling.servlet.resourceTypes", "org.apache.sling.scripting" + + ".examplebundle.scriptmatching"), 20000, 1000); + } + + private void waitForService(Class serviceClass, String filter, long waitTime, long retryAfter) + throws Exception { + Polling p = new Polling() { + @Override + public Boolean call() { + return teleporter.getService(serviceClass, filter) != null; + } + + @Override + protected String message() { + return "Cannot obtain a reference to service " + serviceClass.getName() + " with filter " + filter + " after %1$d ms"; + } + }; + p.poll(waitTime, retryAfter); + } +} diff --git a/src/test/java/org/apache/sling/scripting/resolver/internal/EndpointIT.java b/src/test/java/org/apache/sling/scripting/resolver/internal/EndpointIT.java new file mode 100644 index 0000000..f4f93d0 --- /dev/null +++ b/src/test/java/org/apache/sling/scripting/resolver/internal/EndpointIT.java @@ -0,0 +1,90 @@ +/* + * 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.scripting.resolver.internal; + +import org.jsoup.nodes.Document; +import org.junit.Assert; +import org.junit.Test; + +public class EndpointIT extends AbstractEndpointIT { + + @Test + public void testHelloEndpoint() throws Exception { + Document document = getDocument("/content/srr/examples/hello.html"); + Assert.assertEquals("We're testing some serious scripting here in Version 2", document.select("h2").html()); + Assert.assertTrue(document.body().html().contains("World2")); + Assert.assertTrue(document.body().html().contains("Hello2")); + } + + @Test + public void testPrecompiledHelloEndpoint() throws Exception { + Document document = getDocument("/content/srr/examples/precompiled-hello.html"); + Assert.assertEquals("We're testing some serious scripting here in Version 2", document.select("h2").html()); + Assert.assertTrue(document.body().html().contains("World2")); + Assert.assertTrue(document.body().html().contains("Hello2")); + } + + @Test + public void testHelloEndpointV1() throws Exception { + Document document = getDocument("content/srr/examples/hello-v1.html"); + Assert.assertEquals("We're testing some serious scripting here", document.select("h2").html()); + Assert.assertTrue(document.body().html().contains("World")); + Assert.assertTrue(document.body().html().contains("Hello")); + } + + @Test + public void testPrecompiledHelloEndpointV1() throws Exception { + Document document = getDocument("content/srr/examples/precompiled-hello-v1.html"); + Assert.assertEquals("We're testing some serious scripting here", document.select("h2").html()); + Assert.assertTrue(document.body().html().contains("World")); + Assert.assertTrue(document.body().html().contains("Hello")); + } + + @Test + public void testHelloEndpointV2() throws Exception { + Document document = getDocument("content/srr/examples/hello-v2.html"); + Assert.assertEquals("We're testing some serious scripting here in Version 2", document.select("h2").html()); + Assert.assertTrue(document.body().html().contains("World2")); + Assert.assertTrue(document.body().html().contains("Hello2")); + } + + @Test + public void testPrecompiledHelloEndpointV2() throws Exception { + Document document = getDocument("content/srr/examples/precompiled-hello-v2.html"); + Assert.assertEquals("We're testing some serious scripting here in Version 2", document.select("h2").html()); + Assert.assertTrue(document.body().html().contains("World2")); + Assert.assertTrue(document.body().html().contains("Hello2")); + } + + @Test + public void testHiEndpoint() throws Exception { + Document document = getDocument("content/srr/examples/hi.html"); + Assert.assertEquals("We're testing some serious scripting here", document.select("h2").html()); + Assert.assertTrue(document.body().html().contains("World")); + Assert.assertTrue(document.body().html().contains("Hallo")); + Assert.assertFalse(document.body().html().contains("Hello")); + } + + @Test + public void testHiEndpointV1() throws Exception { + Document document = getDocument("content/srr/examples/hi-v1.html"); + Assert.assertEquals("We're testing some serious scripting here", document.select("h2").html()); + Assert.assertTrue(document.body().html().contains("World")); + Assert.assertTrue(document.body().html().contains("Hallo")); + Assert.assertFalse(document.body().html().contains("Hello")); + } +} diff --git a/src/test/java/org/apache/sling/scripting/resolver/internal/ScriptMatchIT.java b/src/test/java/org/apache/sling/scripting/resolver/internal/ScriptMatchIT.java new file mode 100644 index 0000000..4dfc6d5 --- /dev/null +++ b/src/test/java/org/apache/sling/scripting/resolver/internal/ScriptMatchIT.java @@ -0,0 +1,153 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ 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.scripting.resolver.internal; + +import org.apache.commons.lang3.StringUtils; +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpOptions; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpTrace; +import org.jsoup.nodes.Document; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class ScriptMatchIT extends AbstractEndpointIT { + + @Test + public void testGETMethodMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.html", HttpGet.METHOD_NAME); + } + + @Test + public void testGETMethodSelectorMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.selector-1.html", HttpGet.METHOD_NAME); + } + + @Test + public void testHEADMethodMatching() throws Exception { + HttpResponse response = getResponse(HttpHead.METHOD_NAME, "/content/srr/examples/script-matching.html", 200); + Header[] header = response.getHeaders("X-Script-Name"); + assertEquals("Expected to find one X-Script-Name header.", 1, header.length); + assertEquals("/javax.script/org.apache.sling.scripting.examplebundle.scriptmatching/1.0.0/HEAD.html", header[0].getValue()); + } + + @Test + public void testHEADMethodSelectorMatching() throws Exception { + HttpResponse response = getResponse(HttpHead.METHOD_NAME, "/content/srr/examples/script-matching.selector-1.html", 200); + Header[] header = response.getHeaders("X-Script-Name"); + assertEquals("Expected to find one X-Script-Name header.", 1, header.length); + assertEquals("/javax.script/org.apache.sling.scripting.examplebundle.scriptmatching/1.0.0/HEAD.selector-1.html", + header[0].getValue()); + } + + @Test + public void testOPTIONSMethodMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.html", HttpOptions.METHOD_NAME); + } + + @Test + public void testOPTIONSMethodSelectorMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.selector-1.html", HttpOptions.METHOD_NAME); + } + + @Test + public void testPOSTMethodMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.html", HttpPost.METHOD_NAME); + } + + @Test + public void testPOSTMethodSelectorMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.selector-1.html", HttpPost.METHOD_NAME); + } + + @Test + public void testPATCHMethodMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.html", HttpPatch.METHOD_NAME); + } + + @Test + public void testPATCHMethodSelectorMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.selector-1.html", HttpPatch.METHOD_NAME); + } + + @Test + public void testPUTMethodMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.html", HttpPut.METHOD_NAME); + } + + @Test + public void testPUTMethodSelectorMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.selector-1.html", HttpPut.METHOD_NAME); + } + + @Test + public void testDELETEMethodMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.html", HttpDelete.METHOD_NAME); + } + + @Test + public void testDELETEMethodSelectorMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.selector-1.html", HttpDelete.METHOD_NAME); + } + + @Test + public void testTRACEMethodMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.html", HttpTrace.METHOD_NAME); + } + + @Test + public void testTRACEMethodSelectorMatching() throws Exception { + testHttpMethodScriptMatching("/content/srr/examples/script-matching.selector-1.html", HttpTrace.METHOD_NAME); + } + + @Test + public void testSelectorMatching() throws Exception { + Document document = getDocument("/content/srr/examples/script-matching.selector-2.html"); + assertTrue(document.select("div").html().contains("/javax.script/org.apache.sling.scripting.examplebundle" + + ".scriptmatching/1.0.0/selector-2.html")); + } + + private void testHttpMethodScriptMatching(String url, String httpMethod) throws Exception { + Document document = getDocument(url, httpMethod); + String path = url.substring(url.lastIndexOf('/')); + String[] parts = path.split("\\."); + String selectorString = null; + String extension = null; + if (parts.length == 3) { + selectorString = parts[1]; + extension = parts[2]; + } else if (parts.length == 2) { + extension = parts[1]; + } else { + throw new IllegalArgumentException("The following URL doesn't seem to be correctly handled: " + url); + } + String expectedScriptName = "/javax.script/org.apache.sling.scripting.examplebundle.scriptmatching/1.0.0/" + httpMethod + + (StringUtils.isNotEmpty(selectorString) ? "." + selectorString : "") + "." + extension; + assertTrue(document.select("div").html().contains(expectedScriptName)); + } + +} diff --git a/src/test/provisioning/it-model.txt b/src/test/provisioning/it-model.txt new file mode 100644 index 0000000..9cef862 --- /dev/null +++ b/src/test/provisioning/it-model.txt @@ -0,0 +1,35 @@ +# +# 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. +# + +[feature name=scripting-resolver-it] + +# Dependencies +[artifacts] + org.apache.sling/org.apache.sling.starter/11-SNAPSHOT/slingstart + org.apache.sling/org.apache.sling.scripting.sightly + org.apache.sling/org.apache.sling.scripting.resolver + org.apache.sling/org.apache.sling.scripting.examplebundle + org.apache.sling/org.apache.sling.scripting.examplebundle.precompiled + org.apache.sling/org.apache.sling.scripting.examplebundle.classic + org.apache.sling/org.apache.sling.scripting.examplebundle.hi + org.apache.sling/org.apache.sling.junit.core/1.0.26 + +[configurations] + org.apache.sling.engine.impl.SlingMainServlet + sling.trace.allow=B"true"