Junit test case - Creating packages

santhoshsrg

19-06-2020

I am trying to create Junit test case for creating packages. I have used link https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad... for this. PFB my service and Junit code. I am getting an exception for creating package which i have mentioned below.

 

Any help would be great. Thanks in advance!!

 

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)

 

Service Code:

 

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

 

 

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

 

Accepted Solutions (0)

Answers (0)