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-360 - Standardize logic and support around distributions #312

Merged
merged 9 commits into from
Nov 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void setup() throws Exception {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import java.util.zip.ZipFile;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.io.FileMatchers.anExistingFileOrDirectory;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.openmrs.maven.plugins.SdkMatchers.hasModuleVersion;
import static org.openmrs.maven.plugins.SdkMatchers.hasModuleVersionInDisstro;
import static org.openmrs.maven.plugins.SdkMatchers.hasPlatformVersion;
Expand All @@ -55,29 +57,26 @@ 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 int counter = 0;
protected static final String TEST_DIRECTORY = "integration-test";
protected static final String MOJO_OPTION_TMPL = "-D%s=\"%s\"";
protected static final String BATCH_ANSWERS = "batchAnswers";
protected final ArrayDeque<String> batchAnswers = new ArrayDeque<>();

/**
* maven utility for integration tests
*/
Verifier verifier;
protected Verifier verifier;

/**
* test directory, contains mock files and files created during tests
*/
File testDirectory;

Path testDirectoryPath;

File distroFile;

Path testBaseDir;
Path testResourceDir;
boolean preserveTestOutput;
protected File testDirectory;
protected Path testDirectoryPath;
protected File distroFile;
protected Path testBaseDir;
protected Path testResourceDir;
protected boolean preserveTestOutput;

public String resolveSdkArtifact() throws MojoExecutionException {
Properties sdk = new Properties();
Expand All @@ -90,7 +89,7 @@ public String resolveSdkArtifact() throws MojoExecutionException {
return sdk.get("groupId")+":"+sdk.get("artifactId")+":"+sdk.get("version");
}

void includeDistroPropertiesFile(String... paths) throws Exception {
protected void includeDistroPropertiesFile(String... paths) throws Exception {
Path sourcePath = testResourceDir.resolve(TEST_DIRECTORY);
for (String path : paths) {
sourcePath = sourcePath.resolve(path);
Expand All @@ -99,7 +98,7 @@ void includeDistroPropertiesFile(String... paths) throws Exception {
FileUtils.copyFile(sourcePath.toFile(), targetPath.toFile());
}

void includePomFile(String... paths) throws Exception {
protected void includePomFile(String... paths) throws Exception {
Path sourcePath = testResourceDir.resolve(TEST_DIRECTORY);
for (String path : paths) {
sourcePath = sourcePath.resolve(path);
Expand All @@ -108,9 +107,8 @@ void includePomFile(String... paths) throws Exception {
FileUtils.copyFile(sourcePath.toFile(), targetPath.toFile());
}

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

@Before
Expand Down Expand Up @@ -244,6 +242,16 @@ public File getTestFile(Path path) {
return testDirectoryPath.resolve(path).toFile();
}

/**
* asserts the number of files with the given prefix and suffix are present in the given directory
*/
public void assertNumFilesPresent(int numExpected, Path path, String prefix, String suffix) {
File directory = testDirectoryPath.resolve(path).toFile();
File[] files = directory.listFiles((dir1, name) -> (prefix == null || name.startsWith(prefix)) && (suffix == null || name.endsWith(suffix)));
int numFound = (files == null ? 0 : files.length);
assertThat(numFound, equalTo(numExpected));
}

/**
* asserts that file with given path is present in test directory
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import org.junit.Test;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.PropertiesUtils;

import static org.junit.Assert.assertEquals;
import java.util.Properties;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;

public class AddDependencyIT extends AbstractSdkIT {

@Override
protected void addTestResources() throws Exception {
super.addTestResources();
includeDistroPropertiesFile(DistroProperties.DISTRO_FILE_NAME);
}

@Test
public void shouldAddOmodDependency() throws Exception {
addTaskParam("distro", distroFile.getAbsolutePath());
Expand All @@ -21,10 +29,9 @@ public void shouldAddOmodDependency() throws Exception {
executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
Properties distroProperties = PropertiesUtils.loadPropertiesFromFile(distroFile);
assertNotNull(distroProperties);
assertTrue(distroProperties.getAllKeys().contains("omod.webservices.rest"));
assertEquals(distroProperties.getParam("omod.webservices.rest"), "2.30.0");
assertThat(distroProperties.getProperty("omod.webservices.rest"), equalTo("2.30.0"));
}

@Test
Expand All @@ -37,11 +44,9 @@ public void shouldAddSpaDependency() throws Exception {
executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
Properties distroProperties = PropertiesUtils.loadPropertiesFromFile(distroFile);
assertNotNull(distroProperties);

assertTrue(distroProperties.getAllKeys().contains("spa.frontendModules.@openmrs/esm-system-admin-app"));
assertEquals(distroProperties.getParam("spa.frontendModules.@openmrs/esm-system-admin-app"), "4.0.3");
assertThat(distroProperties.getProperty("spa.frontendModules.@openmrs/esm-system-admin-app"), equalTo("4.0.3"));
}

@Test
Expand All @@ -54,11 +59,9 @@ public void shouldAddOwaDependency() throws Exception {
executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
Properties distroProperties = PropertiesUtils.loadPropertiesFromFile(distroFile);
assertNotNull(distroProperties);

assertTrue(distroProperties.getAllKeys().contains("owa.sysadmin"));
assertEquals(distroProperties.getParam("owa.sysadmin"), "1.2.0");
assertThat(distroProperties.getProperty("owa.sysadmin"), equalTo("1.2.0"));
}

@Test
Expand All @@ -70,11 +73,9 @@ public void shouldAddWarDependency() throws Exception {
executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
Properties distroProperties = PropertiesUtils.loadPropertiesFromFile(distroFile);
assertNotNull(distroProperties);

assertTrue(distroProperties.getAllKeys().contains("war.openmrs"));
assertEquals(distroProperties.getParam("war.openmrs"), "2.6.1");
assertThat(distroProperties.getProperty("war.openmrs"), equalTo("2.6.1"));
}

@Test
Expand All @@ -86,17 +87,15 @@ public void shouldAddCustomDependencyIfTypeIsNotSpecified() throws Exception {
executeTask("add");
assertSuccess();

DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
assertTrue(distroProperties.getAllKeys().contains("custom.property"));
assertEquals(distroProperties.getParam("custom.property"), "1.2.3");

Properties distroProperties = PropertiesUtils.loadPropertiesFromFile(distroFile);
assertThat(distroProperties.getProperty("custom.property"), equalTo("1.2.3"));
}

@Test
public void shouldOverrideIfPropertyAlreadyExists() throws Exception {
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
Properties distroProperties = PropertiesUtils.loadPropertiesFromFile(distroFile);
assertNotNull(distroProperties);
assertTrue(distroProperties.getAllKeys().contains("omod.uiframework"));
assertThat(distroProperties.getProperty("omod.uiframework"), equalTo("3.6"));

addTaskParam("distro", distroFile.getAbsolutePath());
addTaskParam("type", "OMOD");
Expand All @@ -107,10 +106,8 @@ public void shouldOverrideIfPropertyAlreadyExists() throws Exception {
executeTask("add");
assertSuccess();

distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile);
distroProperties = PropertiesUtils.loadPropertiesFromFile(distroFile);
assertNotNull(distroProperties);

assertTrue(distroProperties.getAllKeys().contains("omod.uiframework"));
assertEquals(distroProperties.getParam("omod.uiframework"), "2.30.0");
assertThat(distroProperties.getProperty("omod.uiframework"), equalTo("2.30.0"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.PropertiesUtils;

import java.io.File;
import java.util.Properties;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -14,8 +15,15 @@

public class AddExclusionIT extends AbstractSdkIT {

public DistroProperties getDistroProperties() {
return DistroHelper.getDistroPropertiesFromFile(distroFile);
@Override
protected void addTestResources() throws Exception {
super.addTestResources();
includeDistroPropertiesFile(DistroProperties.DISTRO_FILE_NAME);
}

public DistroProperties getDistroProperties() throws Exception {
Properties properties = PropertiesUtils.loadPropertiesFromFile(distroFile);
return new DistroProperties(properties);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.openmrs.maven.plugins;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openmrs.maven.plugins.model.DistroProperties;

import java.nio.file.Path;
import java.nio.file.Paths;

public class BuildDistroIT extends AbstractSdkIT {

@Test
public void testBuildDistroFromDistroFile() throws Exception {
public void buildDistro_shouldBuildFromDistroPropertiesInCurrentDirectory() throws Exception {
includeDistroPropertiesFile(DistroProperties.DISTRO_FILE_NAME);
addTaskParam("dir", "target");
addTaskParam("ignorePeerDependencies", "false");
executeTask("build-distro");
Expand All @@ -19,14 +25,34 @@ public void testBuildDistroFromDistroFile() throws Exception {
assertFilePresent("target/web/startup.sh");
assertFilePresent("target/web/wait-for-it.sh");
assertFilePresent("target/web/modules");
assertFilePresent("target/web/modules/uiframework-3.6.omod");
assertFilePresent("target/web/modules/uicommons-1.7.omod");
assertFilePresent("target/web/modules/owa-1.4.omod");
assertFilePresent("target/web/owa");
assertFilePresent("target/web/openmrs.war");
assertFilePresent("target/web/openmrs-distro.properties");
assertFileContains("war.openmrs=1.11.5", "target", "web", "openmrs-distro.properties");
assertSuccess();
}

@Test
public void buildDistro_shouldBuildFromDistroPropertiesInCurrentProject() throws Exception {
Path sourcePath = testResourceDir.resolve(TEST_DIRECTORY).resolve("buildDistroIT");
FileUtils.copyDirectory(sourcePath.toFile(), testDirectory);

addTaskParam("dir", "distro");
addTaskParam("ignorePeerDependencies", "false");
executeTask("build-distro");
assertFileContains("war.openmrs=2.6.9", "distro", "web", "openmrs-distro.properties");
assertFilePresent("distro", "web", "openmrs-distro.properties");
assertFilePresent("distro", "web", "openmrs_core", "openmrs.war");
assertFilePresent("distro", "web", "openmrs_modules");
assertNumFilesPresent(12, Paths.get("distro", "web", "openmrs_modules"), null, ".omod");
assertSuccess();
}

@Test
public void testBuildDistroRefApp23() throws Exception {
public void buildDistro_shouldBuildFromRefapp23Artifact() throws Exception {
addTaskParam("distro", "referenceapplication:2.3.1");
addTaskParam("dir", "referenceapplication");
addTaskParam("ignorePeerDependencies", "false");
Expand All @@ -43,6 +69,55 @@ public void testBuildDistroRefApp23() throws Exception {
assertFilePresent("referenceapplication/web/modules");
assertFilePresent("referenceapplication/web/openmrs.war");
assertFilePresent("referenceapplication/web/openmrs-distro.properties");
assertNumFilesPresent(36, Paths.get("referenceapplication", "web", "modules"), null, ".omod");
assertFileContains("war.openmrs=1.11.5", "referenceapplication", "web", "openmrs-distro.properties");
assertSuccess();
}

@Test
public void buildDistro_shouldBuildFromRefapp2xArtifact() throws Exception {
addTaskParam("distro", "referenceapplication:2.13.0");
addTaskParam("dir", "referenceapplication");
addTaskParam("ignorePeerDependencies", "false");
executeTask("build-distro");

assertFilePresent("referenceapplication/docker-compose.yml");
assertFilePresent("referenceapplication/docker-compose.override.yml");
assertFilePresent("referenceapplication/docker-compose.prod.yml");
assertFilePresent("referenceapplication/.env");
assertFilePresent("referenceapplication/web/Dockerfile");
assertFilePresent("referenceapplication/web/openmrs-distro.properties");
assertFilePresent("referenceapplication/web/openmrs_core/openmrs.war");
assertFileContains("war.openmrs=2.5.9", "referenceapplication", "web", "openmrs-distro.properties");
assertFilePresent("referenceapplication/web/openmrs_modules");
assertNumFilesPresent(42, Paths.get("referenceapplication", "web", "openmrs_modules"), null, ".omod");
assertFilePresent("referenceapplication/web/openmrs_owas");
assertFilePresent("referenceapplication/web/openmrs_owas/SystemAdministration.owa");

assertSuccess();
}

@Test
public void buildDistro_shouldBuildFromRefapp3xArtifact() throws Exception {
addTaskParam("distro", "referenceapplication:3.0.0");
addTaskParam("dir", "referenceapplication");
addTaskParam("ignorePeerDependencies", "false");
executeTask("build-distro");

assertFilePresent("referenceapplication/docker-compose.yml");
assertFilePresent("referenceapplication/docker-compose.override.yml");
assertFilePresent("referenceapplication/docker-compose.prod.yml");
assertFilePresent("referenceapplication/.env");
assertFilePresent("referenceapplication/web/Dockerfile");
assertFilePresent("referenceapplication/web/openmrs-distro.properties");
assertFilePresent("referenceapplication/web/openmrs_core/openmrs.war");
assertFileContains("war.openmrs=2.6.7", "referenceapplication", "web", "openmrs-distro.properties");
assertFilePresent("referenceapplication/web/openmrs_modules");
assertNumFilesPresent(24, Paths.get("referenceapplication", "web", "openmrs_modules"), null, ".omod");
assertFileContains("omod.spa", "referenceapplication", "web", "openmrs-distro.properties");
assertFilePresent("referenceapplication/web/openmrs_owas");
assertNumFilesPresent(0, Paths.get("referenceapplication", "web", "openmrs_owas"), null, null);

assertSuccess();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BuildIT extends AbstractSdkIT {

private String serverId;

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