Search This Blog

Monday 13 February 2017

How to create osgi configuration with selection dropdown property/fields



@Property(label = "Select One Value", name = "label", options = {

                     @PropertyOption(name = "title1", value = "value1"),

                     @PropertyOption(name = "title2", value = "value2") 

      }, value = "defaultvalue", description =  "Some Description")

private static final String 

PROPERTY_SCHEDULER_DATASOURCE = "label";



private String label;

//And then

@Activate

protected void activate(final Map < String, Object > config) {

        this.lebel = PropertiesUtil.toString(config.get("label"), 

                           "some_default_value");


}


Saturday 4 February 2017

How to separate normal build, unit test and integration test



Ideally integration test should be created in a separate bundle. Hence the below steps should be done on the integration test pom. Ie unhcr.it.tests/pom.xml.
Ideally during the build, integration test should not be executed but unit test can be executed.

Steps to exclude integration tests by default.
1.      Exclude integration test from maven-surefire-plugin plugin(plugin for unit tests)
                    <excludes>
                        <exclude>**/*ITTest.java</exclude>
                    </excludes>
</configuration>

2.      In maven-failsafe-plugin include IT test.

<configuration>
                    <skipITs>true</skipITs>
                    <includes>
                        <include>**/*ITTest.java</include>
                    </includes>
</ configuration>

3.      Add <skipITs>true</skipITs> as above.

4.     This will allow you to run with tests disabled by default and to run them with this command:
mvn install -DskipITs=false

Similarly <skipTests>true</skipTests> can be used for skipping both unit and integration tests. Since skipTests is also supported by the Surefire Plugin, this will have the effect of not running any tests. If, instead, you want to skip only the integration tests being run by the Failsafe Plugin, you would use the skipITs property instead as already shown above.


Teleporter integration test Aem



No mocking, all test executed on running aem instance.


UPDATING YOUR POM


First you will need to add the following plugins to your project's Maven POM.  The part each plugin acts in the process is described below the plugin configuration.
<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
         <execution>
             <id>copy-runnable-jar</id>
             <goals>
                 <goal>copy-dependencies</goal>
             </goals>
             <phase>package</phase>
             <configuration>
                 <outputDirectory>${project.build.directory}/sling</outputDirectory>
                 <includeArtifactIds>org.apache.sling.launchpad</includeArtifactIds>
                 <excludeTransitive>true</excludeTransitive>
                 <overWriteReleases>false</overWriteReleases>
                 <overWriteSnapshots>false</overWriteSnapshots>
             </configuration>
         </execution>
         <execution>
             <id>copy-dependencies</id>
             <goals>
                 <goal>copy-dependencies</goal>
             </goals>
             <phase>package</phase>
             <configuration>
                 <outputDirectory>${project.build.directory}/sling/additional-bundles</outputDirectory>
                 <!-- Include artifact id's of all of the bundles to install here -->
                 <includeArtifactIds>jstl</includeArtifactIds>
                 <excludeTransitive>true</excludeTransitive>
                 <overWriteReleases>false</overWriteReleases>
                 <overWriteSnapshots>false</overWriteSnapshots>
             </configuration>
         </execution>
     </executions>
 </plugin>
The copy-dependencies Maven Dependency plugin will copy the bundles you need to install for your integration tests from your local Maven Repository to the filesystem. Additionally,
the copy-runnable-jar execution will copy the Sling Launchpad jar into your target directory so it can be executed.
<plugin>
     <artifactId>maven-antrun-plugin</artifactId>
     <executions>
         <execution>
             <phase>package</phase>
             <configuration>
                 <tasks>
                     <copy
                         file="${project.build.directory}/${project.build.finalName}.jar"
                         toDir="${project.build.directory}/sling/additional-bundles"
                         verbose="true" />
                 </tasks>
             </configuration>
             <goals>
                 <goal>run</goal>
             </goals>
         </execution>
     </executions>
 </plugin>
The AntRun Plugin used to copy the output of the built into the additional-bundles folder to be installed into the Sling testing instance when it starts. If your integration tests are part of a separate testing project this will not be required.


Maven Failsafe Plugin


The Failsafe Plugin is designed to run integration tests while the Surefire Plugin is designed to run unit tests. The name (failsafe) was chosen both because it is a synonym of surefire and because it implies that when it fails, it does so in a safe way.
The Maven Failsafe plugin defines all of the properties and runs the tests. Note that the bundles to install must be defined here as well.
<!--
    Define additional bundles to install by specifying the beginning of their artifact name.
    The bundles are installed in lexical order of these property names.
    All bundles must be listed as dependencies in this pom, or they won't be installed.
-->

<!--  Install this bundle for integration tests -->
<sling.additional.bundle.2>${project.artifactId}-${project.version}.jar</sling.additional.bundle.2>

Important to note here is that org.apache.sling.junit.core bundle needs to be installed on the AEM instance so that integration test can be executed else error will be seen in the logs : 404 , not found - /system/sling/junit/<test-class-name>… This bundle is installed thru sling.additional.bundle for which it should also be added as a dependency (do not use provided scope).

Next, add the following dependencies:
Integration test dependencies
<dependency>
     <groupId>org.apache.sling</groupId>
     <artifactId>org.apache.sling.commons.testing</artifactId>
     <version>2.0.24</version>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>org.apache.sling</groupId>
     <artifactId>org.apache.sling.testing.tools</artifactId>
     <version>1.0.6</version>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>org.apache.sling</groupId>
     <artifactId>org.apache.sling.launchpad</artifactId>
     <version>6</version>
     <classifier>standalone</classifier>
     <scope>test</scope>
 </dependency>

Teleporter dependencies
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.junit.teleporter</artifactId>
            <version>1.0.8</version>
</dependency>
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.junit.core</artifactId>
    <version>1.0.14</version>
</dependency>



INTEGRATION TEST CLASS


Create a teleporter customizer in org.apache.sling.junit.teleporter.customizers package for example BWIT_TeleporterCustomizer.
Then create a Teleporter test class for writing the integration tests and create rule as below.
@Rule
    public final TeleporterRule teleporter = TeleporterRule.forClass(
            getClass(), "BWIT_Teleporter");

BWIT_TeleporterCustomizer


The BWIT_TeleporterCustomizer.java relies on SlingTestBase to set the server's base url and credentials. Additionally, the test bundle is adjusted so that the API is not included in it (but rather referenced from another bundle). The bootstrapping of the Sling instance is tweaked through system properties which are desribed here and implicitly done by the customizer itself.
This teleporter.getService() can be used for getting other service or class objects.
msrpSynchronizationService = teleporter
                .getService(MSRPSynchronizationService.class);
resourceResolverFactory = teleporter
                .getService(ResourceResolverFactory.class);

Resourceresolver is important for creating resource and all for testing, and it get be got through
resourceResolver = resourceResolverFactory
                .getAdministrativeResourceResolver(null);
No need to mock anything here. Directly the method to be tested can be called for different content from different test methods.


How, Where and when are test executed?

Where – on running aem instance.
When – during the build.
How – mvn clean install