Skip to content

Commit

Permalink
Modernize aaa-cli-jar
Browse files Browse the repository at this point in the history
Use more modern path resolution, as pointed out by modernizer.

Change-Id: I43373319da84216e03c213a41fbfb3ef56761f34
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
  • Loading branch information
rovarga committed Feb 10, 2025
1 parent 38813d1 commit dc14cc0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 48 deletions.
4 changes: 0 additions & 4 deletions aaa-cli-jar/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
<name>ODL :: aaa :: ${project.artifactId}</name>
<packaging>jar</packaging>

<properties>
<odlparent.modernizer.enforce>false</odlparent.modernizer.enforce>
</properties>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
Expand Down Expand Up @@ -130,7 +131,7 @@ private static OptionParser getOptionParser() {

parser.acceptsAll(List.of(OPTION_HELP, "?"), "Show help").forHelp();
parser.accepts(OPTION_DB_DIR, "databaseDirectory").withRequiredArg().ofType(File.class)
.defaultsTo(new File(".")).describedAs("path");
.defaultsTo(Path.of(".").toFile()).describedAs("path");
parser.acceptsAll(List.of(OPTION_LIST_USERS, "listUsers"), "List all existing users");
parser.acceptsAll(List.of(OPTION_NEW_USER, "newUser"), "New user to create").withRequiredArg();
parser.acceptsAll(List.of(OPTION_CHANGE_USER, "changeUser"), "Existing user name to change password")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.mockito.Mockito.verify;

import java.io.File;
import java.nio.file.Path;
import java.util.List;
import org.junit.Test;
import org.opendaylight.yangtools.testutils.mockito.MoreAnswers;
Expand All @@ -26,6 +27,8 @@
* @author Michael Vorburger.ch
*/
public class AbstractMainTest {
private static final File DOT_FILE = Path.of(".").toFile();

private static AbstractMain mockedMain() {
return mock(AbstractMain.class, MoreAnswers.realOrException());
}
Expand Down Expand Up @@ -94,23 +97,23 @@ public void ifPasswordsThenEitherCreateOrChangeUser() throws Exception {
public void changeUserAndPassword() throws Exception {
AbstractMain main = spy(AbstractMain.class);
assertEquals(0, main.parseArguments(new String[] { "-X", "-cu", "user", "-p", "newpass" }));
verify(main).setDbDirectory(new File("."));
verify(main).setDbDirectory(DOT_FILE);
verify(main).resetPasswords(List.of("user"), List.of("newpass"));
}

@Test
public void addNewUser() throws Exception {
AbstractMain main = spy(AbstractMain.class);
assertEquals(0, main.parseArguments(new String[] { "-X", "-nu", "user", "-p", "newpass" }));
verify(main).setDbDirectory(new File("."));
verify(main).setDbDirectory(DOT_FILE);
verify(main).addNewUsers(List.of("user"), List.of("newpass"), false);
}

@Test
public void addNewAdminUser() throws Exception {
AbstractMain main = spy(AbstractMain.class);
assertEquals(0, main.parseArguments(new String[] { "-X", "-nu", "user", "-p", "newpass", "-a" }));
verify(main).setDbDirectory(new File("."));
verify(main).setDbDirectory(DOT_FILE);
verify(main).addNewUsers(List.of("user"), List.of("newpass"), true);
}

Expand All @@ -119,7 +122,7 @@ public void changeUserAndPasswordInNonDefaultDatabase() throws Exception {
AbstractMain main = spy(AbstractMain.class);
assertEquals(0,
main.parseArguments(new String[] { "-X", "--dbd", "altDbDir", "-cu", "user", "-p", "newpass" }));
verify(main).setDbDirectory(new File("altDbDir"));
verify(main).setDbDirectory(Path.of("altDbDir").toFile());
verify(main).resetPasswords(List.of("user"), List.of("newpass"));
}

Expand Down Expand Up @@ -147,15 +150,15 @@ public void cantAddAndChangeUsersTogether() throws Exception {
public void deleteSingleUser() throws Exception {
AbstractMain main = spy(AbstractMain.class);
assertEquals(0, main.parseArguments(new String[] { "-X", "-du", "duser" }));
verify(main).setDbDirectory(new File("."));
verify(main).setDbDirectory(DOT_FILE);
verify(main).deleteUsers(List.of("duser"));
}

@Test
public void verify1User1Password() throws Exception {
AbstractMain main = spy(AbstractMain.class);
assertEquals(0, main.parseArguments(new String[] { "-X", "-vu", "user", "-p", "pass" }));
verify(main).setDbDirectory(new File("."));
verify(main).setDbDirectory(DOT_FILE);
verify(main).verifyUsers(List.of("user"), List.of("pass"));
}

Expand All @@ -164,7 +167,7 @@ public void verify2User2Password() throws Exception {
AbstractMain main = spy(AbstractMain.class);
assertEquals(0, main.parseArguments(new String[] {
"-X", "-vu", "user1", "-p", "pass1", "-vu", "user2", "-p", "pass2" }));
verify(main).setDbDirectory(new File("."));
verify(main).setDbDirectory(DOT_FILE);
verify(main).verifyUsers(List.of("user1", "user2"), List.of("pass1", "pass2"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;

/**
Expand All @@ -24,10 +23,10 @@ private FilesUtils() {
// Hidden on purpose
}

public static void delete(final String directory) throws IOException {
final Path path = Paths.get(directory);
if (Files.exists(path)) {
Files.walk(path).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
public static void delete(final Path directory) throws IOException {
if (Files.exists(directory)) {
Files.walk(directory).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}
}

}
28 changes: 11 additions & 17 deletions aaa-cli-jar/src/test/java/org/opendaylight/aaa/cli/jar/MainIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

Expand All @@ -27,8 +26,7 @@
* @author Michael Vorburger
*/
public class MainIT {

private static final String DIR = "target/" + MainIT.class.getSimpleName();
private static final Path DIR = Path.of("target", MainIT.class.getSimpleName());

@Test
public void integrationTestBuildJAR() throws Exception {
Expand All @@ -43,13 +41,13 @@ public void integrationTestBuildJAR() throws Exception {
}

private static void runFatJAR(final String... arguments) throws IOException, InterruptedException {
List<String> fullArguments = new ArrayList<>(Arrays.asList(
findJava().getAbsolutePath(),
final var fullArguments = new ArrayList<>(List.of(
Path.of(System.getProperty("java.home")).resolve("bin").resolve("java").toAbsolutePath().toString(),
"-jar",
findExecutableFatJAR().getAbsolutePath(),
findExecutableFatJAR().toAbsolutePath().toString(),
"--dbd",
DIR));
fullArguments.addAll(Arrays.asList(arguments));
DIR.toString()));
fullArguments.addAll(List.of(arguments));

// If Output piping to LOG instead of inheritIO() etc. is needed, then
// consider using https://github.com/vorburger/MariaDB4j/tree/master/mariaDB4j-core/src/main/java/ch/vorburger/exec
Expand All @@ -61,18 +59,14 @@ private static void runFatJAR(final String... arguments) throws IOException, Int
assertEquals(0, process.exitValue());
}

private static File findExecutableFatJAR() {
File targetDirectory = new File(".", "target");
File[] jarFiles = targetDirectory.listFiles((dir, name) -> name.startsWith("aaa-cli-jar-")
private static Path findExecutableFatJAR() {
final var targetDirectory = Path.of(".", "target").toFile();
final var jarFiles = targetDirectory.listFiles((dir, name) -> name.startsWith("aaa-cli-jar-")
&& name.endsWith(".jar")
&& !name.contains("-javadoc")
&& !name.contains("-sources"));
assertNotNull("*jar-with-dependencies.jar files in " + targetDirectory, jarFiles);
assertEquals("*jar-with-dependencies.jar files in " + targetDirectory, 1, jarFiles.length);
return jarFiles[0];
}

private static File findJava() {
return new File(new File(new File(System.getProperty("java.home")), "bin"), "java");
return jarFiles[0].toPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import static org.junit.Assert.assertEquals;

import java.nio.file.Path;
import org.junit.Test;

/**
Expand All @@ -19,32 +20,33 @@
* @author Michael Vorburger.ch
*/
public class MainIntegrationTest {
private static final String DIR = "target/" + MainIntegrationTest.class.getSimpleName();
private static final Path DIR = Path.of("target", MainIntegrationTest.class.getSimpleName());
private static final String DIR_STR = DIR.toString();

@Test
public void testCLI() throws Exception {
FilesUtils.delete(DIR);
assertEquals(0, new Main()
.parseArguments(new String[] { "-X", "--dbd", DIR, "-a", "-nu", "newuser", "-p", "firstpass" }));
.parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-a", "-nu", "newuser", "-p", "firstpass" }));
assertEquals(0, new Main()
.parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "firstpass" }));
.parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "firstpass" }));
assertEquals(-7, new Main()
.parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "wrongpass" }));
.parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "wrongpass" }));
assertEquals(0, new Main()
.parseArguments(new String[] { "-X", "--dbd", DIR, "-cu", "newuser", "-p", "newpass" }));
.parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-cu", "newuser", "-p", "newpass" }));
assertEquals(-7, new Main()
.parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "firstpass" }));
.parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "firstpass" }));
assertEquals(0, new Main()
.parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "newpass" }));
.parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "newpass" }));
assertEquals(0, new Main()
.parseArguments(new String[] { "-X", "--dbd", DIR, "-du", "newuser" }));
.parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-du", "newuser" }));
assertEquals(-7, new Main()
.parseArguments(new String[] { "-X", "--dbd", DIR, "-vu", "newuser", "-p", "newpass" }));
.parseArguments(new String[] { "-X", "--dbd", DIR_STR, "-vu", "newuser", "-p", "newpass" }));
}

@Test
public void testMismatchUsersPasswords() throws Exception {
assertEquals(-3, new Main().parseArguments(new String[] { "-X", "--dbd", DIR,
assertEquals(-3, new Main().parseArguments(new String[] { "-X", "--dbd", DIR_STR,
"-vu", "newuser1", "-p", "newpass1",
"-vu", "newuser2" /* No 2nd -p */ }));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Path;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -24,14 +24,14 @@
* @author Michael Vorburger.ch
*/
public class StandaloneCommandLineInterfaceTest {
private static final String DIR = "target/" + StandaloneCommandLineInterfaceTest.class.getSimpleName();
private static final Path DIR = Path.of("target", StandaloneCommandLineInterfaceTest.class.getSimpleName());

StandaloneCommandLineInterface cli;
private StandaloneCommandLineInterface cli;

@Before
public void before() throws Exception {
FilesUtils.delete(DIR);
cli = new StandaloneCommandLineInterface(new File(DIR));
cli = new StandaloneCommandLineInterface(DIR.toFile());
}

@Test
Expand Down

0 comments on commit dc14cc0

Please sign in to comment.