Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start adding integration tests #67

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
name: Build

on: [push, pull_request]
on:
# Trigger on push to develop or main branches
push:
branches:
- develop
- main

# Trigger on pull request (any branch that creates a PR)
pull_request:
branches:
- develop
- main

jobs:
build:
Expand All @@ -26,3 +37,14 @@ jobs:

- name: Build
run: mvn --batch-mode --update-snapshots install

- name: Integration tests
run: mvn --batch-mode clean verify -pl it.tests -Pit

# Save logs only if the job fails
- name: Upload logs on failure
if: failure()
uses: actions/upload-artifact@v3
with:
name: failure-logs
path: it.tests/target/launchers/model/logs/*
2 changes: 1 addition & 1 deletion bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</goals>
<configuration>
<bnd><![CDATA[
Import-Package: com.day.cq.*;resolution:=optional,org.apache.commons.mail;resolution:=optional,*
Import-Package: com.day.cq.*;resolution:=optional,org.apache.commons.mail;resolution:=optional,org.apache.sling.distribution;resolution:=optional
DynamicImport-Package: *
]]></bnd>
</configuration>
Expand Down
195 changes: 195 additions & 0 deletions it.tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>be.orbinson.aem</groupId>
<artifactId>aem-groovy-console</artifactId>
<version>19.0.9-SNAPSHOT</version>
</parent>

<artifactId>aem-groovy-console-it-tests</artifactId>
<name>AEM Groovy Console - IT Tests</name>

<repositories>
<!-- Be able to depend on the latest Sling feature releases -->
<repository>
<id>apache-snapshot</id>
<url>https://repository.apache.org/content/groups/snapshots</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<executions>
<execution>
<id>bnd-process</id>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>it</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>http.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>slingfeature-maven-plugin</artifactId>
<version>1.8.4</version>
<extensions>true</extensions>
<executions>
<execution>
<id>aggregate-features</id>
<goals>
<goal>aggregate-features</goal>
</goals>
<configuration>
<aggregates>
<aggregate>
<includeArtifact>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.starter</artifactId>
<classifier>oak_tar</classifier>
<type>slingosgifeature</type>
<version>13-SNAPSHOT</version>
</includeArtifact>
<filesInclude>*.json</filesInclude>
</aggregate>
</aggregates>
<replacePropertyVariables>groovy.version</replacePropertyVariables>
</configuration>
</execution>
<execution>
<goals>
<goal>attach-features</goal>
</goals>
<id>attach-features</id>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>feature-launcher-maven-plugin</artifactId>
<version>0.1.6</version>
<configuration>
<launches>
<launch>
<id>model</id>
<skip>${skipTests}</skip>
<feature>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>slingosgifeature</type>
</feature>

<launcherArguments>
<frameworkProperties>
<org.osgi.service.http.port>${http.port}</org.osgi.service.http.port>
</frameworkProperties>
<!-- Uncomment to enable debugging of IT tests -->
<!--<vmOptions>-->
<!-- <value>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5000</value>-->
<!--</vmOptions>-->
</launcherArguments>
<startTimeoutSeconds>180</startTimeoutSeconds>
</launch>
</launches>
</configuration>
<executions>
<execution>
<goals>
<goal>start</goal>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<systemPropertyVariables>
<HTTP_PORT>${http.port}</HTTP_PORT>
<build.log.file>${project.build.directory}/../build.log</build.log.file>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencies>
<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
8 changes: 8 additions & 0 deletions it.tests/src/main/features/groovy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bundles": [
{
"id": "org.apache.groovy:groovy-dateutil:${groovy.version}",
"start-order": "20"
}
]
}
8 changes: 8 additions & 0 deletions it.tests/src/main/features/groovyconsole-repoinit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create path /conf/groovyconsole/replication(sling:Folder)
create path /var/groovyconsole/audit(sling:Folder)

create service user aem-groovy-console-service with path system/aem-groovy-console
set ACL for aem-groovy-console-service
allow jcr:all on /
allow jcr:all on /var/groovyconsole/audit
end
25 changes: 25 additions & 0 deletions it.tests/src/main/features/groovyconsole.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

{
"bundles":[
{
"id": "be.orbinson.aem:groovy-osgi:${project.version}",
"start-order": "19"
},
{
"id":"be.orbinson.aem:aem-groovy-console-api:${project.version}",
"start-order":"20"
},
{
"id":"be.orbinson.aem:aem-groovy-console-bundle:${project.version}",
"start-order":"20"
}
],
"configurations": {
"org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended~groovyconsole":{
"user.mapping":[
"aem-groovy-console-bundle=[aem-groovy-console-service]"
]
}
},
"repoinit:TEXT|true":"@file"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package be.orbinson.aem.groovy.console.it;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.*;

class GroovyConsoleServiceIT {

private static final int SLING_PORT = Integer.getInteger("HTTP_PORT", 8080);

@BeforeEach
void beforeEach() {
await().atMost(30, TimeUnit.SECONDS) // Set maximum wait time to 30 seconds
.pollInterval(5, TimeUnit.SECONDS)
.untilAsserted(() -> assertTrue(servicesAreAvailable()));
}

@Test
void testScriptReturnsResult() throws Exception {
String script = "print 'test'";
JsonObject response = executeScript(script);
System.out.println("Got response script at " + Instant.now());

assertNotNull(response, "Could not get response from API");
assertEquals("test", response.get("output").getAsString());
}

private static boolean servicesAreAvailable() throws IOException {
System.out.println("Checking if services are available");
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpGet printHealth = new HttpGet("http://localhost:" + SLING_PORT + "/system/health.json?tags=systemalive,bundles");
printHealth.addHeader("Authorization", "Basic " + Base64.encodeBase64String("admin:admin".getBytes(StandardCharsets.UTF_8)));
try (CloseableHttpResponse response = httpclient.execute(printHealth)) {
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
JsonObject jsonResponse = JsonParser.parseString(body).getAsJsonObject();
try {
boolean systemAlive = "OK".equals(jsonResponse.get("overallResult").getAsString());
if (!systemAlive) {
System.out.println("Not alive yet:");
System.out.println(body);
}
return systemAlive;
} catch (JsonSyntaxException e) {
return false;
}
}
}
}


private static JsonObject executeScript(String script) throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpPost executeScript = new HttpPost("http://localhost:" + SLING_PORT + "/bin/groovyconsole/post");
List<BasicNameValuePair> basicNameValuePairs = new java.util.ArrayList<>();
basicNameValuePairs.add(new BasicNameValuePair("script", script));
executeScript.setEntity(new UrlEncodedFormEntity(basicNameValuePairs, StandardCharsets.UTF_8));
executeScript.addHeader("Authorization", "Basic " + Base64.encodeBase64String("admin:admin".getBytes(StandardCharsets.UTF_8)));

try (CloseableHttpResponse response = httpclient.execute(executeScript)) {
System.out.println(response.getStatusLine());

String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
try {
return JsonParser.parseString(body).getAsJsonObject();
} catch (JsonSyntaxException e) {
System.out.println("Could not parse body from JSON: " + e.getMessage());
return null;
}
}
}
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<module>ui.apps.aem</module>
<module>ui.config</module>
<module>ui.content</module>
<module>it.tests</module>
</modules>

<url>https://github.com/orbinson/aem-groovy-console</url>
Expand Down
Loading