Expand my Community achievements bar.

SOLVED

AEM6.5 Junit with MockitoJUnitRunner for TagManager

Avatar

Level 8

Hi,

I am using AEM6.5 Junit with MockitoJUnitRunner

 

I need to mock 

import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
public Tag[] getTags( Resource resource, ResourceResolver resourceResolver) {
Resource metadataResource = resource.getChild("jcr:content").getChild("metadata");
TagManager tagMgr = resourceResolver.adaptTo(TagManager.class);
return tagMgr.getTags(metadataResource);
}

 I tried with Test class:-


@Deleted Account
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

@Mock
protected TagManager tagManager;

private Resource resource;

@test
public void testAssetByteSizeValue(){
context.load().json("asset.json", "test.pdf");
resource = context.create().
resource("test.pdf");
Assert.assertEquals("not working"
,assetReportServletPojo.getTags(resource,context.resourceResolver()));
}


Could you please help with how could I write the junit as it is breaking at
1>
TagManager tagMgr = resourceResolver.adaptTo(TagManager.class);
return tagMgr.getTags(metadataResource);

2>
Also how could I pass the Tag[] in expected value instead of "not working" in

Assert.assertEquals("not working"
,assetReportServletPojo.getTags(resource,context.resourceResolver()));

 

Note:-

The asset.json  has the required below tag present under metadata

