Packaging Junit

Avatar

Avatar
Give Back 5
Level 3
santhoshsrg
Level 3

Likes

22 likes

Total Posts

56 posts

Correct reply

2 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 3
Ignite 1
View profile

Avatar
Give Back 5
Level 3
santhoshsrg
Level 3

Likes

22 likes

Total Posts

56 posts

Correct reply

2 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 3
Ignite 1
View profile
santhoshsrg
Level 3

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

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

607 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

607 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

16-06-2020

@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. 

Answers (3)

Answers (3)

Avatar

Avatar
Give Back 5
Level 3
santhoshsrg
Level 3

Likes

22 likes

Total Posts

56 posts

Correct reply

2 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 3
Ignite 1
View profile

Avatar
Give Back 5
Level 3
santhoshsrg
Level 3

Likes

22 likes

Total Posts

56 posts

Correct reply

2 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 3
Ignite 1
View profile
santhoshsrg
Level 3

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

 

 

Avatar

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

484 likes

Total Posts

1,049 posts

Correct reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile

Avatar
Establish
MVP
Veena_Vikram
MVP

Likes

484 likes

Total Posts

1,049 posts

Correct reply

121 solutions
Top badges earned
Establish
Coach
Contributor 2
Seeker
Ignite 5
View profile
Veena_Vikram
MVP

16-06-2020

Avatar

Avatar
Give Back 5
Level 3
santhoshsrg
Level 3

Likes

22 likes

Total Posts

56 posts

Correct reply

2 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 3
Ignite 1
View profile

Avatar
Give Back 5
Level 3
santhoshsrg
Level 3

Likes

22 likes

Total Posts

56 posts

Correct reply

2 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 3
Ignite 1
View profile
santhoshsrg
Level 3

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)