Skip to content

Commit

Permalink
Upgrade to Junit 5 (#4007)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsm5 committed Nov 17, 2022
1 parent ecb0c74 commit 5f3a2c0
Show file tree
Hide file tree
Showing 24 changed files with 312 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .lift.toml
@@ -1,4 +1,4 @@
build = "mvn -Pibissource,codecoverage -DskipTests=true"
build = "mvn install -Pibissource,codecoverage"
jdkVersion = "8"

disableTools = [ "golangci-lint" ]
Expand Down
9 changes: 7 additions & 2 deletions akamai/pom.xml
Expand Up @@ -40,8 +40,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
9 changes: 7 additions & 2 deletions aspose/pom.xml
Expand Up @@ -284,8 +284,13 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
9 changes: 7 additions & 2 deletions aws/pom.xml
Expand Up @@ -41,8 +41,13 @@

<!-- Test scoped and provided dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
9 changes: 7 additions & 2 deletions cmis/pom.xml
Expand Up @@ -84,8 +84,13 @@

<!-- Test scoped and provided dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
9 changes: 7 additions & 2 deletions core/pom.xml
Expand Up @@ -813,8 +813,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
20 changes: 11 additions & 9 deletions credentialProvider/pom.xml
Expand Up @@ -68,19 +68,21 @@
</dependency>


<!-- Test scoped dependencies -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

Expand All @@ -98,9 +100,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-test-framework</artifactId>
<version>${apache-directory-server.version}</version>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
<version>6.0.6</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -95,7 +95,7 @@ public Collection<String> getConfiguredAliases() throws UnsupportedOperationExce

private CredentialStore getCredentialStore(String credentialStore) {
if (cs==null) {
ServiceContainer registry = CurrentServiceContainer.getServiceContainer();
ServiceContainer registry = getServiceContainer();

if (registry==null) {
throw new IllegalStateException("no ServiceContainer registry found");
Expand All @@ -109,4 +109,8 @@ private CredentialStore getCredentialStore(String credentialStore) {
return cs;
}

//Make method mockable
protected ServiceContainer getServiceContainer() {
return CurrentServiceContainer.getServiceContainer();
}
}
Expand Up @@ -16,105 +16,90 @@

package net.wedjaa.ansible.vault.crypto;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VaultHandlerTest {

public class VaultHandlerTest
{
private static final String TEST_STRING = "This is a test";
private static final String TEST_PASSWORD = "password";
private static final String TEST_WRONG_PASSWORD = "not_this_one";
private static final String WRONG_PASS_EX = "HMAC Digest doesn't match - possibly it's the wrong password.";
private static final String DECODED_VAULT = "!net.wedjaa.ansible.vault.ProvisioningInfo\n" + "apiClientId: The provisioner ClientId\n" + "apiPassword: The secret password\n" + "apiUser: Secret User\n";

final static String TEST_STRING = "This is a test";
final static String TEST_PASSWORD = "password";
final static String TEST_WRONG_PASSWORD = "not_this_one";
final static String WRONG_PASS_EX = "HMAC Digest doesn't match - possibly it's the wrong password.";
final static String DECODED_VAULT =
"!net.wedjaa.ansible.vault.ProvisioningInfo\n"
+ "apiClientId: The provisioner ClientId\n"
+ "apiPassword: The secret password\n"
+ "apiUser: Secret User\n";
Logger logger = LoggerFactory.getLogger(VaultHandlerTest.class);

Logger logger = LoggerFactory.getLogger(VaultHandlerTest.class);

@BeforeClass
@BeforeAll
public static void setup() {
String loggings = ClassLoader.getSystemResource("logging.properties").getPath();

System.setProperty("java.util.logging.config.file", loggings);
}


@Test
public void testByteArrayValidVault()
{
logger.info("Testing Byte Array decryption - Valid Password");
try
{
byte [] encryptedTest = VaultHandler.encrypt(TEST_STRING.getBytes(), TEST_PASSWORD);
logger.debug("Encrypted vault:\n{}", new String(encryptedTest));
byte [] decryptedTest = VaultHandler.decrypt(encryptedTest, TEST_PASSWORD);
logger.debug("Decrypted vault:\n{}", new String(decryptedTest));
assertEquals(TEST_STRING, new String(decryptedTest));

} catch(Exception ex) {
fail("Failed to decode the test vault: " + ex.getMessage());
}
}

@Test
public void testByteArrayInvalidVault()
{
logger.info("Testing Byte Array decryption - Invalid Password");
try
{
byte [] encryptedTest = VaultHandler.encrypt(TEST_STRING.getBytes(), TEST_PASSWORD);
logger.debug("Encrypted vault:\n{}", new String(encryptedTest));
byte [] decryptedTest = VaultHandler.decrypt(encryptedTest, TEST_WRONG_PASSWORD);
logger.debug("Decrypted vault:\n{}", new String(decryptedTest));
fail("Should not be able to decrypt text with the wrong password");
} catch(Exception ex) {
assertEquals(WRONG_PASS_EX, ex.getMessage());
}
}

@Test
public void testStreamValidVault()
{
logger.info("Testing decoding vault Stream - Valid password ");
try
{
ByteArrayOutputStream decodedStream = new ByteArrayOutputStream();
InputStream encodedStream = getClass().getClassLoader().getResourceAsStream("test-vault.yml");
VaultHandler.decrypt(encodedStream, decodedStream, TEST_PASSWORD);
String decoded = new String(decodedStream.toByteArray());
assertEquals(DECODED_VAULT, decoded);
} catch(Exception ex) {
fail("Failed to decode the test vault from stream: " + ex.getMessage());
}
}

@Test
public void testStreamInvalidVault()
{
logger.info("Testing decoding vault Stream - Invalid password ");
try
{
ByteArrayOutputStream decodedStream = new ByteArrayOutputStream();
InputStream encodedStream = getClass().getClassLoader().getResourceAsStream("test-vault.yml");
VaultHandler.decrypt(encodedStream, decodedStream, TEST_WRONG_PASSWORD);
// String decoded = new String(decodedStream.toByteArray());
fail("Should not be able to decrypt text with the wrong password");

} catch(Exception ex) {
assertEquals(WRONG_PASS_EX, ex.getMessage());
}
}
}

@Test
public void testByteArrayValidVault() {
logger.info("Testing Byte Array decryption - Valid Password");
try {
byte[] encryptedTest = VaultHandler.encrypt(TEST_STRING.getBytes(), TEST_PASSWORD);
logger.debug("Encrypted vault:\n{}", new String(encryptedTest));
byte[] decryptedTest = VaultHandler.decrypt(encryptedTest, TEST_PASSWORD);
logger.debug("Decrypted vault:\n{}", new String(decryptedTest));
assertEquals(TEST_STRING, new String(decryptedTest));

} catch (Exception ex) {
fail("Failed to decode the test vault: " + ex.getMessage());
}
}

@Test
public void testByteArrayInvalidVault() throws IOException {
logger.info("Testing Byte Array decryption - Invalid Password");
byte[] encryptedTest = VaultHandler.encrypt(TEST_STRING.getBytes(), TEST_PASSWORD);
logger.debug("Encrypted vault:\n{}", new String(encryptedTest));
try {
byte[] decryptedTest = VaultHandler.decrypt(encryptedTest, TEST_WRONG_PASSWORD);
logger.debug("Decrypted vault:\n{}", new String(decryptedTest));
fail("Should not be able to decrypt text with the wrong password");
} catch (Exception ex) {
assertEquals(WRONG_PASS_EX, ex.getMessage());
}
}

@Test
public void testStreamValidVault() {
logger.info("Testing decoding vault Stream - Valid password ");
try {
ByteArrayOutputStream decodedStream = new ByteArrayOutputStream();
InputStream encodedStream = getClass().getClassLoader().getResourceAsStream("test-vault.yml");
VaultHandler.decrypt(encodedStream, decodedStream, TEST_PASSWORD);
String decoded = new String(decodedStream.toByteArray());
assertEquals(DECODED_VAULT, decoded);
} catch (Exception ex) {
fail("Failed to decode the test vault from stream: " + ex.getMessage());
}
}

@Test
public void testStreamInvalidVault() {
logger.info("Testing decoding vault Stream - Invalid password ");
try {
ByteArrayOutputStream decodedStream = new ByteArrayOutputStream();
InputStream encodedStream = getClass().getClassLoader().getResourceAsStream("test-vault.yml");
VaultHandler.decrypt(encodedStream, decodedStream, TEST_WRONG_PASSWORD);
fail("Should not be able to decrypt text with the wrong password");

} catch (Exception ex) {
assertEquals(WRONG_PASS_EX, ex.getMessage());
}
}

}
@@ -1,8 +1,8 @@
package nl.nn.credentialprovider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
Expand All @@ -15,8 +15,8 @@
import java.util.NoSuchElementException;
import java.util.Properties;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import net.wedjaa.ansible.vault.crypto.VaultHandler;

Expand All @@ -29,7 +29,7 @@ public class AnsibleVaultCredentialFactoryTest {

private AnsibleVaultCredentialFactory credentialFactory;

@Before
@BeforeEach
public void setup() throws IOException {
String vaultUrl = this.getClass().getResource(ANSIBLE_VAULT_FILE).toExternalForm();
String vaultFile = Paths.get(vaultUrl.substring(vaultUrl.indexOf(":/")+2)).toString();
Expand Down
@@ -1,11 +1,11 @@
package nl.nn.credentialprovider;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

@Ignore("Can only run before other tests that use CredentialFactory")
@Disabled("Can only run before other tests that use CredentialFactory")
public class CredentialFactoryTest {

@Test
Expand All @@ -15,12 +15,11 @@ public void testFindAliasNoPrefix() {
assertEquals("fakeUsername", c.getUsername());
assertEquals("fakePassword", c.getPassword());
}

@Test
public void testFindAliasWithPrefix() {
ICredentials c = CredentialFactory.getCredentials("fakePrefix:account", null, null);
assertEquals("fakeUsername", c.getUsername());
assertEquals("fakePassword", c.getPassword());
}

}

0 comments on commit 5f3a2c0

Please sign in to comment.