Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

Junit test case - Creating packages

santhoshsrg
Level 4
Level 4

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

 

1 Reply
Theo_Pendle
Community Advisor
Community Advisor
Hi, I would like to help you but reproducing the error locally is going to be very time-consuming. Could you provide a Java class and test that emulate this issue that I can just copy-paste?