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

SDK-358 - Add integration test framework for helper classes #310

Merged
merged 4 commits into from
Nov 19, 2024
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ jobs:
run: mvn --batch-mode org.openmrs.maven.plugins:openmrs-sdk-maven-plugin:setup-sdk -DbatchAnswers=n
- name: Build and run tests
run: mvn test --batch-mode
- name: Build and run integration tests
run: mvn verify -Pintegration-tests
82 changes: 82 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<description>Set of integration tests for OpenMRS SDK Maven Plugin</description>
<url>https://wiki.openmrs.org/display/docs/OpenMRS+SDK</url>

<properties>
<testBaseDir>${project.build.directory}/integration-test-base-dir</testBaseDir>
<testResourceDir>${testBaseDir}/test-resources</testResourceDir>
</properties>

<dependencies>
<!-- openmrs sdk maven plugin -->
<dependency>
Expand Down Expand Up @@ -46,8 +51,85 @@
<groupId>org.semver4j</groupId>
<artifactId>semver4j</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Build a jar containing the test classes so they are available for use outside of this project -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Set up an execution directory and add the source test resources to it for use within tests as needed -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${testResourceDir}</outputDirectory>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>integration-test/invokeIT/pom.xml</include>
</includes>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>integration-test/invokeIT/pom.xml</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<!--
Install the test jar to the local Maven repository so it is available during integration testing
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.3</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<classifier>tests</classifier>
<file>${project.build.directory}/${project.artifactId}-${project.version}-tests.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>integration-tests</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.openmrs.maven.plugins;


import lombok.Getter;
import lombok.Setter;
import org.junit.Before;
import org.openmrs.maven.plugins.utility.MavenEnvironment;

import java.io.File;

