Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

JUNIT for DAM asset

Avatar

Community Advisor

I am trying to write JUNIT using AEMMockitoExtension. 
But below line of code is giving null in main class. I have loaded my json file where ":jcr:data" is also provided for binary type asset which is text.

asset.getOriginal().getStream()

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

I was able to fix this, loading asset directly from JSON file seems to create issue for "getStream" function, as method doesn't recognize "jcr:data" value of JSON as binary stream.
To overcome this I created a asset and added a rendition to it with below line of code

Asset asset = context.create().asset("/content/dam/sample/sample.txt", 1, 1, "text/plain");
asset.addRendition("original", new ByteArrayInputStream("Convert to bytes".getBytes(StandardCharsets.UTF_8)), "jcr:data");





View solution in original post

5 Replies

Avatar

Community Advisor

Hi @TarunKumar ,

 

Firstly, please check asset.getOriginal() returns value. If the value is null, the operation on null throws Nullpointer.


Anyways you can use the below code to Mock.

 

Rendition rendition = mock(Rendition.class);
Asset mockAsset = mock(Asset.class);
when(rendition.getStream()).thenReturn(this.getClass().getClassLoader().getResourceAsStream("image/<image-name>.jpg"));

 

Avatar

Community Advisor

Hi @Vaibhavi_J ,

asset.getOriginal() returns value but the method getStream() returns null. My asset is already mocked as I am directly loading from json file, so I believe no need to mock explicitly as suggested above. So if I follow the above suggested approach then asset that I am loading through JSON file will not take place.
The issue is that "jcr:data" property is not being read from JSON file.

Thanks
Tarun

Avatar

Community Advisor

I tried previously but failed to load asset directly from Json.
You can do one thing,

Asset asset = context.create().asset("/content/dam/mydoc.pdf", 1, 1, "application/pdf");
// Then use 'when' to initialize the asset based on you implementation
// when(initializerFunction()).thenReturn(asset); 

Avatar

Correct answer by
Community Advisor

I was able to fix this, loading asset directly from JSON file seems to create issue for "getStream" function, as method doesn't recognize "jcr:data" value of JSON as binary stream.
To overcome this I created a asset and added a rendition to it with below line of code

Asset asset = context.create().asset("/content/dam/sample/sample.txt", 1, 1, "text/plain");
asset.addRendition("original", new ByteArrayInputStream("Convert to bytes".getBytes(StandardCharsets.UTF_8)), "jcr:data");





Avatar

Community Advisor

Yes, the same thing I told you to do create a mock asset and send it to the original class.