From 3081c95b0850da9e7458b2d1e6193f6c0b13ec85 Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Mon, 18 Jun 2018 13:23:26 -0500 Subject: [PATCH 01/10] version. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 2f903bd..1243d6d 100755 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.icfolson.aem.prosper prosper jar - 12.1.0 + 12.1.1-SNAPSHOT Prosper A Spock-based integration testing library for prosperous AEM (Adobe CQ) development. From f3ace9f416385863c2a636b24fc59a8a729de727 Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Wed, 25 Jul 2018 09:56:43 -0500 Subject: [PATCH 02/10] documentation update. --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1844afa..1080568 100755 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Overview -Prosper is an integration testing library for AEM (Adobe Experience Manager, formerly CQ5) projects using [Spock](http://docs.spockframework.org/), a [Groovy](http://www.groovy-lang.org/)-based testing framework notable for it's expressive specification language. The library contains a base Spock specification that provides an in-memory JCR, Sling framework support, and a mock OSGi context for registering and testing services. +Prosper is an integration testing library for Adobe Experience Manager projects using [Spock](http://docs.spockframework.org/), a [Groovy](http://www.groovy-lang.org/)-based testing framework notable for it's expressive specification language. The library contains a base Spock specification that provides an in-memory JCR, Sling framework support, and a mock OSGi context for registering and testing services. ## Features diff --git a/pom.xml b/pom.xml index 1243d6d..77acbb9 100755 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ jar 12.1.1-SNAPSHOT Prosper - A Spock-based integration testing library for prosperous AEM (Adobe CQ) development. + A Spock-based integration testing library for Adobe Experience Manager. https://github.com/OlsonDigital/prosper From 926e09c410f7099a9bb027239d05b2efb8fd69ba Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Mon, 20 Aug 2018 10:43:27 -0500 Subject: [PATCH 03/10] groovy 2.5 updates. --- pom.xml | 81 +++++++++++++++---- .../request/MockRequestParameterMap.groovy | 2 +- .../mocks/request/MockRequestPathInfo.groovy | 2 +- .../aem/prosper/specs/ProsperSpec.groovy | 2 +- 4 files changed, 67 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 77acbb9..ef88fc4 100755 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,25 @@ https://github.com/OlsonDigital/prosper + + + groovy + https://dl.bintray.com/groovy/maven + + true + + + + + + groovy + https://dl.bintray.com/groovy/maven + + true + + + + src/main/groovy src/test/groovy @@ -54,18 +73,18 @@ org.codehaus.groovy groovy-eclipse-compiler - 2.9.2-01 + 3.0.0-01 org.codehaus.groovy groovy-eclipse-batch - 2.4.3-01 + 2.5.2-01 maven-surefire-plugin - 2.19.1 + 2.22.0 **/*Spec* @@ -233,7 +252,7 @@ maven-surefire-report-plugin - 2.19.1 + 2.22.0 @@ -344,30 +363,58 @@ javax.el-api 3.0.0 + + org.springframework + spring-test + 4.1.7.RELEASE + + + org.springframework + spring-core + 4.1.7.RELEASE + + + org.codehaus.groovy groovy-all - 2.4.13 + 2.5.0 + pom + + + org.codehaus.groovy + groovy-testng + + + + + - org.springframework - spring-core - 4.1.7.RELEASE + org.spockframework + spock-core + 1.2-RC1-groovy-2.5 + + + com.icfolson.aem.groovy.extension + aem-groovy-extension-bundle + 5.0.0 diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMap.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMap.groovy index 301408a..d3b24f5 100644 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMap.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMap.groovy @@ -6,7 +6,7 @@ import org.apache.sling.api.request.RequestParameterMap import javax.servlet.http.HttpServletRequest -@ToString(includes = "map") +@ToString(includeFields = true, includes = "map") class MockRequestParameterMap implements RequestParameterMap { static RequestParameterMap create(HttpServletRequest request) { diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestPathInfo.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestPathInfo.groovy index e22641b..fd222fc 100644 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestPathInfo.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestPathInfo.groovy @@ -5,7 +5,7 @@ import org.apache.sling.api.request.RequestPathInfo import org.apache.sling.api.resource.Resource import org.apache.sling.api.resource.ResourceResolver -@ToString(excludes = "resourceResolver") +@ToString(includeFields = true, excludes = "resourceResolver") class MockRequestPathInfo implements RequestPathInfo { private final ResourceResolver resourceResolver diff --git a/src/main/groovy/com/icfolson/aem/prosper/specs/ProsperSpec.groovy b/src/main/groovy/com/icfolson/aem/prosper/specs/ProsperSpec.groovy index d8d8883..5ed010a 100755 --- a/src/main/groovy/com/icfolson/aem/prosper/specs/ProsperSpec.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/specs/ProsperSpec.groovy @@ -310,7 +310,7 @@ abstract class ProsperSpec extends Specification { private void registerSlingModels() { if (this.class.isAnnotationPresent(ModelSpec)) { - slingContextProvider.addModelsForPackage(this.class.package.name) + slingContextProvider.addModelsForPackage(this.class.getPackage().name) def additionalPackages = this.class.getAnnotation(ModelSpec).additionalPackages() From 90d70ef29cafd3e12bf09a268d9ebd057b4ed30e Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Wed, 22 Aug 2018 14:52:09 -0500 Subject: [PATCH 04/10] reverted groovy 2.5 changes. --- pom.xml | 79 ++++++++++++--------------------------------------------- 1 file changed, 16 insertions(+), 63 deletions(-) diff --git a/pom.xml b/pom.xml index ef88fc4..1fea525 100755 --- a/pom.xml +++ b/pom.xml @@ -37,32 +37,13 @@ https://github.com/OlsonDigital/prosper - - - groovy - https://dl.bintray.com/groovy/maven - - true - - - - - - groovy - https://dl.bintray.com/groovy/maven - - true - - - - src/main/groovy src/test/groovy maven-compiler-plugin - 3.5 + 3.8.0 groovy-eclipse-compiler 1.8 @@ -73,12 +54,12 @@ org.codehaus.groovy groovy-eclipse-compiler - 3.0.0-01 + 2.9.2-01 org.codehaus.groovy groovy-eclipse-batch - 2.5.2-01 + 2.4.3-01 @@ -363,58 +344,30 @@ javax.el-api 3.0.0 - - org.springframework - spring-test - 4.1.7.RELEASE - - - org.springframework - spring-core - 4.1.7.RELEASE - - - org.codehaus.groovy groovy-all - 2.5.0 - pom - - - org.codehaus.groovy - groovy-testng - - - - - - org.spockframework spock-core - 1.2-RC1-groovy-2.5 + 1.1-groovy-2.4 - com.icfolson.aem.groovy.extension - aem-groovy-extension-bundle - 5.0.0 + org.springframework + spring-test + 4.1.7.RELEASE + + + org.springframework + spring-core + 4.1.7.RELEASE From 2a4a23ae7436f69f645bcf009bad61000a393b44 Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Mon, 24 Sep 2018 10:59:48 -0500 Subject: [PATCH 05/10] updated parent version. --- pom.xml | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 1fea525..d8c2c01 100755 --- a/pom.xml +++ b/pom.xml @@ -5,14 +5,14 @@ com.icfolson.aem aem-parent-uber-jar - 6.4.1 + 6.4.1.1 4.0.0 com.icfolson.aem.prosper prosper jar - 12.1.1-SNAPSHOT + 12.2.0-SNAPSHOT Prosper A Spock-based integration testing library for Adobe Experience Manager. @@ -191,27 +191,6 @@ - - external.atlassian.jgitflow - jgitflow-maven-plugin - 1.0-m5.1 - - - com.jcraft - jsch - 0.1.54 - - - - - feature- - release- - hotfix- - - true - true - - @@ -329,10 +308,12 @@ javax.inject javax.inject + org.apache.jackrabbit oak-core-spi @@ -347,7 +328,7 @@ org.codehaus.groovy groovy-all - 2.4.13 + 2.4.15 com.icfolson.aem.groovy.extension From 000a11e41a2333463285c717a90a256bfaeacb7e Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Mon, 24 Sep 2018 12:51:48 -0500 Subject: [PATCH 06/10] dependency updates. --- pom.xml | 43 +++++++------------ .../context/ProsperSlingContext.groovy | 18 -------- .../AdapterFactoryRegistrationCallback.java | 30 +++++++++++++ 3 files changed, 45 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/icfolson/aem/prosper/context/AdapterFactoryRegistrationCallback.java diff --git a/pom.xml b/pom.xml index d8c2c01..0411bd8 100755 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.icfolson.aem.prosper prosper jar - 12.2.0-SNAPSHOT + 12.2.0 Prosper A Spock-based integration testing library for Adobe Experience Manager. @@ -38,7 +38,6 @@ - src/main/groovy src/test/groovy @@ -308,16 +307,18 @@ javax.inject javax.inject - org.apache.jackrabbit oak-core-spi + + org.osgi + osgi.core + + + org.osgi + osgi.cmpn + @@ -333,12 +334,12 @@ com.icfolson.aem.groovy.extension aem-groovy-extension-bundle - 5.0.0 + 5.1.0 org.spockframework spock-core - 1.1-groovy-2.4 + 1.2-groovy-2.4 org.springframework @@ -354,21 +355,13 @@ io.wcm - io.wcm.testing.aem-mock - 2.2.2 + io.wcm.testing.aem-mock.junit4 + 2.3.2 org.slf4j slf4j-simple - - org.apache.sling - org.apache.sling.testing.sling-mock - - - org.apache.sling - org.apache.sling.testing.osgi-mock - org.apache.commons commons-imaging @@ -379,19 +372,13 @@ org.apache.sling - org.apache.sling.testing.sling-mock - 2.2.18 + org.apache.sling.testing.sling-mock.junit4 + 2.3.4 com.icfolson.sling org.apache.sling.testing.sling-mock-oak 2.1.0 - - - org.apache.sling - org.apache.sling.testing.sling-mock - - diff --git a/src/main/groovy/com/icfolson/aem/prosper/context/ProsperSlingContext.groovy b/src/main/groovy/com/icfolson/aem/prosper/context/ProsperSlingContext.groovy index 47188a0..72a3990 100644 --- a/src/main/groovy/com/icfolson/aem/prosper/context/ProsperSlingContext.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/context/ProsperSlingContext.groovy @@ -1,8 +1,6 @@ package com.icfolson.aem.prosper.context import com.icfolson.aem.prosper.adapter.ClosureAdapterFactory -import com.icfolson.aem.prosper.adapter.ProsperAdapterFactory -import io.wcm.testing.mock.aem.MockAemAdapterFactory import org.apache.sling.api.SlingHttpServletRequest import org.apache.sling.api.adapter.AdapterFactory import org.apache.sling.api.resource.Resource @@ -10,7 +8,6 @@ import org.apache.sling.api.resource.ResourceResolver import org.apache.sling.models.spi.Injector import org.apache.sling.testing.mock.sling.junit.SlingContext import org.apache.sling.testing.mock.sling.junit.SlingContextBuilder -import org.apache.sling.testing.mock.sling.junit.SlingContextCallback import org.junit.rules.TestRule import org.osgi.framework.BundleContext @@ -24,21 +21,6 @@ import static org.osgi.framework.Constants.SERVICE_RANKING */ class ProsperSlingContext implements SlingContextProvider, TestRule { - static class AdapterFactoryRegistrationCallback implements SlingContextCallback { - - @Override - void execute(SlingContext slingContext) throws Exception { - // register prosper adapter factory - slingContext.registerService(AdapterFactory, new ProsperAdapterFactory(), [ - (ADAPTABLE_CLASSES): ProsperAdapterFactory.ADAPTABLE_CLASSES, - (ADAPTER_CLASSES): ProsperAdapterFactory.ADAPTER_CLASSES - ]) - - // register mock adapter factory - slingContext.registerService(AdapterFactory, new MockAemAdapterFactory()) - } - } - @Delegate private SlingContext slingContext = new SlingContextBuilder(JCR_OAK) .beforeSetUp(new AdapterFactoryRegistrationCallback()) diff --git a/src/main/java/com/icfolson/aem/prosper/context/AdapterFactoryRegistrationCallback.java b/src/main/java/com/icfolson/aem/prosper/context/AdapterFactoryRegistrationCallback.java new file mode 100644 index 0000000..7c0f313 --- /dev/null +++ b/src/main/java/com/icfolson/aem/prosper/context/AdapterFactoryRegistrationCallback.java @@ -0,0 +1,30 @@ +package com.icfolson.aem.prosper.context; + +import com.google.common.collect.ImmutableMap; +import com.icfolson.aem.prosper.adapter.ProsperAdapterFactory; +import io.wcm.testing.mock.aem.MockAemAdapterFactory; +import org.apache.sling.api.adapter.AdapterFactory; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.apache.sling.testing.mock.sling.junit.SlingContextCallback; + +import java.util.Map; + +import static org.apache.sling.api.adapter.AdapterFactory.ADAPTABLE_CLASSES; +import static org.apache.sling.api.adapter.AdapterFactory.ADAPTER_CLASSES; + +public final class AdapterFactoryRegistrationCallback implements SlingContextCallback { + + private static final Map PROPERTIES = new ImmutableMap.Builder() + .put(ADAPTABLE_CLASSES, ProsperAdapterFactory.ADAPTABLE_CLASSES) + .put(ADAPTER_CLASSES, ProsperAdapterFactory.ADAPTER_CLASSES) + .build(); + + @Override + public void execute(SlingContext context) { + // register prosper adapter factory + context.registerService(AdapterFactory.class, new ProsperAdapterFactory(), PROPERTIES); + + // register mock adapter factory + context.registerService(AdapterFactory.class, new MockAemAdapterFactory()); + } +} From 17bff41642de89a259775e074eca45676a7a951f Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Mon, 24 Sep 2018 14:58:00 -0500 Subject: [PATCH 07/10] removed JSP tag support and removed custom mocks. --- README.md | 4 +- pom.xml | 27 +- .../prosper/builders/RequestBuilder.groovy | 308 +----------------- .../prosper/builders/ResponseBuilder.groovy | 127 +------- .../prosper/context/ProsperPageContext.groovy | 30 -- .../mocks/MockSlingHttpServletRequest.groovy | 161 --------- .../mocks/MockSlingHttpServletResponse.groovy | 26 -- .../ProsperMockSlingHttpServletRequest.groovy | 57 ++++ .../mocks/request/MockRequestParameter.groovy | 54 --- .../request/MockRequestParameterMap.groovy | 38 --- .../mocks/request/MockRequestPathInfo.groovy | 50 --- .../aem/prosper/specs/ProsperSpec.groovy | 2 +- .../aem/prosper/tag/JspTagProxy.groovy | 37 --- .../aem/prosper/traits/JspTagTrait.groovy | 102 ------ .../builders/RequestBuilderSpec.groovy | 4 +- .../builders/ResponseBuilderSpec.groovy | 16 +- .../MockSlingHttpServletRequestSpec.groovy | 87 ----- .../MockRequestParameterMapSpec.groovy | 48 --- .../aem/prosper/traits/JspTagTraitSpec.groovy | 100 ------ .../aem/prosper/traits/TestTag.groovy | 21 -- 20 files changed, 98 insertions(+), 1201 deletions(-) delete mode 100644 src/main/groovy/com/icfolson/aem/prosper/context/ProsperPageContext.groovy delete mode 100755 src/main/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletRequest.groovy delete mode 100644 src/main/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletResponse.groovy create mode 100644 src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy delete mode 100644 src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameter.groovy delete mode 100644 src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMap.groovy delete mode 100644 src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestPathInfo.groovy delete mode 100644 src/main/groovy/com/icfolson/aem/prosper/tag/JspTagProxy.groovy delete mode 100644 src/main/groovy/com/icfolson/aem/prosper/traits/JspTagTrait.groovy delete mode 100755 src/test/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletRequestSpec.groovy delete mode 100644 src/test/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMapSpec.groovy delete mode 100644 src/test/groovy/com/icfolson/aem/prosper/traits/JspTagTraitSpec.groovy delete mode 100644 src/test/groovy/com/icfolson/aem/prosper/traits/TestTag.groovy diff --git a/README.md b/README.md index 1080568..7e3656b 100755 --- a/README.md +++ b/README.md @@ -445,7 +445,7 @@ class ServletSpec extends ProsperSpec { assertNodeExists("/content/prosper", [testProperty: "two"]) and: - response.contentAsString == '{"path":"/content/prosper","value":"two"}' + response.outputAsString == '{"path":"/content/prosper","value":"two"}' } } ``` @@ -672,7 +672,7 @@ trait MobileRequestTrait { abstract RequestBuilder getRequestBuilder() SlingHttpServletRequest buildMobileRequest(Map parameters) { - requestBuilder.setSelectors(["mobile"]).setParameters(parameters).build() + requestBuilder.setSelectors(["mobile"]).setParameterMap(parameters).build() } } ``` diff --git a/pom.xml b/pom.xml index 0411bd8..5be37dd 100755 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.icfolson.aem.prosper prosper jar - 12.2.0 + 13.0.0-SNAPSHOT Prosper A Spock-based integration testing library for Adobe Experience Manager. @@ -87,7 +87,7 @@ classpathref="maven.compile.classpath"/> @@ -104,8 +104,6 @@ href="https://docs.adobe.com/docs/en/aem/6-2/develop/ref/javadoc"/> - @@ -299,10 +297,6 @@ javax.servlet javax.servlet-api - - javax.servlet - jsp-api - javax.inject javax.inject @@ -320,12 +314,7 @@ osgi.cmpn - - - javax.el - javax.el-api - 3.0.0 - + org.codehaus.groovy groovy-all @@ -341,16 +330,6 @@ spock-core 1.2-groovy-2.4 - - org.springframework - spring-test - 4.1.7.RELEASE - - - org.springframework - spring-core - 4.1.7.RELEASE - diff --git a/src/main/groovy/com/icfolson/aem/prosper/builders/RequestBuilder.groovy b/src/main/groovy/com/icfolson/aem/prosper/builders/RequestBuilder.groovy index 397bdd3..a2aee5e 100755 --- a/src/main/groovy/com/icfolson/aem/prosper/builders/RequestBuilder.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/builders/RequestBuilder.groovy @@ -1,37 +1,27 @@ package com.icfolson.aem.prosper.builders -import com.icfolson.aem.prosper.mocks.MockSlingHttpServletRequest +import com.icfolson.aem.prosper.mocks.ProsperMockSlingHttpServletRequest import org.apache.sling.api.resource.ResourceResolver -import org.springframework.mock.web.MockHttpServletRequest - -import javax.servlet.http.Cookie -import javax.servlet.http.HttpSession -import java.security.Principal +import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest +import org.osgi.framework.BundleContext /** * Builder to assist in creating SlingHttpServletRequest objects. */ class RequestBuilder { - private final MockHttpServletRequest mockRequest = new MockHttpServletRequest() - private final ResourceResolver resourceResolver - private final List selectors = [] - - private String path = "/" - - private String suffix = "" - - private String extension = "" + private final BundleContext bundleContext /** * Create a request builder for a test spec. * * @param resourceResolver Sling resource resolver */ - RequestBuilder(ResourceResolver resourceResolver) { + RequestBuilder(ResourceResolver resourceResolver, BundleContext bundleContext) { this.resourceResolver = resourceResolver + this.bundleContext = bundleContext } /** @@ -52,8 +42,7 @@ class RequestBuilder { * .Closure)">with method. * *
-     *  new RequestBuilder(resourceResolver).build {
-     *      serverName = "localhost"
+     *  new RequestBuilder(resourceResolver).build {*      serverName = "localhost"
      *      path = "/content"
      *      method = "GET"
      *      parameters = ["a": ["1", "2"], "b": ["1"]]
@@ -64,288 +53,15 @@ class RequestBuilder {
      * .RELEASE/javadoc-api/org/springframework/mock/web/MockHttpServletRequest.html">MockHttpServletRequest
      * @return request
      */
-    MockSlingHttpServletRequest build(@DelegatesTo(RequestBuilder) Closure closure) {
+    MockSlingHttpServletRequest build(@DelegatesTo(ProsperMockSlingHttpServletRequest) Closure closure) {
+        def request = new ProsperMockSlingHttpServletRequest(resourceResolver, bundleContext)
+
         if (closure) {
-            closure.delegate = this
+            closure.delegate = request
             closure.resolveStrategy = Closure.DELEGATE_ONLY
             closure()
         }
 
-        new MockSlingHttpServletRequest(mockRequest, resourceResolver, path, selectors, extension, suffix)
-    }
-
-    /**
-     * Set the request path.
-     *
-     * @param path JCR path
-     */
-    RequestBuilder setPath(String path) {
-        this.path = path
-        this
-    }
-
-    /**
-     * Set the request suffix.
-     *
-     * @param suffix suffix
-     */
-    RequestBuilder setSuffix(String suffix) {
-        this.suffix = suffix
-        this
-    }
-
-    /**
-     * Set the request extension
-     *
-     * @param extension extension
-     */
-    RequestBuilder setExtension(String extension) {
-        this.extension = extension
-        this
-    }
-
-    /**
-     * Add selectors to the request.
-     *
-     * @param selectors list of selectors
-     */
-    RequestBuilder setSelectors(List selectors) {
-        this.selectors.addAll(selectors)
-        this
-    }
-
-    /**
-     * Add request parameters from a map.  Map values should either be strings or lists of strings (for multivalued
-     * parameters).
-     *
-     * @param parameters map of parameter names and values
-     */
-    RequestBuilder setParameters(Map parameters) {
-        parameters.each { name, value ->
-            if (value instanceof Collection) {
-                mockRequest.setParameter(name, value as String[])
-            } else {
-                mockRequest.setParameter(name, value as String)
-            }
-        }
-
-        this
-    }
-
-    // delegate methods
-
-    RequestBuilder setCharacterEncoding(String characterEncoding) {
-        mockRequest.setCharacterEncoding(characterEncoding)
-        this
-    }
-
-    RequestBuilder setContent(byte[] content) {
-        mockRequest.setContent(content)
-        this
-    }
-
-    RequestBuilder setContentType(String contentType) {
-        mockRequest.setContentType(contentType)
-        this
-    }
-
-    RequestBuilder setParameter(String name, String value) {
-        mockRequest.setParameter(name, value)
-        this
-    }
-
-    RequestBuilder setParameter(String name, String[] values) {
-        mockRequest.setParameter(name, values)
-        this
-    }
-
-    RequestBuilder addParameter(String name, String value) {
-        mockRequest.addParameter(name, value)
-        this
-    }
-
-    RequestBuilder addParameter(String name, String[] values) {
-        mockRequest.addParameter(name, values)
-        this
-    }
-
-    RequestBuilder addParameters(Map params) {
-        mockRequest.addParameters(params)
-        this
-    }
-
-    RequestBuilder removeParameter(String name) {
-        mockRequest.removeParameter(name)
-        this
-    }
-
-    RequestBuilder removeAllParameters() {
-        mockRequest.removeAllParameters()
-        this
-    }
-
-    RequestBuilder setProtocol(String protocol) {
-        mockRequest.setProtocol(protocol)
-        this
-    }
-
-    RequestBuilder setScheme(String scheme) {
-        mockRequest.setScheme(scheme)
-        this
-    }
-
-    RequestBuilder setServerName(String serverName) {
-        mockRequest.setServerName(serverName)
-        this
-    }
-
-    RequestBuilder setServerPort(int serverPort) {
-        mockRequest.setServerPort(serverPort)
-        this
-    }
-
-    RequestBuilder setRemoteAddr(String remoteAddr) {
-        mockRequest.setRemoteAddr(remoteAddr)
-        this
-    }
-
-    RequestBuilder setRemoteHost(String remoteHost) {
-        mockRequest.setRemoteHost(remoteHost)
-        this
-    }
-
-    RequestBuilder setAttribute(String name, Object value) {
-        mockRequest.setAttribute(name, value)
-        this
-    }
-
-    RequestBuilder removeAttribute(String name) {
-        mockRequest.removeAttribute(name)
-        this
-    }
-
-    RequestBuilder clearAttributes() {
-        mockRequest.clearAttributes()
-        this
-    }
-
-    RequestBuilder addPreferredLocale(Locale locale) {
-        mockRequest.addPreferredLocale(locale)
-        this
-    }
-
-    RequestBuilder setPreferredLocales(List locales) {
-        mockRequest.setPreferredLocales(locales)
-        this
-    }
-
-    RequestBuilder setSecure(boolean secure) {
-        mockRequest.setSecure(secure)
-        this
-    }
-
-    RequestBuilder setRemotePort(int remotePort) {
-        mockRequest.setRemotePort(remotePort)
-        this
-    }
-
-    RequestBuilder setLocalName(String localName) {
-        mockRequest.setLocalName(localName)
-        this
-    }
-
-    RequestBuilder setLocalAddr(String localAddr) {
-        mockRequest.setLocalAddr(localAddr)
-        this
-    }
-
-    RequestBuilder setLocalPort(int localPort) {
-        mockRequest.setLocalPort(localPort)
-        this
-    }
-
-    RequestBuilder setAuthType(String authType) {
-        mockRequest.setAuthType(authType)
-        this
-    }
-
-    RequestBuilder setCookies(Cookie... cookies) {
-        mockRequest.setCookies(cookies)
-        this
-    }
-
-    RequestBuilder addHeader(String name, Object value) {
-        mockRequest.addHeader(name, value)
-        this
-    }
-
-    RequestBuilder setMethod(String method) {
-        mockRequest.setMethod(method)
-        this
-    }
-
-    RequestBuilder setPathInfo(String pathInfo) {
-        mockRequest.setPathInfo(pathInfo)
-        this
-    }
-
-    RequestBuilder setContextPath(String contextPath) {
-        mockRequest.setContextPath(contextPath)
-        this
-    }
-
-    RequestBuilder setQueryString(String queryString) {
-        mockRequest.setQueryString(queryString)
-        this
-    }
-
-    RequestBuilder setRemoteUser(String remoteUser) {
-        mockRequest.setRemoteAddr(remoteUser)
-        this
-    }
-
-    RequestBuilder addUserRole(String role) {
-        mockRequest.addUserRole(role)
-        this
-    }
-
-    RequestBuilder setUserPrincipal(Principal userPrincipal) {
-        mockRequest.setUserPrincipal(userPrincipal)
-        this
-    }
-
-    RequestBuilder setRequestedSessionId(String requestedSessionId) {
-        mockRequest.setRequestedSessionId(requestedSessionId)
-        this
-    }
-
-    RequestBuilder setRequestURI(String requestURI) {
-        mockRequest.setRequestURI(requestURI)
-        this
-    }
-
-    RequestBuilder setServletPath(String servletPath) {
-        mockRequest.setServletPath(servletPath)
-        this
-    }
-
-    RequestBuilder setSession(HttpSession session) {
-        mockRequest.setSession(session)
-        this
-    }
-
-    RequestBuilder setRequestedSessionIdValid(boolean requestedSessionIdValid) {
-        mockRequest.setRequestedSessionIdValid(requestedSessionIdValid)
-        this
-    }
-
-    RequestBuilder setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) {
-        mockRequest.setRequestedSessionIdFromCookie(requestedSessionIdFromCookie)
-        this
-    }
-
-    RequestBuilder setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) {
-        mockRequest.setRequestedSessionIdFromURL(requestedSessionIdFromURL)
-        this
+        request
     }
 }
diff --git a/src/main/groovy/com/icfolson/aem/prosper/builders/ResponseBuilder.groovy b/src/main/groovy/com/icfolson/aem/prosper/builders/ResponseBuilder.groovy
index 67f1d82..ed62bb7 100644
--- a/src/main/groovy/com/icfolson/aem/prosper/builders/ResponseBuilder.groovy
+++ b/src/main/groovy/com/icfolson/aem/prosper/builders/ResponseBuilder.groovy
@@ -1,17 +1,12 @@
 package com.icfolson.aem.prosper.builders
 
-import com.icfolson.aem.prosper.mocks.MockSlingHttpServletResponse
-import org.springframework.mock.web.MockHttpServletResponse
-
-import javax.servlet.http.Cookie
+import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse
 
 /**
  * Builder to assist in creating SlingHttpServletResponse objects.
  */
 class ResponseBuilder {
 
-    private final MockHttpServletResponse mockResponse = new MockHttpServletResponse()
-
     /**
      * Build a Sling response with default values.
      *
@@ -30,131 +25,25 @@ class ResponseBuilder {
      * .Closure)">with method.
      *
      * 
-     *  new ResponseBuilder().build {
-     *      status = 200
+     *  new ResponseBuilder().build {*      status = 200
      *      characterEncoding = "UTF-8"
      *      contentType = "application/json"
      *      addHeader "Connection", "close"
-     * }
+ *}
* * @param closure closure that delegates to this builder and MockHttpServletResponse * @return response */ - MockSlingHttpServletResponse build(@DelegatesTo(ResponseBuilder) Closure closure) { + MockSlingHttpServletResponse build(@DelegatesTo(MockSlingHttpServletResponse) Closure closure) { + def response = new MockSlingHttpServletResponse() + if (closure) { - closure.delegate = this + closure.delegate = response closure.resolveStrategy = Closure.DELEGATE_ONLY closure() } - new MockSlingHttpServletResponse(mockResponse) - } - - // delegate methods - - ResponseBuilder addCookie(Cookie cookie) { - mockResponse.addCookie(cookie) - this - } - - ResponseBuilder addDateHeader(String name, long value) { - mockResponse.addDateHeader(name, value) - this - } - - ResponseBuilder addHeader(String name, String value) { - mockResponse.addHeader(name, value) - this - } - - ResponseBuilder addIncludedUrl(String includedUrl) { - mockResponse.addIncludedUrl(includedUrl) - this - } - - ResponseBuilder addIntHeader(String name, int value) { - mockResponse.addIntHeader(name, value) - this - } - - ResponseBuilder setBufferSize(int bufferSize) { - mockResponse.setBufferSize(bufferSize) - this - } - - ResponseBuilder setCharacterEncoding(String characterEncoding) { - mockResponse.setCharacterEncoding(characterEncoding) - this - } - - ResponseBuilder setCommitted(boolean committed) { - mockResponse.setCommitted(committed) - this - } - - ResponseBuilder setContentLength(int contentLength) { - mockResponse.setContentLength(contentLength) - this - } - - ResponseBuilder setContentLengthLong(long contentLength) { - mockResponse.setContentLengthLong(contentLength) - this - } - - ResponseBuilder setContentType(String contentType) { - mockResponse.setContentType(contentType) - this - } - - ResponseBuilder setDateHeader(String name, long value) { - mockResponse.setDateHeader(name, value) - this - } - - ResponseBuilder setForwardedUrl(String forwardedUrl) { - mockResponse.setForwardedUrl(forwardedUrl) - this - } - - ResponseBuilder setHeader(String name, String value) { - mockResponse.setHeader(name, value) - this - } - - ResponseBuilder setIncludedUrl(String includedUrl) { - mockResponse.setIncludedUrl(includedUrl) - this - } - - ResponseBuilder setIntHeader(String name, int value) { - mockResponse.setIntHeader(name, value) - this - } - - ResponseBuilder setLocale(Locale locale) { - mockResponse.setLocale(locale) - this - } - - ResponseBuilder setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed) { - mockResponse.setOutputStreamAccessAllowed(outputStreamAccessAllowed) - this - } - - ResponseBuilder setStatus(int status) { - mockResponse.setStatus(status) - this - } - - ResponseBuilder setStatus(int status, String errorMessage) { - mockResponse.setStatus(status, errorMessage) - this - } - - ResponseBuilder setWriterAccessAllowed(boolean writerAccessAllowed) { - mockResponse.setWriterAccessAllowed(writerAccessAllowed) - this + response } } diff --git a/src/main/groovy/com/icfolson/aem/prosper/context/ProsperPageContext.groovy b/src/main/groovy/com/icfolson/aem/prosper/context/ProsperPageContext.groovy deleted file mode 100644 index 71e71a5..0000000 --- a/src/main/groovy/com/icfolson/aem/prosper/context/ProsperPageContext.groovy +++ /dev/null @@ -1,30 +0,0 @@ -package com.icfolson.aem.prosper.context - -import org.apache.sling.api.SlingHttpServletRequest -import org.apache.sling.api.SlingHttpServletResponse -import org.springframework.mock.web.MockJspWriter -import org.springframework.mock.web.MockPageContext - -import javax.servlet.jsp.JspWriter - -/** - * Extension of Spring MockPageContext that captures the tag output in a Writer. - */ -class ProsperPageContext extends MockPageContext { - - /** - * Writer containing tag output. - */ - Writer writer - - ProsperPageContext(SlingHttpServletRequest request, SlingHttpServletResponse response, Writer writer) { - super(null, request, response) - - this.writer = writer - } - - @Override - JspWriter getOut() { - new MockJspWriter(writer) - } -} diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletRequest.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletRequest.groovy deleted file mode 100755 index 866bacb..0000000 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletRequest.groovy +++ /dev/null @@ -1,161 +0,0 @@ -package com.icfolson.aem.prosper.mocks - -import com.icfolson.aem.prosper.mocks.request.MockRequestPathInfo -import com.icfolson.aem.prosper.mocks.request.MockRequestParameterMap -import groovy.transform.ToString -import org.apache.sling.api.SlingHttpServletRequest -import org.apache.sling.api.adapter.SlingAdaptable -import org.apache.sling.api.request.RequestDispatcherOptions -import org.apache.sling.api.request.RequestParameter -import org.apache.sling.api.request.RequestParameterMap -import org.apache.sling.api.request.RequestPathInfo -import org.apache.sling.api.request.RequestProgressTracker -import org.apache.sling.api.resource.Resource -import org.apache.sling.api.resource.ResourceResolver -import org.springframework.mock.web.MockHttpServletRequest - -import javax.servlet.RequestDispatcher -import javax.servlet.http.Cookie - -/** - * Mock Sling request that delegates to a Spring MockHttpServletRequest. This class should not be used - * directly; rather, use a RequestBuilder instance from test specs to instantiate mock requests. - */ -@ToString(includes = ["resource", "requestPathInfo", "requestParameterMap"]) -class MockSlingHttpServletRequest extends SlingAdaptable implements SlingHttpServletRequest { - - @Delegate - private final MockHttpServletRequest mockRequest - - private final ResourceResolver resourceResolver - - private final Resource resource - - private final RequestParameterMap requestParameterMap - - private final RequestPathInfo requestPathInfo - - MockSlingHttpServletRequest(MockHttpServletRequest mockRequest, ResourceResolver resourceResolver, String path, - List selectors, String extension, String suffix) { - this.mockRequest = mockRequest - this.resourceResolver = resourceResolver - - resource = resourceResolver.resolve(path) - requestParameterMap = MockRequestParameterMap.create(mockRequest) - requestPathInfo = new MockRequestPathInfo(resourceResolver, path, selectors, extension, suffix) - } - - @Override - Resource getResource() { - resource - } - - @Override - ResourceResolver getResourceResolver() { - resourceResolver - } - - @Override - RequestPathInfo getRequestPathInfo() { - requestPathInfo - } - - @Override - RequestParameter getRequestParameter(String name) { - requestParameterMap.getValue(name) - } - - @Override - RequestParameter[] getRequestParameters(String name) { - requestParameterMap.getValues(name) - } - - @Override - RequestParameterMap getRequestParameterMap() { - requestParameterMap - } - - @Override - List getRequestParameterList() { - def result = [] - - requestParameterMap.values().each { requestParameterArray -> - result.addAll(requestParameterArray as List) - } - - result - } - - @Override - RequestDispatcher getRequestDispatcher(String path, RequestDispatcherOptions options) { - throw new UnsupportedOperationException() - } - - @Override - RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions options) { - throw new UnsupportedOperationException() - } - - @Override - RequestDispatcher getRequestDispatcher(Resource resource) { - throw new UnsupportedOperationException() - } - - @Override - Cookie getCookie(String name) { - cookies.find { it.name == name } - } - - @Override - String getResponseContentType() { - throw new UnsupportedOperationException() - } - - @Override - Enumeration getResponseContentTypes() { - throw new UnsupportedOperationException() - } - - @Override - ResourceBundle getResourceBundle(Locale locale) { - throw new UnsupportedOperationException() - } - - @Override - ResourceBundle getResourceBundle(String baseName, Locale locale) { - throw new UnsupportedOperationException() - } - - @Override - RequestProgressTracker getRequestProgressTracker() { - throw new UnsupportedOperationException() - } - - @Override - String getQueryString() { - // check for overridden query string - def queryString = mockRequest.queryString - - if (!queryString) { - def builder = new StringBuilder() - def map = mockRequest.getParameterMap() - - if (map) { - map.each { name, values -> - values.each { value -> - builder.append(name) - builder.append('=') - builder.append(value) - builder.append('&') - } - } - - builder.deleteCharAt(builder.length() - 1) - } - - queryString = builder.toString() - } - - queryString - } -} diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletResponse.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletResponse.groovy deleted file mode 100644 index 69e8881..0000000 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletResponse.groovy +++ /dev/null @@ -1,26 +0,0 @@ -package com.icfolson.aem.prosper.mocks - -import com.google.common.base.Objects -import org.apache.sling.api.SlingHttpServletResponse -import org.apache.sling.api.adapter.SlingAdaptable -import org.springframework.mock.web.MockHttpServletResponse - -/** - * Mock Sling response that delegates to a Spring MockHttpServletResponse. This class should not be used - * directly; rather, use a ResponseBuilder instance from test specs to instantiate mock requests. - */ -class MockSlingHttpServletResponse extends SlingAdaptable implements SlingHttpServletResponse { - - @Delegate - private final MockHttpServletResponse mockResponse - - MockSlingHttpServletResponse(MockHttpServletResponse mockResponse) { - this.mockResponse = mockResponse - } - - @Override - String toString() { - Objects.toStringHelper(this).add("contentType", mockResponse.contentType).add("characterEncoding", - mockResponse.characterEncoding).add("contentAsString", mockResponse.contentAsString).toString() - } -} diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy new file mode 100644 index 0000000..1ca995b --- /dev/null +++ b/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy @@ -0,0 +1,57 @@ +package com.icfolson.aem.prosper.mocks + +import org.apache.sling.api.resource.ResourceResolver +import org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo +import org.osgi.framework.BundleContext + +class ProsperMockSlingHttpServletRequest extends org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest { + + ProsperMockSlingHttpServletRequest(ResourceResolver resourceResolver, BundleContext bundleContext) { + super(resourceResolver, bundleContext) + + resource = resourceResolver.resolve("/") + } + + /** + * Set the request path. + * + * @param path JCR path + */ + void setPath(String path) { + resource = resourceResolver.resolve(path) + (requestPathInfo as MockRequestPathInfo).resourcePath = path + } + + /** + * Set the request suffix. + * + * @param suffix suffix + */ + void setSuffix(String suffix) { + (requestPathInfo as MockRequestPathInfo).suffix = suffix + } + + /** + * Set the request extension + * + * @param extension extension + */ + void setExtension(String extension) { + (requestPathInfo as MockRequestPathInfo).extension = extension + } + + /** + * Add selectors to the request. + * + * @param selectors list of selectors + */ + void setSelectors(List selectors) { + (requestPathInfo as MockRequestPathInfo).selectorString = selectors.join(".") + } + + void setParameters(Map parameters) { + setParameterMap(parameters.collectEntries { name, value -> + [name, value instanceof Collection ? value as String[] : value] + }) + } +} diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameter.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameter.groovy deleted file mode 100644 index a086905..0000000 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameter.groovy +++ /dev/null @@ -1,54 +0,0 @@ -package com.icfolson.aem.prosper.mocks.request - -import groovy.transform.Immutable -import groovy.transform.ToString -import org.apache.sling.api.request.RequestParameter - -@Immutable -@ToString(includes = "value") -class MockRequestParameter implements RequestParameter { - - String name - - String value - - @Override - boolean isFormField() { - throw new UnsupportedOperationException() - } - - @Override - String getContentType() { - throw new UnsupportedOperationException() - } - - @Override - long getSize() { - throw new UnsupportedOperationException() - } - - @Override - byte[] get() { - throw new UnsupportedOperationException() - } - - @Override - InputStream getInputStream() throws IOException { - throw new UnsupportedOperationException() - } - - @Override - String getFileName() { - throw new UnsupportedOperationException() - } - - @Override - String getString() { - value - } - - @Override - String getString(String encoding) throws UnsupportedEncodingException { - value - } -} diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMap.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMap.groovy deleted file mode 100644 index d3b24f5..0000000 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMap.groovy +++ /dev/null @@ -1,38 +0,0 @@ -package com.icfolson.aem.prosper.mocks.request - -import groovy.transform.ToString -import org.apache.sling.api.request.RequestParameter -import org.apache.sling.api.request.RequestParameterMap - -import javax.servlet.http.HttpServletRequest - -@ToString(includeFields = true, includes = "map") -class MockRequestParameterMap implements RequestParameterMap { - - static RequestParameterMap create(HttpServletRequest request) { - def map = (request.parameterMap as Map).collectEntries { name, values -> - [(name): values.collect { new MockRequestParameter(name, it) }.toArray(new RequestParameter[values.size()])] - } - - new MockRequestParameterMap(map) - } - - @Delegate - private final Map map - - MockRequestParameterMap(map) { - this.map = map - } - - @Override - RequestParameter[] getValues(String name) { - map[name] ?: null - } - - @Override - RequestParameter getValue(String name) { - def values = map[name] - - values ? values[0] : null - } -} diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestPathInfo.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestPathInfo.groovy deleted file mode 100644 index fd222fc..0000000 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestPathInfo.groovy +++ /dev/null @@ -1,50 +0,0 @@ -package com.icfolson.aem.prosper.mocks.request - -import groovy.transform.ToString -import org.apache.sling.api.request.RequestPathInfo -import org.apache.sling.api.resource.Resource -import org.apache.sling.api.resource.ResourceResolver - -@ToString(includeFields = true, excludes = "resourceResolver") -class MockRequestPathInfo implements RequestPathInfo { - - private final ResourceResolver resourceResolver - - private final List selectors - - final String resourcePath - - final String extension - - final String suffix - - MockRequestPathInfo(ResourceResolver resourceResolver, String resourcePath, List selectors, - String extension, String suffix) { - this.resourceResolver = resourceResolver - this.resourcePath = resourcePath - this.selectors = selectors - this.extension = extension - this.suffix = suffix - } - - @Override - String getSelectorString() { - selectors ? selectors.join(".") : null - } - - @Override - String[] getSelectors() { - selectors as String[] - } - - @Override - Resource getSuffixResource() { - def suffixResource = null - - if (suffix) { - suffixResource = resourceResolver.getResource(suffix) - } - - suffixResource - } -} diff --git a/src/main/groovy/com/icfolson/aem/prosper/specs/ProsperSpec.groovy b/src/main/groovy/com/icfolson/aem/prosper/specs/ProsperSpec.groovy index 5ed010a..9bc75e4 100755 --- a/src/main/groovy/com/icfolson/aem/prosper/specs/ProsperSpec.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/specs/ProsperSpec.groovy @@ -178,7 +178,7 @@ abstract class ProsperSpec extends Specification { * @return request builder instance for this resource resolver */ RequestBuilder getRequestBuilder() { - new RequestBuilder(resourceResolver) + new RequestBuilder(resourceResolver, slingContext.bundleContext) } /** diff --git a/src/main/groovy/com/icfolson/aem/prosper/tag/JspTagProxy.groovy b/src/main/groovy/com/icfolson/aem/prosper/tag/JspTagProxy.groovy deleted file mode 100644 index f68a6d2..0000000 --- a/src/main/groovy/com/icfolson/aem/prosper/tag/JspTagProxy.groovy +++ /dev/null @@ -1,37 +0,0 @@ -package com.icfolson.aem.prosper.tag - -import groovy.transform.TupleConstructor - -import javax.servlet.jsp.PageContext -import javax.servlet.jsp.tagext.TagSupport - -/** - * Composite class containing a JSP tag instance and mock page context that exposes the tag writer output. - */ -@TupleConstructor -class JspTagProxy { - - /** - * Tag instance under test. - */ - TagSupport tag - - /** - * Mock page context for tag under test. - */ - PageContext pageContext - - /** - * Writer for capturing tag output. - */ - Writer writer - - /** - * Get the output value for the JSP writer. - * - * @return output string - */ - final String getOutput() { - writer.toString() - } -} diff --git a/src/main/groovy/com/icfolson/aem/prosper/traits/JspTagTrait.groovy b/src/main/groovy/com/icfolson/aem/prosper/traits/JspTagTrait.groovy deleted file mode 100644 index fc62c63..0000000 --- a/src/main/groovy/com/icfolson/aem/prosper/traits/JspTagTrait.groovy +++ /dev/null @@ -1,102 +0,0 @@ -package com.icfolson.aem.prosper.traits - -import com.icfolson.aem.prosper.builders.RequestBuilder -import com.icfolson.aem.prosper.builders.ResponseBuilder -import com.icfolson.aem.prosper.context.ProsperPageContext -import com.icfolson.aem.prosper.context.SlingContextProvider -import com.icfolson.aem.prosper.tag.JspTagProxy -import com.day.cq.wcm.api.PageManager -import org.apache.sling.api.SlingHttpServletRequest -import org.apache.sling.api.SlingHttpServletResponse -import org.apache.sling.api.resource.Resource -import org.apache.sling.api.resource.ResourceResolver -import org.apache.sling.api.resource.ValueMap -import org.apache.sling.testing.mock.sling.MockSling - -import javax.jcr.Node -import javax.servlet.jsp.PageContext -import javax.servlet.jsp.tagext.TagSupport - -import static com.day.cq.wcm.tags.DefineObjectsTag.DEFAULT_CURRENT_PAGE_NAME -import static com.day.cq.wcm.tags.DefineObjectsTag.DEFAULT_PAGE_MANAGER_NAME -import static com.day.cq.wcm.tags.DefineObjectsTag.DEFAULT_PAGE_PROPERTIES_NAME -import static com.day.cq.wcm.tags.DefineObjectsTag.DEFAULT_PROPERTIES_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_NODE_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_REQUEST_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_RESOURCE_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_RESOURCE_RESOLVER_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_RESPONSE_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_SLING_NAME - -/** - * Trait providing methods for initializing JSP tag support classes with a mocked page context. - */ -trait JspTagTrait { - - abstract SlingContextProvider getSlingContext() - - abstract ResourceResolver getResourceResolver() - - abstract RequestBuilder getRequestBuilder() - - abstract ResponseBuilder getResponseBuilder() - - /** - * Initialize the tag instance for the given class. - * - * @param tagClass tag class to initialize - * @param resourcePath path to set Resource instance and related objects in page context - * @return proxy tag instance containing mocked page context and writer - */ - JspTagProxy init(Class tagClass, String resourcePath) { - init(tagClass, resourcePath, [:]) - } - - /** - * Initialize the tag instance for the given class and additional page context attributes. - * - * @param tagClass tag class to initialize - * @param resourcePath path to set Resource instance and related objects in page context - * @param additionalPageContextAttributes additional attributes to set in the mocked page context - * @return proxy tag instance containing mocked page context and writer - */ - JspTagProxy init(Class tagClass, String resourcePath, - Map additionalPageContextAttributes) { - def resource = resourceResolver.resolve(resourcePath) - def request = requestBuilder.setPath(resource.path).build() - def response = responseBuilder.build() - - def writer = new StringWriter() - def pageContext = new ProsperPageContext(request, response, writer) - - setDefaultPageContextAttributes(pageContext, request, response, resource) - - additionalPageContextAttributes.each { name, value -> - pageContext.setAttribute(name, value) - } - - def tag = tagClass.newInstance() - - tag.pageContext = pageContext - - new JspTagProxy(tag, pageContext, writer) - } - - private void setDefaultPageContextAttributes(PageContext pageContext, SlingHttpServletRequest request, - SlingHttpServletResponse response, Resource resource) { - def pageManager = resourceResolver.adaptTo(PageManager) - def currentPage = pageManager.getContainingPage(resource) - - pageContext.setAttribute(DEFAULT_RESOURCE_RESOLVER_NAME, resourceResolver) - pageContext.setAttribute(DEFAULT_RESOURCE_NAME, resource) - pageContext.setAttribute(DEFAULT_PROPERTIES_NAME, resource.valueMap) - pageContext.setAttribute(DEFAULT_NODE_NAME, resource.adaptTo(Node)) - pageContext.setAttribute(DEFAULT_REQUEST_NAME, request) - pageContext.setAttribute(DEFAULT_RESPONSE_NAME, response) - pageContext.setAttribute(DEFAULT_SLING_NAME, MockSling.newSlingScriptHelper(request, response, - slingContext.bundleContext)) - pageContext.setAttribute(DEFAULT_PAGE_MANAGER_NAME, pageManager) - pageContext.setAttribute(DEFAULT_CURRENT_PAGE_NAME, currentPage) - pageContext.setAttribute(DEFAULT_PAGE_PROPERTIES_NAME, currentPage ? currentPage.properties : ValueMap.EMPTY) - } -} \ No newline at end of file diff --git a/src/test/groovy/com/icfolson/aem/prosper/builders/RequestBuilderSpec.groovy b/src/test/groovy/com/icfolson/aem/prosper/builders/RequestBuilderSpec.groovy index 2931361..bb0ef0c 100644 --- a/src/test/groovy/com/icfolson/aem/prosper/builders/RequestBuilderSpec.groovy +++ b/src/test/groovy/com/icfolson/aem/prosper/builders/RequestBuilderSpec.groovy @@ -67,7 +67,7 @@ class RequestBuilderSpec extends ProsperSpec { where: selectorList | selectorString - [] | null + [] | "" ["a"] | "a" ["a", "b"] | "a.b" } @@ -95,7 +95,7 @@ class RequestBuilderSpec extends ProsperSpec { where: map | queryString - [:] | "" + [:] | null ["a": ["1"]] | "a=1" ["a": "1"] | "a=1" ["a": ["1", "2"]] | "a=1&a=2" diff --git a/src/test/groovy/com/icfolson/aem/prosper/builders/ResponseBuilderSpec.groovy b/src/test/groovy/com/icfolson/aem/prosper/builders/ResponseBuilderSpec.groovy index 982bc56..5277867 100644 --- a/src/test/groovy/com/icfolson/aem/prosper/builders/ResponseBuilderSpec.groovy +++ b/src/test/groovy/com/icfolson/aem/prosper/builders/ResponseBuilderSpec.groovy @@ -1,7 +1,7 @@ package com.icfolson.aem.prosper.builders import com.icfolson.aem.prosper.specs.ProsperSpec -import com.google.common.base.Charsets +import groovy.json.JsonBuilder class ResponseBuilderSpec extends ProsperSpec { @@ -10,9 +10,8 @@ class ResponseBuilderSpec extends ProsperSpec { def response = responseBuilder.build() expect: - response.characterEncoding == Charsets.ISO_8859_1.name() !response.contentType - !response.contentAsString + !response.outputAsString } def "build response with closure"() { @@ -26,4 +25,15 @@ class ResponseBuilderSpec extends ProsperSpec { response.status == 500 response.getHeader("foo") == "bar" } + + def "write JSON to response"() { + setup: + def response = responseBuilder.build() + + when: + new JsonBuilder([path: "/content/prosper"]).writeTo(response.writer) + + then: + response.outputAsString == '{"path":"/content/prosper"}' + } } diff --git a/src/test/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletRequestSpec.groovy b/src/test/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletRequestSpec.groovy deleted file mode 100755 index ffdf381..0000000 --- a/src/test/groovy/com/icfolson/aem/prosper/mocks/MockSlingHttpServletRequestSpec.groovy +++ /dev/null @@ -1,87 +0,0 @@ -package com.icfolson.aem.prosper.mocks - -import com.icfolson.aem.prosper.specs.ProsperSpec -import org.apache.sling.api.resource.SyntheticResource - -class MockSlingHttpServletRequestSpec extends ProsperSpec { - - def "resolve resource for path"() { - setup: - def request = requestBuilder.build { - path = "/content" - } - - expect: - !(request.resource instanceof SyntheticResource) - request.resource.path == "/content" - } - - def "resolve resource for non-existent path"() { - setup: - def request = requestBuilder.build { - path = "/content/spock" - } - - expect: - request.resource instanceof SyntheticResource - request.resource.path == "/content/spock" - } - - def "get request parameter returns null"() { - setup: - def request = requestBuilder.build() - - expect: - !request.getRequestParameter("a") - !request.getRequestParameters("a") - } - - def "get request parameter"() { - setup: - def request = requestBuilder.build { - parameters = ["a": ["alpha"]] - } - - expect: - request.getRequestParameter("a").string == "alpha" - } - - def "get request parameters"() { - setup: - def request = requestBuilder.build { - parameters = ["a": ["alpha1", "alpha2"]] - } - - expect: - request.getRequestParameters("a")*.string == ["alpha1", "alpha2"] - } - - def "get parameter returns null"() { - setup: - def request = requestBuilder.build() - - expect: - !request.getParameter("a") - !request.getParameterValues("a") - } - - def "get parameter"() { - setup: - def request = requestBuilder.build { - parameters = ["a": ["alpha"]] - } - - expect: - request.getParameter("a") == "alpha" - } - - def "get parameters"() { - setup: - def request = requestBuilder.build { - parameters = ["a": ["alpha1", "alpha2"]] - } - - expect: - request.getParameterValues("a") as List == ["alpha1", "alpha2"] - } -} diff --git a/src/test/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMapSpec.groovy b/src/test/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMapSpec.groovy deleted file mode 100644 index f4f2a7c..0000000 --- a/src/test/groovy/com/icfolson/aem/prosper/mocks/request/MockRequestParameterMapSpec.groovy +++ /dev/null @@ -1,48 +0,0 @@ -package com.icfolson.aem.prosper.mocks.request - -import org.springframework.mock.web.MockHttpServletRequest -import spock.lang.Specification - -class MockRequestParameterMapSpec extends Specification { - - def "get values"() { - setup: - def request = new MockHttpServletRequest() - - request.addParameter "a", "1" - request.addParameter "a", "2" - - def requestParameterMap = MockRequestParameterMap.create(request) - - def values = requestParameterMap.getValues("a") - - expect: - values.length == 2 - values*.string == ["1", "2"] - } - - def "get value"() { - setup: - def request = new MockHttpServletRequest() - - request.setParameter "a", "1" - - def requestParameterMap = MockRequestParameterMap.create(request) - - expect: - requestParameterMap.getValue("a").string == "1" - } - - def "get value when multiple values are present"() { - setup: - def request = new MockHttpServletRequest() - - request.addParameter "a", "1" - request.addParameter "a", "2" - - def requestParameterMap = MockRequestParameterMap.create(request) - - expect: - requestParameterMap.getValue("a").string == "1" - } -} diff --git a/src/test/groovy/com/icfolson/aem/prosper/traits/JspTagTraitSpec.groovy b/src/test/groovy/com/icfolson/aem/prosper/traits/JspTagTraitSpec.groovy deleted file mode 100644 index e696713..0000000 --- a/src/test/groovy/com/icfolson/aem/prosper/traits/JspTagTraitSpec.groovy +++ /dev/null @@ -1,100 +0,0 @@ -package com.icfolson.aem.prosper.traits - -import com.icfolson.aem.prosper.specs.ProsperSpec -import com.day.cq.wcm.api.Page -import org.apache.sling.api.SlingHttpServletRequest -import org.apache.sling.api.resource.Resource -import spock.lang.Unroll - -import javax.jcr.Node - -import static com.day.cq.wcm.tags.DefineObjectsTag.DEFAULT_CURRENT_PAGE_NAME -import static com.day.cq.wcm.tags.DefineObjectsTag.DEFAULT_PAGE_MANAGER_NAME -import static com.day.cq.wcm.tags.DefineObjectsTag.DEFAULT_PAGE_PROPERTIES_NAME -import static com.day.cq.wcm.tags.DefineObjectsTag.DEFAULT_PROPERTIES_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_NODE_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_RESOURCE_NAME -import static org.apache.sling.scripting.jsp.taglib.DefineObjectsTag.DEFAULT_RESOURCE_RESOLVER_NAME - -@Unroll -class JspTagTraitSpec extends ProsperSpec implements JspTagTrait { - - def setupSpec() { - pageBuilder.content { - prosper { - "jcr:content" { - component() - } - } - } - } - - def "init tag sets default page context attributes"() { - setup: - def proxy = init(TestTag, path) - def pageContext = proxy.pageContext - - expect: "default attributes are non-null" - pageContext.getAttribute(DEFAULT_RESOURCE_RESOLVER_NAME) - pageContext.getAttribute(DEFAULT_PAGE_MANAGER_NAME) - pageContext.getAttribute(DEFAULT_PROPERTIES_NAME) != null - pageContext.getAttribute(DEFAULT_PAGE_PROPERTIES_NAME) != null - - and: "resource-specific values have correct paths" - (pageContext.getAttribute(DEFAULT_RESOURCE_NAME) as Resource).path == path - (pageContext.getAttribute(DEFAULT_NODE_NAME) as Node).path == path - (pageContext.getAttribute(DEFAULT_CURRENT_PAGE_NAME) as Page).path == "/content/prosper" - - where: - path << ["/content/prosper/jcr:content", "/content/prosper/jcr:content/component"] - } - - def "init tag for non-existing resource sets default page context attributes"() { - setup: - def proxy = init(TestTag, "/content/test") - def pageContext = proxy.pageContext - - expect: "default attributes are non-null" - pageContext.getAttribute(DEFAULT_RESOURCE_RESOLVER_NAME) - pageContext.getAttribute(DEFAULT_PAGE_MANAGER_NAME) - pageContext.getAttribute(DEFAULT_PROPERTIES_NAME) != null - pageContext.getAttribute(DEFAULT_PAGE_PROPERTIES_NAME) != null - - and: "resource-specific values have correct paths" - (pageContext.getAttribute(DEFAULT_RESOURCE_NAME) as Resource).path == "/content/test" - - and: "current node and current page are null" - !pageContext.getAttribute(DEFAULT_NODE_NAME) - !pageContext.getAttribute(DEFAULT_CURRENT_PAGE_NAME) - } - - def "init tag and get result"() { - setup: - def proxy = init(TestTag, "/content/prosper/jcr:content") - - when: - proxy.tag.doStartTag() - - then: - proxy.output == "hello" - } - - def "init tag with additional page context attributes and get result"() { - setup: - def proxy = init(TestTag, "/content/prosper/jcr:content", ["testName": "testValue"]) - - when: - proxy.tag.doEndTag() - - then: - proxy.output == "testValue" - } - - def "init request"() { - setup: - def proxy = init(TestTag, "/content/prosper/jcr:content") - - expect: - proxy.pageContext.request instanceof SlingHttpServletRequest - } -} diff --git a/src/test/groovy/com/icfolson/aem/prosper/traits/TestTag.groovy b/src/test/groovy/com/icfolson/aem/prosper/traits/TestTag.groovy deleted file mode 100644 index 6029713..0000000 --- a/src/test/groovy/com/icfolson/aem/prosper/traits/TestTag.groovy +++ /dev/null @@ -1,21 +0,0 @@ -package com.icfolson.aem.prosper.traits - -import javax.servlet.jsp.JspException -import javax.servlet.jsp.tagext.TagSupport - -class TestTag extends TagSupport { - - @Override - int doStartTag() throws JspException { - pageContext.out.write("hello") - - EVAL_PAGE - } - - @Override - int doEndTag() throws JspException { - pageContext.out.write(pageContext.getAttribute("testName") as String) - - EVAL_PAGE - } -} From f51fa079feb70e1254748a96cfbd76f38eb99ede Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Mon, 24 Sep 2018 15:23:06 -0500 Subject: [PATCH 08/10] updated override method. --- .../mocks/ProsperMockSlingHttpServletRequest.groovy | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy index 1ca995b..38ae4c9 100644 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy @@ -2,9 +2,10 @@ package com.icfolson.aem.prosper.mocks import org.apache.sling.api.resource.ResourceResolver import org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo +import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest import org.osgi.framework.BundleContext -class ProsperMockSlingHttpServletRequest extends org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest { +class ProsperMockSlingHttpServletRequest extends MockSlingHttpServletRequest { ProsperMockSlingHttpServletRequest(ResourceResolver resourceResolver, BundleContext bundleContext) { super(resourceResolver, bundleContext) @@ -49,9 +50,12 @@ class ProsperMockSlingHttpServletRequest extends org.apache.sling.testing.mock.s (requestPathInfo as MockRequestPathInfo).selectorString = selectors.join(".") } - void setParameters(Map parameters) { - setParameterMap(parameters.collectEntries { name, value -> + @Override + void setParameterMap(Map parameters) { + def parameterMap = parameters.collectEntries { name, value -> [name, value instanceof Collection ? value as String[] : value] - }) + } + + super.setParameterMap(parameterMap) } } From 52da6486351e3690bab355e663a55b40e3fda6e0 Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Mon, 24 Sep 2018 16:47:16 -0500 Subject: [PATCH 09/10] updated documentation for new version. --- README.md | 29 ++++++++++++------- pom.xml | 13 +++++---- .../prosper/builders/RequestBuilder.groovy | 19 +++++------- .../prosper/builders/ResponseBuilder.groovy | 17 +++++------ .../ProsperMockSlingHttpServletRequest.groovy | 12 ++++++-- src/site/site.xml | 2 +- .../builders/RequestBuilderSpec.groovy | 4 +-- 7 files changed, 54 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 7e3656b..17813d3 100755 --- a/README.md +++ b/README.md @@ -14,7 +14,6 @@ Prosper is an integration testing library for Adobe Experience Manager projects * Supplies mock OSGi bundle and [Sling](https://sling.apache.org/documentation/development/sling-mock.html) contexts for registering services, adapters, and Sling models. * Utilizes Groovy builders from the [AEM Groovy Extension](https://github.com/OlsonDigital/aem-groovy-extension) to provide a simple DSL for creating test content. * Provides additional builders for mock Sling requests and responses to simplify setup of test cases. -* Supports testing of JSP tag classes using a mixable Groovy trait. ## Requirements @@ -25,13 +24,23 @@ Prosper is an integration testing library for Adobe Experience Manager projects Prosper Version(s) | AEM Version ------------ | ------------- -12.x.x | 6.4 +13.x.x, 12.x.x | 6.4 11.x.x | 6.3 10.x.x, 9.x.x, 8.x.x | 6.2 7.x.x, 6.x.x, 5.x.x, 4.x.x | 6.1 3.x.x, 2.x.x, 1.x.x | 6.0 <= 0.10.x | 5.6 (CQ) +## Release Notes + +### 13.0.0 + +* Removed custom Sling Mock classes. Mock classes (e.g. `SlingHttpServletRequest`) are now provided by the [Sling Mock](http://sling.apache.org/documentation/development/sling-mock.html) framework to prevent conflicts with other dependencies. +* Removed JSP tag support. +* Upgraded Groovy to version `2.4.15`. +* Upgraded Spock to version `1.2-groovy-2.4`. + + ## Getting Started Add Maven dependency to project `pom.xml`. @@ -40,7 +49,7 @@ Add Maven dependency to project `pom.xml`. com.icfolson.aem.prosper prosper - 11.1.0 + 13.0.0 test ``` @@ -87,7 +96,7 @@ Configure Groovy compiler and Surefire plugin in Maven `pom.xml`. Additional co maven-compiler-plugin - 3.5 + 3.8.0 groovy-eclipse-compiler 1.8 @@ -124,7 +133,7 @@ Configure Groovy compiler and Surefire plugin in Maven `pom.xml`. Additional co org.codehaus.groovy groovy-all - 2.4.11 + 2.4.15 ``` @@ -184,9 +193,9 @@ The base specification exposes a number of fields and methods for use in tests. Field Name | Type | Description :---------|:---------|:----------- -session | [javax.jcr.Session](http://www.day.com/maven/jsr170/javadocs/jcr-2.0/javax/jcr/Session.html) | Administrative JCR session +session | [javax.jcr.Session](https://docs.adobe.com/docs/en/spec/javax.jcr/javadocs/jcr-2.0/javax/jcr/Session.html) | Administrative JCR session resourceResolver | [org.apache.sling.api.resource.ResourceResolver](http://sling.apache.org/apidocs/sling7/org/apache/sling/api/resource/ResourceResolver.html) | Administrative Sling Resource Resolver -pageManager | [com.day.cq.wcm.api.PageManager](https://docs.adobe.com/docs/en/aem/6-1/ref/javadoc/com/day/cq/wcm/api/PageManager.html) | AEM Page Manager +pageManager | [com.day.cq.wcm.api.PageManager](https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/com/day/cq/wcm/api/PageManager.html) | AEM Page Manager nodeBuilder | [com.icfolson.aem.groovy.extension.builders.NodeBuilder](http://code.digitalatolson.com/aem-groovy-extension/groovydocs/com/icfolson/aem/groovy/extension/builders/NodeBuilder.html) | JCR [Node Builder](https://github.com/Citytechinc/prosper#content-builders) pageBuilder | [com.icfolson.aem.groovy.extension.builders.PageBuilder](http://code.digitalatolson.com/aem-groovy-extension/groovydocs/com/icfolson/aem/groovy/extension/builders/PageBuilder.html) | AEM [Page Builder](https://github.com/Citytechinc/prosper#content-builders) slingContext | [com.icfolson.aem.prosper.context.SlingContextProvider](https://sling.apache.org/documentation/development/sling-mock.html) | Prosper extension of Sling/OSGi Context @@ -241,7 +250,7 @@ The above example will create an `nt:unstructured` (the default type) node at `/ Both builders automatically save the underlying JCR session after executing the provided closure. -In addition to the content builders, the [session](https://docs.adobe.com/content/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/Session.html) and [pageManager](https://docs.adobe.com/docs/en/aem/6-2/develop/ref/javadoc/com/day/cq/wcm/api/PageManager.html) instances provided by the base specification can be used directly to create test content in the JCR. +In addition to the content builders, the [session](https://docs.adobe.com/content/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/Session.html) and [pageManager](https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/com/day/cq/wcm/api/PageManager.html) instances provided by the base specification can be used directly to create test content in the JCR. ### Content Import @@ -431,7 +440,7 @@ class ServletSpec extends ProsperSpec { def servlet = new TestServlet() def request = requestBuilder.build { - parameters = [path: "/content/prosper"] + parameterMap = [path: "/content/prosper"] selectors = ["one", "two"] contentType = "application/json" } @@ -450,7 +459,7 @@ class ServletSpec extends ProsperSpec { } ``` -The mock request and response objects delegate to the [MockHttpServletRequest](http://docs.spring.io/spring/docs/4.1.7.RELEASE/javadoc-api/org/springframework/mock/web/MockHttpServletRequest.html) and [MockHttpServletResponse](http://docs.spring.io/spring/docs/4.1.7.RELEASE/javadoc-api/org/springframework/mock/web/MockHttpServletResponse.html) objects from the Spring Test Framework. The setter methods exposed by these classes are thus made available in the `build` closures for the request and response builders to assist in setting appropriate mock values for the class under test. +The mock request and response objects delegate to the [MockSlingHttpServletRequest](http://static.javadoc.io/org.apache.sling/org.apache.sling.testing.sling-mock/2.2.20/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletRequest.html) and [MockSlingHttpServletResponse](http://static.javadoc.io/org.apache.sling/org.apache.sling.testing.sling-mock/2.2.20/org/apache/sling/testing/mock/sling/servlet/MockSlingHttpServletResponse.html) objects from the Sling Mocks framework. The setter methods exposed by these classes are thus made available in the `build` closures for the request and response builders to assist in setting appropriate mock values for the class under test. ### Sling Context diff --git a/pom.xml b/pom.xml index 5be37dd..0dbe16b 100755 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.icfolson.aem.prosper prosper jar - 13.0.0-SNAPSHOT + 13.0.0 Prosper A Spock-based integration testing library for Adobe Experience Manager. @@ -99,11 +99,12 @@ href="http://spockframework.github.io/spock/javadoc/1.0"/> + - - + href="https://helpx.adobe.com/experience-manager/6-4/sites/developing/using/reference-materials/javadoc"/> + + @@ -274,7 +275,7 @@ com.icfolson.aem.groovy.extension aem-groovy-extension-bundle - 5.0.0 + 6.0.0 org.slf4j @@ -323,7 +324,7 @@ com.icfolson.aem.groovy.extension aem-groovy-extension-bundle - 5.1.0 + 6.0.0 org.spockframework diff --git a/src/main/groovy/com/icfolson/aem/prosper/builders/RequestBuilder.groovy b/src/main/groovy/com/icfolson/aem/prosper/builders/RequestBuilder.groovy index a2aee5e..157bcb3 100755 --- a/src/main/groovy/com/icfolson/aem/prosper/builders/RequestBuilder.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/builders/RequestBuilder.groovy @@ -34,24 +34,21 @@ class RequestBuilder { } /** - * Build a Sling request using a closure to set request properties. The closure delegates to this builder and an - * instance of MockHttpServletRequest, - * so methods for these instances may be called directly in the closure (see example below). This pattern is - * similar to the Groovy with method. + * Build a Sling request using a closure to set request properties. The closure delegates to an instance of + * MockSlingHttpServletRequest, + * so methods for this instances may be called directly in the closure (see example below). * *
-     *  new RequestBuilder(resourceResolver).build {*      serverName = "localhost"
+     *  new RequestBuilder(resourceResolver).build {
+     *      serverName = "localhost"
      *      path = "/content"
      *      method = "GET"
-     *      parameters = ["a": ["1", "2"], "b": ["1"]]
+     *      parameterMap = ["a": ["1", "2"], "b": ["1"]]
      *      extension = "html"
      *}
* - * @param closure closure that delegates to this builder and MockHttpServletRequest - * @return request + * @param closure closure that delegates to MockSlingHttpServletRequest + * @return mock request */ MockSlingHttpServletRequest build(@DelegatesTo(ProsperMockSlingHttpServletRequest) Closure closure) { def request = new ProsperMockSlingHttpServletRequest(resourceResolver, bundleContext) diff --git a/src/main/groovy/com/icfolson/aem/prosper/builders/ResponseBuilder.groovy b/src/main/groovy/com/icfolson/aem/prosper/builders/ResponseBuilder.groovy index ed62bb7..b216374 100644 --- a/src/main/groovy/com/icfolson/aem/prosper/builders/ResponseBuilder.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/builders/ResponseBuilder.groovy @@ -17,23 +17,20 @@ class ResponseBuilder { } /** - * Build a Sling response using a closure to set response properties. The closure delegates to this builder and an - * instance of MockHttpServletResponse, - * so methods on the response instance may be called directly in the closure (see example below). This pattern - * is similar to the Groovy with method. + * Build a Sling response using a closure to set response properties. The closure delegates to an instance of + * MockSlingHttpServletResponse, + * so methods on the response instance may be called directly in the closure (see example below). * *
-     *  new ResponseBuilder().build {*      status = 200
+     *  new ResponseBuilder().build {
+     *      status = 200
      *      characterEncoding = "UTF-8"
      *      contentType = "application/json"
      *      addHeader "Connection", "close"
      *}
* - * @param closure closure that delegates to this builder and MockHttpServletResponse - * @return response + * @param closure closure that delegates to MockSlingHttpServletResponse + * @return mock response */ MockSlingHttpServletResponse build(@DelegatesTo(MockSlingHttpServletResponse) Closure closure) { def response = new MockSlingHttpServletResponse() diff --git a/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy b/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy index 38ae4c9..2f58eb5 100644 --- a/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy +++ b/src/main/groovy/com/icfolson/aem/prosper/mocks/ProsperMockSlingHttpServletRequest.groovy @@ -5,6 +5,9 @@ import org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest import org.osgi.framework.BundleContext +/** + * Extension of Sling mock with additional convenience methods for setting request path info. + */ class ProsperMockSlingHttpServletRequest extends MockSlingHttpServletRequest { ProsperMockSlingHttpServletRequest(ResourceResolver resourceResolver, BundleContext bundleContext) { @@ -14,9 +17,9 @@ class ProsperMockSlingHttpServletRequest extends MockSlingHttpServletRequest { } /** - * Set the request path. + * Set the resource path. * - * @param path JCR path + * @param path JCR resource path */ void setPath(String path) { resource = resourceResolver.resolve(path) @@ -50,6 +53,11 @@ class ProsperMockSlingHttpServletRequest extends MockSlingHttpServletRequest { (requestPathInfo as MockRequestPathInfo).selectorString = selectors.join(".") } + /** + * Set the request parameter map. Values can either be single-valued Strings, arrays, or collections. + * + * @param parameters parameter map + */ @Override void setParameterMap(Map parameters) { def parameterMap = parameters.collectEntries { name, value -> diff --git a/src/site/site.xml b/src/site/site.xml index fe91cd9..7ef2a00 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -6,7 +6,7 @@ com.icfolson.maven icfolson-maven-skin - 1.0.0 + 1.1.0 diff --git a/src/test/groovy/com/icfolson/aem/prosper/builders/RequestBuilderSpec.groovy b/src/test/groovy/com/icfolson/aem/prosper/builders/RequestBuilderSpec.groovy index bb0ef0c..abb94e4 100644 --- a/src/test/groovy/com/icfolson/aem/prosper/builders/RequestBuilderSpec.groovy +++ b/src/test/groovy/com/icfolson/aem/prosper/builders/RequestBuilderSpec.groovy @@ -76,7 +76,7 @@ class RequestBuilderSpec extends ProsperSpec { setup: def request = requestBuilder.build { path = "/content" - parameters = ["a": ["1", "2"], "b": ["1"]] + parameterMap = ["a": ["1", "2"], "b": ["1"]] } expect: @@ -87,7 +87,7 @@ class RequestBuilderSpec extends ProsperSpec { setup: def request = requestBuilder.build { path = "/content" - parameters = map + parameterMap = map } expect: From 61452b804740a121d7e26e613d20381aa48b1c2b Mon Sep 17 00:00:00 2001 From: Mark Daugherty Date: Mon, 24 Sep 2018 16:49:33 -0500 Subject: [PATCH 10/10] doc updates. --- README.md | 68 ++----------------------------------------------------- 1 file changed, 2 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 17813d3..0eeaddf 100755 --- a/README.md +++ b/README.md @@ -537,7 +537,7 @@ Specs can register adapters in the Sling context by adding `AdapterFactory` inst class ExampleAdapterFactory implements AdapterFactory { @Override - public AdapterType getAdapter(Object adaptable, Class type) { + AdapterType getAdapter(Object adaptable, Class type) { AdapterType result = null if (type == String) { @@ -670,7 +670,7 @@ class ExampleSpec extends ProsperSpec { ### Traits -[Traits](http://groovy-lang.org/objectorientation.html#_traits) are a Groovy language feature that can be utilized to "mix in" new functionality to test specs. The JSP tag trait is the only one currently provided, but custom traits can be defined to support domain-specific features. +[Traits](http://groovy-lang.org/objectorientation.html#_traits) are a Groovy language feature that can be utilized to "mix in" new functionality to test specs. Custom traits can be defined to support domain-specific features. ```groovy import com.icfolson.aem.prosper.builders.RequestBuilder @@ -703,70 +703,6 @@ class MobileRequestTraitSpec extends ProsperSpec implements MobileRequestTrait { } ``` -#### JSP Tag Trait - -The `init` methods in `com.icfolson.aem.prosper.traits.JspTagTrait` initialize `TagSupport` instances with a mock `PageContext` containing a `Writer` for capturing tag output. The returned proxy allows test cases to evaluate page context attributes and verify the written output (i.e. calls to `pageContext.getOut().write()`. Tags can also be initialized with additional page context attributes. - -If the `init` method's `resourcePath` argument maps to valid JCR path, the mock `PageContext` will be initialized with the appropriate `Resource`, `Page`, `Node`, and `ValueMap` attributes for the addressed resource. See the `JspTagTrait` implementation for the specific attribute names and values. - -```groovy -import javax.servlet.jsp.JspException -import javax.servlet.jsp.tagext.TagSupport - -class SimpleTag extends TagSupport { - - String name - - @Override - int doStartTag() throws JspException { - pageContext.out.write("hello") - - EVAL_PAGE - } - - @Override - int doEndTag() throws JspException { - def prefix = pageContext.getAttribute("prefix") as String - - pageContext.setAttribute("name", prefix + name) - - EVAL_PAGE - } -} -``` -```groovy -import com.icfolson.aem.prosper.traits.JspTagTrait - -class SimpleTagSpec extends ProsperSpec implements JspTagTrait { - - def "start tag writes 'hello'"() { - setup: - def proxy = init(SimpleTag, "/") - - when: - proxy.tag.doStartTag() - - then: - proxy.output == "hello" - } - - def "end tag sets page context attribute"() { - setup: - def proxy = init(SimpleTag, "/", ["prefix": "LiveLongAnd"]) - def tag = proxy.tag as SimpleTag - - tag.name = "Prosper" - - when: - tag.doEndTag() - - then: - proxy.pageContext.getAttribute("name") == "LiveLongAndProsper" - } -} - -``` - ## References * [Prosper GroovyDocs](http://code.digitalatolson.com/prosper/groovydocs/index.html)