Expand my Community achievements bar.

SOLVED

Packaging Junit

Avatar

Level 4

Hi,

 

I have created a service to create packages. I want to know how to write Junit test for this service. PFB the service code for creating package. 

 

Any help would be great. Thanks in advance!!

 

     @reference
     private Packaging packaging;

     public void createPackage(Node rootNode, String pkgName) {
        JcrPackageManager jcrPackageManager = packaging.getPackageManager(session);
        JcrPackage jcrPackage = jcrPackageManager.create(rootNode, pkgName);
		JcrPackageDefinition jcrPackageDefinition = jcrPackage.getDefinition();
		DefaultWorkspaceFilter workspaceFilter = new DefaultWorkspaceFilter();
		
		for (PathFilterSet packageFilter : packageFilters) {
			workspaceFilter.add(packageFilter);
		}
		jcrPackageDefinition.setFilter(workspaceFilter, AUTO_SAVE); 
		jcrPackageDefinition.set(JcrPackageDefinition.PN_DESCRIPTION, PACKAGE_DESCRIPTION + pkgName, false);
		jcrPackageDefinition.set(JcrPackageDefinition.PN_GROUP, rootNode.getName() , false);
		jcrPackageManager.assemble(jcrPackage, listener);
     }

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@santhoshsrg,

The testing libraries I would use here are:

If you are looking for examples, take a look at this example of how they mocked the Packaging dependency, https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad....

Another example here, https://www.programcreek.com/java-api-examples/?code=Adobe-Consulting-Services/acs-aem-commons/acs-a....

I hope these examples will steer you in a better direction. 

View solution in original post

6 Replies

Avatar

Correct answer by
Community Advisor

@santhoshsrg,

The testing libraries I would use here are:

If you are looking for examples, take a look at this example of how they mocked the Packaging dependency, https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad....

Another example here, https://www.programcreek.com/java-api-examples/?code=Adobe-Consulting-Services/acs-aem-commons/acs-a....

I hope these examples will steer you in a better direction. 

Avatar

Level 4

Thanks @BrianKasingli and @VeenaVikraman  for answers.

 

@BrianKasingli The first example you shared matches most of my scenario. But still I am not able to create package using that approach. I have updated my code based on that. But I am getting different exception now. PFB part of my code.

Could you please let me know if there is any mistake. The service actually works fine. The problem is happening only in Junit.

Exception:

javax.jcr.RepositoryException: Item.save() not allowed on new item

at org.apache.jackrabbit.oak.jcr.session.ItemImpl.save(ItemImpl.java:267)

at org.apache.jackrabbit.oak.jcr.session.NodeImpl.save(NodeImpl.java:105)

at org.apache.jackrabbit.vault.packaging.impl.JcrPackageImpl.createNew(JcrPackageImpl.java:221)

at org.apache.jackrabbit.vault.packaging.impl.JcrPackageManagerImpl.create(JcrPackageManagerImpl.java:344)

 

Junit Code:

 

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class CreateComplianceFileProcessTest {

	public final AemContext context = new AemContext(ResourceResolverType.JCR_OAK);

	@InjectMocks
	private CreateComplianceFileProcess createCompFileWP;

    @Test
	void testExecute() {
	        resolver = context.resourceResolver();
			session = resolver.adaptTo(Session.class);
			when(complianceResolverService.getResourceResolver()).thenReturn(resolver);
			when(packaging.getPackageManager(any(Session.class))).thenReturn(new JcrPackageManagerImpl(session));
            createCompFileWP.execute(item, null, null);
    }
}

 

 

Avatar

Community Advisor
It looks like you are on the right track.

Avatar

Level 4
Thanks for update. I have followed the same approach as in the first example but i am getting issue on OOTB org.apache.jackrabbit.oak.jcr.session.ItemImpl.save method during jcrPackageManager.create(parentNode, pkgName) function call

Avatar

Level 4

In the example https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad..., they have used jcrPackageManager.create(groupName, name, version) which is also working for me. But methods mentioned below are not working. These are throwing exceptions in "org.apache.jackrabbit" classes.

 

Methods not working:

  • jcrPackageManager.create(rootNode, pkgName)
  • jcrPackageManager.assemble(jcrPackage, listener)