"cq:tags":["tag1","tag2"]

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @srinivas_chann1, below you can find fully working example. Please keep in mind that I have added comparator, because the order of tags in your method getTags is not guarantee. Also I did not know how you exposing your method, is it via servlet, OSGi service or Sling Model. In my example I am using Sling Model - but in general it should be the same for all above types. I was able to run it without any problem.

  • Sling Model with your method:
    package com.mysite.core.models;
    
    import com.day.cq.tagging.Tag;
    import com.day.cq.tagging.TagManager;
    import org.apache.sling.api.resource.Resource;
    import org.apache.sling.api.resource.ResourceResolver;
    import org.apache.sling.models.annotations.Model;
    
    @Model(adaptables = Resource.class)
    public class MyModel {
    
        public Tag[] getTags(Resource resource, ResourceResolver resourceResolver) {
            Resource metadataResource = resource.getChild("jcr:content").getChild("metadata");
            TagManager tagMgr = resourceResolver.adaptTo(TagManager.class);
            return tagMgr.getTags(metadataResource);
        }
    }
  • json file, called assets.json and it is located directly under resources folder
    {
      "test.pdf": {
        "jcr:primaryType": "dam:Asset",
        "jcr:content": {
          "jcr:primaryType": "dam:AssetContent",
          "metadata": {
            "jcr:primaryType": "nt:unstructured",
            "cq:tags": ["tag1", "tag2"]
          }
        }
      }
    }
  • JUnit test
    package com.mysite.core.models;
    
    import com.day.cq.tagging.InvalidTagFormatException;
    
    import io.wcm.testing.mock.aem.junit.AemContext;
    import org.apache.sling.api.resource.Resource;
    import org.apache.sling.testing.mock.sling.ResourceResolverType;
    import org.junit.Assert;
    import org.junit.Test;
    import com.day.cq.tagging.Tag;
    import com.day.cq.tagging.TagManager;
    import org.mockito.junit.MockitoJUnitRunner;
    import org.junit.Rule;
    import org.junit.runner.RunWith;
    
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    
    @RunWith(MockitoJUnitRunner.class)
    public class MyModelTest {
    
        @Rule
        public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
    
        private Resource resource;
    
        @Test
        public void testAssetByteSizeValue() throws InvalidTagFormatException {
            context.load().json("/asset.json", "/content/dam");
            resource = context.resourceResolver().getResource("/content/dam/test.pdf");
    
            TagManager tagManager = context.resourceResolver().adaptTo(TagManager.class);
            Tag tag1 = tagManager.createTag("tag1", "Tag 1 title", "Tag 1 desc");
            Tag tag2 = tagManager.createTag("tag2", "Tag 2 title", "Tag 2 desc");
            Tag[] expectedTags = new Tag[] {tag1, tag2};
    
            Tag[] actualTags = new MyModel()
                    .getTags(resource, context.resourceResolver());
    
            Collections.sort(Arrays.asList(expectedTags), new TagsByNameComparator());
            Collections.sort(Arrays.asList(actualTags), new TagsByNameComparator());
    
            Assert.assertArrayEquals(expectedTags, actualTags);
        }
    
        private class TagsByNameComparator implements Comparator<Tag> {
            public int compare(Tag tag1, Tag tag2) {
                return tag1.getName().compareTo(tag2.getName());
            }
        }
    }

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @srinivas_chann1, below you can find fully working example. Please keep in mind that I have added comparator, because the order of tags in your method getTags is not guarantee. Also I did not know how you exposing your method, is it via servlet, OSGi service or Sling Model. In my example I am using Sling Model - but in general it should be the same for all above types. I was able to run it without any problem.

  • Sling Model with your method:
    package com.mysite.core.models;
    
    import com.day.cq.tagging.Tag;
    import com.day.cq.tagging.TagManager;
    import org.apache.sling.api.resource.Resource;
    import org.apache.sling.api.resource.ResourceResolver;
    import org.apache.sling.models.annotations.Model;
    
    @Model(adaptables = Resource.class)
    public class MyModel {
    
        public Tag[] getTags(Resource resource, ResourceResolver resourceResolver) {
            Resource metadataResource = resource.getChild("jcr:content").getChild("metadata");
            TagManager tagMgr = resourceResolver.adaptTo(TagManager.class);
            return tagMgr.getTags(metadataResource);
        }
    }
  • json file, called assets.json and it is located directly under resources folder
    {
      "test.pdf": {
        "jcr:primaryType": "dam:Asset",
        "jcr:content": {
          "jcr:primaryType": "dam:AssetContent",
          "metadata": {
            "jcr:primaryType": "nt:unstructured",
            "cq:tags": ["tag1", "tag2"]
          }
        }
      }
    }
  • JUnit test
    package com.mysite.core.models;
    
    import com.day.cq.tagging.InvalidTagFormatException;
    
    import io.wcm.testing.mock.aem.junit.AemContext;
    import org.apache.sling.api.resource.Resource;
    import org.apache.sling.testing.mock.sling.ResourceResolverType;
    import org.junit.Assert;
    import org.junit.Test;
    import com.day.cq.tagging.Tag;
    import com.day.cq.tagging.TagManager;
    import org.mockito.junit.MockitoJUnitRunner;
    import org.junit.Rule;
    import org.junit.runner.RunWith;
    
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    
    @RunWith(MockitoJUnitRunner.class)
    public class MyModelTest {
    
        @Rule
        public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);
    
        private Resource resource;
    
        @Test
        public void testAssetByteSizeValue() throws InvalidTagFormatException {
            context.load().json("/asset.json", "/content/dam");
            resource = context.resourceResolver().getResource("/content/dam/test.pdf");
    
            TagManager tagManager = context.resourceResolver().adaptTo(TagManager.class);
            Tag tag1 = tagManager.createTag("tag1", "Tag 1 title", "Tag 1 desc");
            Tag tag2 = tagManager.createTag("tag2", "Tag 2 title", "Tag 2 desc");
            Tag[] expectedTags = new Tag[] {tag1, tag2};
    
            Tag[] actualTags = new MyModel()
                    .getTags(resource, context.resourceResolver());
    
            Collections.sort(Arrays.asList(expectedTags), new TagsByNameComparator());
            Collections.sort(Arrays.asList(actualTags), new TagsByNameComparator());
    
            Assert.assertArrayEquals(expectedTags, actualTags);
        }
    
        private class TagsByNameComparator implements Comparator<Tag> {
            public int compare(Tag tag1, Tag tag2) {
                return tag1.getName().compareTo(tag2.getName());
            }
        }
    }