Posts

Create/build and install OSGi bundle of third party APIs/dependency using maven plugins

Image
This post details the steps to create and deploy OSGI bundle out of third party API/dependency using relevant plugins in simple maven project. We would have been in situation where OSGi bundle created out of third party jar is added/available in our instance and our project code base using the third party bundle being in active state. However at the time of execution, ClassNotFound or NoClassDefFound error pops up. This is the case when the dependencies of our desired third party API is not included/available when it is added as an OSGi bundle. We can overcome this by creating the bundle using maven-bundle-plugin which has option to include direct (desired third party dependency - highlighted in red in below screenshot -  * -  to include all the dependency entry in the pom.xml file or  artifactId name if we would like to mention explicitly  Note : There are several other possible options documented for <Embed-Dependency> in official plugin page (Mentioned the...

Customization in AEM Asset Quick Publish Action

Image
This post is about a requirement raised in Experience League Community forum related to Custom implementation on Asset Publish.   Project has custom field named "Effective date". If an asset is selected to publish before an Effective date, pop up should be triggered with option to either "Cancel" or "Continue to Publish" Requirement of this sort involving customizations on top of an OOB functionality can be approached as follows Locate the OOB files related to the functionality Custom code readiness Understanding the existing logic Incorporating our custom logic by overlaying the OOB files. Testing all possible use cases related to custom functionality without hindering any other related  OOB flow.  Locate the OOB files: As part of locating the OOB files, first step is to trigger a "Quick Publish" on an Asset and observe the flow. While observing the flow for this purpose, we need to have Developer tools on -> Network Tab with "Preserve L...

Unit testing in AEM - Debugging issues in Test class

Image
This post is for illustrating the possible errors/exception we get while writing Test class. Also highlights the possible ways of creating Test content and the ResourceResolverType available for creating AemContext (from io.wcm framework) Test content : Most important aspect of Unit Testing is creating Test content which in AEM sense is to provide a means of repository at the time of code build phase to test our Java code (written interacting with repo) For creating the same, we already saw that there are 3 possible APIs -  Content Loader, Content Builder and Resource Builder available. Most often we use Content Loader making use of JSON file at class path and loading it to dummy content path. Below are the sample snippet making use of other two APIs. Content Builder : private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK); ContentBuilder contentBuilder = aemContext.create(); With this contentBuilder object, we can create page , asset , tag etc - Full...

Unit Testing Hands on - For Sling Servlet

Image
This post is about creating Unit Test class for Sling Servlet, another commonly used Java class as part of an AEM application.  In Sling servlets, we have  SlingSafeMethodsServlet - read only servlet supporting GET  (doGet) SlingAllMethodsServlet - Supports POST, PUT and DELETE (doPost/doPut/doDelete) In either case, we have request and response objects using which desired code logic is written in Servlet.  For Unit testing, we have Mock sling request and sling response from Sling Mocks ( org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest  and org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse respectively) Writing unit test for Servlet involves the following Instantiate Servlet to be tested. Get mock request and response from AemContext Prepare the mock request object (per the code logic before calling doGet/doPost) Call doGet/doPost method of Servlet to be tested Assert the Servlet response (per the code logic) Inst...

Unit Testing Hands on - For WCMUsePojo

Image
This post illustrate the unit test for WCMUsePojo , Java class for backend logic as part of  AEM Component development. Quick Recap about WCMUsePojo :   WCMUsePojo initializes the objects associated with Bindings (Eg: WCMBindings/SlingBindings) via its init(Bindings) method which in turn calls activate() method for post initialization tasks. This init(Bindings bindings) method gets called if the POJO is instantiated in HTL via data-sly-use attribute. Bindings(javax.script.Bindings)  extends  Map(java.util.Map)  and hence it is basically a map object where  key  (type String) is the global variable and  value  is the respective object.  Once when the bindings are initialized, we are able to make use of the global variables which is available to us via methods like  getCurrentPage(), getProperties() etc. Example : When we call getCurrentPage() to get page object, below happens behind the scene  (considering the above flow) get...

Unit Testing in AEM - Hands on

Image
This post is about hands-on on Unit Testing Java class, part of an AEM application. We will be using AEM Mocks from  io.wcm.testing.mock.aem.junit5.*  and Mockito framework -  org.mockito.* (reason for using this is explained in previous post ) JUnit Version : JUnit 5 AEM Maven archetype : 22 IDE : Eclipse Testing related maven dependency : (available by default in AEM Maven archetype) JUnit5 (org.junit) AEM mocks (io.wcm.testing.aem-mock.junit5) Mockito framework (org.mockito) We mostly follow "Implementation first development" approach - Desired functionality is first coded and then test case is written for the same. For creating Test Java class,  Create Test Java Class Create AemContext Implement setUp method (creating test fixture, annotated with @BeforeEach ) Implement methods to be tested. (annotated with @Test ) Create Test Java Class: In Eclipse IDE, right click on the Java class to be tested ->  New -> Other -> JUnit -> JUnit Test case We ...

Unit testing in AEM - Introduction

Image
This post is about an introduction to Unit testing the Java class part of AEM application by starting with quick recap of JUnit framework followed by Mocking and APIs available specific to AEM with respect to testing.  Java class that we write as part of AEM involves Sling API/JCR API/AEM related APIs and it all ultimately targets the content on our repository.  In other words, the logic revolves around the content which in AEM context, is a Resource/Node and its related properties (may it be a Sling model/WCMUsePojo/Sling Servlets/OSGI component/any related for that matter)  Quick recap of basics: JUnit is the testing framework for Java and is available under the package - org.junit.* It provides Test Fixture, Test Runner and Test class Test fixture is fixed state of objects in which tests are run. One of the methods it includes which is relevant for us is setUp method that we write in Test class. (Annotated with @BeforeEach, JUnit5 / @Before, JUnit 4) Test class has m...