-
Notifications
You must be signed in to change notification settings - Fork 86
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-354 - Fixing bugs and adding test cases for the AddExclusions goal #306
Changes from all commits
934cc98
07e7dc5
f793477
4a182b5
1ed8e29
cbf22a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,88 @@ | ||
package org.openmrs.maven.plugins; | ||
|
||
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 java.io.File; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class AddExclusionIntegrationTest extends AbstractSdkIntegrationTest { | ||
|
||
public DistroProperties getDistroProperties() { | ||
return DistroHelper.getDistroPropertiesFromFile(distroFile); | ||
} | ||
|
||
@Test | ||
public void shouldAddExclusionIfNoParentDefined() throws Exception { | ||
includeDistroPropertiesFile("openmrs-distro.properties"); | ||
assertNotNull(getDistroProperties()); | ||
assertNull(getDistroProperties().getParentDistroArtifact()); | ||
assertTrue(getDistroProperties().getExclusions().isEmpty()); | ||
executeExclusionTask(distroFile, "omod.uicommons"); | ||
assertNotNull(getDistroProperties()); | ||
assertTrue(getDistroProperties().getExclusions().contains("omod.uicommons")); | ||
assertTrue(getLogContents().contains(AddExclusion.WARNING_NO_PARENT_DISTRO)); | ||
} | ||
|
||
@Test | ||
public void shouldAddExclusion() throws Exception { | ||
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile); | ||
assertNotNull(distroProperties); | ||
assertFalse(distroProperties.getExclusions().contains("omod.uicommons")); | ||
public void shouldAddExclusionForPropertyContainedInParent() throws Exception { | ||
includeDistroPropertiesFile("openmrs-distro-parent-as-parent.properties"); | ||
assertNotNull(getDistroProperties()); | ||
assertNotNull(getDistroProperties().getParentDistroArtifact()); | ||
assertTrue(getDistroProperties().getExclusions().isEmpty()); | ||
executeExclusionTask(distroFile, "omod.uicommons"); | ||
assertNotNull(getDistroProperties()); | ||
assertTrue(getDistroProperties().getExclusions().contains("omod.uicommons")); | ||
assertFalse(getLogContents().contains(AddExclusion.WARNING_NO_PARENT_DISTRO)); | ||
assertFalse(getLogContents().contains(AddExclusion.WARNING_PROPERTY_NOT_IN_PARENT)); | ||
} | ||
|
||
addTaskParam("distro", distroFile.getAbsolutePath()); | ||
addTaskParam("property", "omod.uicommons"); | ||
executeTask("exclusion"); | ||
@Test | ||
public void shouldAddExclusionForPropertyContainedInDistro() throws Exception { | ||
includeDistroPropertiesFile("openmrs-distro-parent-as-distro.properties"); | ||
assertNotNull(getDistroProperties()); | ||
assertNotNull(getDistroProperties().getParentDistroArtifact()); | ||
assertTrue(getDistroProperties().getExclusions().isEmpty()); | ||
executeExclusionTask(distroFile, "omod.uicommons"); | ||
assertNotNull(getDistroProperties()); | ||
assertTrue(getDistroProperties().getExclusions().contains("omod.uicommons")); | ||
assertFalse(getLogContents().contains(AddExclusion.WARNING_NO_PARENT_DISTRO)); | ||
assertFalse(getLogContents().contains(AddExclusion.WARNING_PROPERTY_NOT_IN_PARENT)); | ||
} | ||
|
||
@Test | ||
public void shouldAddExclusionForPropertyNotContainedInParent() throws Exception { | ||
includeDistroPropertiesFile("openmrs-distro-parent-as-parent.properties"); | ||
assertNotNull(getDistroProperties()); | ||
assertNotNull(getDistroProperties().getParentDistroArtifact()); | ||
assertTrue(getDistroProperties().getExclusions().isEmpty()); | ||
executeExclusionTask(distroFile, "omod.invalidmodulename"); | ||
assertNotNull(getDistroProperties()); | ||
assertTrue(getDistroProperties().getExclusions().contains("omod.invalidmodulename")); | ||
assertFalse(getLogContents().contains(AddExclusion.WARNING_NO_PARENT_DISTRO)); | ||
assertTrue(getLogContents().contains(AddExclusion.WARNING_PROPERTY_NOT_IN_PARENT)); | ||
} | ||
|
||
private String getLogContents() throws Exception { | ||
File logFile = new File(testDirectory, "log.txt"); | ||
assertTrue(logFile.exists()); | ||
return FileUtils.readFileToString(logFile, "UTF-8"); | ||
} | ||
|
||
private void executeExclusionTask(File distroFile, String exclusion) throws Exception { | ||
if (distroFile != null) { | ||
addTaskParam("distro", distroFile.getAbsolutePath()); | ||
} | ||
if (exclusion != null) { | ||
addTaskParam("property", exclusion); | ||
} | ||
executeTask("exclusion"); | ||
assertSuccess(); | ||
distroProperties = DistroHelper.getDistroPropertiesFromFile(distroFile); | ||
assertTrue(distroProperties.getExclusions().contains("omod.uicommons")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name=Spa Artifact Example | ||
version=2.5 | ||
distro.referenceapplication=2.6.1 | ||
omod.htmlformentry=3.3.1 | ||
db.h2.supported=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name=Spa Artifact Example | ||
version=2.5 | ||
parent.groupId=org.openmrs.distro | ||
parent.artifactId=referenceapplication-package | ||
parent.version=2.6.1 | ||
parent.type=jar | ||
omod.htmlformentry=3.3.1 | ||
db.h2.supported=false |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
name=Spa Artifact Example | ||
version=1.0.0-SNAPSHOT | ||
|
||
war.openmrs=2.6.9 | ||
|
||
omod.spa=2.0.0 | ||
|
||
spa.artifactId=openmrs-frontend-zl | ||
spa.groupId=org.pih.openmrs | ||
spa.version=1.3.0 | ||
spa.includes=openmrs-frontend-zl-1.3.0 | ||
|
||
db.h2.supported=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
@Mojo(name = "exclusion", requiresProject = false) | ||
public class AddExclusion extends AbstractTask { | ||
|
||
public static final String WARNING_PROPERTY_NOT_IN_PARENT = "The property is not included in the parent distro"; | ||
public static final String WARNING_NO_PARENT_DISTRO = "This distro properties file does not contain a valid parent distro"; | ||
|
||
/** | ||
* Path to the openmrs-distro.properties file to modify | ||
*/ | ||
|
@@ -51,22 +54,23 @@ public void executeTask() throws MojoExecutionException, MojoFailureException { | |
throw new MojoFailureException("Invalid distro file"); | ||
} | ||
|
||
Artifact distroArtifact = originalDistroProperties.getParentArtifact(); | ||
if (StringUtils.isNotBlank(distroArtifact.getArtifactId()) && StringUtils.isNotBlank(distroArtifact.getGroupId()) && StringUtils.isNotBlank(distroArtifact.getVersion())) { | ||
DistroProperties distroProperties = distroHelper.resolveParentArtifact(distroArtifact, new File(distro).getParentFile(), originalDistroProperties, null); | ||
Artifact parentDistro = originalDistroProperties.getParentDistroArtifact(); | ||
if (parentDistro != null) { | ||
DistroProperties parentProperties = distroHelper.downloadDistroProperties(new File(distro).getParentFile(), parentDistro); | ||
parentProperties = distroHelper.getDistroPropertiesForFullAncestry(parentProperties, new File(distro).getParentFile()); | ||
if (StringUtils.isBlank(property)) { | ||
List<String> currentExclusions = distroProperties.getExclusions(); | ||
List<String> options = distroProperties.getPropertyNames().stream().filter(prop -> !currentExclusions.contains(prop)).collect(Collectors.toList()); | ||
property = wizard.promptForMissingValueWithOptions("Enter the property you want to exclude", | ||
null, null, options); | ||
} else { | ||
if (!distroProperties.getPropertyNames().contains(property)) { | ||
wizard.showWarning("The property is not included in the parent distro"); | ||
List<String> currentExclusions = originalDistroProperties.getExclusions(); | ||
List<String> options = parentProperties.getPropertyNames().stream().filter(prop -> !currentExclusions.contains(prop)).collect(Collectors.toList()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notice the difference here from above - the options need to come from the parent properties, but the current exclusions need to come from the child properties |
||
property = wizard.promptForMissingValueWithOptions("Enter the property you want to exclude", null, null, options); | ||
} | ||
else { | ||
if (!parentProperties.getPropertyNames().contains(property)) { | ||
wizard.showWarning(WARNING_PROPERTY_NOT_IN_PARENT); | ||
} | ||
} | ||
|
||
} else { | ||
wizard.showWarning("This distro properties file does not contain a valid parent distro"); | ||
} | ||
else { | ||
wizard.showWarning(WARNING_NO_PARENT_DISTRO); | ||
if (StringUtils.isBlank(property)) { | ||
property = wizard.promptForValueIfMissing(null, "property to exclude"); | ||
if (StringUtils.isBlank(property)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,46 +109,59 @@ private String extractPropertyName(String key) { | |
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can review this in more detail in SDK-348, but for now I have consolidated the two methods of specifying a parent distro so that both are equally supported. See change below. |
||
public Artifact getDistroArtifact() { | ||
/** | ||
* Allowed formats for artifactId and version | ||
* distro.artifactId=version | ||
* distro.artifactId.groupId= | ||
* distro.artifactId.type= | ||
* -OR- | ||
* distro.anything=version | ||
* distro.anything.artifactId= | ||
* distro.anything.groupId= | ||
* distro.anything.type= | ||
* -OR- | ||
* parent.artifactId= | ||
* parent.groupId= | ||
* parent.version= | ||
* parent.type= | ||
*/ | ||
public Artifact getParentDistroArtifact() throws MojoExecutionException { | ||
Artifact artifact = null; | ||
for (Object keyObject: getAllKeys()) { | ||
String key = keyObject.toString(); | ||
String artifactType = getArtifactType(key); | ||
if(artifactType.equals(TYPE_DISTRO)) { | ||
return new Artifact(checkIfOverwritten(key, ARTIFACT_ID), getParam(key), checkIfOverwritten(key, GROUP_ID), checkIfOverwritten(key, TYPE), "jar"); | ||
if (artifactType.equals(TYPE_DISTRO)) { | ||
String artifactId = checkIfOverwritten(key, ARTIFACT_ID); | ||
String version = getParam(key); | ||
String groupId = checkIfOverwritten(key, GROUP_ID); | ||
String type = checkIfOverwritten(key, TYPE); | ||
if (artifact != null) { | ||
throw new MojoExecutionException("Only a single " + TYPE_DISTRO + " property can be added to indicate the parent distribution"); | ||
} | ||
artifact = new Artifact(artifactId, version, groupId, type); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public Artifact getParentArtifact() { | ||
String parentArtifactId = getParam("parent.artifactId"); | ||
String parentGroupId = getParam("parent.groupId"); | ||
String parentVersion = getParam("parent.version"); | ||
|
||
int missingCount = 0; | ||
if (StringUtils.isBlank(parentArtifactId)) { | ||
missingCount++; | ||
} | ||
if (StringUtils.isBlank(parentGroupId)) { | ||
missingCount++; | ||
} | ||
if (StringUtils.isBlank(parentVersion)) { | ||
missingCount++; | ||
} | ||
|
||
// We are only going to throw an error if only one or two parameters are missing | ||
if (missingCount > 0 && missingCount < 3) { | ||
throw new IllegalArgumentException("Missing arguments for the parent"); | ||
String artifactId = getParam(TYPE_PARENT + "." + ARTIFACT_ID); | ||
String version = getParam(TYPE_PARENT + "." + VERSION); | ||
String groupId = getParam(TYPE_PARENT + "." + GROUP_ID); | ||
String type = getParam(TYPE_PARENT + "." + TYPE, TYPE_ZIP); | ||
if (StringUtils.isNotBlank(artifactId)) { | ||
if (artifact != null) { | ||
throw new MojoExecutionException("Distro properties cannot define both a " + TYPE_DISTRO + " and a " + TYPE_PARENT + " property"); | ||
} | ||
if (StringUtils.isBlank(version) || StringUtils.isBlank(groupId)) { | ||
throw new MojoExecutionException("You must specify a " + TYPE_PARENT + " groupId and version if you specify and artifactId"); | ||
} | ||
artifact = new Artifact(artifactId, version, groupId, type); | ||
} | ||
|
||
return new Artifact(parentArtifactId, parentVersion, parentGroupId, "zip"); | ||
return DistroHelper.normalizeArtifact(artifact, null); | ||
} | ||
|
||
public List<Artifact> getModuleArtifacts(DistroHelper distroHelper, File directory) throws MojoExecutionException { | ||
List<Artifact> childArtifacts = getModuleArtifacts(); | ||
List<Artifact> parentArtifacts = new ArrayList<>(); | ||
|
||
Artifact artifact = getDistroArtifact(); | ||
Artifact artifact = getParentDistroArtifact(); | ||
if (artifact != null) { | ||
DistroProperties distroProperties = distroHelper.downloadDistroProperties(directory, artifact); | ||
parentArtifacts.addAll(distroProperties.getModuleArtifacts(distroHelper, directory)); | ||
|
@@ -160,7 +173,7 @@ public List<Artifact> getOwaArtifacts(DistroHelper distroHelper, File directory) | |
List<Artifact> childArtifacts = getOwaArtifacts(); | ||
List<Artifact> parentArtifacts = new ArrayList<>(); | ||
|
||
Artifact artifact = getDistroArtifact(); | ||
Artifact artifact = getParentDistroArtifact(); | ||
if (artifact != null) { | ||
DistroProperties distroProperties = distroHelper.downloadDistroProperties(directory, artifact); | ||
parentArtifacts.addAll(distroProperties.getOwaArtifacts(distroHelper, directory)); | ||
|
@@ -171,7 +184,7 @@ public List<Artifact> getOwaArtifacts(DistroHelper distroHelper, File directory) | |
public Map<String, String> getSpaProperties(DistroHelper distroHelper, File directory) throws MojoExecutionException { | ||
Map<String, String> spaProperties = getSpaProperties(); | ||
|
||
Artifact artifact = getDistroArtifact(); | ||
Artifact artifact = getParentDistroArtifact(); | ||
if (artifact != null) { | ||
DistroProperties distroProperties = distroHelper.downloadDistroProperties(directory, artifact); | ||
spaProperties.putAll(distroProperties.getSpaProperties(distroHelper, directory)); | ||
|
@@ -183,7 +196,7 @@ public List<Artifact> getWarArtifacts(DistroHelper distroHelper, File directory) | |
List<Artifact> childArtifacts = getWarArtifacts(); | ||
List<Artifact> parentArtifacts = new ArrayList<>(); | ||
|
||
Artifact artifact = getDistroArtifact(); | ||
Artifact artifact = getParentDistroArtifact(); | ||
if (artifact != null) { | ||
DistroProperties distroProperties = distroHelper.downloadDistroProperties(directory, artifact); | ||
parentArtifacts.addAll(distroProperties.getWarArtifacts(distroHelper, directory)); | ||
|
@@ -192,7 +205,7 @@ public List<Artifact> getWarArtifacts(DistroHelper distroHelper, File directory) | |
} | ||
|
||
public String getPlatformVersion(DistroHelper distroHelper, File directory) throws MojoExecutionException{ | ||
Artifact artifact = getDistroArtifact(); | ||
Artifact artifact = getParentDistroArtifact(); | ||
if (artifact != null) { | ||
DistroProperties distroProperties = distroHelper.downloadDistroProperties(directory, artifact); | ||
return distroProperties.getPlatformVersion(distroHelper, directory); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my testing, this was actually not getting the full set of parent properties. This code also does not resolve full ancestors (eg. if the parent has a parent). The change below intends to fix this.