Skip to content

Commit

Permalink
migrate credentials tests to jUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
swalchemist committed Mar 9, 2024
1 parent eddc909 commit d7188b0
Show file tree
Hide file tree
Showing 68 changed files with 386 additions and 482 deletions.
8 changes: 6 additions & 2 deletions components/credentials/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ dependencies {
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.boot:spring-boot-starter-validation')
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("junit:junit")

testImplementation(platform('org.junit:junit-bom:5.10.2'))
testImplementation('org.junit.jupiter:junit-jupiter')
implementation('com.fasterxml.jackson.module:jackson-module-kotlin')
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-reflect")
Expand Down Expand Up @@ -93,6 +93,10 @@ test {
exceptionFormat "full"
}

test {
useJUnitPlatform {}
}

systemProperties = System.properties
systemProperties["spring.profiles.active"] = System.getProperty("spring.profiles.active", "unit-test-h2")
systemProperties["java.security.egd"] = System.getProperty("java.security.egd", "file:/dev/urandom")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.cloudfoundry.credhub.credential;

import org.cloudfoundry.credhub.CryptSaltFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

@RunWith(JUnit4.class)
public class UserCredentialValueTest {
@Test
public void getSalt_returnsAConsistentSalt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import org.cloudfoundry.credhub.TestHelper;
import org.cloudfoundry.credhub.entities.EncryptedValue;
import org.cloudfoundry.credhub.entity.CertificateCredentialVersionData;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.cloudfoundry.credhub.utils.CertificateStringConstants.PRIVATE_KEY;
Expand All @@ -20,7 +18,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class CertificateCredentialVersionTest {

private CertificateCredentialVersion subject;
Expand All @@ -32,7 +29,7 @@ public class CertificateCredentialVersionTest {
private byte[] encryptedValue;
private byte[] nonce;

@Before
@BeforeEach
public void setup() {
TestHelper.getBouncyCastleFipsProvider();
encryptor = mock(Encryptor.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.cloudfoundry.credhub.domain;

import org.cloudfoundry.credhub.requests.CertificateGenerationRequestParameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

@RunWith(JUnit4.class)
public class CertificateGenerationParametersTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import org.cloudfoundry.credhub.utils.BouncyCastleFipsConfigurer;
import org.cloudfoundry.credhub.utils.JsonObjectMapper;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.cloudfoundry.credhub.utils.CertificateStringConstants.SELF_SIGNED_CA_CERT;
Expand All @@ -34,7 +32,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class CredentialFactoryTest {

private static final String CREDENTIAL_NAME = "/test";
Expand All @@ -56,12 +53,12 @@ public class CredentialFactoryTest {
private JsonObjectMapper objectMapper;
private StringGenerationParameters generationParameters;

@BeforeClass
@BeforeAll
public static void setUpAll() {
BouncyCastleFipsConfigurer.configure();
}

@Before
@BeforeEach
public void setup() throws JsonProcessingException {

if (Security.getProvider(BouncyCastleFipsProvider.PROVIDER_NAME) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@
import org.cloudfoundry.credhub.entity.ValueCredentialVersionData;
import org.cloudfoundry.credhub.requests.StringGenerationParameters;
import org.cloudfoundry.credhub.services.RetryingEncryptionService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class CredentialRotationTest {

private UUID activeEncryptionKeyUuid;
Expand All @@ -34,7 +31,7 @@ public class CredentialRotationTest {
private RetryingEncryptionService encryptionService;
private Encryptor encryptor;

@Before
@BeforeEach
public void beforeEach() throws Exception {
encryptionService = mock(RetryingEncryptionService.class);
encryptor = new DefaultEncryptor(encryptionService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,25 @@
import org.cloudfoundry.credhub.entities.EncryptedValue;
import org.cloudfoundry.credhub.entity.JsonCredentialVersionData;
import org.cloudfoundry.credhub.exceptions.ParameterizedValidationException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static junit.framework.TestCase.fail;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class JsonCredentialVersionTest {

private JsonCredentialVersion subject;
private JsonNode value;

private JsonCredentialVersionData jsonCredentialData;

@Before
@BeforeEach
public void beforeEach() throws JsonProcessingException {
final String jsonString = "{\"simple\":\"just-a-string\",\"complex\":{\"key\":\"value\"}}";
final ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@
import org.cloudfoundry.credhub.requests.StringGenerationParameters;
import org.cloudfoundry.credhub.utils.JsonObjectMapper;
import org.hamcrest.MatcherAssert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class PasswordCredentialVersionTest {

private static final String PASSWORD = "my-password";
Expand All @@ -40,7 +38,7 @@ public class PasswordCredentialVersionTest {
private byte[] encryptedParametersValue;
private byte[] parametersNonce;

@Before
@BeforeEach
public void beforeEach() throws Exception {
canaryUuid = UUID.randomUUID();
encryptor = mock(Encryptor.class);
Expand Down Expand Up @@ -122,9 +120,11 @@ public void setPasswordAndGenerationParameters_canDecryptValues() {
MatcherAssert.assertThat(subject.getGenerationParameters().isExcludeUpper(), equalTo(false));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void setPasswordAndGenerationParameters_throwsAnExceptionWhenSettingANullValue() {
subject.setPasswordAndGenerationParameters(null, null);
assertThrows(IllegalArgumentException.class, () ->
subject.setPasswordAndGenerationParameters(null, null)
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

import org.cloudfoundry.credhub.entity.RsaCredentialVersionData;
import org.cloudfoundry.credhub.requests.RsaGenerationParameters;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class RsaCredentialVersionTest {

private RsaCredentialVersion subject;
private RsaCredentialVersionData credentialVersionData;

@Before
@BeforeEach
public void setUp() {
credentialVersionData = mock(RsaCredentialVersionData.class);
subject = new RsaCredentialVersion(credentialVersionData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
import org.cloudfoundry.credhub.entity.SshCredentialVersionData;
import org.cloudfoundry.credhub.requests.SshGenerationParameters;
import org.cloudfoundry.credhub.utils.SshPublicKeyParser;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class SshCredentialVersionTest {

private SshCredentialVersion subject;
private SshPublicKeyParser sshPublicKeyParser;

@Before
@BeforeEach
public void setUp() {
sshPublicKeyParser = mock(SshPublicKeyParser.class);
final SshCredentialVersionData credentialVersionData = mock(SshCredentialVersionData.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@
import org.cloudfoundry.credhub.entity.UserCredentialVersionData;
import org.cloudfoundry.credhub.requests.StringGenerationParameters;
import org.cloudfoundry.credhub.utils.JsonObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.samePropertyValuesAs;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class UserCredentialVersionTest {
private final String CREDENTIAL_NAME = "/test/user";
private final String USER_PASSWORD = "test-user-password";
Expand All @@ -41,7 +38,7 @@ public UserCredentialVersionTest() throws Exception {
super();
}

@Before
@BeforeEach
public void beforeEach() throws JsonProcessingException {
encryptor = mock(Encryptor.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@

import org.cloudfoundry.credhub.entities.EncryptedValue;
import org.cloudfoundry.credhub.entity.ValueCredentialVersionData;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
public class ValueCredentialVersionTest {

private ValueCredentialVersion subject;
private Encryptor encryptor;
private UUID canaryUuid;
private ValueCredentialVersionData valueCredentialData;

@Before
@BeforeEach
public void beforeEach() {
canaryUuid = UUID.randomUUID();
encryptor = mock(Encryptor.class);
Expand Down Expand Up @@ -67,12 +65,14 @@ public void getValue_decryptsValue() {
assertThat(subject.getValue(), equalTo("my-value"));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void setValue_whenValueIsNull_throwsException() {
valueCredentialData = new ValueCredentialVersionData("foo");
subject = new ValueCredentialVersion(valueCredentialData);

subject.setEncryptor(encryptor);
subject.setValue(null);
assertThrows(IllegalArgumentException.class, () ->
subject.setValue(null)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package org.cloudfoundry.credhub.entities;

import org.cloudfoundry.credhub.entity.PasswordCredentialVersionData;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsEqual.equalTo;

@RunWith(JUnit4.class)
public class CredentialVersionDataTest {

private PasswordCredentialVersionData passwordCredentialData;

@Before
@BeforeEach
public void beforeEach() {
passwordCredentialData = new PasswordCredentialVersionData("/Picard");
}
Expand Down
Loading

0 comments on commit d7188b0

Please sign in to comment.