From 7db229b278c6b6606d37f5011a3fa1fd5f2625f4 Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Fri, 17 Jan 2020 17:51:14 +0200 Subject: [PATCH 01/21] CIF-1134 - Unified configuration of site settings Update the `MagentoGraphqlClient` to read configuration data from `/conf`. Update the unit tests --- .../client/MagentoGraphqlClient.java | 58 ++++++++++++++++--- .../models/v1/product/ProductImpl.java | 6 +- .../models/v1/product/ProductImplTest.java | 11 ++++ .../src/test/resources/context/jcr-conf.json | 15 +++++ 4 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 bundles/core/src/test/resources/context/jcr-conf.json diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index 00655ae8b0..1b8fe19896 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -16,9 +16,12 @@ import java.util.Collections; +import org.apache.commons.lang3.StringUtils; import org.apache.http.Header; import org.apache.http.message.BasicHeader; import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.caconfig.resource.ConfigurationResourceResolver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,12 +52,13 @@ public class MagentoGraphqlClient { public static final String STORE_CODE_PROPERTY = "magentoStore"; private GraphqlClient graphqlClient; + private RequestOptions requestOptions; /** * Instantiates and returns a new MagentoGraphqlClient. * This method returns null if the client cannot be instantiated. - * + * * @param resource The JCR resource to use to adapt to the lower-level {@link GraphqlClient}. * @return A new MagentoGraphqlClient instance. */ @@ -67,6 +71,41 @@ public static MagentoGraphqlClient create(Resource resource) { } } + public static MagentoGraphqlClient create(Resource resource, ConfigurationResourceResolver configurationResourceResolver) { + try { + return new MagentoGraphqlClient(resource, configurationResourceResolver); + } catch (Exception e) { + LOGGER.error(e.getMessage()); + return null; + } + } + + public MagentoGraphqlClient(Resource resource, ConfigurationResourceResolver configurationResourceResolver) { + graphqlClient = resource.adaptTo(GraphqlClient.class); + if (graphqlClient == null) { + throw new RuntimeException("GraphQL client not available for resource " + resource.getPath()); + } + + requestOptions = new RequestOptions().withGson(QueryDeserializer.getGson()); + Page page = resource.getResourceResolver() + .adaptTo(PageManager.class) + .getContainingPage(resource); + Resource configs = configurationResourceResolver.getResource(page.adaptTo(Resource.class), "settings", "commerce/default"); + + if (configs == null) { + throw new RuntimeException(String.format("Configuration not found at /conf/{0}/{1}", "settings", "commerce/default")); + } + + ValueMap properties = configs.getValueMap(); + + String storeCode = properties.get(STORE_CODE_PROPERTY, ""); + if (StringUtils.isNotEmpty(storeCode)) { + Header storeHeader = new BasicHeader("Store", storeCode); + requestOptions.withHeaders(Collections.singletonList(storeHeader)); + } + + } + private MagentoGraphqlClient(Resource resource) { graphqlClient = resource.adaptTo(GraphqlClient.class); if (graphqlClient == null) { @@ -76,7 +115,9 @@ private MagentoGraphqlClient(Resource resource) { requestOptions = new RequestOptions().withGson(QueryDeserializer.getGson()); InheritanceValueMap properties; - Page page = resource.getResourceResolver().adaptTo(PageManager.class).getContainingPage(resource); + Page page = resource.getResourceResolver() + .adaptTo(PageManager.class) + .getContainingPage(resource); if (page != null) { properties = new HierarchyNodeInheritanceValueMap(page.getContentResource()); } else { @@ -102,7 +143,7 @@ private MagentoGraphqlClient(Resource resource) { * Executes the given Magento query and returns the response. This method will use * the default HTTP method defined in the OSGi configuration of the underlying {@link GraphqlClient}. * Use {@link #execute(String, HttpMethod)} if you want to specify the HTTP method yourself. - * + * * @param query The GraphQL query. * @return The GraphQL response. */ @@ -113,18 +154,17 @@ public GraphqlResponse execute(String query) { /** * Executes the given Magento query and returns the response. This method * uses the given httpMethod to fetch the data. - * - * @param query The GraphQL query. + * + * @param query The GraphQL query. * @param httpMethod The HTTP method that will be used to fetch the data. * @return The GraphQL response. */ public GraphqlResponse execute(String query, HttpMethod httpMethod) { // We do not set the HTTP method in 'this.requestOptions' to avoid setting it as the new default - RequestOptions options = new RequestOptions() - .withGson(requestOptions.getGson()) - .withHeaders(requestOptions.getHeaders()) - .withHttpMethod(httpMethod); + RequestOptions options = new RequestOptions().withGson(requestOptions.getGson()) + .withHeaders(requestOptions.getHeaders()) + .withHttpMethod(httpMethod); return graphqlClient.execute(new GraphqlRequest(query), Query.class, Error.class, options); } diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java index 2abd5b73a5..3c9da6f538 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java @@ -30,6 +30,7 @@ import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.caconfig.resource.ConfigurationResourceResolver; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -96,6 +97,9 @@ public class ProductImpl implements Product { @Inject private XSSAPI xssApi; + @Inject + private ConfigurationResourceResolver configurationResourceResolver; + private NumberFormat priceFormatter; private Boolean configurable; private Boolean loadClientPrice; @@ -108,7 +112,7 @@ private void initModel() { String slug = parseProductSlug(); // Get MagentoGraphqlClient from the resource. - MagentoGraphqlClient magentoGraphqlClient = MagentoGraphqlClient.create(resource); + MagentoGraphqlClient magentoGraphqlClient = MagentoGraphqlClient.create(resource, configurationResourceResolver); if (StringUtils.isNotBlank(slug)) { productRetriever = new ProductRetriever(magentoGraphqlClient); diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java index d92147799e..6158f45472 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java @@ -24,6 +24,7 @@ import org.apache.commons.io.IOUtils; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.scripting.SlingBindings; +import org.apache.sling.caconfig.resource.ConfigurationResourceResolver; import org.apache.sling.servlethelpers.MockRequestPathInfo; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.xss.XSSAPI; @@ -34,6 +35,7 @@ import org.mockito.Mockito; import org.mockito.internal.util.reflection.Whitebox; +import com.adobe.cq.commerce.common.ValueMapDecorator; import com.adobe.cq.commerce.core.components.models.product.Asset; import com.adobe.cq.commerce.core.components.models.product.Variant; import com.adobe.cq.commerce.core.components.models.product.VariantAttribute; @@ -56,6 +58,7 @@ import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.designer.Style; import com.day.cq.wcm.scripting.WCMBindingsConstants; +import com.google.common.collect.ImmutableMap; import io.wcm.testing.mock.aem.junit.AemContext; import io.wcm.testing.mock.aem.junit.AemContextCallback; @@ -74,6 +77,8 @@ private static AemContext createContext(String contentPath) { (AemContextCallback) context -> { // Load page structure context.load().json(contentPath, "/content"); + context.load().json("/context/jcr-conf.json", "/conf"); + }, ResourceResolverType.JCR_MOCK); } @@ -116,6 +121,12 @@ public void setUp() throws Exception { when(xssApi.filterHTML(Mockito.anyString())).then(i -> i.getArgumentAt(0, String.class)); slingBindings.put("xssApi", xssApi); + ConfigurationResourceResolver configurationResourceResolver = mock(ConfigurationResourceResolver.class); + Resource mockConfigurationResource = mock(Resource.class); + when(mockConfigurationResource.getValueMap()).thenReturn(new ValueMapDecorator(ImmutableMap.of("cq:graphqlClient", "my-catalog"))); + when(configurationResourceResolver.getResource(any(Resource.class), any(String.class), any(String.class))).thenReturn(mockConfigurationResource); + slingBindings.put("configurationResourceResolver", configurationResourceResolver); + Style style = mock(Style.class); when(style.get(Mockito.anyString(), Mockito.anyBoolean())).then(i -> i.getArgumentAt(1, Boolean.class)); slingBindings.put("currentStyle", style); diff --git a/bundles/core/src/test/resources/context/jcr-conf.json b/bundles/core/src/test/resources/context/jcr-conf.json new file mode 100644 index 0000000000..21cc8c02de --- /dev/null +++ b/bundles/core/src/test/resources/context/jcr-conf.json @@ -0,0 +1,15 @@ +{ + "commerce": { + "jcr:primaryType": "cq:Page", + "jcr:mixinTypes": [ + "rep:AccessControllable" + ], + "jcr:createdBy": "admin", + "jcr:created": "Thu Jan 16 2020 12:20:34 GMT+0200", + "default": { + "jcr:primaryType": "nt:unstructured", + "cq:graphqlClient": "default", + "magentoStore": "default" + } + } +} \ No newline at end of file From 901ea0e79e5e84a1da94f7b19bb81706f4f732fc Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Tue, 28 Jan 2020 11:39:48 +0200 Subject: [PATCH 02/21] Merged local changes with remote --- .../client/MagentoGraphqlClient.java | 85 +++++++------------ .../models/v1/product/ProductImpl.java | 5 +- 2 files changed, 31 insertions(+), 59 deletions(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index 1b8fe19896..edd6e63452 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -21,7 +21,7 @@ import org.apache.http.message.BasicHeader; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; -import org.apache.sling.caconfig.resource.ConfigurationResourceResolver; +import org.apache.sling.caconfig.ConfigurationBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -70,73 +70,25 @@ public static MagentoGraphqlClient create(Resource resource) { return null; } } - - public static MagentoGraphqlClient create(Resource resource, ConfigurationResourceResolver configurationResourceResolver) { - try { - return new MagentoGraphqlClient(resource, configurationResourceResolver); - } catch (Exception e) { - LOGGER.error(e.getMessage()); - return null; - } - } - - public MagentoGraphqlClient(Resource resource, ConfigurationResourceResolver configurationResourceResolver) { + + private MagentoGraphqlClient(Resource resource) { graphqlClient = resource.adaptTo(GraphqlClient.class); if (graphqlClient == null) { throw new RuntimeException("GraphQL client not available for resource " + resource.getPath()); } requestOptions = new RequestOptions().withGson(QueryDeserializer.getGson()); - Page page = resource.getResourceResolver() - .adaptTo(PageManager.class) - .getContainingPage(resource); - Resource configs = configurationResourceResolver.getResource(page.adaptTo(Resource.class), "settings", "commerce/default"); - if (configs == null) { - throw new RuntimeException(String.format("Configuration not found at /conf/{0}/{1}", "settings", "commerce/default")); - } + ConfigurationBuilder configBuilder = resource.adaptTo(ConfigurationBuilder.class); + ValueMap properties = configBuilder.name("commerce/default") + .asValueMap(); - ValueMap properties = configs.getValueMap(); + String storeCode = properties.get(STORE_CODE_PROPERTY, readFallBackConfiguration(resource, STORE_CODE_PROPERTY)); - String storeCode = properties.get(STORE_CODE_PROPERTY, ""); if (StringUtils.isNotEmpty(storeCode)) { Header storeHeader = new BasicHeader("Store", storeCode); requestOptions.withHeaders(Collections.singletonList(storeHeader)); } - - } - - private MagentoGraphqlClient(Resource resource) { - graphqlClient = resource.adaptTo(GraphqlClient.class); - if (graphqlClient == null) { - throw new RuntimeException("GraphQL client not available for resource " + resource.getPath()); - } - - requestOptions = new RequestOptions().withGson(QueryDeserializer.getGson()); - - InheritanceValueMap properties; - Page page = resource.getResourceResolver() - .adaptTo(PageManager.class) - .getContainingPage(resource); - if (page != null) { - properties = new HierarchyNodeInheritanceValueMap(page.getContentResource()); - } else { - properties = new ComponentInheritanceValueMap(resource); - } - - // get Magento store code - String storeCode = properties.getInherited(STORE_CODE_PROPERTY, String.class); - // for backward compatibility also check of the old cq:magentoStore property - if (storeCode == null) { - storeCode = properties.getInherited("cq:" + STORE_CODE_PROPERTY, String.class); - if (storeCode != null) { - LOGGER.warn("Deprecated 'cq:magentoStore' still in use for {}. Please update to 'magentoStore'.", resource.getPath()); - } - } - if (storeCode != null) { - Header storeHeader = new BasicHeader("Store", storeCode); - requestOptions.withHeaders(Collections.singletonList(storeHeader)); - } } /** @@ -168,4 +120,27 @@ public GraphqlResponse execute(String query, HttpMethod httpMethod return graphqlClient.execute(new GraphqlRequest(query), Query.class, Error.class, options); } + + private String readFallBackConfiguration(Resource resource, String propertyName) { + + InheritanceValueMap properties; + String storeCode; + + Page page = resource.getResourceResolver() + .adaptTo(PageManager.class) + .getContainingPage(resource); + if (page != null) { + properties = new HierarchyNodeInheritanceValueMap(page.getContentResource()); + } else { + properties = new ComponentInheritanceValueMap(resource); + } + storeCode = properties.getInherited(propertyName, String.class); + if (storeCode == null) { + storeCode = properties.getInherited("cq:" + propertyName, String.class); + if (storeCode != null) { + LOGGER.warn("Deprecated 'cq:magentoStore' still in use for {}. Please update to 'magentoStore'.", resource.getPath()); + } + } + return storeCode; + } } diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java index 3c9da6f538..4d664098fa 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java @@ -97,9 +97,6 @@ public class ProductImpl implements Product { @Inject private XSSAPI xssApi; - @Inject - private ConfigurationResourceResolver configurationResourceResolver; - private NumberFormat priceFormatter; private Boolean configurable; private Boolean loadClientPrice; @@ -112,7 +109,7 @@ private void initModel() { String slug = parseProductSlug(); // Get MagentoGraphqlClient from the resource. - MagentoGraphqlClient magentoGraphqlClient = MagentoGraphqlClient.create(resource, configurationResourceResolver); + MagentoGraphqlClient magentoGraphqlClient = MagentoGraphqlClient.create(resource); if (StringUtils.isNotBlank(slug)) { productRetriever = new ProductRetriever(magentoGraphqlClient); From c2bda748bbd6236cdbb8314325eea84d469649e5 Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Tue, 28 Jan 2020 11:52:54 +0200 Subject: [PATCH 03/21] CIF-1134 - Unified configuration of site settings Fix unit tests so that they don't fail when the ConfigurationBuilder is not available --- .../client/MagentoGraphqlClient.java | 14 ++-- .../client/MagentoGraphqlClientTest.java | 67 +++++++++++++------ 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index edd6e63452..4d545cfc04 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -70,7 +70,7 @@ public static MagentoGraphqlClient create(Resource resource) { return null; } } - + private MagentoGraphqlClient(Resource resource) { graphqlClient = resource.adaptTo(GraphqlClient.class); if (graphqlClient == null) { @@ -80,11 +80,15 @@ private MagentoGraphqlClient(Resource resource) { requestOptions = new RequestOptions().withGson(QueryDeserializer.getGson()); ConfigurationBuilder configBuilder = resource.adaptTo(ConfigurationBuilder.class); - ValueMap properties = configBuilder.name("commerce/default") - .asValueMap(); - - String storeCode = properties.get(STORE_CODE_PROPERTY, readFallBackConfiguration(resource, STORE_CODE_PROPERTY)); + String storeCode; + if (configBuilder != null) { + ValueMap properties = configBuilder.name("commerce/default") + .asValueMap(); + storeCode = properties.get(STORE_CODE_PROPERTY, readFallBackConfiguration(resource, STORE_CODE_PROPERTY)); + } else { + storeCode = readFallBackConfiguration(resource, STORE_CODE_PROPERTY); + } if (StringUtils.isNotEmpty(storeCode)) { Header storeHeader = new BasicHeader("Store", storeCode); requestOptions.withHeaders(Collections.singletonList(storeHeader)); diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java index 304a4b5840..64f653f25e 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java @@ -20,7 +20,9 @@ import org.apache.http.Header; import org.apache.http.message.BasicHeader; import org.apache.sling.api.resource.Resource; +import org.apache.sling.caconfig.ConfigurationBuilder; import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.apache.sling.testing.resourceresolver.MockValueMap; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -34,70 +36,90 @@ import com.adobe.cq.commerce.magento.graphql.gson.QueryDeserializer; import io.wcm.testing.mock.aem.junit.AemContext; import io.wcm.testing.mock.aem.junit.AemContextCallback; +import sun.security.krb5.Config; public class MagentoGraphqlClientTest { private GraphqlClient graphqlClient; + private ConfigurationBuilder mockConfigurationBuilder; + @Rule public final AemContext context = createContext("/context/jcr-content.json"); private static AemContext createContext(String contentPath) { - return new AemContext( - (AemContextCallback) context -> { - // Load page structure - context.load().json(contentPath, "/content"); - }, - ResourceResolverType.JCR_MOCK); + return new AemContext((AemContextCallback) context -> { + // Load page structure + context.load() + .json(contentPath, "/content"); + }, ResourceResolverType.JCR_MOCK); } @Before public void setup() { graphqlClient = Mockito.mock(GraphqlClient.class); - Mockito.when(graphqlClient.execute(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(null); + Mockito.when(graphqlClient.execute(Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(null); + + mockConfigurationBuilder = Mockito.mock(ConfigurationBuilder.class); + Mockito.when(mockConfigurationBuilder.name(Mockito.any(String.class))) + .thenReturn(mockConfigurationBuilder); + + + Mockito.when(mockConfigurationBuilder.asValueMap()).thenReturn(MockValueMap.EMPTY); + } private void testMagentoStoreProperty(Resource resource, boolean withStoreHeader) { - Mockito.when(resource.adaptTo(GraphqlClient.class)).thenReturn(graphqlClient); + Mockito.when(resource.adaptTo(GraphqlClient.class)) + .thenReturn(graphqlClient); + Mockito.when(resource.adaptTo(ConfigurationBuilder.class)).thenReturn(mockConfigurationBuilder); + MagentoGraphqlClient client = MagentoGraphqlClient.create(resource); // Verify parameters with default execute() method and store property client.execute("{dummy}"); List
headers = withStoreHeader ? Collections.singletonList(new BasicHeader("Store", "my-store")) : Collections.emptyList(); RequestOptionsMatcher matcher = new RequestOptionsMatcher(headers, null); - Mockito.verify(graphqlClient).execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); + Mockito.verify(graphqlClient) + .execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); // Verify setting a custom HTTP method client.execute("{dummy}", HttpMethod.GET); matcher = new RequestOptionsMatcher(headers, HttpMethod.GET); - Mockito.verify(graphqlClient).execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); + Mockito.verify(graphqlClient) + .execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); } @Test public void testMagentoStoreProperty() { // Get page which has the magentoStore property in its jcr:content node - Resource resource = Mockito.spy(context.resourceResolver().getResource("/content/pageA")); + Resource resource = Mockito.spy(context.resourceResolver() + .getResource("/content/pageA")); testMagentoStoreProperty(resource, true); } @Test public void testInheritedMagentoStoreProperty() { // Get page whose parent has the magentoStore property in its jcr:content node - Resource resource = Mockito.spy(context.resourceResolver().getResource("/content/pageB/pageC")); + Resource resource = Mockito.spy(context.resourceResolver() + .getResource("/content/pageB/pageC")); testMagentoStoreProperty(resource, true); } @Test public void testMissingMagentoStoreProperty() { // Get page whose parent has the magentoStore property in its jcr:content node - Resource resource = Mockito.spy(context.resourceResolver().getResource("/content/pageD")); + Resource resource = Mockito.spy(context.resourceResolver() + .getResource("/content/pageD")); testMagentoStoreProperty(resource, false); } @Test public void testOldMagentoStoreProperty() { // Get page which has the old cq:magentoStore property in its jcr:content node - Resource resource = Mockito.spy(context.resourceResolver().getResource("/content/pageE")); + Resource resource = Mockito.spy(context.resourceResolver() + .getResource("/content/pageE")); testMagentoStoreProperty(resource, true); } @@ -105,15 +127,18 @@ public void testOldMagentoStoreProperty() { public void testNewMagentoStoreProperty() { // Get page which has both the new magentoStore property and old cq:magentoStore property // in its jcr:content node and make sure the new one is prefered - Resource resource = Mockito.spy(context.resourceResolver().getResource("/content/pageF")); + Resource resource = Mockito.spy(context.resourceResolver() + .getResource("/content/pageF")); testMagentoStoreProperty(resource, true); } @Test public void testError() { // Get page which has the magentoStore property in its jcr:content node - Resource resource = Mockito.spy(context.resourceResolver().getResource("/content/pageA")); - Mockito.when(resource.adaptTo(GraphqlClient.class)).thenReturn(null); + Resource resource = Mockito.spy(context.resourceResolver() + .getResource("/content/pageA")); + Mockito.when(resource.adaptTo(GraphqlClient.class)) + .thenReturn(null); MagentoGraphqlClient client = MagentoGraphqlClient.create(resource); Assert.assertNull(client); @@ -125,6 +150,7 @@ public void testError() { private static class RequestOptionsMatcher extends ArgumentMatcher { private List
headers; + private HttpMethod httpMethod; public RequestOptionsMatcher(List
headers, HttpMethod httpMethod) { @@ -147,8 +173,11 @@ public boolean matches(Object obj) { } for (Header header : headers) { - if (!requestOptions.getHeaders().stream().anyMatch( - h -> h.getName().equals(header.getName()) && h.getValue().equals(header.getValue()))) { + if (!requestOptions.getHeaders() + .stream() + .anyMatch(h -> h.getName() + .equals(header.getName()) && h.getValue() + .equals(header.getValue()))) { return false; } } From e0261af7964877b75143b922e09c54e8b33a584c Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Tue, 28 Jan 2020 16:26:38 +0200 Subject: [PATCH 04/21] (WIP) Update unit tests to use the ConfigurationBuilder as well --- bundles/core/pom.xml | 7 + .../client/MagentoGraphqlClient.java | 8 +- .../client/MagentoGraphqlClientTest.java | 73 ++++-- .../src/test/resources/context/jcr-conf.json | 25 +- .../test/resources/context/jcr-content.json | 215 ++++++++++-------- 5 files changed, 195 insertions(+), 133 deletions(-) diff --git a/bundles/core/pom.xml b/bundles/core/pom.xml index ee658cd0b2..ccaade604c 100644 --- a/bundles/core/pom.xml +++ b/bundles/core/pom.xml @@ -490,6 +490,13 @@ 2.5.0 + + org.apache.sling + org.apache.sling.testing.caconfig-mock-plugin + 1.3.2 + test + + com.adobe.commerce.cif diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index 4d545cfc04..893a7bd46b 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -51,6 +51,9 @@ public class MagentoGraphqlClient { public static final String STORE_CODE_PROPERTY = "magentoStore"; + public static final String CONFIGURATION_NAME = "commerce/default"; + + private GraphqlClient graphqlClient; private RequestOptions requestOptions; @@ -79,12 +82,11 @@ private MagentoGraphqlClient(Resource resource) { requestOptions = new RequestOptions().withGson(QueryDeserializer.getGson()); - ConfigurationBuilder configBuilder = resource.adaptTo(ConfigurationBuilder.class); String storeCode; + ConfigurationBuilder configBuilder = resource.adaptTo(ConfigurationBuilder.class); if (configBuilder != null) { - ValueMap properties = configBuilder.name("commerce/default") - .asValueMap(); + ValueMap properties = configBuilder.name(CONFIGURATION_NAME).asValueMap(); storeCode = properties.get(STORE_CODE_PROPERTY, readFallBackConfiguration(resource, STORE_CODE_PROPERTY)); } else { storeCode = readFallBackConfiguration(resource, STORE_CODE_PROPERTY); diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java index 64f653f25e..3977eb9be3 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java @@ -14,13 +14,18 @@ package com.adobe.cq.commerce.core.components.client; +import java.io.IOException; import java.util.Collections; +import java.util.Dictionary; +import java.util.Hashtable; import java.util.List; import org.apache.http.Header; import org.apache.http.message.BasicHeader; import org.apache.sling.api.resource.Resource; import org.apache.sling.caconfig.ConfigurationBuilder; +import org.apache.sling.caconfig.ConfigurationResolver; +import org.apache.sling.testing.mock.caconfig.ContextPlugins; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.resourceresolver.MockValueMap; import org.junit.Assert; @@ -29,54 +34,75 @@ import org.junit.Test; import org.mockito.ArgumentMatcher; import org.mockito.Mockito; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.adobe.cq.commerce.graphql.client.GraphqlClient; import com.adobe.cq.commerce.graphql.client.HttpMethod; import com.adobe.cq.commerce.graphql.client.RequestOptions; import com.adobe.cq.commerce.magento.graphql.gson.QueryDeserializer; import io.wcm.testing.mock.aem.junit.AemContext; -import io.wcm.testing.mock.aem.junit.AemContextCallback; -import sun.security.krb5.Config; +import io.wcm.testing.mock.aem.junit.AemContextBuilder; public class MagentoGraphqlClientTest { - + private static final Logger LOG = LoggerFactory.getLogger(MagentoGraphqlClientTest.class); private GraphqlClient graphqlClient; private ConfigurationBuilder mockConfigurationBuilder; @Rule - public final AemContext context = createContext("/context/jcr-content.json"); - - private static AemContext createContext(String contentPath) { - return new AemContext((AemContextCallback) context -> { - // Load page structure - context.load() - .json(contentPath, "/content"); - }, ResourceResolverType.JCR_MOCK); - } + public final AemContext context = new AemContextBuilder(ResourceResolverType.JCR_MOCK).plugin(ContextPlugins.CACONFIG) + .build(); @Before - public void setup() { + public void setup() throws IOException { + + context.load() + .json("/context/jcr-content.json", "/content"); + context.load() + .json("/context/jcr-conf.json", "/conf/test-config"); graphqlClient = Mockito.mock(GraphqlClient.class); Mockito.when(graphqlClient.execute(Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(null); - mockConfigurationBuilder = Mockito.mock(ConfigurationBuilder.class); - Mockito.when(mockConfigurationBuilder.name(Mockito.any(String.class))) - .thenReturn(mockConfigurationBuilder); + ConfigurationAdmin configurationAdmin = context.getService(ConfigurationAdmin.class); + + Configuration serviceConfiguration = configurationAdmin.getConfiguration("org.apache.sling.caconfig.resource.impl.def.DefaultContextPathStrategy"); + Dictionary props = new Hashtable<>(); + props.put("configRefResourceNames", new String[] { ".", "jcr:content" }); + props.put("configRefPropertyNames", "cq:conf"); + serviceConfiguration.update(props); + serviceConfiguration = configurationAdmin.getConfiguration("org.apache.sling.caconfig.resource.impl.def.DefaultConfigurationResourceResolvingStrategy"); + props = new Hashtable<>(); + props.put("configPath", "/conf"); + serviceConfiguration.update(props); - Mockito.when(mockConfigurationBuilder.asValueMap()).thenReturn(MockValueMap.EMPTY); + + serviceConfiguration = configurationAdmin.getConfiguration("org.apache.sling.caconfig.impl.ConfigurationResolverImpl"); + props = new Hashtable<>(); + props.put("configBucketNames", new String[] { "settings" }); + serviceConfiguration.update(props); } private void testMagentoStoreProperty(Resource resource, boolean withStoreHeader) { Mockito.when(resource.adaptTo(GraphqlClient.class)) .thenReturn(graphqlClient); - Mockito.when(resource.adaptTo(ConfigurationBuilder.class)).thenReturn(mockConfigurationBuilder); + Mockito.when(resource.adaptTo(ConfigurationBuilder.class)) + .thenReturn(mockConfigurationBuilder); + + Mockito.when(mockConfigurationBuilder.asValueMap()) + .thenReturn(MockValueMap.EMPTY); MagentoGraphqlClient client = MagentoGraphqlClient.create(resource); + executeAndCheck(withStoreHeader, client); + } + + private void executeAndCheck(boolean withStoreHeader, MagentoGraphqlClient client) { // Verify parameters with default execute() method and store property client.execute("{dummy}"); List
headers = withStoreHeader ? Collections.singletonList(new BasicHeader("Store", "my-store")) : Collections.emptyList(); @@ -91,6 +117,17 @@ private void testMagentoStoreProperty(Resource resource, boolean withStoreHeader .execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); } + @Test + public void testMagentoStorePropertyWithConfigBuilder() { + + Resource pageWithConfig = Mockito.spy(context.resourceResolver() + .getResource("/content/pageG")); + Mockito.when(pageWithConfig.adaptTo(GraphqlClient.class)) + .thenReturn(graphqlClient); + MagentoGraphqlClient client = MagentoGraphqlClient.create(pageWithConfig); + executeAndCheck(true, client); + } + @Test public void testMagentoStoreProperty() { // Get page which has the magentoStore property in its jcr:content node diff --git a/bundles/core/src/test/resources/context/jcr-conf.json b/bundles/core/src/test/resources/context/jcr-conf.json index 21cc8c02de..9aa33813ba 100644 --- a/bundles/core/src/test/resources/context/jcr-conf.json +++ b/bundles/core/src/test/resources/context/jcr-conf.json @@ -1,15 +1,18 @@ { - "commerce": { - "jcr:primaryType": "cq:Page", - "jcr:mixinTypes": [ - "rep:AccessControllable" - ], - "jcr:createdBy": "admin", - "jcr:created": "Thu Jan 16 2020 12:20:34 GMT+0200", - "default": { - "jcr:primaryType": "nt:unstructured", - "cq:graphqlClient": "default", - "magentoStore": "default" + "settings": { + "jcr:primaryType": "sling:Folder", + "commerce": { + "jcr:primaryType": "cq:Page", + "jcr:mixinTypes": [ + "rep:AccessControllable" + ], + "jcr:createdBy": "admin", + "jcr:created": "Thu Jan 16 2020 12:20:34 GMT+0200", + "default": { + "jcr:primaryType": "nt:unstructured", + "cq:graphqlClient": "default", + "magentoStore": "default" + } } } } \ No newline at end of file diff --git a/bundles/core/src/test/resources/context/jcr-content.json b/bundles/core/src/test/resources/context/jcr-content.json index 7b1a7d470b..3816b8923d 100644 --- a/bundles/core/src/test/resources/context/jcr-content.json +++ b/bundles/core/src/test/resources/context/jcr-content.json @@ -1,107 +1,120 @@ { - "pageA": { - "jcr:primaryType": "cq:Page", - "jcr:content": { - "jcr:language": "en_US", - "jcr:primaryType": "cq:PageContent", - "magentoStore": "my-store", - "root": { - "responsivegrid": { - "productteaser-simple": { - "selection": "MJ01", - "sling:resourceType": "venia/components/commerce/productteaser" - }, - "productteaser-variant": { - "selection": "MJ01#MJ01-XS-Orange", - "sling:resourceType": "venia/components/commerce/productteaser", - "cta": "addToCart" - }, - "productteaser-path": { - "selection": "/var/commerce/products/catalog/luma/men/tops-men/jackets-men/MJ01", - "sling:resourceType": "venia/components/commerce/productteaser" - }, - "productcarousel": { - "product": ["NOT-FOUND", "24-MG01", "MJ01#MJ01-L-Red", "WJ01"], - "sling:resourceType": "venia/components/commerce/productcarousel" - }, - "product": { - "sling:resourceType": "venia/components/commerce/product" - }, - "productlist": { - "loadClientPrice": true, - "showImage": true, - "showTitle": true, - "sling:resourceType": "venia/components/commerce/productlist" - }, - "searchresults": { - "sling:resourceType": "venia/components/commerce/searchresults" - }, - "relatedproducts": { - "product": "24-MG01", - "relationType": "RELATED_PRODUCTS", - "sling:resourceType": "venia/components/commerce/relatedproducts" - }, - "upsellproducts": { - "relationType": "UPSELL_PRODUCTS", - "sling:resourceType": "venia/components/commerce/relatedproducts" - }, - "crosssellproducts": { - "relationType": "CROSS_SELL_PRODUCTS", - "sling:resourceType": "venia/components/commerce/relatedproducts" - }, - "featuredcategorylist": { - "sling:resourceType": "venia/components/commerce/featuredcategorylist", - "items": { - "item0": { - "categoryId": "5" - }, - "item1": { - "categoryId": "6", - "asset": "/content/dam/venia/landing_page_image4.jpg" - }, - "item2": { - "categoryId": "7", - "asset": "invalid-asset" - }, - "item3": {} + "pageA": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:language": "en_US", + "jcr:primaryType": "cq:PageContent", + "magentoStore": "my-store", + "cq:conf": "/conf/test-config", + "root": { + "responsivegrid": { + "productteaser-simple": { + "selection": "MJ01", + "sling:resourceType": "venia/components/commerce/productteaser" + }, + "productteaser-variant": { + "selection": "MJ01#MJ01-XS-Orange", + "sling:resourceType": "venia/components/commerce/productteaser", + "cta": "addToCart" + }, + "productteaser-path": { + "selection": "/var/commerce/products/catalog/luma/men/tops-men/jackets-men/MJ01", + "sling:resourceType": "venia/components/commerce/productteaser" + }, + "productcarousel": { + "product": [ + "NOT-FOUND", + "24-MG01", + "MJ01#MJ01-L-Red", + "WJ01" + ], + "sling:resourceType": "venia/components/commerce/productcarousel" + }, + "product": { + "sling:resourceType": "venia/components/commerce/product" + }, + "productlist": { + "loadClientPrice": true, + "showImage": true, + "showTitle": true, + "sling:resourceType": "venia/components/commerce/productlist" + }, + "searchresults": { + "sling:resourceType": "venia/components/commerce/searchresults" + }, + "relatedproducts": { + "product": "24-MG01", + "relationType": "RELATED_PRODUCTS", + "sling:resourceType": "venia/components/commerce/relatedproducts" + }, + "upsellproducts": { + "relationType": "UPSELL_PRODUCTS", + "sling:resourceType": "venia/components/commerce/relatedproducts" + }, + "crosssellproducts": { + "relationType": "CROSS_SELL_PRODUCTS", + "sling:resourceType": "venia/components/commerce/relatedproducts" + }, + "featuredcategorylist": { + "sling:resourceType": "venia/components/commerce/featuredcategorylist", + "items": { + "item0": { + "categoryId": "5" + }, + "item1": { + "categoryId": "6", + "asset": "/content/dam/venia/landing_page_image4.jpg" + }, + "item2": { + "categoryId": "7", + "asset": "invalid-asset" + }, + "item3": {} + } + } + } } - } } - } - } - }, - "pageB": { - "jcr:primaryType": "cq:Page", - "jcr:content": { - "jcr:primaryType": "cq:PageContent", - "magentoStore": "my-store" }, - "pageC": { - "jcr:primaryType": "cq:Page", - "jcr:content": { - "jcr:primaryType": "cq:PageContent" - } - } - }, - "pageD": { - "jcr:primaryType": "cq:Page", - "jcr:content": { - "jcr:primaryType": "cq:PageContent" - } - }, - "pageE": { - "jcr:primaryType": "cq:Page", - "jcr:content": { - "jcr:primaryType": "cq:PageContent", - "cq:magentoStore": "my-store" - } - }, - "pageF": { - "jcr:primaryType": "cq:Page", - "jcr:content": { - "jcr:primaryType": "cq:PageContent", - "magentoStore": "my-store", - "cq:magentoStore": "old-store" + "pageB": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:primaryType": "cq:PageContent", + "magentoStore": "my-store" + }, + "pageC": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:primaryType": "cq:PageContent" + } + } + }, + "pageD": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:primaryType": "cq:PageContent" + } + }, + "pageE": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:primaryType": "cq:PageContent", + "cq:magentoStore": "my-store" + } + }, + "pageF": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:primaryType": "cq:PageContent", + "magentoStore": "my-store", + "cq:magentoStore": "old-store" + } + }, + "pageG": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:primaryType": "cq:PageContent", + "cq:conf": "/conf/test-config" + } } - } } \ No newline at end of file From b7f64edd31048ad1a0603fc57ac4fe261a5f503f Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Wed, 29 Jan 2020 13:11:49 +0200 Subject: [PATCH 05/21] CIF-1202 - Update the CIF Core Components to use configurations from /conf Fix the MagentoGraphqlClientTest --- .../client/MagentoGraphqlClient.java | 11 ++- .../models/v1/product/ProductImpl.java | 1 - .../client/MagentoGraphqlClientTest.java | 87 +++++++++---------- .../models/v1/product/ProductImplTest.java | 6 +- .../src/test/resources/context/jcr-conf.json | 2 +- 5 files changed, 51 insertions(+), 56 deletions(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index 893a7bd46b..2204b88880 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -53,7 +53,6 @@ public class MagentoGraphqlClient { public static final String CONFIGURATION_NAME = "commerce/default"; - private GraphqlClient graphqlClient; private RequestOptions requestOptions; @@ -113,7 +112,7 @@ public GraphqlResponse execute(String query) { * Executes the given Magento query and returns the response. This method * uses the given httpMethod to fetch the data. * - * @param query The GraphQL query. + * @param query The GraphQL query. * @param httpMethod The HTTP method that will be used to fetch the data. * @return The GraphQL response. */ @@ -121,8 +120,8 @@ public GraphqlResponse execute(String query, HttpMethod httpMethod // We do not set the HTTP method in 'this.requestOptions' to avoid setting it as the new default RequestOptions options = new RequestOptions().withGson(requestOptions.getGson()) - .withHeaders(requestOptions.getHeaders()) - .withHttpMethod(httpMethod); + .withHeaders(requestOptions.getHeaders()) + .withHttpMethod(httpMethod); return graphqlClient.execute(new GraphqlRequest(query), Query.class, Error.class, options); } @@ -133,8 +132,8 @@ private String readFallBackConfiguration(Resource resource, String propertyName) String storeCode; Page page = resource.getResourceResolver() - .adaptTo(PageManager.class) - .getContainingPage(resource); + .adaptTo(PageManager.class) + .getContainingPage(resource); if (page != null) { properties = new HierarchyNodeInheritanceValueMap(page.getContentResource()); } else { diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java index 4d664098fa..2abd5b73a5 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java @@ -30,7 +30,6 @@ import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; -import org.apache.sling.caconfig.resource.ConfigurationResourceResolver; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; import org.apache.sling.models.annotations.injectorspecific.Self; diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java index 3977eb9be3..34baddee62 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java @@ -24,7 +24,6 @@ import org.apache.http.message.BasicHeader; import org.apache.sling.api.resource.Resource; import org.apache.sling.caconfig.ConfigurationBuilder; -import org.apache.sling.caconfig.ConfigurationResolver; import org.apache.sling.testing.mock.caconfig.ContextPlugins; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.resourceresolver.MockValueMap; @@ -47,55 +46,51 @@ import io.wcm.testing.mock.aem.junit.AemContextBuilder; public class MagentoGraphqlClientTest { - private static final Logger LOG = LoggerFactory.getLogger(MagentoGraphqlClientTest.class); + private static final Logger LOG = LoggerFactory.getLogger(MagentoGraphqlClientTest.class); private GraphqlClient graphqlClient; private ConfigurationBuilder mockConfigurationBuilder; @Rule public final AemContext context = new AemContextBuilder(ResourceResolverType.JCR_MOCK).plugin(ContextPlugins.CACONFIG) - .build(); + .beforeSetUp(context -> { + ConfigurationAdmin configurationAdmin = context.getService(ConfigurationAdmin.class); + Configuration serviceConfiguration = configurationAdmin.getConfiguration( + "org.apache.sling.caconfig.resource.impl.def.DefaultContextPathStrategy"); + + Dictionary props = new Hashtable<>(); + props.put("configRefResourceNames", new String[] { ".", "jcr:content" }); + props.put("configRefPropertyNames", "cq:conf"); + serviceConfiguration.update(props); + + serviceConfiguration = configurationAdmin.getConfiguration( + "org.apache.sling.caconfig.resource.impl.def.DefaultConfigurationResourceResolvingStrategy"); + props = new Hashtable<>(); + props.put("configPath", "/conf"); + serviceConfiguration.update(props); + + serviceConfiguration = configurationAdmin.getConfiguration("org.apache.sling.caconfig.impl.ConfigurationResolverImpl"); + props = new Hashtable<>(); + props.put("configBucketNames", new String[] { "settings" }); + serviceConfiguration.update(props); + }).build(); @Before public void setup() throws IOException { context.load() - .json("/context/jcr-content.json", "/content"); + .json("/context/jcr-content.json", "/content"); context.load() - .json("/context/jcr-conf.json", "/conf/test-config"); + .json("/context/jcr-conf.json", "/conf/test-config"); graphqlClient = Mockito.mock(GraphqlClient.class); Mockito.when(graphqlClient.execute(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(null); - - ConfigurationAdmin configurationAdmin = context.getService(ConfigurationAdmin.class); - - Configuration serviceConfiguration = configurationAdmin.getConfiguration("org.apache.sling.caconfig.resource.impl.def.DefaultContextPathStrategy"); - Dictionary props = new Hashtable<>(); - props.put("configRefResourceNames", new String[] { ".", "jcr:content" }); - props.put("configRefPropertyNames", "cq:conf"); - serviceConfiguration.update(props); - - serviceConfiguration = configurationAdmin.getConfiguration("org.apache.sling.caconfig.resource.impl.def.DefaultConfigurationResourceResolvingStrategy"); - props = new Hashtable<>(); - props.put("configPath", "/conf"); - serviceConfiguration.update(props); - - - serviceConfiguration = configurationAdmin.getConfiguration("org.apache.sling.caconfig.impl.ConfigurationResolverImpl"); - props = new Hashtable<>(); - props.put("configBucketNames", new String[] { "settings" }); - serviceConfiguration.update(props); + .thenReturn(null); } private void testMagentoStoreProperty(Resource resource, boolean withStoreHeader) { Mockito.when(resource.adaptTo(GraphqlClient.class)) - .thenReturn(graphqlClient); - Mockito.when(resource.adaptTo(ConfigurationBuilder.class)) - .thenReturn(mockConfigurationBuilder); - - Mockito.when(mockConfigurationBuilder.asValueMap()) - .thenReturn(MockValueMap.EMPTY); + .thenReturn(graphqlClient); MagentoGraphqlClient client = MagentoGraphqlClient.create(resource); executeAndCheck(withStoreHeader, client); @@ -108,22 +103,22 @@ private void executeAndCheck(boolean withStoreHeader, MagentoGraphqlClient clien List
headers = withStoreHeader ? Collections.singletonList(new BasicHeader("Store", "my-store")) : Collections.emptyList(); RequestOptionsMatcher matcher = new RequestOptionsMatcher(headers, null); Mockito.verify(graphqlClient) - .execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); + .execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); // Verify setting a custom HTTP method client.execute("{dummy}", HttpMethod.GET); matcher = new RequestOptionsMatcher(headers, HttpMethod.GET); Mockito.verify(graphqlClient) - .execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); + .execute(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.argThat(matcher)); } @Test public void testMagentoStorePropertyWithConfigBuilder() { Resource pageWithConfig = Mockito.spy(context.resourceResolver() - .getResource("/content/pageG")); + .getResource("/content/pageG")); Mockito.when(pageWithConfig.adaptTo(GraphqlClient.class)) - .thenReturn(graphqlClient); + .thenReturn(graphqlClient); MagentoGraphqlClient client = MagentoGraphqlClient.create(pageWithConfig); executeAndCheck(true, client); } @@ -132,7 +127,7 @@ public void testMagentoStorePropertyWithConfigBuilder() { public void testMagentoStoreProperty() { // Get page which has the magentoStore property in its jcr:content node Resource resource = Mockito.spy(context.resourceResolver() - .getResource("/content/pageA")); + .getResource("/content/pageA")); testMagentoStoreProperty(resource, true); } @@ -140,7 +135,7 @@ public void testMagentoStoreProperty() { public void testInheritedMagentoStoreProperty() { // Get page whose parent has the magentoStore property in its jcr:content node Resource resource = Mockito.spy(context.resourceResolver() - .getResource("/content/pageB/pageC")); + .getResource("/content/pageB/pageC")); testMagentoStoreProperty(resource, true); } @@ -148,7 +143,7 @@ public void testInheritedMagentoStoreProperty() { public void testMissingMagentoStoreProperty() { // Get page whose parent has the magentoStore property in its jcr:content node Resource resource = Mockito.spy(context.resourceResolver() - .getResource("/content/pageD")); + .getResource("/content/pageD")); testMagentoStoreProperty(resource, false); } @@ -156,7 +151,7 @@ public void testMissingMagentoStoreProperty() { public void testOldMagentoStoreProperty() { // Get page which has the old cq:magentoStore property in its jcr:content node Resource resource = Mockito.spy(context.resourceResolver() - .getResource("/content/pageE")); + .getResource("/content/pageE")); testMagentoStoreProperty(resource, true); } @@ -165,7 +160,7 @@ public void testNewMagentoStoreProperty() { // Get page which has both the new magentoStore property and old cq:magentoStore property // in its jcr:content node and make sure the new one is prefered Resource resource = Mockito.spy(context.resourceResolver() - .getResource("/content/pageF")); + .getResource("/content/pageF")); testMagentoStoreProperty(resource, true); } @@ -173,9 +168,9 @@ public void testNewMagentoStoreProperty() { public void testError() { // Get page which has the magentoStore property in its jcr:content node Resource resource = Mockito.spy(context.resourceResolver() - .getResource("/content/pageA")); + .getResource("/content/pageA")); Mockito.when(resource.adaptTo(GraphqlClient.class)) - .thenReturn(null); + .thenReturn(null); MagentoGraphqlClient client = MagentoGraphqlClient.create(resource); Assert.assertNull(client); @@ -211,10 +206,10 @@ public boolean matches(Object obj) { for (Header header : headers) { if (!requestOptions.getHeaders() - .stream() - .anyMatch(h -> h.getName() - .equals(header.getName()) && h.getValue() - .equals(header.getValue()))) { + .stream() + .anyMatch(h -> h.getName() + .equals(header.getName()) && h.getValue() + .equals(header.getValue()))) { return false; } } diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java index 6158f45472..b29e83480e 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java @@ -123,8 +123,10 @@ public void setUp() throws Exception { ConfigurationResourceResolver configurationResourceResolver = mock(ConfigurationResourceResolver.class); Resource mockConfigurationResource = mock(Resource.class); - when(mockConfigurationResource.getValueMap()).thenReturn(new ValueMapDecorator(ImmutableMap.of("cq:graphqlClient", "my-catalog"))); - when(configurationResourceResolver.getResource(any(Resource.class), any(String.class), any(String.class))).thenReturn(mockConfigurationResource); + when(mockConfigurationResource.getValueMap()).thenReturn(new ValueMapDecorator(ImmutableMap.of("cq:graphqlClient", + "my-catalog"))); + when(configurationResourceResolver.getResource(any(Resource.class), any(String.class), any(String.class))).thenReturn( + mockConfigurationResource); slingBindings.put("configurationResourceResolver", configurationResourceResolver); Style style = mock(Style.class); diff --git a/bundles/core/src/test/resources/context/jcr-conf.json b/bundles/core/src/test/resources/context/jcr-conf.json index 9aa33813ba..46a0ba9888 100644 --- a/bundles/core/src/test/resources/context/jcr-conf.json +++ b/bundles/core/src/test/resources/context/jcr-conf.json @@ -11,7 +11,7 @@ "default": { "jcr:primaryType": "nt:unstructured", "cq:graphqlClient": "default", - "magentoStore": "default" + "magentoStore": "my-store" } } } From 22cd4c46b20fca6d36def7efad247f4b66955c0a Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Wed, 29 Jan 2020 13:20:33 +0200 Subject: [PATCH 06/21] @trivial Fix formatting --- .../core/components/client/MagentoGraphqlClientTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java index 34baddee62..e4a5a993f0 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java @@ -26,7 +26,6 @@ import org.apache.sling.caconfig.ConfigurationBuilder; import org.apache.sling.testing.mock.caconfig.ContextPlugins; import org.apache.sling.testing.mock.sling.ResourceResolverType; -import org.apache.sling.testing.resourceresolver.MockValueMap; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -56,7 +55,7 @@ public class MagentoGraphqlClientTest { .beforeSetUp(context -> { ConfigurationAdmin configurationAdmin = context.getService(ConfigurationAdmin.class); Configuration serviceConfiguration = configurationAdmin.getConfiguration( - "org.apache.sling.caconfig.resource.impl.def.DefaultContextPathStrategy"); + "org.apache.sling.caconfig.resource.impl.def.DefaultContextPathStrategy"); Dictionary props = new Hashtable<>(); props.put("configRefResourceNames", new String[] { ".", "jcr:content" }); @@ -64,7 +63,7 @@ public class MagentoGraphqlClientTest { serviceConfiguration.update(props); serviceConfiguration = configurationAdmin.getConfiguration( - "org.apache.sling.caconfig.resource.impl.def.DefaultConfigurationResourceResolvingStrategy"); + "org.apache.sling.caconfig.resource.impl.def.DefaultConfigurationResourceResolvingStrategy"); props = new Hashtable<>(); props.put("configPath", "/conf"); serviceConfiguration.update(props); From 03a73443c5c1e0a0d562cfc4c657b68700b9095f Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Wed, 29 Jan 2020 17:08:20 +0200 Subject: [PATCH 07/21] Follow-up on the review: - optimize the "fallback solution" so that the fallback configuration is only read when the context configuration is missing --- .../core/components/client/MagentoGraphqlClient.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index 2204b88880..e024eb4c88 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -86,7 +86,10 @@ private MagentoGraphqlClient(Resource resource) { if (configBuilder != null) { ValueMap properties = configBuilder.name(CONFIGURATION_NAME).asValueMap(); - storeCode = properties.get(STORE_CODE_PROPERTY, readFallBackConfiguration(resource, STORE_CODE_PROPERTY)); + storeCode = properties.get(STORE_CODE_PROPERTY, String.class); + if (storeCode == null) { + storeCode = readFallBackConfiguration(resource, STORE_CODE_PROPERTY); + }; } else { storeCode = readFallBackConfiguration(resource, STORE_CODE_PROPERTY); } From 51d1234796213d8a0d93e3beb727c773aa2eb233 Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Wed, 29 Jan 2020 17:21:46 +0200 Subject: [PATCH 08/21] @trivial Fix formatting --- .../commerce/core/components/client/MagentoGraphqlClient.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index e024eb4c88..4f0d132972 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -89,7 +89,8 @@ private MagentoGraphqlClient(Resource resource) { storeCode = properties.get(STORE_CODE_PROPERTY, String.class); if (storeCode == null) { storeCode = readFallBackConfiguration(resource, STORE_CODE_PROPERTY); - }; + } + ; } else { storeCode = readFallBackConfiguration(resource, STORE_CODE_PROPERTY); } From 9f4f49842bd5e42023145eebdf99c2d16834917f Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Mon, 3 Feb 2020 15:14:17 +0200 Subject: [PATCH 09/21] CIF-1201 - Implement the configuration wizard Update the GraphqlClientDatasource to support not showing the "empty" option. --- bundles/core/pom.xml | 6 ++++++ .../GraphqlClientDataSourceServlet.java | 17 +++++++++++++++-- .../GraphqlClientDataSourceServletTest.java | 4 ++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/bundles/core/pom.xml b/bundles/core/pom.xml index ccaade604c..9d6d77d6f8 100644 --- a/bundles/core/pom.xml +++ b/bundles/core/pom.xml @@ -321,6 +321,12 @@ javax.servlet javax.servlet-api + + javax.servlet + jsp-api + 2.0 + provided + javax.jcr jcr diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServlet.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServlet.java index fee566ede7..0ab08f9c7d 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServlet.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServlet.java @@ -39,6 +39,9 @@ import org.osgi.service.component.annotations.ReferencePolicy; import com.adobe.cq.commerce.graphql.client.GraphqlClient; +import com.adobe.granite.ui.components.Config; +import com.adobe.granite.ui.components.ExpressionHelper; +import com.adobe.granite.ui.components.ExpressionResolver; import com.adobe.granite.ui.components.ds.DataSource; import com.adobe.granite.ui.components.ds.SimpleDataSource; import com.day.cq.i18n.I18n; @@ -58,6 +61,9 @@ public class GraphqlClientDataSourceServlet extends SlingSafeMethodsServlet { private Set identifiers = new ConcurrentHashSet<>(); + @Reference + private ExpressionResolver expressionResolver; + @Reference( service = GraphqlClient.class, bind = "bindGraphqlClient", @@ -83,9 +89,16 @@ List getGraphqlClients(@Nonnull SlingHttpServletRequest request) { ResourceResolver resolver = request.getResourceResolver(); List graphqlClients = new ArrayList<>(); + final Config cfg = new Config(request.getResource().getChild(Config.DATASOURCE)); + boolean showEmptyOption = false; + if (cfg != null) { + ExpressionHelper expressionHelper = new ExpressionHelper(expressionResolver, request); + showEmptyOption = expressionHelper.getBoolean(cfg.get("showEmptyOption")); + } // Add empty option - graphqlClients.add(new GraphqlClientResource(i18n.get("Inherit", "Inherit property"), StringUtils.EMPTY, resolver)); - + if (!showEmptyOption) { + graphqlClients.add(new GraphqlClientResource(i18n.get("Inherit", "Inherit property"), StringUtils.EMPTY, resolver)); + } // Add other configurations for (String identifier : identifiers) { graphqlClients.add(new GraphqlClientResource(identifier, identifier, resolver)); diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java index 90c2facb8c..ef39ef7924 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java @@ -34,6 +34,7 @@ import org.osgi.framework.InvalidSyntaxException; import com.adobe.cq.commerce.graphql.client.GraphqlClient; +import com.adobe.granite.ui.components.ExpressionResolver; import com.adobe.granite.ui.components.ds.DataSource; import com.day.cq.i18n.I18n; import io.wcm.testing.mock.aem.junit.AemContext; @@ -53,6 +54,9 @@ public class GraphqlClientDataSourceServletTest { @Before public void setUp() { servlet = new GraphqlClientDataSourceServlet(); + ExpressionResolver expressionResolver = Mockito.mock(ExpressionResolver.class); + context.registerService(ExpressionResolver.class, expressionResolver); + context.registerInjectActivateService(servlet); } @Test From 707467ce8cf8441515bff276c3016f8e2d33e089 Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Tue, 4 Feb 2020 11:52:38 +0200 Subject: [PATCH 10/21] CIF-1201 - Implement the configuration wizard Update unit tests for the GraphQL clients datasource --- .../GraphqlClientDataSourceServlet.java | 2 +- .../GraphqlClientDataSourceServletTest.java | 58 ++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServlet.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServlet.java index 0ab08f9c7d..7e3df42186 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServlet.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServlet.java @@ -96,7 +96,7 @@ List getGraphqlClients(@Nonnull SlingHttpServletRequest request) { showEmptyOption = expressionHelper.getBoolean(cfg.get("showEmptyOption")); } // Add empty option - if (!showEmptyOption) { + if (showEmptyOption) { graphqlClients.add(new GraphqlClientResource(i18n.get("Inherit", "Inherit property"), StringUtils.EMPTY, resolver)); } // Add other configurations diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java index ef39ef7924..0d31d4d28c 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java @@ -18,13 +18,18 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; +import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.testing.resourceresolver.MockResource; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -34,6 +39,7 @@ import org.osgi.framework.InvalidSyntaxException; import com.adobe.cq.commerce.graphql.client.GraphqlClient; +import com.adobe.granite.ui.components.Config; import com.adobe.granite.ui.components.ExpressionResolver; import com.adobe.granite.ui.components.ds.DataSource; import com.day.cq.i18n.I18n; @@ -51,12 +57,15 @@ public class GraphqlClientDataSourceServletTest { private GraphqlClientDataSourceServlet servlet; + private ExpressionResolver mockExpressionResolver; + @Before public void setUp() { servlet = new GraphqlClientDataSourceServlet(); - ExpressionResolver expressionResolver = Mockito.mock(ExpressionResolver.class); - context.registerService(ExpressionResolver.class, expressionResolver); + mockExpressionResolver = Mockito.mock(ExpressionResolver.class); + context.registerService(ExpressionResolver.class, mockExpressionResolver); context.registerInjectActivateService(servlet); + } @Test @@ -97,6 +106,16 @@ public void testGetGraphqlClients() throws IOException, InvalidSyntaxException { identifiers.add("my-identifier"); Whitebox.setInternalState(servlet, "identifiers", identifiers); + Resource mockResource = Mockito.mock(Resource.class); + Map datasourceProps = new HashMap(); + datasourceProps.put("showEmptyOption", true); + Resource datasourceDefinition = new MockResource("/some/random/path", datasourceProps, context.resourceResolver()); + Mockito.when(mockResource.getChild(Config.DATASOURCE)).thenReturn(datasourceDefinition); + + Mockito.when(mockExpressionResolver.resolve(Mockito.any(String.class), Mockito.any(Locale.class), Mockito.any(Class.class), Mockito.any(SlingHttpServletRequest.class))).thenReturn(true); + + context.request().setResource(mockResource); + // Call method List resources = servlet.getGraphqlClients(context.request()); @@ -118,6 +137,41 @@ public void testGetGraphqlClients() throws IOException, InvalidSyntaxException { Assert.assertFalse(client.getSelected()); } + @Test + public void testGetGraphqlClientsNoEmptyOption() throws IOException, InvalidSyntaxException { + // Stub i18n + I18n i18n = mock(I18n.class); + Mockito.doReturn("inherit-translated").when(i18n).get(eq("Inherit"), any()); + Whitebox.setInternalState(servlet, "i18n", i18n); + + // Add fake identifiers + Set identifiers = new HashSet<>(); + identifiers.add("my-identifier"); + Whitebox.setInternalState(servlet, "identifiers", identifiers); + + Resource mockResource = Mockito.mock(Resource.class); + Map datasourceProps = new HashMap(); + datasourceProps.put("showEmptyOption", false); + Resource datasourceDefinition = new MockResource("/some/random/path", datasourceProps, context.resourceResolver()); + + Mockito.when(mockResource.getChild(Config.DATASOURCE)).thenReturn(datasourceDefinition); + + context.request().setResource(mockResource); + + // Call method + List resources = servlet.getGraphqlClients(context.request()); + + // Verify list + Assert.assertEquals(1, resources.size()); + + // Verify actual client + GraphqlClientDataSourceServlet.GraphqlClientResource client = (GraphqlClientDataSourceServlet.GraphqlClientResource) resources.get( + 0); + Assert.assertEquals("my-identifier", client.getText()); + Assert.assertEquals("my-identifier", client.getValue()); + Assert.assertFalse(client.getSelected()); + } + @Test public void testGraphqlClientResourceAdapt() { // Create resource From 1fcaf3b47a9d11cb92ff64c47a47c4b133bd426d Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Tue, 4 Feb 2020 13:42:40 +0200 Subject: [PATCH 11/21] @trivial Fix formatting --- .../internal/servlets/GraphqlClientDataSourceServletTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java index 0d31d4d28c..fce2cee3cc 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/GraphqlClientDataSourceServletTest.java @@ -112,7 +112,8 @@ public void testGetGraphqlClients() throws IOException, InvalidSyntaxException { Resource datasourceDefinition = new MockResource("/some/random/path", datasourceProps, context.resourceResolver()); Mockito.when(mockResource.getChild(Config.DATASOURCE)).thenReturn(datasourceDefinition); - Mockito.when(mockExpressionResolver.resolve(Mockito.any(String.class), Mockito.any(Locale.class), Mockito.any(Class.class), Mockito.any(SlingHttpServletRequest.class))).thenReturn(true); + Mockito.when(mockExpressionResolver.resolve(Mockito.any(String.class), Mockito.any(Locale.class), Mockito.any(Class.class), Mockito + .any(SlingHttpServletRequest.class))).thenReturn(true); context.request().setResource(mockResource); From 3245d9939801b8c2f303fb4f53b4da5b6d58a146 Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Thu, 6 Feb 2020 15:35:18 +0200 Subject: [PATCH 12/21] Read the configuration from "cloudconfigs/commerce" instead of "commerce/default" --- .../commerce/core/components/client/MagentoGraphqlClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index 4f0d132972..a8aa967e82 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -51,7 +51,7 @@ public class MagentoGraphqlClient { public static final String STORE_CODE_PROPERTY = "magentoStore"; - public static final String CONFIGURATION_NAME = "commerce/default"; + public static final String CONFIGURATION_NAME = "cloudconfigs/commerce"; private GraphqlClient graphqlClient; From 7c26ef7daae9bc480836bdae069394218e001e8e Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Fri, 7 Feb 2020 18:26:48 +0200 Subject: [PATCH 13/21] Update test content to work in the testing environment. --- .../components/client/MagentoGraphqlClientTest.java | 9 ++++++++- .../core/src/test/resources/context/jcr-conf.json | 12 ++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java index e4a5a993f0..48b9813ff2 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClientTest.java @@ -113,7 +113,14 @@ private void executeAndCheck(boolean withStoreHeader, MagentoGraphqlClient clien @Test public void testMagentoStorePropertyWithConfigBuilder() { - + /* + * The content for this test looks slightly different than it does in AEM: + * In AEM there the tree structure is /conf//settings/cloudconfigs/commerce/jcr:content + * In our test content it's /conf//settings/cloudconfigs/commerce + * The reason is that AEM has a specific CaConfig API implementation that reads the configuration + * data from the jcr:content node of the configuration page, something which we cannot reproduce in + * a unit test scenario. + */ Resource pageWithConfig = Mockito.spy(context.resourceResolver() .getResource("/content/pageG")); Mockito.when(pageWithConfig.adaptTo(GraphqlClient.class)) diff --git a/bundles/core/src/test/resources/context/jcr-conf.json b/bundles/core/src/test/resources/context/jcr-conf.json index 46a0ba9888..e13ecfd803 100644 --- a/bundles/core/src/test/resources/context/jcr-conf.json +++ b/bundles/core/src/test/resources/context/jcr-conf.json @@ -1,18 +1,18 @@ { "settings": { "jcr:primaryType": "sling:Folder", + "cloudconfigs": { + "jcr:primaryType": "sling:Folder", "commerce": { "jcr:primaryType": "cq:Page", "jcr:mixinTypes": [ "rep:AccessControllable" ], + "cq:graphqlClient": "default", + "magentoStore": "my-store", "jcr:createdBy": "admin", - "jcr:created": "Thu Jan 16 2020 12:20:34 GMT+0200", - "default": { - "jcr:primaryType": "nt:unstructured", - "cq:graphqlClient": "default", - "magentoStore": "my-store" - } + "jcr:created": "Thu Jan 16 2020 12:20:34 GMT+0200" + } } } } \ No newline at end of file From 51433cff660072ddb1802e04771e91bd117f7988 Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Mon, 17 Feb 2020 18:23:11 +0200 Subject: [PATCH 14/21] Fix the test content after a faulty merge. --- bundles/core/src/test/resources/context/jcr-content.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bundles/core/src/test/resources/context/jcr-content.json b/bundles/core/src/test/resources/context/jcr-content.json index 64f65cb72f..825904df2e 100644 --- a/bundles/core/src/test/resources/context/jcr-content.json +++ b/bundles/core/src/test/resources/context/jcr-content.json @@ -135,6 +135,14 @@ "cq:magentoStore": "old-store" } }, + "pageG": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:primaryType": "cq:PageContent", + "magentoStore": "my-store", + "cq:magentoStore": "old-store" + } + }, "product-page": { "jcr:primaryType": "cq:Page", "jcr:content": { From de1dd154f6de9c9700e2a9807e755a7022f7b56b Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Mon, 17 Feb 2020 18:25:21 +0200 Subject: [PATCH 15/21] @trivial Remove extra line --- .../cq/commerce/core/components/client/MagentoGraphqlClient.java | 1 - 1 file changed, 1 deletion(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java index a8aa967e82..1f2e0fb73f 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java @@ -90,7 +90,6 @@ private MagentoGraphqlClient(Resource resource) { if (storeCode == null) { storeCode = readFallBackConfiguration(resource, STORE_CODE_PROPERTY); } - ; } else { storeCode = readFallBackConfiguration(resource, STORE_CODE_PROPERTY); } From 603eaf9fbfb546102d32ecd0b686f14e8c87a562 Mon Sep 17 00:00:00 2001 From: Christophe Jelger Date: Wed, 26 Feb 2020 11:27:44 +0100 Subject: [PATCH 16/21] CIF-1134 - Unified configuration of site settings - fix git merge issue --- .../components/internal/models/v1/product/ProductImplTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java index 54b8af258d..c41c907af3 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java @@ -60,6 +60,7 @@ import io.wcm.testing.mock.aem.junit.AemContext; import io.wcm.testing.mock.aem.junit.AemContextCallback; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; From ceff093b997fce14785473fd67cdaf769ecc6d91 Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Wed, 26 Feb 2020 15:49:37 +0200 Subject: [PATCH 17/21] Fix the navigation to use the context-aware configurations. Remove the "Commerce" tab from the page configuration dialog since it's not needed anymore. --- .../models/v1/navigation/NavigationImpl.java | 7 +-- .../v1/navigation/NavigationImplTest.java | 11 +++- .../page/v1/page/_cq_dialog/.content.xml | 55 ------------------- 3 files changed, 13 insertions(+), 60 deletions(-) delete mode 100644 ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/page/v1/page/_cq_dialog/.content.xml diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java index 0a268c92cd..41b077f08d 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java @@ -25,6 +25,7 @@ import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.caconfig.ConfigurationBuilder; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.Via; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; @@ -36,8 +37,6 @@ import com.adobe.cq.commerce.core.components.models.navigation.NavigationItem; import com.adobe.cq.commerce.core.components.utils.SiteNavigation; import com.adobe.cq.commerce.magento.graphql.CategoryTree; -import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap; -import com.day.cq.commons.inherit.InheritanceValueMap; import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; import com.day.cq.wcm.api.designer.Style; @@ -160,8 +159,8 @@ private void expandCatalogRoot(Page catalogPage, List pages) { return; } - final InheritanceValueMap catalogPageProperties = new HierarchyNodeInheritanceValueMap(catalogPage.getContentResource()); - Integer rootCategoryId = catalogPageProperties.getInherited(PN_MAGENTO_ROOT_CATEGORY_ID, Integer.class); + ValueMap configurationProperties = catalogPage.adaptTo(ConfigurationBuilder.class).name("cloudconfigs/commerce").asValueMap(); + Integer rootCategoryId = configurationProperties.get(PN_MAGENTO_ROOT_CATEGORY_ID, Integer.class); if (rootCategoryId == null) { LOGGER.warn("Magento root category ID property (" + PN_MAGENTO_ROOT_CATEGORY_ID + ") not found"); return; diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java index 5d76489735..9f0a1a5e7e 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java @@ -22,7 +22,9 @@ import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.SyntheticResource; +import org.apache.sling.api.resource.ValueMap; import org.apache.sling.api.wrappers.ValueMapDecorator; +import org.apache.sling.caconfig.ConfigurationBuilder; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,6 +37,7 @@ import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; import com.day.cq.wcm.api.designer.Style; +import com.google.common.collect.ImmutableMap; import static com.adobe.cq.commerce.core.components.internal.models.v1.navigation.NavigationImpl.DEFAULT_STRUCTURE_DEPTH; import static com.adobe.cq.commerce.core.components.internal.models.v1.navigation.NavigationImpl.MAX_STRUCTURE_DEPTH; @@ -531,9 +534,15 @@ private void initCatalogPage(boolean catalogRoot, boolean showMainCategories) { when(catalogPageContent.isResourceType(RT_CATALOG_PAGE)).thenReturn(catalogRoot); Map catalogPageProperties = new HashMap<>(); catalogPageProperties.put(PN_SHOW_MAIN_CATEGORIES, showMainCategories); - catalogPageProperties.put(PN_MAGENTO_ROOT_CATEGORY_ID, 4); when(catalogPageContent.getValueMap()).thenReturn(new ValueMapDecorator(catalogPageProperties)); when(catalogPage.getContentResource()).thenReturn(catalogPageContent); when(pageManager.getPage(CATALOG_PAGE_PATH)).thenReturn(catalogPage); + + ValueMap configProperties = new ValueMapDecorator(ImmutableMap.of(PN_MAGENTO_ROOT_CATEGORY_ID, 4)); + ConfigurationBuilder mockConfigBuilder = mock(ConfigurationBuilder.class); + when(mockConfigBuilder.name(any(String.class))).thenReturn(mockConfigBuilder); + when(mockConfigBuilder.asValueMap()).thenReturn(configProperties); + + when(catalogPage.adaptTo(ConfigurationBuilder.class)).thenReturn(mockConfigBuilder); } } diff --git a/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/page/v1/page/_cq_dialog/.content.xml b/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/page/v1/page/_cq_dialog/.content.xml deleted file mode 100644 index de66309b1d..0000000000 --- a/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/page/v1/page/_cq_dialog/.content.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - From bfbd3b268200e331ae97bec9557a4be613b61f1b Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Wed, 26 Feb 2020 16:08:42 +0200 Subject: [PATCH 18/21] Update the navigation to fall back to reading the configuration from the page properties if the property is missing in /conf --- .../models/v1/navigation/NavigationImpl.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java index 41b077f08d..8a5e19281b 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java @@ -37,6 +37,8 @@ import com.adobe.cq.commerce.core.components.models.navigation.NavigationItem; import com.adobe.cq.commerce.core.components.utils.SiteNavigation; import com.adobe.cq.commerce.magento.graphql.CategoryTree; +import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap; +import com.day.cq.commons.inherit.InheritanceValueMap; import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; import com.day.cq.wcm.api.designer.Style; @@ -159,8 +161,19 @@ private void expandCatalogRoot(Page catalogPage, List pages) { return; } - ValueMap configurationProperties = catalogPage.adaptTo(ConfigurationBuilder.class).name("cloudconfigs/commerce").asValueMap(); - Integer rootCategoryId = configurationProperties.get(PN_MAGENTO_ROOT_CATEGORY_ID, Integer.class); + Integer rootCategoryId; + ConfigurationBuilder configBuilder = catalogPage.adaptTo(ConfigurationBuilder.class); + + if (configBuilder != null) { + ValueMap properties = configBuilder.name("cloudconfigs/commerce").asValueMap(); + rootCategoryId = properties.get(PN_MAGENTO_ROOT_CATEGORY_ID, Integer.class); + if (rootCategoryId == null) { + rootCategoryId = readFallBackConfiguration(catalogPage, PN_MAGENTO_ROOT_CATEGORY_ID); + } + } else { + rootCategoryId = readFallBackConfiguration(catalogPage, PN_MAGENTO_ROOT_CATEGORY_ID); + } + if (rootCategoryId == null) { LOGGER.warn("Magento root category ID property (" + PN_MAGENTO_ROOT_CATEGORY_ID + ") not found"); return; @@ -195,6 +208,11 @@ public String getParentId() { return null; } + private Integer readFallBackConfiguration(Page page, String propertyName) { + InheritanceValueMap properties = new HierarchyNodeInheritanceValueMap(page.getContentResource()); + return properties.getInherited(propertyName, Integer.class); + } + class PageNavigationItem extends AbstractNavigationItem { private final com.adobe.cq.wcm.core.components.models.NavigationItem wcmItem; From 0169770a0f6d4e57292c6ab432fda3aa0b806695 Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Thu, 27 Feb 2020 18:39:33 +0200 Subject: [PATCH 19/21] Update the navigation to fall back to reading the configuration from `/conf` only when the `magentoRootCategoryId` is not found in the page properties. Remove the "Store config" property from the catalog page properties --- .../models/v1/navigation/NavigationImpl.java | 19 ++++++++----------- .../v1/navigation/NavigationImplTest.java | 1 + .../v1/catalogpage/_cq_dialog/.content.xml | 9 --------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java index 8a5e19281b..0917d69f08 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java @@ -161,17 +161,14 @@ private void expandCatalogRoot(Page catalogPage, List pages) { return; } - Integer rootCategoryId; - ConfigurationBuilder configBuilder = catalogPage.adaptTo(ConfigurationBuilder.class); - - if (configBuilder != null) { - ValueMap properties = configBuilder.name("cloudconfigs/commerce").asValueMap(); - rootCategoryId = properties.get(PN_MAGENTO_ROOT_CATEGORY_ID, Integer.class); - if (rootCategoryId == null) { - rootCategoryId = readFallBackConfiguration(catalogPage, PN_MAGENTO_ROOT_CATEGORY_ID); + Integer rootCategoryId = readPageConfiguration(catalogPage, PN_MAGENTO_ROOT_CATEGORY_ID); + if (rootCategoryId == null) { + ConfigurationBuilder configBuilder = catalogPage.adaptTo(ConfigurationBuilder.class); + + if (configBuilder != null) { + ValueMap properties = configBuilder.name("cloudconfigs/commerce").asValueMap(); + rootCategoryId = properties.get(PN_MAGENTO_ROOT_CATEGORY_ID, Integer.class); } - } else { - rootCategoryId = readFallBackConfiguration(catalogPage, PN_MAGENTO_ROOT_CATEGORY_ID); } if (rootCategoryId == null) { @@ -208,7 +205,7 @@ public String getParentId() { return null; } - private Integer readFallBackConfiguration(Page page, String propertyName) { + private Integer readPageConfiguration(Page page, String propertyName) { InheritanceValueMap properties = new HierarchyNodeInheritanceValueMap(page.getContentResource()); return properties.getInherited(propertyName, Integer.class); } diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java index 9f0a1a5e7e..338c53101e 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java @@ -534,6 +534,7 @@ private void initCatalogPage(boolean catalogRoot, boolean showMainCategories) { when(catalogPageContent.isResourceType(RT_CATALOG_PAGE)).thenReturn(catalogRoot); Map catalogPageProperties = new HashMap<>(); catalogPageProperties.put(PN_SHOW_MAIN_CATEGORIES, showMainCategories); + catalogPageProperties.put(PN_MAGENTO_ROOT_CATEGORY_ID, 4); when(catalogPageContent.getValueMap()).thenReturn(new ValueMapDecorator(catalogPageProperties)); when(catalogPage.getContentResource()).thenReturn(catalogPageContent); when(pageManager.getPage(CATALOG_PAGE_PATH)).thenReturn(catalogPage); diff --git a/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/catalogpage/v1/catalogpage/_cq_dialog/.content.xml b/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/catalogpage/v1/catalogpage/_cq_dialog/.content.xml index 77b7e110fe..428276ccca 100644 --- a/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/catalogpage/v1/catalogpage/_cq_dialog/.content.xml +++ b/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/catalogpage/v1/catalogpage/_cq_dialog/.content.xml @@ -62,15 +62,6 @@ jcr:title="Magento Store Configuration" sling:resourceType="granite/ui/components/coral/foundation/form/fieldset"> - - - Date: Thu, 12 Mar 2020 15:02:24 +0200 Subject: [PATCH 20/21] Update the unit test to improve the converage of the `NavigationImpl` class --- .../v1/navigation/NavigationImplTest.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java index 338c53101e..51feff7ab6 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java @@ -19,8 +19,10 @@ import java.util.List; import java.util.Map; +import org.apache.sling.api.SlingConstants; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.SyntheticResource; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.api.wrappers.ValueMapDecorator; @@ -70,11 +72,13 @@ public void init() { Page currentPage = mock(Page.class); pageManager = mock(PageManager.class); when(currentPage.getPageManager()).thenReturn(pageManager); + when(currentPage.getPath()).thenReturn("/content/currentPage"); Resource currentPageContent = mock(Resource.class); Map currentPageProperties = new HashMap<>(); currentPageProperties.put("cq:cifCategoryPage", CATEGORY_PAGE_PATH); Page categoryPage = mock(Page.class); when(categoryPage.getPath()).thenReturn(CATEGORY_PAGE_PATH); + when(currentPageContent.getPath()).thenReturn(CATEGORY_PAGE_PATH + "/jcr:content"); when(pageManager.getPage(CATEGORY_PAGE_PATH)).thenReturn(categoryPage); when(currentPageContent.getValueMap()).thenReturn(new ValueMapDecorator(currentPageProperties)); when(currentPage.getContentResource()).thenReturn(currentPageContent); @@ -173,7 +177,7 @@ public void testEmptyNavigationNoPagesNoCategories() { public void testNavigationCatalogRootDisabled() { String catalogTitle = "Catalog 1"; - initCatalogPage(false, false); + initCatalogPage(false, false, false); NavigationItem catalogItem = mock(NavigationItem.class); when(catalogItem.getPath()).thenReturn(CATALOG_PAGE_PATH); @@ -187,7 +191,7 @@ public void testNavigationCatalogRootDisabled() { public void testNavigationCategoriesDisabled() { String catalogTitle = "Catalog 1"; - initCatalogPage(true, false); + initCatalogPage(true, false, false); NavigationItem catalogItem = mock(NavigationItem.class); when(catalogItem.getPath()).thenReturn(CATALOG_PAGE_PATH); @@ -339,7 +343,7 @@ public void testNavigationCategoriesOnly() { Integer categoryId = 0; String categoryName = "Category 1"; - initCatalogPage(true, true); + initCatalogPage(true, true, false); NavigationItem item = mock(NavigationItem.class); when(item.getPath()).thenReturn(CATALOG_PAGE_PATH); @@ -371,7 +375,7 @@ public void testNavigationCategoryHierarchy() { Integer categoryId = 0; String categoryName = "Category 1"; - initCatalogPage(true, true); + initCatalogPage(true, true, true); NavigationItem item = mock(NavigationItem.class); when(item.getPath()).thenReturn(CATALOG_PAGE_PATH); @@ -422,7 +426,7 @@ public void testNavigationCategoryHierarchySelection() { Integer categoryId = 0; String categoryName = "Category 1"; - initCatalogPage(true, true); + initCatalogPage(true, true, false ); NavigationItem item = mock(NavigationItem.class); when(item.getPath()).thenReturn(CATALOG_PAGE_PATH); @@ -491,7 +495,7 @@ private void testNavigationItemOrdering(boolean pageBeforeCategory) { String pageTitle = "Page 1"; String categoryTitle = "Category 1"; - initCatalogPage(true, true); + initCatalogPage(true, true, false); NavigationItem pageItem = mock(NavigationItem.class); when(pageItem.getTitle()).thenReturn(pageTitle); @@ -528,15 +532,23 @@ private void testNavigationItemOrdering(boolean pageBeforeCategory) { Assert.assertEquals(navigation, navigationList.get(0)); } - private void initCatalogPage(boolean catalogRoot, boolean showMainCategories) { + private void initCatalogPage(boolean catalogRoot, boolean showMainCategories, boolean useCaConfig) { Page catalogPage = mock(Page.class); Resource catalogPageContent = mock(Resource.class); when(catalogPageContent.isResourceType(RT_CATALOG_PAGE)).thenReturn(catalogRoot); Map catalogPageProperties = new HashMap<>(); catalogPageProperties.put(PN_SHOW_MAIN_CATEGORIES, showMainCategories); - catalogPageProperties.put(PN_MAGENTO_ROOT_CATEGORY_ID, 4); + + if (!useCaConfig) { + catalogPageProperties.put(PN_MAGENTO_ROOT_CATEGORY_ID, 4); + } when(catalogPageContent.getValueMap()).thenReturn(new ValueMapDecorator(catalogPageProperties)); when(catalogPage.getContentResource()).thenReturn(catalogPageContent); + when(catalogPage.getPath()).thenReturn("/content/catalog"); + when(catalogPageContent.getPath()).thenReturn("/content/catalog/jcr:content"); + ResourceResolver mockResourceResolver = mock(ResourceResolver.class); + when(mockResourceResolver.getResource(any(String.class))).thenReturn(null); + when(catalogPageContent.getResourceResolver()).thenReturn(mockResourceResolver); when(pageManager.getPage(CATALOG_PAGE_PATH)).thenReturn(catalogPage); ValueMap configProperties = new ValueMapDecorator(ImmutableMap.of(PN_MAGENTO_ROOT_CATEGORY_ID, 4)); From 58604549455b8c1609096350e799901536374e9c Mon Sep 17 00:00:00 2001 From: Daniel Platon Date: Thu, 12 Mar 2020 15:08:49 +0200 Subject: [PATCH 21/21] @trivial Fix formatting --- .../internal/models/v1/navigation/NavigationImplTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java index 51feff7ab6..fcdf157960 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java @@ -19,7 +19,6 @@ import java.util.List; import java.util.Map; -import org.apache.sling.api.SlingConstants; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; @@ -426,7 +425,7 @@ public void testNavigationCategoryHierarchySelection() { Integer categoryId = 0; String categoryName = "Category 1"; - initCatalogPage(true, true, false ); + initCatalogPage(true, true, false); NavigationItem item = mock(NavigationItem.class); when(item.getPath()).thenReturn(CATALOG_PAGE_PATH); @@ -538,7 +537,7 @@ private void initCatalogPage(boolean catalogRoot, boolean showMainCategories, bo when(catalogPageContent.isResourceType(RT_CATALOG_PAGE)).thenReturn(catalogRoot); Map catalogPageProperties = new HashMap<>(); catalogPageProperties.put(PN_SHOW_MAIN_CATEGORIES, showMainCategories); - + if (!useCaConfig) { catalogPageProperties.put(PN_MAGENTO_ROOT_CATEGORY_ID, 4); }