Skip to content

Commit

Permalink
#2872 update the AEM Mocks to 5.6.2 and made all tests working again
Browse files Browse the repository at this point in the history
  • Loading branch information
joerghoh committed Sep 30, 2024
1 parent d2a7b02 commit b669fc9
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void testResourceExternalLink() {
context.currentResource(linkResource);
Link link = getUnderTest().get(linkResource).build();

assertValidLink(link, "http://myhost");
assertValidLink(link, "http://myhost",context);
assertNull(link.getReference());
assertEquals("http://myhost", link.getMappedURL());
}
Expand Down Expand Up @@ -103,7 +103,7 @@ void testResourceExternalLinkWithInvalidTargets(String target) {
Link link = getUnderTest().get(linkResource).build();

// invalid target or _self target should be stripped away
assertValidLink(link, "http://myhost");
assertValidLink(link, "http://myhost",context);
assertNull(link.getReference());
}

Expand All @@ -113,7 +113,7 @@ void testResourcePageLink() {
PN_LINK_URL, page.getPath());
context.currentResource(linkResource);
Link link = getUnderTest().get(linkResource).build();
assertValidLink(link, page.getPath() + ".html");
assertValidLink(link, page.getPath() + ".html", context);
assertEquals(page, link.getReference());
assertEquals((page.getPath() + ".html").replaceAll("^\\/content\\/links\\/site1\\/(.+)","/content/site1/$1"),
link.getMappedURL());
Expand All @@ -134,16 +134,17 @@ void testResourceInvalidPageLink() {
Link link = getUnderTest().get(linkResource).build();

// TODO: this link should be handled as invalid. but we keep this behavior for now to keep backwards compatibility
assertValidLink(link, "/content/non-existing");
assertValidLink(link, "/content/non-existing", context);
assertNull(link.getReference());
}

@Test
void testPageLink() {
Link link = getUnderTest().get(page).build();

assertValidLink(link, page.getPath() + ".html");
assertEquals("https://example.org" + page.getPath() + ".html", link.getExternalizedURL());
assertValidLink(link, page.getPath() + ".html", context);
final String mappedPath = context.resourceResolver().map(page.getPath());
assertEquals("https://example.org" + mappedPath + ".html", link.getExternalizedURL());
assertEquals(page, link.getReference());
}

