Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Aug 26, 2024
2 parents 08271c6 + 64a5cff commit e3ae2bd
Show file tree
Hide file tree
Showing 33 changed files with 813 additions and 76 deletions.
9 changes: 9 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="5.6.0" date="2024-08-26">
<action type="add" dev="senn" issue="49">
Add mock implementation of Adobe Granite Asset API.
</action>
<action type="update" dev="sseifert">
Update to latest OSGi Mock, Resource Resolver Mock, Sling Mock.
</action>
</release>

<release version="5.5.4" date="2024-07-18">
<action type="update" dev="sseifert" issue="45">
MockExternalizer: Apply Sling Mapping for externalized URLs.
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.parent</artifactId>
<version>5.5.4</version>
<version>5.6.0</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
/**
* Mock implementation of {@link ContentElement} for structured content.
*/
@SuppressWarnings("java:S112") // allow throwing RuntimException
class MockContentFragment_ContentElement_Structured implements ContentElement {

private final MockContentFragment contentFragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
/**
* Mock implementation of {@link ContentElement} for text-based content.
*/
@SuppressWarnings("java:S112") // allow throwing RuntimException
class MockContentFragment_ContentElement_Text implements ContentElement {

private final MockContentFragment contentFragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,19 @@ public List<Template> getTemplates(String policyAbsolutePath, Predicate filter)

// AEM Cloud
@SuppressWarnings("unused")
public boolean isPolicyUsed(String arg0, @Nullable Predicate arg1) {
public boolean isPolicyUsed(String arg0, @Nullable Predicate filter) {
throw new UnsupportedOperationException();
}

// AEM Cloud
@SuppressWarnings("unused")
public List<Template> getTemplates(String policyAbsolutePath, @Nullable java.util.function.Predicate filter) {
throw new UnsupportedOperationException();
}

// AEM Cloud
@SuppressWarnings("unused")
public boolean isPolicyUsed(String policyAbsolutePath, @Nullable java.util.function.Predicate filter) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* their mappings. Basically it stores one global content policy per resource type, and one mapping
* for it. This is usually enough for unit tests.
*/
@SuppressWarnings("java:S112") // allow throwing RuntimException
public final class MockContentPolicyStorage {

static final String RT_CONTENTPOLICY = "wcm/core/components/policy/policy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
@ProviderType
public final class MockLanguageManager implements LanguageManager {

/**
* @deprecated Deprecated
*/
@Override
@Deprecated(forRemoval = true)
public Map<Locale, Info> getAdjacentInfo(final ResourceResolver resourceResolver, final String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* Mock implementation of {@link TagManager}.
*/
@ProviderType
@SuppressWarnings("java:S112") // allow throwing RuntimException
public final class MockTagManager implements TagManager {

/** resource type for created tags */
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/io/wcm/testing/mock/aem/MockTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ class MockTemplate extends ResourceWrapper implements Template {
}

@Override
@SuppressWarnings("java:S1185") // overwrite due to different null constraints
public String getPath() {
return super.getPath();
}

@Override
@SuppressWarnings("java:S1185") // overwrite due to different null constraints
public String getName() {
return super.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@
* Helper class for building test content in the resource hierarchy with as less boilerplate code as possible.
*/
@ProviderType
@SuppressWarnings("null")
@SuppressWarnings({
"null",
"java:S112", // allow throwing RuntimException
"java:S2176" // same name as superclass
})
public final class ContentBuilder extends org.apache.sling.testing.mock.sling.builder.ContentBuilder {

static final String DUMMY_TEMPLATE = "/apps/sample/templates/template1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* Should not be used directly but via the JUnit 4 rule or JUnit 5 extension.
*/
@ConsumerType
@SuppressWarnings("java:S112") // allow throwing RuntimException
public class AemContextImpl extends SlingContextImpl {

// default to publish instance run mode
Expand Down Expand Up @@ -180,6 +181,17 @@ protected final Map<String, Object> resourceResolverFactoryActivatorPropsMergeWi
return assetManager;
}

/**
* @return Adobe Granite Asset manager
*/
public @NotNull com.adobe.granite.asset.api.AssetManager graniteAssetManager() {
com.adobe.granite.asset.api.AssetManager assetManager = resourceResolver().adaptTo(com.adobe.granite.asset.api.AssetManager.class);
if (assetManager == null) {
throw new RuntimeException("No granite asset manager");
}
return assetManager;
}

/**
* @return Content builder for building test content
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* unique root paths for each run, and cleans them up when done.
*/
@ProviderType
@SuppressWarnings("java:S2176") // same name as superclass
public final class UniqueRoot extends org.apache.sling.testing.mock.sling.context.UniqueRoot {

private Resource damRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
/**
* AEM context implementation for unit tests.
*/
@org.osgi.annotation.versioning.Version("2.0.0")
@org.osgi.annotation.versioning.Version("2.1.0")
package io.wcm.testing.mock.aem.context;
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
AdapterFactory.ADAPTABLE_CLASSES + "=org.apache.sling.api.resource.ResourceResolver",
AdapterFactory.ADAPTER_CLASSES + "=com.day.cq.dam.api.Asset",
AdapterFactory.ADAPTER_CLASSES + "=com.day.cq.dam.api.AssetManager",
AdapterFactory.ADAPTER_CLASSES + "=com.day.cq.dam.api.Rendition"
AdapterFactory.ADAPTER_CLASSES + "=com.day.cq.dam.api.Rendition",
AdapterFactory.ADAPTER_CLASSES + "=com.adobe.granite.asset.api.Asset",
AdapterFactory.ADAPTER_CLASSES + "=com.adobe.granite.asset.api.AssetManager",
AdapterFactory.ADAPTER_CLASSES + "=com.adobe.granite.asset.api.Rendition"
})
@ProviderType
public final class MockAemDamAdapterFactory implements AdapterFactory {
Expand All @@ -71,21 +74,27 @@ private void activate(BundleContext context) {
return null;
}

@SuppressWarnings("unchecked")
private @Nullable <AdapterType> AdapterType getAdapter(@NotNull final Resource resource, @NotNull final Class<AdapterType> type) {
if (type == Asset.class && DamUtil.isAsset(resource)) {
return (AdapterType)new MockAsset(resource, eventAdmin, bundleContext);
if (DamUtil.isAsset(resource)) {
if (type == com.adobe.granite.asset.api.Asset.class) {
return type.cast(new MockGraniteAssetWrapper(new MockAsset(resource, eventAdmin, bundleContext)));
}
else if (type == Asset.class) {
return type.cast(new MockAsset(resource, eventAdmin, bundleContext));
}
}
if (type == Rendition.class && DamUtil.isRendition(resource)) {
return (AdapterType)new MockRendition(resource);
if ((type == Rendition.class || type == com.adobe.granite.asset.api.Rendition.class) && DamUtil.isRendition(resource)) {
return type.cast(new MockRendition(resource));
}
return null;
}

@SuppressWarnings("unchecked")
private @Nullable <AdapterType> AdapterType getAdapter(@NotNull final ResourceResolver resolver, @NotNull final Class<AdapterType> type) {
if (type == AssetManager.class) {
return (AdapterType)new MockAssetManager(resolver, eventAdmin, bundleContext);
return type.cast(new MockAssetManager(resolver, eventAdmin, bundleContext));
}
else if (type == com.adobe.granite.asset.api.AssetManager.class) {
return type.cast(new MockGraniteAssetManagerWrapper(resolver));
}
return null;
}
Expand Down
42 changes: 25 additions & 17 deletions core/src/main/java/io/wcm/testing/mock/aem/dam/MockAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.osgi.framework.BundleContext;
import org.osgi.service.event.EventAdmin;

import com.adobe.granite.asset.api.RenditionHandler;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.DamConstants;
Expand All @@ -54,7 +55,10 @@
/**
* Mock implementation of {@link Asset}.
*/
@SuppressWarnings("null")
@SuppressWarnings({
"null",
"java:S112" // allow throwing RuntimException
})
class MockAsset extends ResourceWrapper implements Asset {

private final ResourceResolver resourceResolver;
Expand All @@ -76,11 +80,14 @@ class MockAsset extends ResourceWrapper implements Asset {
this.bundleContext = bundleContext;
}

@SuppressWarnings("unchecked")
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type == Resource.class) {
return (AdapterType)resource;
return type.cast(resource);
}
//to be able to adapt to granite asset
if (type == com.adobe.granite.asset.api.Asset.class) {
return type.cast(new MockGraniteAssetWrapper(this));
}
return super.adaptTo(type);
}
Expand Down Expand Up @@ -243,14 +250,18 @@ public String getID() {
return resource.getValueMap().get(JcrConstants.JCR_UUID, "");
}


// --- unsupported operations ---

@Override
public Rendition addRendition(String name, InputStream is, Map<String, Object> map) {
throw new UnsupportedOperationException();
Object mimeTypeObject = map.get(RenditionHandler.PROPERTY_RENDITION_MIME_TYPE);
if (mimeTypeObject instanceof String) {
return addRendition(name, is, mimeTypeObject.toString());
}
throw new UnsupportedOperationException("Mime type property missing in map: " + RenditionHandler.PROPERTY_RENDITION_MIME_TYPE);
}


// --- unsupported operations ---

@Override
public Rendition getCurrentOriginal() {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -311,27 +322,24 @@ public void initAssetState() {
throw new UnsupportedOperationException();
}

// AEM 6.5
@SuppressWarnings("unused")
public Rendition addRendition(String arg0, Binary arg1, String arg2) {
@Override
public Rendition addRendition(String name, Binary binary, String mimeType) {
throw new UnsupportedOperationException();
}

// AEM 6.5
@SuppressWarnings("unused")
public Rendition addRendition(String arg0, Binary arg1, Map<String, Object> arg2) {
@Override
public Rendition addRendition(String name, Binary binary, Map<String, Object> arg2) {
throw new UnsupportedOperationException();
}

// AEM 6.5.5
@SuppressWarnings("unused")
public Revision createRevision(String arg0, String arg1, User arg2) {
@Override
public Revision createRevision(String name, String binary, User arg2) {
throw new UnsupportedOperationException();
}

// AEM Cloud
@SuppressWarnings("unused")
public Rendition setRendition(String arg0, Binary arg1, String arg2) {
public Rendition setRendition(String name, Binary binary, String mimeType) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
/**
* Mock implementation of {@link AssetManager}
*/
@SuppressWarnings("null")
@SuppressWarnings({
"null",
"java:S112" // allow throwing RuntimException
})
class MockAssetManager implements AssetManager {

private final ResourceResolver resourceResolver;
Expand Down
Loading

0 comments on commit e3ae2bd

Please sign in to comment.