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

#342: implement unit tests for mvn ToolCommandlet #373

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
06ef780
first implementation
mvomiero May 28, 2024
acb5bfc
code cleanup
mvomiero May 31, 2024
21bdc6f
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero May 31, 2024
0224dba
commandlet arguments refactoring in test
mvomiero May 31, 2024
6d9beea
remove plugins
mvomiero May 31, 2024
cf008fd
check settingssecurityfile
mvomiero May 31, 2024
43dd789
settings.xml added to git
mvomiero Jun 3, 2024
5ee8cd7
update mvn rights
mvomiero Jun 3, 2024
a58348a
add mvn executable for windows
mvomiero Jun 3, 2024
3208742
fix operating system name
mvomiero Jun 3, 2024
4f5137e
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
mvomiero Jun 14, 2024
eed1d84
implement change requests
mvomiero Jun 14, 2024
252eaaf
implement change request
mvomiero Jun 14, 2024
5b609cc
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jun 19, 2024
e8d1609
implement assertion for content of settings files
mvomiero Jun 19, 2024
ea57690
add javadoc
mvomiero Jun 20, 2024
c2258cd
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jun 21, 2024
051ee46
Update cli/src/test/resources/ide-projects/mvn/project/workspaces/mai…
mvomiero Jun 26, 2024
d46ae28
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
hohwille Jul 1, 2024
0770b17
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jul 2, 2024
5d69643
implement change request and fix bug format encrypted values
mvomiero Jul 2, 2024
483d238
Merge remote-tracking branch 'upstream/main' into enhancement/342-Imp…
mvomiero Jul 2, 2024
33408ca
Merge branch 'main' into enhancement/342-ImplementUnitTestsForMvn
hohwille Jul 4, 2024
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
30 changes: 16 additions & 14 deletions cli/src/main/java/com/devonfw/tools/ide/tool/mvn/Mvn.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@ public class Mvn extends PluginBasedCommandlet {
*/
public static final String MVN_CONFIG_LEGACY_FOLDER = ".m2";

/** The name of the settings-security.xml */
public static final String SETTINGS_SECURITY_FILE = "settings-security.xml";

/**
* The name of the settings.xml
*/
public static final String SETTINGS_FILE = "settings.xml";

hohwille marked this conversation as resolved.
Show resolved Hide resolved
private static final String SETTINGS_SECURITY_FILE = "settings-security.xml";

private static final String DOCUMENTATION_PAGE_CONF = "https://github.com/devonfw/IDEasy/blob/main/documentation/conf.adoc";

private static final String ERROR_SETTINGS_FILE_MESSAGE =
"Failed to create settings file at: {}. For further details see:\n" + DOCUMENTATION_PAGE_CONF;
private static final String ERROR_SETTINGS_FILE_MESSAGE = "Failed to create settings file at: {}. For further details see:\n" + DOCUMENTATION_PAGE_CONF;

private static final String ERROR_SETTINGS_SECURITY_FILE_MESSAGE =
"Failed to create settings security file at: {}. For further details see:\n" + DOCUMENTATION_PAGE_CONF;
public static final VariableSyntax VARIABLE_SYNTAX = VariableSyntax.SQUARE;
"Failed to create settings security file at: {}. For further details see:\n" + DOCUMENTATION_PAGE_CONF;

private static final VariableSyntax VARIABLE_SYNTAX = VariableSyntax.SQUARE;

/**
* The constructor.
Expand Down Expand Up @@ -96,7 +97,8 @@ public void postInstall() {
templatesConfMvnFolder = templatesConfMvnLegacyFolder;
legacy = true;
} else {
this.context.warning("No maven templates found. Neither in {} nor in {} - configuration broken", templatesConfMvnFolder, templatesConfMvnLegacyFolder);
this.context.warning("No maven templates found. Neither in {} nor in {} - configuration broken", templatesConfMvnFolder,
templatesConfMvnLegacyFolder);
hasMvnTemplates = false;
}
}
Expand Down Expand Up @@ -133,11 +135,11 @@ private void createSettingsSecurityFile(Path settingsSecurityFile) {

ProcessResult result = pc.run(ProcessMode.DEFAULT_CAPTURE);

String encryptedMasterPassword = result.getOut().toString();
String encryptedMasterPassword = result.getOut().get(0);

String settingsSecurityXml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<settingsSecurity>\n" + " <master>" + encryptedMasterPassword + "</master>\n"
+ "</settingsSecurity>";
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<settingsSecurity>\n" + " <master>" + encryptedMasterPassword + "</master>\n"
+ "</settingsSecurity>";
try {
Files.writeString(settingsSecurityFile, settingsSecurityXml);
step.success();
Expand Down Expand Up @@ -169,7 +171,7 @@ private void createSettingsFile(Path settingsFile, Path settingsSecurityFile, Pa
} else if (!gitSettingsUrl.equals(gitContext.DEFAULT_SETTINGS_GIT_URL)) {
Set<String> variables = findVariables(content);
for (String variable : variables) {
String secret = getEncryptedPassword(variable, settingsSecurityFile);
hohwille marked this conversation as resolved.
Show resolved Hide resolved
String secret = getEncryptedPassword(variable);
content = content.replace(VARIABLE_SYNTAX.create(variable), secret);
}
}
Expand All @@ -182,15 +184,15 @@ private void createSettingsFile(Path settingsFile, Path settingsSecurityFile, Pa
}
}

private String getEncryptedPassword(String variable, Path settingsSecurityFile) {
private String getEncryptedPassword(String variable) {

String input = this.context.askForInput("Please enter secret value for variable " + variable + ":");

ProcessContext pc = this.context.newProcess().executable("mvn");
pc.addArgs("--encrypt-password", input);
ProcessResult result = pc.run(ProcessMode.DEFAULT_CAPTURE);

String encryptedPassword = result.getOut().toString();
String encryptedPassword = result.getOut().get(0);
hohwille marked this conversation as resolved.
Show resolved Hide resolved
this.context.info("Encrypted as " + encryptedPassword);

return encryptedPassword;
Expand All @@ -216,7 +218,7 @@ public void installPlugin(PluginDescriptor plugin) {
this.context.success("Successfully added {} to {}", plugin.getName(), mavenPlugin.toString());
} else {
this.context.warning("Plugin {} has wrong properties\n" //
+ "Please check the plugin properties file in {}", mavenPlugin.getFileName(), mavenPlugin.toAbsolutePath());
+ "Please check the plugin properties file in {}", mavenPlugin.getFileName(), mavenPlugin.toAbsolutePath());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* Mock implementation of {@link GitContext}.
*/
public class GitContextMock implements GitContext {

private static final String MOCKED_URL_VALUE = "mocked url value";

@Override
public void pullOrCloneIfNeeded(String repoUrl, String branch, Path targetRepository) {

Expand Down Expand Up @@ -49,6 +52,6 @@ public void cleanup(Path targetRepository) {
@Override
public String retrieveGitUrl(Path repository) {

return null;
return MOCKED_URL_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import com.devonfw.tools.ide.repo.ToolRepository;

import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;

/**
* Implementation of {@link IdeContext} for testing.
*/
public class IdeTestContext extends AbstractIdeTestContext {

private LinkedList<String> inputValues;

/**
* The constructor.
*
Expand Down Expand Up @@ -62,4 +66,20 @@ public static IdeTestContext of() {
return new IdeTestContext(Path.of("/"));
}

/**
* Set a mocked value to be returned by the {@link IdeContext#askForInput(String)} method
*
* @param values a {@link LinkedList} with the mocked input value
*/
public void setInputValues(List<String> values) {

this.inputValues = new LinkedList<>(values);
}

@Override
public String askForInput(String message) {

return inputValues.isEmpty() ? null : inputValues.poll();
}

}
96 changes: 96 additions & 0 deletions cli/src/test/java/com/devonfw/tools/ide/tool/mvn/MvnTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.devonfw.tools.ide.tool.mvn;

import com.devonfw.tools.ide.commandlet.InstallCommandlet;
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Integration test of {@link Mvn}.
*/
public class MvnTest extends AbstractIdeContextTest {

private static final String PROJECT_MVN = "mvn";

//private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\[(.*?)\\]");
private static final Pattern VARIABLE_PATTERN = Pattern.compile("<([^>]+)>(.*?)</\\1>");
hohwille marked this conversation as resolved.
Show resolved Hide resolved

/**
* Tests the installation of {@link Mvn}
*
* @throws IOException if an I/O error occurs during the installation process
*/
@Test
public void testMvnInstall() throws IOException {

// arrange
IdeTestContext context = newContext(PROJECT_MVN);
context.setInputValues(List.of("testLogin", "testPassword"));
InstallCommandlet install = context.getCommandletManager().getCommandlet(InstallCommandlet.class);
install.tool.setValueAsString("mvn", context);

// act
install.run();

// assert
checkInstallation(context);
}

/**
* Tests the execution of {@link Mvn}
*
* @throws IOException if an I/O error occurs during the installation process
*/
@Test
public void testMvnRun() throws IOException {
// arrange
IdeTestContext context = newContext(PROJECT_MVN);
context.setInputValues(List.of("testLogin", "testPassword"));
InstallCommandlet install = context.getCommandletManager().getCommandlet(InstallCommandlet.class);
install.tool.setValueAsString("mvn", context);
Mvn commandlet = (Mvn) install.tool.getValue();
commandlet.arguments.addValue("foo");
commandlet.arguments.addValue("bar");

// act
commandlet.run();

// assert
assertLogMessage(context, IdeLogLevel.INFO, "mvn " + "foo bar");
checkInstallation(context);
}

private void checkInstallation(IdeTestContext context) throws IOException {

assertThat(context.getSoftwarePath().resolve("java/bin/java")).exists();

assertThat(context.getSoftwarePath().resolve("mvn/.ide.software.version")).exists().hasContent("3.9.7");
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully installed mvn in version 3.9.7");

Path settingsFile = context.getConfPath().resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_FILE);
assertThat(settingsFile).exists();
assertFileContent(settingsFile, List.of("${env.M2_REPO}", "repository", "testLogin", "testPassword"));
hohwille marked this conversation as resolved.
Show resolved Hide resolved

Path settingsSecurityFile = context.getConfPath().resolve(Mvn.MVN_CONFIG_FOLDER).resolve(Mvn.SETTINGS_SECURITY_FILE);
assertThat(settingsSecurityFile).exists();
assertFileContent(settingsSecurityFile, List.of("masterPassword"));
}

private void assertFileContent(Path filePath, List<String> expectedValues) throws IOException {

String content = new String(Files.readAllBytes(filePath));
Matcher matcher = VARIABLE_PATTERN.matcher(content);
List<String> values = matcher.results().map(matchResult -> matchResult.group(2)).collect(Collectors.toList());

assertThat(values).containsExactlyInAnyOrderElementsOf(expectedValues);
}
}
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/mvn/_ide/urls/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the download metadata
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the users HOME directory
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/mvn/project/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_HOME directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JAVA_VERSION=17.0.10_7
MVN_VERSION=3.9.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<localRepository>${env.M2_REPO}</localRepository>

<servers>
<server>
<id>repository</id>
<username>$[login]</username>
<password>$[password]</password>
</server>
</servers>

</settings>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the main workspace of mvn test case
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/mvn/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_ROOT directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "java mvn"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

if [ "$1" == "--encrypt-master-password" ]; then
echo "masterPassword"
elif [ "$1" == "--encrypt-password" ]; then
echo "$2"
else
echo "mvn $*"
fi
Loading