Packaging Junit

santhoshsrg

15-06-2020

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);
     }

 

Answers (3)

Answers (3)

santhoshsrg

19-06-2020

Thanks @BrianKasingli and @Veena_Vikram  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);
    }
}

 

 

santhoshsrg

22-06-2020

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)