Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2872 update the AEM Mocks to 5.6.2 and made all tests working again #2875

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing is off here (and in other places too).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, fixed it.

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()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a missing /links which puzzles me, who is removing it and why? Is this a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is caused by a change in the AEM Mocks (wcm-io/io.wcm.testing.aem-mock#44), which has been added in AEM Mocks 5.5.4

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();
Copy link
Member

@vladbailescu vladbailescu Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid passing the context in all assertValidLinks? I see the context is instantiated here, maybe we can do that in LinkTestUtils?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertValidLink is also used by other test classes; for example in LInkManagerTest I see that the context is also used in other ways. I haven't investigated if these tests would also work with 2 different instances of the AemContext, but I also don't think that it's worth the effort.


@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);
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
Loading