Expand my Community achievements bar.

SOLVED

does anyone know how to adopt json resource as asset while writing junit?

Avatar

Level 4

context.load().json("/my/folder/co/aem/core/workflow/dam/xyz.json", "/content/dam/test/abc.zip");

Resource resource = context.resourceResolver().getResource("/content/dam/test/abc.zip");

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

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
10 Replies

Avatar

Level 4

Hi Arun,

 

Thanks for your reply. i didnot get any reference.

i'm trying to adapt "resource.adaptTo(Asset.class)" resource as Asset but i'm getting null not sure why this is happening.

Here i'm getting resource.

Avatar

Community Advisor

Can you try to chekc if Resource is null or not?

If Resource is not null then make sure your json contains node type of dam:Asset

I have shared the reference to let you know, how you can created resources using json in your test cases



Arun Patidar

Avatar

Level 4

Hi Arun,

 

Resource is not null and node type is dam:Asset. still i'm not getting the Asset asset = resource.adaptTo(Asset.class); 

Avatar

Community Advisor

Can you please share the json here?



Arun Patidar

Avatar

Level 4

Hi Arun,  here its

{
"jcr:primaryType":"sling:Folder",
"jcr:createdBy":"admin",
"jcr:created":"Tue Nov 15 2022 12:24:07 GMT+0530",
"abc.zip":{
"jcr:primaryType":"dam:Asset",
"jcr:mixinTypes":[
"mix:referenceable"
],
"jcr:createdBy":"admin",
"jcr:created":"Tue Nov 15 2022 13:17:18 GMT+0530",
"jcr:uuid":"5335343gfdsfgafdaafdf",
"jcr:content":{
"jcr:primaryType":"dam:AssetContent",
"dam:assetState":"processing",
"renditions":{
"jcr:primaryType":"nt:folder",
"jcr:createdBy":"admin",
"jcr:created":"Tue Nov 15 2022 13:17:18 GMT+0530",
"original":{
"jcr:primaryType":"nt:file",
"jcr:createdBy":"admin",
"jcr:created":"Tue Nov 15 2022 13:17:18 GMT+0530",
"jcr:content":{
"jcr:primaryType":"oak:Resource",
"jcr:lastModifiedBy":"admin",
"jcr:mimeType":"application/zip",
"jcr:lastModified":"Tue Nov 15 2022 13:17:18 GMT+0530",
":jcr:data":13721771
}
}
},
"related":{
"jcr:primaryType":"nt:unstructured"
},
"metadata":{
"jcr:primaryType":"nt:unstructured",
"jcr:mixinTypes":[
"cq:Taggable"
]
}
}
},
"jcr:content":{
"jcr:primaryType":"nt:unstructured",
"jcr:title":"testing",
"dam:noThumbnail":true,
"sourcing":"false",
"folderThumbnail":{
"jcr:primaryType":"nt:file",
"jcr:createdBy":"abc-service",
"jcr:created":"Tue Nov 15 2022 13:01:03 GMT+0530",
"jcr:content":{
"jcr:primaryType":"nt:unstructured",
"dam:folderThumbnailPaths":[

],
"height":140,
"bgcolor":-1,
"width":240,
"jcr:lastModified":"Tue Nov 15 2022 13:17:10 GMT+0530",
"jcr:data":631
}
}
}
}

Avatar

Community Advisor

Your json is incorrect?

Try to check the primary type of Resource, it should be dam:Asset ?

 

you can remove the first nesting e.g. your json should look like below

{
    "jcr:primaryType": "dam:Asset",
    "jcr:mixinTypes": [
        "mix:referenceable"
    ],
    "jcr:content":
    {
        "jcr:primaryType": "dam:AssetContent",
        "dam:assetState": "processing",
        "renditions":
        {
            "jcr:primaryType": "nt:folder",
            "original":
            {
                "jcr:primaryType": "nt:file",
                "jcr:content":
                {
                    "jcr:primaryType": "oak:Resource",
                    "jcr:mimeType": "application/zip",
                    ":jcr:data": 13721771
                }
            }
        },
        "related":
        {
            "jcr:primaryType": "nt:unstructured"
        },
        "metadata":
        {
            "jcr:primaryType": "nt:unstructured",
            "jcr:mixinTypes": [
                "cq:Taggable"
            ]
        }
    }
}


Arun Patidar

Avatar

Level 4

Hi Arun,

primary type of Resource is dam:Asset but still i'm not able to adopt it as asset.

Avatar

Correct answer by
Community Advisor

Avatar

Level 4

Hi Arun,

it works for me. Thank you so much for helping on this. I really  appreciate your effort on this!!