1

我尝试在 AEM 中使用 JUnit 测试来测试一些 DAM 资产。

起初,我将现有资产作为 JSON 复制到资源文件中。接下来,我使用 ContentLoader 将 JSON 文件写入模拟存储库。

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

然后,我使用 ResourceResolver 来解析destinationPath 中的资源。

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

到目前为止,一切都很好。当我尝试将assetRecource 调整为Asset.class 时,结果为null。

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

在我看来,这是因为缺少演绎。

有人知道如何解决这个问题吗?也许使用 ContentLoader#binaryResource()?

4

2 回答 2

1

检查资源的 resourceType 是否为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.

adaptTo 为 null 的可能原因是资源不能适应资产类别。

参考文献:

  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)
于 2017-06-28T20:42:16.287 回答
0

您需要添加以下依赖项来测试 DAM 资产:

<!-- 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>
于 2017-07-11T10:39:44.117 回答