-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KYAN-246 - Make placeholder image a web resource instead of providing…
… as an asset. (#499)
- Loading branch information
1 parent
38eae57
commit 0413016
Showing
11 changed files
with
210 additions
and
21 deletions.
There are no files selected for viewing
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
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
126 changes: 126 additions & 0 deletions
126
...ions/common/backend/src/test/java/pl/ds/kyanite/common/components/utils/LinkUtilTest.java
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
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
20
applications/common/backend/src/test/resources/links/assets.json
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
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
20
applications/common/backend/src/test/resources/links/pages.json
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
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" | ||
} | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
applications/common/backend/src/test/resources/links/web-resources.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"image.png": { | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
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.
8 changes: 0 additions & 8 deletions
8
...main/content/jcr_root/content/kyanite/assets/images/personal/Placeholder.svg/.content.xml
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...tent/jcr_root/content/kyanite/assets/images/personal/Placeholder.svg/LICENSE.md
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...ite/assets/images/personal/Placeholder.svg/_jcr_content/renditions/original.svg
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
...ets/images/personal/Placeholder.svg/_jcr_content/renditions/original.svg.dir/.content.xml
This file was deleted.
Oops, something went wrong.