Skip to content

Commit

Permalink
KYAN-246 - Make placeholder image a web resource instead of providing…
Browse files Browse the repository at this point in the history
… as an asset. (#499)
  • Loading branch information
otto-nagy-ds authored Aug 21, 2024
1 parent 38eae57 commit 0413016
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

public class LinkUtil {

private static final String ANCHOR_LINK_PREFIX = "#";
public static final String PUBLISHED = "/published";
public static final String CONTENT = "/content";
private static final String ANCHOR_LINK_PREFIX = "#";
private static final String KYANITE_WEB_RESOURCE_ROOT_PATH = "/libs/kyanite/webroot/";


private LinkUtil() {
// no instance
Expand Down Expand Up @@ -66,7 +68,7 @@ public static boolean isAnchorLink(String link) {
}

public static boolean isInternal(String link, ResourceResolver resourceResolver) {
return Objects.nonNull(getResource(link, resourceResolver));
return Objects.nonNull(getResource(link, resourceResolver)) && !isWebResource(link);
}

private static boolean isAsset(String link, ResourceResolver resourceResolver) {
Expand Down Expand Up @@ -105,4 +107,8 @@ private static String getPrimaryType(Resource resource) {
}
return resource.getValueMap().get("jcr:primaryType", String.class);
}
}

private static boolean isWebResource(final String link) {
return StringUtils.startsWith(link, KYANITE_WEB_RESOURCE_ROOT_PATH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"jcr:primaryType": "nt:unstructured",
"imageSrcType": "asset",
"height": "720",
"assetReference": "/content/kyanite/assets/images/personal/Placeholder.svg",
"assetReference": "/libs/kyanite/webroot/images/placeholder.svg",
"hasShadow": "false",
"hasVideoOptions": "false",
"alt": "",
Expand All @@ -50,7 +50,7 @@
"jcr:primaryType": "nt:unstructured",
"imageSrcType": "asset",
"height": "720",
"assetReference": "/content/kyanite/assets/images/personal/Placeholder.svg",
"assetReference": "/libs/kyanite/webroot/images/placeholder.svg",
"hasVideoOptions": "false",
"alt": "",
"type": "none",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (C) 2024 Dynamic Solutions
*
* 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.
*/

package pl.ds.kyanite.common.components.utils;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.util.function.Function;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit5.SlingContext;
import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import pl.ds.websight.assets.core.api.Asset;
import pl.ds.websight.assets.core.api.Rendition;

@ExtendWith(SlingContextExtension.class)
class LinkUtilTest {

private final SlingContext context = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

private final Asset assetMock = Mockito.mock(Asset.class);
private final Rendition renditionMock = Mockito.mock(Rendition.class);

@BeforeEach
public void init() {
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
context.load()
.json(requireNonNull(contextClassLoader.getResourceAsStream("links/pages.json")),
"/content/test/pages");
context.load()
.json(requireNonNull(contextClassLoader.getResourceAsStream("links/assets.json")),
"/content/test/assets");
context.load()
.json(requireNonNull(contextClassLoader.getResourceAsStream("links/web-resources.json")),
"/libs/kyanite/webroot");

context.registerAdapter(Resource.class, Asset.class,
(Function<Resource, Asset>) (Resource adaptable) -> {
if ("/content/test/assets/PortfolioForum.png".equals(adaptable.getPath())) {
when(assetMock.getOriginalRendition()).thenReturn(renditionMock);
when(renditionMock.getPath()).thenReturn(
"/content/test/assets/PortfolioForum.png/jcr:content/renditions/original.png");

return assetMock;
} else {
return null;
}
});
}

@Test
public void shouldProduceCorrectLinkWhenPageExists() {
assertLink("/content/test/pages/links/page",
"/content/test/pages/links/page.html");
}

@Test
public void shouldProduceCorrectLinkForPublishedPagesWhenPageExists() {
assertLink("/published/test/pages/links/page",
"/published/test/pages/links/page.html");
}

@Test
public void shouldNotAddHtmlExtensionWhenPageDoesNotExist() {
assertLink("/content/test/pages/links/non-existent-page",
"/content/test/pages/links/non-existent-page");
}

@Test
public void shouldProduceCorrectLinkForAnchors() {
assertLink("#i-am-an-anchor", "#i-am-an-anchor");
}

@Test
public void shouldProduceCorrectLinkToOriginalRenditionWhenAssetExists() {
assertLink("/content/test/assets/PortfolioForum.png",
"/content/test/assets/PortfolioForum.png/jcr:content/renditions/original.png");

verify(assetMock, times(2)).getOriginalRendition();
verify(renditionMock).getPath();
}

@Test
public void shouldProduceOriginalLinkWhenAssetDoesNotExist() {
assertLink("/content/test/assets/no-such-asset.png",
"/content/test/assets/no-such-asset.png");

verifyNoInteractions(assetMock);
verifyNoInteractions(renditionMock);
}

@Test
public void shouldNotRewriteLinksForWebResources() {
assertLink("/libs/test/webroot/image.png",
"/libs/test/webroot/image.png");
}

private void assertLink(final String path, final String expectedLink) {
final String actualLink = LinkUtil.handleLink(path, context.resourceResolver());

assertThat(actualLink).isNotNull();
assertThat(actualLink).isEqualTo(expectedLink);
}
}
20 changes: 20 additions & 0 deletions applications/common/backend/src/test/resources/links/assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"jcr:primaryType": "ws:Assets",
"PortfolioForum.png": {
"jcr:primaryType": "ws:Asset",
"jcr:content": {
"jcr:primaryType": "ws:AssetContent",
"renditions": {
"jcr:primaryType": "nt:folder",
"original.png": {
"jcr:primaryType": "nt:file",
"jcr:content": {
"jcr:primaryType": "oak:Resource",
"jcr:mimeType": "image/png",
":jcr:data": 177642
}
}
}
}
}
}
20 changes: 20 additions & 0 deletions applications/common/backend/src/test/resources/links/pages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"jcr:primaryType": "ws:Pages",
"ws:template": "kyanite/common/templates/pagesspace",
"links": {
"jcr:primaryType": "ws:Page",
"jcr:content": {
"jcr:primaryType": "ws:PageContent",
"ws:template": "/libs/kyanite/common/templates/structurepage",
"sling:resourceType": "kyanite/common/components/page"
},
"page": {
"jcr:primaryType": "ws:Page",
"jcr:content": {
"jcr:primaryType": "ws:PageContent",
"ws:template": "/libs/kyanite/common/templates/homepage",
"sling:resourceType": "kyanite/common/components/page"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"image.png": {
}
}
29 changes: 29 additions & 0 deletions applications/common/frontend/src/resources/images/placeholder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 0413016

Please sign in to comment.