1
votes

I try to test some DAM Assets with a JUnit test in AEM.

At first I copied an existing asset as JSON in a resource file. Next, I use the ContentLoader to write the JSON file into the simulated repository.

final ContentLoader contentLoader = new ContentLoader(this.getResourceResolver());
contentLoader.json("content/assets/testAsset.json", "/content/assets/testAsset");

Then, I use the ResourceResolver to resolve the Resource in the destinationPath.

Resource assetRecource = resourceResolver.resolve("/content/assets/testAsset");

So far so good. When I try to adapt the assetRecource to an Asset.class is the result null.

Asset asset = assetRecource.adaptTo(Asset.class); // is null

In my opinion it's because the missing renditions.

Do someone have an idea how to fix this? Maybe with the ContentLoader#binaryResource()?

2
I imagine the testAsset json has primaryType of dam:Asset. You can also try to check the resource that gets resolved has the same resource type.Abhishek

2 Answers

1
votes

Check the resourceType for the resource is to be dam:Asset.

Resource assetRecource = resourceResolver.resolve("/content/assets/testAsset");
DamUtil.isAsset(assetRecource);   // This would be true if the asset is dam:Asset
assetRecource.getResourceType();  // This would return the resourceType of the resource.

The possible reason for adaptTo to be null is only if the resource is not adaptable to Asset Class.

Refrences:

  1. https://docs.adobe.com/content/docs/en/cq/5-6-1/javadoc/com/day/cq/dam/api/Asset.html
  2. http://sling.apache.org/apidocs/sling5/org/apache/sling/api/adapter/Adaptable.html#adaptTo(java.lang.Class)
0
votes

You need to add the below dependency to test the DAM assests:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-imaging -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-imaging</artifactId>
            <version>1.0-R1534292</version>
        </dependency>