Instantly share code, notes, and snippets.
Created
October 20, 2021 17:22
-
Save dfparker2002/fbc41e658ae688b664b70b9d79521693 to your computer and use it in GitHub Desktop.
Sample AEM test with mocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
src: https://raw.githubusercontent.com/Adobe-Consulting-Services/acs-aem-commons/f3f6e9e164edcfb7abf16063e9cd5c3691388669/bundle/src/test/java/com/adobe/acs/commons/wcm/properties/shared/impl/SharedComponentPropertiesBindingsValuesProviderTest.java | |
/* | |
* #%L | |
* ACS AEM Commons Bundle | |
* %% | |
* Copyright (C) 2017 Adobe | |
* %% | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* #L% | |
*/ | |
package com.adobe.acs.commons.wcm.properties.shared.impl; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNull; | |
import static org.mockito.ArgumentMatchers.anyString; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.when; | |
import com.adobe.acs.commons.wcm.PageRootProvider; | |
import com.adobe.acs.commons.wcm.properties.shared.SharedComponentProperties; | |
import com.day.cq.wcm.api.Page; | |
import com.day.cq.wcm.api.components.Component; | |
import com.day.cq.wcm.api.components.ComponentManager; | |
import org.apache.sling.api.resource.Resource; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.api.resource.ValueMap; | |
import org.apache.sling.api.wrappers.ValueMapDecorator; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.junit.MockitoJUnitRunner; | |
import org.powermock.reflect.Whitebox; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.function.BiFunction; | |
import java.util.function.Function; | |
import javax.script.Bindings; | |
import javax.script.SimpleBindings; | |
@RunWith(MockitoJUnitRunner.class) | |
public class SharedComponentPropertiesBindingsValuesProviderTest { | |
public static final String SITE_ROOT = "/content/acs-commons"; | |
public static final String RESOURCE_TYPE = "acs-commons/components/content/generic-text"; | |
private PageRootProvider pageRootProvider; | |
private Resource resource; | |
private Resource sharedPropsResource; | |
private Resource globalPropsResource; | |
private Page page; | |
private Bindings bindings; | |
private ResourceResolver resourceResolver; | |
private ValueMap sharedProps; | |
private ValueMap globalProps; | |
@Before | |
public void setUp() throws Exception { | |
resource = mock(Resource.class); | |
pageRootProvider = mock(PageRootProvider.class); | |
page = mock(Page.class); | |
bindings = new SimpleBindings(); | |
sharedPropsResource = mock(Resource.class); | |
globalPropsResource = mock(Resource.class); | |
resourceResolver = mock(ResourceResolver.class); | |
final String globalPropsPath = SITE_ROOT + "/jcr:content/" + SharedComponentProperties.NN_GLOBAL_COMPONENT_PROPERTIES; | |
final String sharedPropsPath = SITE_ROOT + "/jcr:content/" + SharedComponentProperties.NN_SHARED_COMPONENT_PROPERTIES + "/" | |
+ RESOURCE_TYPE; | |
bindings.put("resource", resource); | |
when(resource.getResourceResolver()).thenReturn(resourceResolver); | |
when(resource.getResourceType()).thenReturn(RESOURCE_TYPE); | |
when(resourceResolver.getSearchPath()).thenReturn(new String[]{"/apps/", "/libs/"}); | |
when(resourceResolver.getResource(sharedPropsPath)).thenReturn(sharedPropsResource); | |
when(resourceResolver.getResource(globalPropsPath)).thenReturn(globalPropsResource); | |
when(page.getPath()).thenReturn(SITE_ROOT); | |
when(pageRootProvider.getRootPage(resource)).thenReturn(page); | |
when(resource.getPath()).thenReturn(SITE_ROOT); | |
when(pageRootProvider.getRootPagePath(anyString())).thenReturn(SITE_ROOT); | |
sharedProps = new ValueMapDecorator(new HashMap<String, Object>()); | |
globalProps = new ValueMapDecorator(new HashMap<String, Object>()); | |
sharedProps.put("shared", "value"); | |
globalProps.put("global", "value"); | |
when(globalPropsResource.getValueMap()).thenReturn(globalProps); | |
when(sharedPropsResource.getValueMap()).thenReturn(sharedProps); | |
} | |
@Test | |
public void testGetCanonicalResourceTypeRelativePath() { | |
// make this test readable by wrapping the long method name with a function | |
final BiFunction<String, List<String>, String> asFunction = | |
(resourceType, searchPaths) -> SharedComponentPropertiesBindingsValuesProvider | |
.getCanonicalResourceTypeRelativePath(resourceType, | |
Optional.ofNullable(searchPaths) | |
.map(list -> list.toArray(new String[0])).orElse(null)); | |
final List<String> emptySearchPaths = Collections.emptyList(); | |
final List<String> realSearchPaths = Arrays.asList("/apps/", "/libs/"); | |
assertNull("expect null for null rt", asFunction.apply(null, emptySearchPaths)); | |
assertNull("expect null for empty rt", asFunction.apply("", emptySearchPaths)); | |
assertNull("expect null for absolute rt and null search paths", | |
asFunction.apply("/fail/" + RESOURCE_TYPE, null)); | |
assertNull("expect null for cq:Page", | |
asFunction.apply("cq:Page", realSearchPaths)); | |
assertNull("expect null for nt:unstructured", | |
asFunction.apply("nt:unstructured", realSearchPaths)); | |
assertNull("expect null for absolute rt and empty search paths", | |
asFunction.apply("/fail/" + RESOURCE_TYPE, emptySearchPaths)); | |
assertNull("expect null for sling nonexisting rt", | |
asFunction.apply(Resource.RESOURCE_TYPE_NON_EXISTING, emptySearchPaths)); | |
assertEquals("expect same for relative rt", RESOURCE_TYPE, | |
asFunction.apply(RESOURCE_TYPE, emptySearchPaths)); | |
assertEquals("expect same for relative rt and real search paths", RESOURCE_TYPE, | |
asFunction.apply(RESOURCE_TYPE, realSearchPaths)); | |
assertEquals("expect relative for /apps/ + relative and real search paths", RESOURCE_TYPE, | |
asFunction.apply("/apps/" + RESOURCE_TYPE, realSearchPaths)); | |
assertEquals("expect relative for /libs/ + relative and real search paths", RESOURCE_TYPE, | |
asFunction.apply("/libs/" + RESOURCE_TYPE, realSearchPaths)); | |
assertNull("expect null for /fail/ + relative and real search paths", | |
asFunction.apply("/fail/" + RESOURCE_TYPE, realSearchPaths)); | |
} | |
@Test | |
public void addBindings() { | |
final SharedComponentPropertiesBindingsValuesProvider sharedComponentPropertiesBindingsValuesProvider | |
= new SharedComponentPropertiesBindingsValuesProvider(); | |
Whitebox.setInternalState(sharedComponentPropertiesBindingsValuesProvider, "pageRootProvider", pageRootProvider); | |
sharedComponentPropertiesBindingsValuesProvider.addBindings(bindings); | |
assertEquals(sharedPropsResource, bindings.get(SharedComponentProperties.SHARED_PROPERTIES_RESOURCE)); | |
assertEquals(globalPropsResource, bindings.get(SharedComponentProperties.GLOBAL_PROPERTIES_RESOURCE)); | |
assertEquals(sharedProps, bindings.get(SharedComponentProperties.SHARED_PROPERTIES)); | |
assertEquals(globalProps, bindings.get(SharedComponentProperties.GLOBAL_PROPERTIES)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment