Skip to content

Commit

Permalink
[#306] update tests to check all plugins are migrated in test file
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilkins-csi committed Aug 30, 2024
1 parent 9346794 commit 66f7b80
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
*/

import com.boozallen.aissemble.upgrade.migration.AbstractMigrationTest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.BuildBase;
import org.apache.maven.model.PluginManagement;
import org.apache.maven.model.Profile;
import org.technologybrewery.baton.util.pom.PomHelper;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.slf4j.Logger;
Expand All @@ -37,42 +41,72 @@ public class OrphedomosToFabric8MigrationSteps extends AbstractMigrationTest {
private static final Logger logger = LoggerFactory.getLogger(OrphedomosToFabric8MigrationSteps.class);
protected List<String> profilePluginsArtifactIds;
private String section;
private Plugin currentPlugin;
private List<String> profiles;
private List<Plugin> migratedPlugins;

@Given("A pom with Orphedomos in the {string} section")
public void a_pom_with_orphedomos_in_the_section(String section) {
setupTestFile(section, "");
}

@Given("A pom with an Orphedomos config in the {string} section of {string} profile\\(s)")
public void aPomWithAnOrphedomosConfigInTheBuildSectionOfProfileS(String section, String profiles) {
setupTestFile(section, profiles);
}

private void setupTestFile(String section, String profiles) {
this.section = section;
testFile = getTestFile(String.format("v1_8_0/OrphedomosToFabric8Migration/migrate-%s/pom.xml", this.section));
this.profiles = new ArrayList<>();
if(StringUtils.isNotBlank(profiles)) {
this.profiles.addAll(List.of(profiles.split(",")));
}
if (this.profiles.isEmpty()) {
testFile = getTestFile(String.format("v1_8_0/OrphedomosToFabric8Migration/migrate-%s/pom.xml", this.section));
} else if (this.profiles.size() == 1) {
testFile = getTestFile(String.format("v1_8_0/OrphedomosToFabric8Migration/migrate-profile-%s/%s/pom.xml", this.section, this.profiles.get(0)));
} else {
testFile = getTestFile(String.format("v1_8_0/OrphedomosToFabric8Migration/migrate-profile-%s/multiple/pom.xml", this.section));
}
}

@When("The migration executes")
public void the_migration_executes() {
performMigration(new OrphedomosToFabric8Migration());
assertTrue("The migration should execute", shouldExecute);
}
@Then("The pom is updated to use the Fabric8 docker-maven-plugin")
public void the_pom_is_updated_to_use_the_fabric8_docker_maven_plugin() {
Model model = PomHelper.getLocationAnnotatedModel(testFile);

// check the appropriate section for the updated plugin
if (this.section.equals("build")) {
this.currentPlugin = model.getBuild().getPlugins().get(0);
} else {
this.currentPlugin = model.getBuild().getPluginManagement().getPlugins().get(0);
this.migratedPlugins = new ArrayList<>();
addPluginConfigsFromBuild(migratedPlugins, model.getBuild());
if (!model.getProfiles().isEmpty()) {
for (Profile profile : model.getProfiles()) {
addPluginConfigsFromBuild(migratedPlugins, profile.getBuild());
}
}
}

assertEquals(OrphedomosToFabric8Migration.FABRIC8_GROUP_ID, this.currentPlugin .getGroupId().toString());
assertEquals(OrphedomosToFabric8Migration.FABRIC8_ARTIFACT_ID, this.currentPlugin .getArtifactId().toString());
@Then("The pom is updated to use the Fabric8 docker-maven-plugin")
public void the_pom_is_updated_to_use_the_fabric8_docker_maven_plugin() {
for (Plugin plugin : migratedPlugins) {
assertEquals("Plugin was not migrated to Fabric8 group @ " + plugin.getLocation(""),
OrphedomosToFabric8Migration.FABRIC8_GROUP_ID, plugin.getGroupId());
assertEquals("Plugin was not migrated to Fabric8 artifact @ " + plugin.getLocation(""),
OrphedomosToFabric8Migration.FABRIC8_ARTIFACT_ID, plugin.getArtifactId());
}
}

@Then("The previous configuration is replaced with the Fabric8 configuration")
public void the_previous_configuration_is_replaced_with_the_Fabric8_configuration() {
String expectedConfig = OrphedomosToFabric8Migration.getBuildConfigCode("", "test", "1.0.0", 2);

// Trim raw configuration object to just contain the relevant pom xml portion
String rawConfig = this.currentPlugin.getConfiguration().toString();
String actualConfig = rawConfig.substring(rawConfig.indexOf("<configuration>")) + "\n";
for (Plugin plugin : migratedPlugins) {
// Trim raw configuration object to just contain the relevant pom xml portion
String rawConfig = plugin.getConfiguration().toString();
String actualConfig = rawConfig.substring(rawConfig.indexOf("<configuration>")) + "\n";
assertEquals("The configuration was not replaced properly @ " + plugin.getLocation(""),
expectedConfig, actualConfig);
}

assertEquals("The configuration was not replaced properly", expectedConfig, actualConfig);
}

@Given("A pom with its packaging set to Orphedomos")
Expand Down Expand Up @@ -116,4 +150,22 @@ public void fabric8_is_configured_properly_in_the_profile_s(String profileId) {
public void the_previous_profile_configuration_was_removed(String profileId) {
assertFalse(String.format("Orphedomos should not be present in profile \"%s\".", profileId), profilePluginsArtifactIds.contains("orphedomos-maven-plugin"));
}

/**
* Adds the first plugin from the build > plugins and pluginManagement > plugins sections to the collector
*
* @param collector list of plugins to add to
* @param build build object to check for plugins
*/
private static void addPluginConfigsFromBuild(List<Plugin> collector, BuildBase build) {
if (build != null) {
if (CollectionUtils.isNotEmpty(build.getPlugins())) {
collector.add(build.getPlugins().get(0));
}
PluginManagement pluginMgmt = build.getPluginManagement();
if (pluginMgmt != null && CollectionUtils.isNotEmpty(pluginMgmt.getPlugins())) {
collector.add(pluginMgmt.getPlugins().get(0));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ Feature: Migrate from using Orphedomos to Fabric8 docker-maven-plugin
| build |
| plugin-management |

# Scenario Outline: Pom file with Orphedomos plugin configuration in profile(s) build section is migrated to Fabric8 docker-maven-plugin
# Given A pom with an Orphedomos config in "<profile>" profile(s)
# When The migration executes
# Then The pom is updated to use the Fabric8 docker-maven-plugin
# And The previous configuration is replaced with the Fabric8 configuration
# Examples:
# | profile |
# | integration-test |
# | ci |
# | other |
# | multiple |
Scenario Outline: Pom file with Orphedomos plugin configuration in profile(s) build section is migrated to Fabric8 docker-maven-plugin
Given A pom with an Orphedomos config in the "build" section of "<profile>" profile(s)
When The migration executes
Then The pom is updated to use the Fabric8 docker-maven-plugin
And The previous configuration is replaced with the Fabric8 configuration
Examples:
| profile |
| integration-test |
| ci |
| other |
| integration-test,ci,other |

# Scenario Outline: Pom file with Orphedomos plugin configuration in profile(s) plugin management section is migrated to Fabric8 docker-maven-plugin
# Given A pom with an Orphedomos config in "<profile>" profile(s)
# When The migration executes
# Then The pom is updated to use the Fabric8 docker-maven-plugin
# And The previous configuration is replaced with the Fabric8 configuration
# Examples:
# | profile |
# | integration-test |
# | ci |
# | other |
# | multiple |
Scenario Outline: Pom file with Orphedomos plugin configuration in profile(s) plugin management section is migrated to Fabric8 docker-maven-plugin
Given A pom with an Orphedomos config in the "plugin-management" section of "<profile>" profile(s)
When The migration executes
Then The pom is updated to use the Fabric8 docker-maven-plugin
And The previous configuration is replaced with the Fabric8 configuration
Examples:
| profile |
| integration-test |
| ci |
| other |
| integration-test,ci,other |

Scenario: Pom file with Orphedomos packaging type is migrated to Fabric8 docker-build
Given A pom with its packaging set to Orphedomos
Expand Down

0 comments on commit 66f7b80

Please sign in to comment.