From b669fc952773f8d9354b0a47fa13c1fe15e4530a Mon Sep 17 00:00:00 2001 From: Joerg Hoh Date: Mon, 30 Sep 2024 19:03:38 +0200 Subject: [PATCH] #2872 update the AEM Mocks to 5.6.2 and made all tests working again --- .../commons/link/LinkManagerTest.java | 34 +++++++++++-------- .../link/DefaultPathProcessorTest.java | 2 +- .../internal/link/LinkImplTest.java | 17 +++++++--- .../internal/link/LinkTestUtils.java | 17 +++++++--- .../internal/models/v1/ButtonImplTest.java | 2 +- .../internal/models/v2/ButtonImplTest.java | 4 +-- .../internal/models/v2/TeaserImplTest.java | 4 +-- .../internal/models/v3/ImageImplTest.java | 2 +- parent/pom.xml | 2 +- 9 files changed, 52 insertions(+), 32 deletions(-) diff --git a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/commons/link/LinkManagerTest.java b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/commons/link/LinkManagerTest.java index 734c260370..efa75535f6 100644 --- a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/commons/link/LinkManagerTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/commons/link/LinkManagerTest.java @@ -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()); } @@ -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()); } @@ -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()); @@ -134,7 +134,7 @@ 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()); } @@ -142,8 +142,9 @@ void testResourceInvalidPageLink() { 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()); } @@ -167,7 +168,7 @@ 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()); } @@ -175,7 +176,7 @@ void testLinkURLPageLinkWithTarget() { void testLinkWithTargetAsset() { Link link = getUnderTest().get(asset).build(); - assertValidLink(link, asset.getPath()); + assertValidLink(link, asset.getPath(),context); assertEquals(asset, link.getReference()); } @@ -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()); } @@ -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()); } @@ -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()); } @@ -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()); } diff --git a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/DefaultPathProcessorTest.java b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/DefaultPathProcessorTest.java index 72d1ca9dac..56fc56e288 100644 --- a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/DefaultPathProcessorTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/DefaultPathProcessorTest.java @@ -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())); diff --git a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/LinkImplTest.java b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/LinkImplTest.java index 7cb7b180bc..3a489beb83 100644 --- a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/LinkImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/LinkImplTest.java @@ -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 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()); } @@ -46,7 +53,7 @@ void testValidLink() { void testValidLinkWithTarget() { Link link = new LinkImpl(URL, URL, CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + URL, null, new HashMap() {{ put(ATTR_TARGET, "_blank"); }}); - assertValidLink(link, URL, "_blank"); + assertValidLink(link, URL, "_blank", context); assertNull(link.getReference()); } @@ -54,7 +61,7 @@ void testValidLinkWithTarget() { 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()); } @@ -64,7 +71,7 @@ void testValidLinkWithTargetAndTargetPage() { Link link = new LinkImpl<>(URL, URL, CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + URL, page, new HashMap() {{ put(ATTR_TARGET, "_blank"); }}); - assertValidLink(link, URL, "_blank"); + assertValidLink(link, URL, "_blank", context); assertSame(page, link.getReference()); } @@ -107,7 +114,7 @@ void testValidLikWithFilteredHtmlAttributes() { String invalidAttribute = "invalidAttribute"; Link 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)); } } diff --git a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/LinkTestUtils.java b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/LinkTestUtils.java index ee181c27e6..f32b331eff 100644 --- a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/LinkTestUtils.java +++ b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/link/LinkTestUtils.java @@ -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; @@ -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); + assertEquals(CoreComponentTestContext.EXTERNALIZER_PUBLISH_DOMAIN + mappedPath, link.getExternalizedURL(), "linkExternalizedUrl"); if (request != null && StringUtils.isNotEmpty(request.getContextPath()) && !linkURL.startsWith("http")) { linkURL = request.getContextPath().concat(linkURL); } @@ -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"); } diff --git a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v1/ButtonImplTest.java b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v1/ButtonImplTest.java index 4c9c17c305..36c91c884a 100644 --- a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v1/ButtonImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v1/ButtonImplTest.java @@ -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; diff --git a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v2/ButtonImplTest.java b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v2/ButtonImplTest.java index 10cf582646..8c788d459b 100644 --- a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v2/ButtonImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v2/ButtonImplTest.java @@ -48,7 +48,7 @@ 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")); } @@ -56,7 +56,7 @@ protected void testGetLink() { 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")); } diff --git a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v2/TeaserImplTest.java b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v2/TeaserImplTest.java index 68f12d0b5b..adc279cad9 100644 --- a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v2/TeaserImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v2/TeaserImplTest.java @@ -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")); } @@ -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")); } diff --git a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v3/ImageImplTest.java b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v3/ImageImplTest.java index e2e3da1c75..98dd54c5f9 100644 --- a/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v3/ImageImplTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/wcm/core/components/internal/models/v3/ImageImplTest.java @@ -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)); diff --git a/parent/pom.xml b/parent/pom.xml index f365950558..d76fa2cc78 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -796,7 +796,7 @@ io.wcm io.wcm.testing.aem-mock.junit5 - 5.5.2 + 5.6.2 test