Expand All @@ -167,15 +168,15 @@ void testEmptyLink() {
void testLinkURLPageLinkWithTarget() {
Link link = getUnderTest().get(page.getPath()).withLinkTarget("_blank").build();

assertValidLink(link, page.getPath() + ".html", "_blank");
assertValidLink(link, page.getPath() + ".html", "_blank",context);
assertEquals(page, link.getReference());
}

@Test
void testLinkWithTargetAsset() {
Link link = getUnderTest().get(asset).build();

assertValidLink(link, asset.getPath());
assertValidLink(link, asset.getPath(),context);
assertEquals(asset, link.getReference());
}

Expand All @@ -198,8 +199,9 @@ void testLinkWithRedirect() {
Link link = getUnderTest().get(linkResource).build();

assertTrue(link.isValid());
assertValidLink(link, targetPage2.getPath() + ".html");
assertEquals("https://example.org" + targetPage2.getPath() + ".html", link.getExternalizedURL());
assertValidLink(link, targetPage2.getPath() + ".html", context);
final String mappedPath = context.resourceResolver().map(targetPage2.getPath());
assertEquals("https://example.org" + mappedPath + ".html", link.getExternalizedURL());
assertEquals(targetPage2, link.getReference());
}

Expand All @@ -225,8 +227,9 @@ void testLinkWithRedirect_shadowingDisabledByProperty() {
Link link = getUnderTest().get(linkResource).build();

assertTrue(link.isValid());
assertValidLink(link, targetPage1.getPath() + ".html");
assertEquals("https://example.org" + targetPage1.getPath() + ".html", link.getExternalizedURL());
final String mappedPath = context.resourceResolver().map(targetPage1.getPath());
assertValidLink(link, targetPage1.getPath() + ".html",context);
assertEquals("https://example.org" + mappedPath + ".html", link.getExternalizedURL());
assertEquals(targetPage1, link.getReference());
}

Expand Down Expand Up @@ -255,8 +258,9 @@ void testLinkWithRedirect_shadowingDisabledByStyle() {
Link link = getUnderTest().get(linkResource).build();

assertTrue(link.isValid());
assertValidLink(link, targetPage1.getPath() + ".html");
assertEquals("https://example.org" + targetPage1.getPath() + ".html", link.getExternalizedURL());
assertValidLink(link, targetPage1.getPath() + ".html", context);
final String mappedPath = context.resourceResolver().map(targetPage1.getPath());
assertEquals("https://example.org" + mappedPath + ".html", link.getExternalizedURL());
assertEquals(targetPage1, link.getReference());
}

Expand All @@ -282,7 +286,7 @@ void testLinkWithRedirectToExternal() {
Link link = getUnderTest().get(linkResource).build();

assertTrue(link.isValid());
assertValidLink(link, "http://myhost");
assertValidLink(link, "http://myhost",context);
assertEquals(targetPage1, link.getReference());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void testVanityConfig() {
DefaultPathProcessor underTest = context.registerInjectActivateService(new DefaultPathProcessor(), ImmutableMap.of(
"vanityConfig", "shouldBeDefault"));
assertEquals("/content/site1/en.html", underTest.map(page.getPath() + HTML_EXTENSION, context.request()));
assertEquals("https://example.org/content/links/site1/en.html", underTest.externalize(page.getPath() + HTML_EXTENSION, context.request()));
assertEquals("https://example.org/content/site1/en.html", underTest.externalize(page.getPath() + HTML_EXTENSION, context.request()));
context.request().setContextPath("/cp");
underTest = context.registerInjectActivateService(new DefaultPathProcessor(), ImmutableMap.of(
"vanityConfig", DefaultPathProcessor.VanityConfig.ALWAYS.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,32 @@

import com.adobe.cq.wcm.core.components.context.CoreComponentTestContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import com.adobe.cq.wcm.core.components.commons.link.Link;
import com.day.cq.wcm.api.Page;
import com.google.common.collect.ImmutableMap;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

import static com.adobe.cq.wcm.core.components.internal.link.LinkImpl.*;
import static com.adobe.cq.wcm.core.components.internal.link.LinkTestUtils.assertInvalidLink;
import static com.adobe.cq.wcm.core.components.internal.link.LinkTestUtils.assertValidLink;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

@ExtendWith(AemContextExtension.class)
class LinkImplTest {

private static final String URL = "/url.html";

private final AemContext context = CoreComponentTestContext.newAemContext();

@Test
void testValidLink() {
Link<Page> link = new LinkImpl<>(URL, URL, CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + URL, null, null);
assertValidLink(link, URL);
assertValidLink(link, URL, context);
assertNull(link.getReference());
assertEquals(URL, link.getMappedURL());
}
Expand All @@ -46,15 +53,15 @@ void testValidLink() {
void testValidLinkWithTarget() {
Link<Page> link = new LinkImpl(URL, URL, CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + URL, null,
new HashMap<String, String>() {{ put(ATTR_TARGET, "_blank"); }});
assertValidLink(link, URL, "_blank");
assertValidLink(link, URL, "_blank", context);
assertNull(link.getReference());
}

@Test
void testValidLinkWithoutTarget() {
Link link = new LinkImpl(URL, URL, CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + URL,null, null);

assertValidLink(link, URL, (String)null);
assertValidLink(link, URL, (String)null, context);
assertNull(link.getReference());
}

Expand All @@ -64,7 +71,7 @@ void testValidLinkWithTargetAndTargetPage() {
Link<Page> link = new LinkImpl<>(URL, URL, CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + URL, page,
new HashMap<String, String>() {{ put(ATTR_TARGET,
"_blank"); }});
assertValidLink(link, URL, "_blank");
assertValidLink(link, URL, "_blank", context);
assertSame(page, link.getReference());
}

Expand Down Expand Up @@ -107,7 +114,7 @@ void testValidLikWithFilteredHtmlAttributes() {
String invalidAttribute = "invalidAttribute";
Link<Page> link = new LinkImpl<>(URL, URL, CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + URL, page, ImmutableMap.of(invalidAttribute,
"invalidValue"));
assertValidLink(link, URL);
assertValidLink(link, URL,context);
assertNull(link.getHtmlAttributes().get(invalidAttribute));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.adobe.cq.wcm.core.components.commons.link.Link;
import com.google.common.collect.ImmutableMap;

import io.wcm.testing.mock.aem.junit5.AemContext;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -35,11 +37,17 @@ public final class LinkTestUtils {
public static void assertValidLink(@NotNull Link link, @NotNull String linkURL) {
assertValidLink(link, linkURL, (SlingHttpServletRequest)null);
}

public static void assertValidLink(@NotNull Link link, @NotNull String linkURL, AemContext context) {
assertValidLink(link, linkURL, context.request());
}


public static void assertValidLink(@NotNull Link link, @NotNull String linkURL,
@Nullable SlingHttpServletRequest request) {
assertTrue(link.isValid(), "linkValid");
assertEquals(CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + linkURL, link.getExternalizedURL(), "linkExternalizedUrl");
final String mappedPath = request.getResourceResolver().map(linkURL);

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning test

Variable
request
may be null at this access because of
this
null argument.
Variable
request
may be null at this access as suggested by
this
null guard.
assertEquals(CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + mappedPath, link.getExternalizedURL(), "linkExternalizedUrl");
if (request != null && StringUtils.isNotEmpty(request.getContextPath()) && !linkURL.startsWith("http")) {
linkURL = request.getContextPath().concat(linkURL);
}
Expand All @@ -48,15 +56,16 @@ public static void assertValidLink(@NotNull Link link, @NotNull String linkURL,
assertEquals(ImmutableMap.of("href", linkURL), link.getHtmlAttributes(), "linkHtmlAttributes");
}

public static void assertValidLink(@NotNull Link link, @NotNull String linkURL, @Nullable String linkTarget) {
public static void assertValidLink(@NotNull Link link, @NotNull String linkURL, @Nullable String linkTarget, AemContext context) {
if (linkTarget == null) {
assertValidLink(link, linkURL);
assertValidLink(link, linkURL, context.request());
return;
}
assertTrue(link.isValid(), "linkValid");
assertEquals(linkURL, link.getURL(), "linkUrl");
assertEquals(linkURL.replaceAll("^\\/content\\/links\\/site1\\/(.+)","/content/site1/$1"), link.getMappedURL(), "linkMappedUrl");
assertEquals(CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + linkURL, link.getExternalizedURL(), "linkExternalizedUrl");
final String mappedPath = context.resourceResolver().map(linkURL);
assertEquals(CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + mappedPath, link.getExternalizedURL(), "linkExternalizedUrl");
assertEquals(ImmutableMap.of("href", linkURL, "target", linkTarget), link.getHtmlAttributes(), "linkHtmlAttributes");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ButtonImplTest {
protected static final String TEST_ROOT_PAGE_GRID = "/button/jcr:content/root/responsivegrid";
protected static final String BUTTON_1 = TEST_ROOT_PAGE + TEST_ROOT_PAGE_GRID + "/button-1";

private final AemContext context = CoreComponentTestContext.newAemContext();
protected final AemContext context = CoreComponentTestContext.newAemContext();

protected String testBase;
protected String resourceType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ protected void setUp() {
protected void testGetLink() {
Button button = getButtonUnderTest(BUTTON_1);
assertEquals("https://www.adobe.com", button.getLink());
assertValidLink(button.getButtonLink(), "https://www.adobe.com", "_blank");
assertValidLink(button.getButtonLink(), "https://www.adobe.com", "_blank", context);
Utils.testJSONExport(button, Utils.getTestExporterJSONPath(testBase, "button1"));
}

@Test
protected void testGetLink_withOldLinkProp() {
Button button = getButtonUnderTest(BUTTON_2);
assertEquals("https://www.adobe.com", button.getLink());
assertValidLink(button.getButtonLink(), "https://www.adobe.com", "_blank");
assertValidLink(button.getButtonLink(), "https://www.adobe.com", "_blank", context);
Utils.testJSONExport(button, Utils.getTestExporterJSONPath(testBase, "button2"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected void testFullyConfiguredTeaser() {
}
assertEquals(TITLE, teaser.getTitle());
assertEquals(DESCRIPTION, teaser.getDescription());
assertValidLink(teaser.getLink(), LINK);
assertValidLink(teaser.getLink(), LINK, context);
Utils.testJSONExport(teaser, Utils.getTestExporterJSONPath(testBase, "teaser1"));
}

Expand All @@ -104,7 +104,7 @@ protected void testTeaserWithActions() {
assertEquals("http://www.adobe.com", action.getPath(), "Action link does not match");
assertEquals("Adobe", action.getTitle(), "Action text does not match");
assertEquals("http://www.adobe.com", action.getURL());
assertValidLink(action.getLink(), "http://www.adobe.com");
assertValidLink(action.getLink(), "http://www.adobe.com", context);
Utils.testJSONExport(teaser, Utils.getTestExporterJSONPath(testBase, "teaser9"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ protected void testImageWithMap() {
assertEquals(expectedAreas[index][3], area.getHref(), "The image area's href is not as expected.");
assertEquals(expectedAreas[index][4], area.getTarget(), "The image area's target is not as expected.");
assertEquals(expectedAreas[index][5], area.getAlt(), "The image area's alt text is not as expected.");
assertValidLink(area.getLink(), (String) expectedAreas[index][3], StringUtils.trimToNull((String) expectedAreas[index][4]));
assertValidLink(area.getLink(), (String) expectedAreas[index][3], StringUtils.trimToNull((String) expectedAreas[index][4]),context);
index++;
}
Utils.testJSONExport(image, Utils.getTestExporterJSONPath(testBase, AbstractImageTest.IMAGE24_PATH));
Expand Down
2 changes: 1 addition & 1 deletion parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.5.2</version>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down

0 comments on commit b669fc9

Please sign in to comment.