@Getter @Setter
public abstract class AbstractMavenIT extends AbstractSdkIT {

MavenEnvironment mavenEnvironment = null;

@Override
@Before
public void setup() throws Exception {
super.setup();
mavenEnvironment = null;
}

@Override
void addTestResources() throws Exception {
includePomFile("invokeIT", "pom.xml");
}

protected void executeTest(MavenTestFunction testFunction) throws Exception {
StackTraceElement invoker = Thread.currentThread().getStackTrace()[2];
String className = invoker.getClassName();
String testMethod = invoker.getMethodName();
if (mavenEnvironment == null) {
addTaskParam("className", className);
addTaskParam("methodName", testMethod);
addTaskParam(BATCH_ANSWERS, getAnswers());
addTaskParam("testMode", "true");
String plugin = resolveSdkArtifact();
verifier.executeGoal(plugin + ":" + InvokeMethod.NAME);
}
else {
testFunction.executeTest();
}
}

protected File getMavenTestDirectory() {
return new File(mavenEnvironment.getMavenProject().getBuild().getDirectory());
}

/**
* Simple interface that encapsulates a test that should be evaluated by tests that use this Mojo
*/
public interface MavenTestFunction {
void executeTest() throws Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.apache.commons.lang.StringUtils;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.apache.maven.it.util.ResourceExtractor;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
Expand Down Expand Up @@ -32,6 +31,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.UUID;
import java.util.zip.ZipFile;
Expand All @@ -52,10 +52,10 @@ public abstract class AbstractSdkIT {
/**
* contains name of directory in project's target dir, where integration tests are conducted
*/
static int counter = 0;
static final String TEST_DIRECTORY = "integration-test";
static final String MOJO_OPTION_TMPL = "-D%s=\"%s\"";
protected static final String BATCH_ANSWERS = "batchAnswers";

protected final ArrayDeque<String> batchAnswers = new ArrayDeque<>();

/**
Expand All @@ -72,6 +72,9 @@ public abstract class AbstractSdkIT {

File distroFile;

Path testBaseDir;
Path testResourceDir;

public String resolveSdkArtifact() throws MojoExecutionException {
Properties sdk = new Properties();
try (InputStream sdkPom = getClass().getClassLoader().getResourceAsStream("sdk.properties")) {
Expand All @@ -83,38 +86,39 @@ public String resolveSdkArtifact() throws MojoExecutionException {
return sdk.get("groupId")+":"+sdk.get("artifactId")+":"+sdk.get("version");
}

void includeTestResource(String fileName) throws Exception {
File source = getTestFile(TEST_DIRECTORY, fileName);
File target = new File(testDirectory, fileName);
if (source.isDirectory()) {
FileUtils.copyDirectory(source, testDirectory);
}
else {
FileUtils.copyFile(source, target);
void includeDistroPropertiesFile(String... paths) throws Exception {
Path sourcePath = testResourceDir.resolve(TEST_DIRECTORY);
for (String path : paths) {
sourcePath = sourcePath.resolve(path);
}
Path targetPath = testDirectoryPath.resolve(DistroProperties.DISTRO_FILE_NAME);
FileUtils.copyFile(sourcePath.toFile(), targetPath.toFile());
}

void includeDistroPropertiesFile(String fileName) throws Exception {
File source = getTestFile(TEST_DIRECTORY, fileName);
File target = new File(testDirectory, DistroProperties.DISTRO_FILE_NAME);
FileUtils.copyFile(source, target);
void includePomFile(String... paths) throws Exception {
Path sourcePath = testResourceDir.resolve(TEST_DIRECTORY);
for (String path : paths) {
sourcePath = sourcePath.resolve(path);
}
Path targetPath = testDirectoryPath.resolve("pom.xml");
FileUtils.copyFile(sourcePath.toFile(), targetPath.toFile());
}

void addTestResources() throws Exception {
includeTestResource("pom.xml");
includePomFile("pom.xml");
includeDistroPropertiesFile(DistroProperties.DISTRO_FILE_NAME);
}

@Before
public void setup() throws Exception {
String tempDirPath = System.getProperty("maven.test.tmpdir", System.getProperty("java.io.tmpdir"));
String executionDirName = "openmrs-sdk-" + getClass().getSimpleName() + "-" + UUID.randomUUID();
testDirectoryPath = Paths.get(tempDirPath, executionDirName);
Path classesPath = Paths.get(Objects.requireNonNull(getClass().getResource("/")).toURI());
testBaseDir = classesPath.resolveSibling("integration-test-base-dir");
testResourceDir = testBaseDir.resolve("test-resources");
testDirectoryPath = testBaseDir.resolve(getClass().getSimpleName() + "_" + nextCounter());
testDirectory = testDirectoryPath.toFile();
if (!testDirectory.mkdirs()) {
throw new RuntimeException("Unable to create test directory: " + testDirectory);
}
ResourceExtractor.extractResourcePath(getClass(), "/" + TEST_DIRECTORY, testDirectory, true);
addTestResources();
verifier = new Verifier(testDirectory.getAbsolutePath());
verifier.setAutoclean(false);
Expand All @@ -128,6 +132,10 @@ public void teardown() {
cleanAnswers();
}

static synchronized int nextCounter() {
return counter++;
}

public String setupTestServer() throws Exception{
Verifier setupServer = new Verifier(testDirectory.getAbsolutePath());
String serverId = UUID.randomUUID().toString();
Expand Down Expand Up @@ -191,10 +199,6 @@ public void executeTask(String goal) throws Exception {
verifier.executeGoal(sdk+":"+goal);
}

public File getLogFile(){
return new File(testDirectory, "log.txt");
}

protected void assertPlatformUpdated(String serverId, String version) throws MojoExecutionException {
Server.setServersPath(testDirectory.getAbsolutePath());
Server server = Server.loadServer(serverId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package org.openmrs.maven.plugins;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.maven.plugins.model.Project;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.utility.SDKConstants;

import java.io.File;
import java.nio.file.Path;

public class BuildIT extends AbstractSdkIT {

private String serverId;

void addTestResources() throws Exception {
includeTestResource("buildIT");
Path sourcePath = testResourceDir.resolve(TEST_DIRECTORY).resolve("buildIT");
FileUtils.copyDirectory(sourcePath.toFile(), testDirectory);
}

@Before
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.openmrs.maven.plugins.utility;


import lombok.Getter;
import lombok.Setter;
import org.apache.maven.it.VerificationException;
import org.junit.Test;
import org.openmrs.maven.plugins.AbstractMavenIT;
import org.openmrs.maven.plugins.model.Artifact;

import java.io.File;

import static org.junit.Assert.assertTrue;

@Getter @Setter
public class ArtifactHelperIT extends AbstractMavenIT {

@Test
public void test_downloadModuleWithDefaultName() throws Exception {
executeTest(() -> {
ArtifactHelper artifactHelper = new ArtifactHelper(getMavenEnvironment());
Artifact artifact = new Artifact("idgen-omod", "4.14.0", "org.openmrs.module", "jar");
artifactHelper.downloadArtifact(artifact, getMavenTestDirectory(), null);
File expectedFile = new File(getMavenTestDirectory(), "idgen-omod-4.14.0.jar");
assertTrue(expectedFile.exists());
});
}

@Test
public void downloadModuleWithSpecificName() throws Exception {
executeTest(() -> {
ArtifactHelper artifactHelper = new ArtifactHelper(getMavenEnvironment());
Artifact artifact = new Artifact("idgen-omod", "4.14.0", "org.openmrs.module", "jar");
artifactHelper.downloadArtifact(artifact, getMavenTestDirectory(), "idgen.omod");
File expectedFile = new File(getMavenTestDirectory(), "idgen.omod");
assertTrue(expectedFile.exists());
});
}

@Test(expected = VerificationException.class)
public void downloadModuleThatDoesNotExist() throws Exception {
executeTest(() -> {
ArtifactHelper artifactHelper = new ArtifactHelper(getMavenEnvironment());
Artifact artifact = new Artifact("idgen-omod", "4.0.0", "org.openmrs.module", "jar");
artifactHelper.downloadArtifact(artifact, getMavenTestDirectory(), "idgen.omod");
});
}
}
Loading
Loading