Hello,
I have a test workflow written in aem where i need to get the Asset
where the java test case is written as:
JsonFile
{
"test":
{
"jcr:primaryType":"dam:Asset",
"jcr:createdBy":"admin"
}
}
please help me solve from above code on how to get the asset path/object so that i can retrive asset.getPath(), asset.getName() asset.getAssetMetadata() if required
for now its giving the value as null
request to share the sample code
Thanks & Regards.
Solved! Go to Solution.
Views
Replies
Total Likes
Correct. It will return null if you use the com.adobe.granite.asset.api.Asset
because it is not adaptable. Why are you using that? You should use the Asset implementation from com.day.cq.dam.api.Asset
.
If you really want to stick with the other Asset implementation, you need to mock that class and not rely on the aemContext
object. That's what I suggested as option #1 in my previous response. You should mock all of the following objects: ResourceResolver
, the AdaptTo
method, the AssetManager
, and the Asset
. The drawback with this approach is that you are not testing anything because you are setting the expected value at each step. So the best solution is to use the correct Asset implementation.
Hope this helps
Hi @VJain03 ,
Create an asset object using the below snippet in your Junit for the import com.adobe.granite.asset.api.Asset.
// to create an asset AssetManager assetManager = resolver.adaptTo(AssetManager.class); Asset newAsset = assetManager.createAsset("/path/to/asset/document.pdf"); // to get an existing asset Asset asset = assetManager.getAsset("/path/to/asset/document.pdf"); // to get asset by adapting Asset asset = resource.adaptTo(Asset.class);
Asset properties can be retrieved or set by adapting it to a ValueMap.
eg: // to get Asset properties value map ValueMap map = asset.adaptTo(ValueMap.class);
I hope this helps!!
Can you please share your code snippet here.
JsonFile
{
"test.mp4":
{
"jcr:primaryType":"dam:Asset",
"jcr:createdBy":"admin"
}
}
Please refer this thread for the same
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-create-junit-test-c...
The adaptTo is null, because the resource is not of type dam:Asset.
Similar discussion at: https://stackoverflow.com/questions/44807635/aem-junit-test-with-a-dam-asset
Thanks for the reply @aanchal-sikka
Here the resoure is of type dam:Asset
and i have checked
DamUtil.isAsset(resource);
and its returning true
but for
Asset asset = resource.adaptTo(Asset.class);
System.out.println(asset.getPath()); // Output: asset path: null
if the Asset is imported from "import com.day.cq.dam.api.Asset;" then its adapting properly
but when Asset is imported from "import com.adobe.granite.asset.api.Asset;" then its giving null
request you to please solve this
There are 2 ways to "mock" this type of "Asset.class", what I've read so far in your replies is that you are mixing both. Here are the options:
1) You use a mocked resourceResolver and then you must mock each method that will be invoked by this object (especially the adapTo) and manually return another mock object, and the cycle repeats.
2) You use aemContext.resourceResolver() and configure it to be adapted to a AssetManager.class,
My recommendation would be to go with method #2.
Usually, you would do something like this:
@Mock
private AssetManager mockAssetManager;
private AemContext context = new AemContext();
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
//This will register the adapTo(AssetManager)
context.registerAdapter(ResourceResolver.class, AssetManager.class, mockAssetManager);
}
@Test
void test_example() {
Asset asset = context.resourceResolver().getResource("/path/to/your/asset").adaptTo(Asset.class);
when(AssetManager.getAsset("/path/to/your/asset")).thenReturn(asset);
}
Check these references:
https://taylor.callsen.me/unit-testing-aem-sling-models-and-servlets/
Hope this helps
Thank you for your reply @EstebanBustamante
Below code is giving null
please check it
Correct. It will return null if you use the com.adobe.granite.asset.api.Asset
because it is not adaptable. Why are you using that? You should use the Asset implementation from com.day.cq.dam.api.Asset
.
If you really want to stick with the other Asset implementation, you need to mock that class and not rely on the aemContext
object. That's what I suggested as option #1 in my previous response. You should mock all of the following objects: ResourceResolver
, the AdaptTo
method, the AssetManager
, and the Asset
. The drawback with this approach is that you are not testing anything because you are setting the expected value at each step. So the best solution is to use the correct Asset implementation.
Hope this helps
Thanks For your Reply @EstebanBustamante
In our project we are using "com.adobe.granite.asset.api.Asset" and we were told to use this api only as this is the latest one.