test: use assertThrows instead of try-catch blocks
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
2b0f458da3
commit
76a5ea4fe9
@ -30,6 +30,7 @@ import org.apache.http.message.BasicStatusLine;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -44,7 +45,8 @@ import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ -85,35 +87,32 @@ class HTTPVaultConnectorOfflineTest {
|
||||
// Test invalid response code.
|
||||
final int responseCode = 400;
|
||||
mockResponse(responseCode, "", ContentType.APPLICATION_JSON);
|
||||
try {
|
||||
connector.getHealth();
|
||||
fail("Querying health status succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertThat("Unexpected type of exception", e, instanceOf(InvalidResponseException.class));
|
||||
InvalidResponseException e = assertThrows(
|
||||
InvalidResponseException.class,
|
||||
connector::getHealth,
|
||||
"Querying health status succeeded on invalid instance"
|
||||
);
|
||||
assertThat("Unexpected exception message", e.getMessage(), is("Invalid response code"));
|
||||
assertThat("Unexpected status code in exception", ((InvalidResponseException) e).getStatusCode(), is(responseCode));
|
||||
assertThat("Response message where none was expected", ((InvalidResponseException) e).getResponse(), is(nullValue()));
|
||||
}
|
||||
|
||||
// Simulate permission denied response.
|
||||
mockResponse(responseCode, "{\"errors\":[\"permission denied\"]}", ContentType.APPLICATION_JSON);
|
||||
try {
|
||||
connector.getHealth();
|
||||
fail("Querying health status succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertThat("Unexpected type of exception", e, instanceOf(PermissionDeniedException.class));
|
||||
}
|
||||
assertThrows(
|
||||
PermissionDeniedException.class,
|
||||
connector::getHealth,
|
||||
"Querying health status succeeded on invalid instance"
|
||||
);
|
||||
|
||||
// Test exception thrown during request.
|
||||
when(httpMock.execute(any())).thenThrow(new IOException("Test Exception"));
|
||||
try {
|
||||
connector.getHealth();
|
||||
fail("Querying health status succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertThat("Unexpected type of exception", e, instanceOf(InvalidResponseException.class));
|
||||
e = assertThrows(
|
||||
InvalidResponseException.class,
|
||||
connector::getHealth,
|
||||
"Querying health status succeeded on invalid instance"
|
||||
);
|
||||
assertThat("Unexpected exception message", e.getMessage(), is("Unable to read response"));
|
||||
assertThat("Unexpected cause", e.getCause(), instanceOf(IOException.class));
|
||||
}
|
||||
|
||||
// Now simulate a failing request that succeeds on second try.
|
||||
connector = new HTTPVaultConnector("https://127.0.0.1", null, 1, 250);
|
||||
@ -125,11 +124,7 @@ class HTTPVaultConnectorOfflineTest {
|
||||
.when(responseMock).getStatusLine();
|
||||
when(responseMock.getEntity()).thenReturn(new StringEntity("{}", ContentType.APPLICATION_JSON));
|
||||
|
||||
try {
|
||||
connector.getHealth();
|
||||
} catch (Exception e) {
|
||||
fail("Request failed unexpectedly: " + e.getMessage());
|
||||
}
|
||||
assertDoesNotThrow(connector::getHealth, "Request failed unexpectedly");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -188,54 +183,47 @@ class HTTPVaultConnectorOfflineTest {
|
||||
* This test is designed to test exceptions caught and thrown by seal-methods if Vault is not reachable.
|
||||
*/
|
||||
@Test
|
||||
void sealExceptionTest() throws IOException {
|
||||
void sealExceptionTest() {
|
||||
HTTPVaultConnector connector = new HTTPVaultConnector(INVALID_URL);
|
||||
try {
|
||||
connector.sealStatus();
|
||||
fail("Querying seal status succeeded on invalid URL");
|
||||
} catch (Exception e) {
|
||||
assertThat("Unexpected type of exception", e, instanceOf(InvalidRequestException.class));
|
||||
VaultConnectorException e = assertThrows(
|
||||
InvalidRequestException.class,
|
||||
connector::sealStatus,
|
||||
"Querying seal status succeeded on invalid URL"
|
||||
);
|
||||
assertThat("Unexpected exception message", e.getMessage(), is("Invalid URI format"));
|
||||
}
|
||||
|
||||
connector = new HTTPVaultConnector("https://127.0.0.1", null, 0, 250);
|
||||
|
||||
// Simulate NULL response (mock not supplied with data).
|
||||
|
||||
try {
|
||||
connector.sealStatus();
|
||||
fail("Querying seal status succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertThat("Unexpected type of exception", e, instanceOf(InvalidResponseException.class));
|
||||
connector = new HTTPVaultConnector("https://127.0.0.1", null, 0, 250);
|
||||
e = assertThrows(
|
||||
InvalidResponseException.class,
|
||||
connector::sealStatus,
|
||||
"Querying seal status succeeded on invalid instance"
|
||||
);
|
||||
assertThat("Unexpected exception message", e.getMessage(), is("Response unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This test is designed to test exceptions caught and thrown by seal-methods if Vault is not reachable.
|
||||
*/
|
||||
@Test
|
||||
void healthExceptionTest() throws IOException {
|
||||
void healthExceptionTest() {
|
||||
HTTPVaultConnector connector = new HTTPVaultConnector(INVALID_URL);
|
||||
try {
|
||||
connector.getHealth();
|
||||
fail("Querying health status succeeded on invalid URL");
|
||||
} catch (Exception e) {
|
||||
assertThat("Unexpected type of exception", e, instanceOf(InvalidRequestException.class));
|
||||
VaultConnectorException e = assertThrows(
|
||||
InvalidRequestException.class,
|
||||
connector::getHealth,
|
||||
"Querying health status succeeded on invalid URL"
|
||||
);
|
||||
assertThat("Unexpected exception message", e.getMessage(), is("Invalid URI format"));
|
||||
}
|
||||
|
||||
connector = new HTTPVaultConnector("https://127.0.0.1", null, 0, 250);
|
||||
|
||||
// Simulate NULL response (mock not supplied with data).
|
||||
try {
|
||||
connector.getHealth();
|
||||
fail("Querying health status succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertThat("Unexpected type of exception", e, instanceOf(InvalidResponseException.class));
|
||||
connector = new HTTPVaultConnector("https://127.0.0.1", null, 0, 250);
|
||||
e = assertThrows(
|
||||
InvalidResponseException.class,
|
||||
connector::getHealth,
|
||||
"Querying health status succeeded on invalid instance"
|
||||
);
|
||||
assertThat("Unexpected exception message", e.getMessage(), is("Response unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test behavior on unparsable responses.
|
||||
@ -249,114 +237,25 @@ class HTTPVaultConnectorOfflineTest {
|
||||
mockResponse(200, "invalid", ContentType.APPLICATION_JSON);
|
||||
|
||||
// Now test the methods.
|
||||
try {
|
||||
connector.sealStatus();
|
||||
fail("sealStatus() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
assertParseError(connector::sealStatus, "sealStatus() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.unseal("key"), "unseal() succeeded on invalid instance");
|
||||
assertParseError(connector::getHealth, "getHealth() succeeded on invalid instance");
|
||||
assertParseError(connector::getAuthBackends, "getAuthBackends() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.authToken("token"), "authToken() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.lookupAppRole("roleName"), "lookupAppRole() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.getAppRoleID("roleName"), "getAppRoleID() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.createAppRoleSecret("roleName"), "createAppRoleSecret() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.lookupAppRoleSecret("roleName", "secretID"), "lookupAppRoleSecret() succeeded on invalid instance");
|
||||
assertParseError(connector::listAppRoles, "listAppRoles() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.listAppRoleSecrets("roleName"), "listAppRoleSecrets() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.read("key"), "read() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.list("path"), "list() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.renew("leaseID"), "renew() succeeded on invalid instance");
|
||||
assertParseError(() -> connector.lookupToken("token"), "lookupToken() succeeded on invalid instance");
|
||||
}
|
||||
|
||||
try {
|
||||
connector.unseal("key");
|
||||
fail("unseal() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.getHealth();
|
||||
fail("getHealth() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.getAuthBackends();
|
||||
fail("getAuthBackends() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.authToken("token");
|
||||
fail("authToken() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.lookupAppRole("roleName");
|
||||
fail("lookupAppRole() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.getAppRoleID("roleName");
|
||||
fail("getAppRoleID() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.createAppRoleSecret("roleName");
|
||||
fail("createAppRoleSecret() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.lookupAppRoleSecret("roleName", "secretID");
|
||||
fail("lookupAppRoleSecret() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.listAppRoles();
|
||||
fail("listAppRoles() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.listAppRoleSecrets("roleName");
|
||||
fail("listAppRoleSecrets() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.read("key");
|
||||
fail("read() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.list("path");
|
||||
fail("list() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.renew("leaseID");
|
||||
fail("renew() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
|
||||
try {
|
||||
connector.lookupToken("token");
|
||||
fail("lookupToken() succeeded on invalid instance");
|
||||
} catch (Exception e) {
|
||||
assertParseError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertParseError(Exception e) {
|
||||
assertThat("Unexpected type of exception", e, instanceOf(InvalidResponseException.class));
|
||||
private void assertParseError(Executable executable, String message) {
|
||||
InvalidResponseException e = assertThrows(InvalidResponseException.class, executable, message);
|
||||
assertThat("Unexpected exception message", e.getMessage(), is("Unable to parse response"));
|
||||
}
|
||||
|
||||
@ -372,68 +271,59 @@ class HTTPVaultConnectorOfflineTest {
|
||||
mockResponse(200, "{}", ContentType.APPLICATION_JSON);
|
||||
|
||||
// Now test the methods expecting a 204.
|
||||
try {
|
||||
connector.registerAppId("appID", "policy", "displayName");
|
||||
fail("registerAppId() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.registerAppId("appID", "policy", "displayName"),
|
||||
"registerAppId() with 200 response succeeded"
|
||||
);
|
||||
|
||||
try {
|
||||
connector.registerUserId("appID", "userID");
|
||||
fail("registerUserId() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.registerUserId("appID", "userID"),
|
||||
"registerUserId() with 200 response succeeded"
|
||||
);
|
||||
|
||||
try {
|
||||
connector.createAppRole("appID", Collections.singletonList("policy"));
|
||||
fail("createAppRole() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.createAppRole("appID", Collections.singletonList("policy")),
|
||||
"createAppRole() with 200 response succeeded"
|
||||
);
|
||||
|
||||
try {
|
||||
connector.deleteAppRole("roleName");
|
||||
fail("deleteAppRole() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.deleteAppRole("roleName"),
|
||||
"deleteAppRole() with 200 response succeeded"
|
||||
);
|
||||
|
||||
try {
|
||||
connector.setAppRoleID("roleName", "roleID");
|
||||
fail("setAppRoleID() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.setAppRoleID("roleName", "roleID"),
|
||||
"setAppRoleID() with 200 response succeeded"
|
||||
);
|
||||
|
||||
try {
|
||||
connector.destroyAppRoleSecret("roleName", "secretID");
|
||||
fail("destroyAppRoleSecret() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.destroyAppRoleSecret("roleName", "secretID"),
|
||||
"destroyAppRoleSecret() with 200 response succeeded"
|
||||
);
|
||||
|
||||
try {
|
||||
connector.destroyAppRoleSecret("roleName", "secretUD");
|
||||
fail("destroyAppRoleSecret() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.destroyAppRoleSecret("roleName", "secretUD"),
|
||||
"destroyAppRoleSecret() with 200 response succeeded"
|
||||
);
|
||||
|
||||
try {
|
||||
connector.delete("key");
|
||||
fail("delete() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.delete("key"),
|
||||
"delete() with 200 response succeeded"
|
||||
);
|
||||
|
||||
try {
|
||||
connector.revoke("leaseID");
|
||||
fail("destroyAppRoleSecret() with 200 response succeeded");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> connector.revoke("leaseID"),
|
||||
"destroyAppRoleSecret() with 200 response succeeded"
|
||||
);
|
||||
}
|
||||
|
||||
private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) {
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -18,7 +18,6 @@ package de.stklcode.jvault.connector.builder;
|
||||
|
||||
import de.stklcode.jvault.connector.HTTPVaultConnector;
|
||||
import de.stklcode.jvault.connector.exception.TlsException;
|
||||
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||
import de.stklcode.jvault.connector.test.EnvironmentMock;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@ -30,7 +29,8 @@ import java.nio.file.NoSuchFileException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* JUnit test for HTTP Vault connector factory
|
||||
@ -53,15 +53,11 @@ class HTTPVaultConnectorBuilderTest {
|
||||
void testFromEnv() throws NoSuchFieldException, IllegalAccessException, IOException {
|
||||
/* Provide address only should be enough */
|
||||
setenv(VAULT_ADDR, null, null, null);
|
||||
|
||||
HTTPVaultConnectorBuilder factory = null;
|
||||
HTTPVaultConnector connector;
|
||||
try {
|
||||
factory = VaultConnectorBuilder.http().fromEnv();
|
||||
} catch (VaultConnectorException e) {
|
||||
fail("Factory creation from minimal environment failed");
|
||||
}
|
||||
connector = factory.build();
|
||||
HTTPVaultConnectorBuilder factory = assertDoesNotThrow(
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
"Factory creation from minimal environment failed"
|
||||
);
|
||||
HTTPVaultConnector connector = factory.build();
|
||||
|
||||
assertThat("URL nor set correctly", getRequestHelperPrivate(connector, "baseURL"), is(equalTo(VAULT_ADDR + "/v1/")));
|
||||
assertThat("Trusted CA cert set when no cert provided", getRequestHelperPrivate(connector, "trustedCaCert"), is(nullValue()));
|
||||
@ -70,11 +66,10 @@ class HTTPVaultConnectorBuilderTest {
|
||||
/* Provide address and number of retries */
|
||||
setenv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), null);
|
||||
|
||||
try {
|
||||
factory = VaultConnectorBuilder.http().fromEnv();
|
||||
} catch (VaultConnectorException e) {
|
||||
fail("Factory creation from environment failed");
|
||||
}
|
||||
factory = assertDoesNotThrow(
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
"Factory creation from environment failed"
|
||||
);
|
||||
connector = factory.build();
|
||||
|
||||
assertThat("URL nor set correctly", getRequestHelperPrivate(connector, "baseURL"), is(equalTo(VAULT_ADDR + "/v1/")));
|
||||
@ -85,23 +80,21 @@ class HTTPVaultConnectorBuilderTest {
|
||||
String VAULT_CACERT = tempDir.toString() + "/doesnotexist";
|
||||
setenv(VAULT_ADDR, VAULT_CACERT, VAULT_MAX_RETRIES.toString(), null);
|
||||
|
||||
try {
|
||||
VaultConnectorBuilder.http().fromEnv();
|
||||
fail("Creation with unknown cert path failed.");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat(e, is(instanceOf(TlsException.class)));
|
||||
TlsException e = assertThrows(
|
||||
TlsException.class,
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
"Creation with unknown cert path failed."
|
||||
);
|
||||
assertThat(e.getCause(), is(instanceOf(NoSuchFileException.class)));
|
||||
assertThat(((NoSuchFileException) e.getCause()).getFile(), is(VAULT_CACERT));
|
||||
}
|
||||
|
||||
/* Automatic authentication */
|
||||
setenv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), VAULT_TOKEN);
|
||||
|
||||
try {
|
||||
factory = VaultConnectorBuilder.http().fromEnv();
|
||||
} catch (VaultConnectorException e) {
|
||||
fail("Factory creation from minimal environment failed");
|
||||
}
|
||||
factory = assertDoesNotThrow(
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
"Factory creation from minimal environment failed"
|
||||
);
|
||||
assertThat("Token nor set correctly", getPrivate(factory, "token"), is(equalTo(VAULT_TOKEN)));
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,6 @@ package de.stklcode.jvault.connector.factory;
|
||||
|
||||
import de.stklcode.jvault.connector.HTTPVaultConnector;
|
||||
import de.stklcode.jvault.connector.exception.TlsException;
|
||||
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||
import de.stklcode.jvault.connector.test.EnvironmentMock;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@ -30,7 +29,8 @@ import java.nio.file.NoSuchFileException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* JUnit test for HTTP Vault connector factory
|
||||
@ -54,14 +54,11 @@ class HTTPVaultConnectorFactoryTest {
|
||||
/* Provide address only should be enough */
|
||||
setenv(VAULT_ADDR, null, null, null);
|
||||
|
||||
HTTPVaultConnectorFactory factory = null;
|
||||
HTTPVaultConnector connector;
|
||||
try {
|
||||
factory = VaultConnectorFactory.httpFactory().fromEnv();
|
||||
} catch (VaultConnectorException e) {
|
||||
fail("Factory creation from minimal environment failed");
|
||||
}
|
||||
connector = factory.build();
|
||||
HTTPVaultConnectorFactory factory = assertDoesNotThrow(
|
||||
() -> VaultConnectorFactory.httpFactory().fromEnv(),
|
||||
"Factory creation from minimal environment failed"
|
||||
);
|
||||
HTTPVaultConnector connector = factory.build();
|
||||
|
||||
assertThat("URL nor set correctly", getRequestHelperPrivate(connector, "baseURL"), is(equalTo(VAULT_ADDR + "/v1/")));
|
||||
assertThat("Trusted CA cert set when no cert provided", getRequestHelperPrivate(connector, "trustedCaCert"), is(nullValue()));
|
||||
@ -70,11 +67,10 @@ class HTTPVaultConnectorFactoryTest {
|
||||
/* Provide address and number of retries */
|
||||
setenv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), null);
|
||||
|
||||
try {
|
||||
factory = VaultConnectorFactory.httpFactory().fromEnv();
|
||||
} catch (VaultConnectorException e) {
|
||||
fail("Factory creation from environment failed");
|
||||
}
|
||||
factory = assertDoesNotThrow(
|
||||
() -> VaultConnectorFactory.httpFactory().fromEnv(),
|
||||
"Factory creation from environment failed"
|
||||
);
|
||||
connector = factory.build();
|
||||
|
||||
assertThat("URL nor set correctly", getRequestHelperPrivate(connector, "baseURL"), is(equalTo(VAULT_ADDR + "/v1/")));
|
||||
@ -85,23 +81,21 @@ class HTTPVaultConnectorFactoryTest {
|
||||
String VAULT_CACERT = tempDir.toString() + "/doesnotexist";
|
||||
setenv(VAULT_ADDR, VAULT_CACERT, VAULT_MAX_RETRIES.toString(), null);
|
||||
|
||||
try {
|
||||
VaultConnectorFactory.httpFactory().fromEnv();
|
||||
fail("Creation with unknown cert path failed.");
|
||||
} catch (VaultConnectorException e) {
|
||||
assertThat(e, is(instanceOf(TlsException.class)));
|
||||
TlsException e = assertThrows(
|
||||
TlsException.class,
|
||||
() -> VaultConnectorFactory.httpFactory().fromEnv(),
|
||||
"Creation with unknown cert path failed."
|
||||
);
|
||||
assertThat(e.getCause(), is(instanceOf(NoSuchFileException.class)));
|
||||
assertThat(((NoSuchFileException) e.getCause()).getFile(), is(VAULT_CACERT));
|
||||
}
|
||||
|
||||
/* Automatic authentication */
|
||||
setenv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), VAULT_TOKEN);
|
||||
|
||||
try {
|
||||
factory = VaultConnectorFactory.httpFactory().fromEnv();
|
||||
} catch (VaultConnectorException e) {
|
||||
fail("Factory creation from minimal environment failed");
|
||||
}
|
||||
factory = assertDoesNotThrow(
|
||||
() -> VaultConnectorFactory.httpFactory().fromEnv(),
|
||||
"Factory creation from minimal environment failed"
|
||||
);
|
||||
assertThat("Token nor set correctly", getPrivate(getPrivate(factory, "delegate"), "token"), is(equalTo(VAULT_TOKEN)));
|
||||
}
|
||||
|
||||
|
@ -16,11 +16,9 @@
|
||||
|
||||
package de.stklcode.jvault.connector.model;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@ -29,7 +27,7 @@ import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
|
||||
@ -120,26 +118,17 @@ class AppRoleSecretTest {
|
||||
|
||||
/* A simple roundtrip first. All set fields should be present afterwards. */
|
||||
AppRoleSecret secret = new AppRoleSecret(TEST_ID, TEST_META, TEST_CIDR);
|
||||
String secretJson = "";
|
||||
try {
|
||||
secretJson = mapper.writeValueAsString(secret);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
fail("Serialization failed");
|
||||
}
|
||||
String secretJson = assertDoesNotThrow(() -> mapper.writeValueAsString(secret), "Serialization failed");
|
||||
/* CIDR list is comma-separated when used as input, but List otherwise, hence convert string to list */
|
||||
secretJson = commaSeparatedToList(secretJson);
|
||||
String secretJson2 = commaSeparatedToList(secretJson);
|
||||
|
||||
AppRoleSecret secret2;
|
||||
try {
|
||||
secret2 = mapper.readValue(secretJson, AppRoleSecret.class);
|
||||
AppRoleSecret secret2 = assertDoesNotThrow(
|
||||
() -> mapper.readValue(secretJson2, AppRoleSecret.class),
|
||||
"Deserialization failed"
|
||||
);
|
||||
assertThat(secret.getId(), is(secret2.getId()));
|
||||
assertThat(secret.getMetadata(), is(secret2.getMetadata()));
|
||||
assertThat(secret.getCidrList(), is(secret2.getCidrList()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail("Deserialization failed");
|
||||
}
|
||||
|
||||
/* Test fields, that should not be written to JSON */
|
||||
setPrivateField(secret, "accessor", "TEST_ACCESSOR");
|
||||
@ -154,14 +143,11 @@ class AppRoleSecretTest {
|
||||
assumeTrue(secret.getNumUses() == 678);
|
||||
setPrivateField(secret, "ttl", 12345);
|
||||
assumeTrue(secret.getTtl() == 12345);
|
||||
try {
|
||||
secretJson = mapper.writeValueAsString(secret);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
fail("Serialization failed");
|
||||
}
|
||||
try {
|
||||
secret2 = mapper.readValue(commaSeparatedToList(secretJson), AppRoleSecret.class);
|
||||
String secretJson3 = assertDoesNotThrow(() -> mapper.writeValueAsString(secret), "Serialization failed");
|
||||
secret2 = assertDoesNotThrow(
|
||||
() -> mapper.readValue(commaSeparatedToList(secretJson3), AppRoleSecret.class),
|
||||
"Deserialization failed"
|
||||
);
|
||||
assertThat(secret.getId(), is(secret2.getId()));
|
||||
assertThat(secret.getMetadata(), is(secret2.getMetadata()));
|
||||
assertThat(secret.getCidrList(), is(secret2.getCidrList()));
|
||||
@ -171,28 +157,19 @@ class AppRoleSecretTest {
|
||||
assertThat(secret2.getLastUpdatedTime(), is(nullValue()));
|
||||
assertThat(secret2.getNumUses(), is(nullValue()));
|
||||
assertThat(secret2.getTtl(), is(nullValue()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail("Deserialization failed");
|
||||
}
|
||||
|
||||
/* Those fields should be deserialized from JSON though */
|
||||
secretJson = "{\"secret_id\":\"abc123\",\"metadata\":{\"number\":1337,\"foo\":\"bar\"}," +
|
||||
String secretJson4 = "{\"secret_id\":\"abc123\",\"metadata\":{\"number\":1337,\"foo\":\"bar\"}," +
|
||||
"\"cidr_list\":[\"203.0.113.0/24\",\"198.51.100.0/24\"],\"secret_id_accessor\":\"TEST_ACCESSOR\"," +
|
||||
"\"creation_time\":\"TEST_CREATION\",\"expiration_time\":\"TEST_EXPIRATION\"," +
|
||||
"\"last_updated_time\":\"TEST_LASTUPDATE\",\"secret_id_num_uses\":678,\"secret_id_ttl\":12345}";
|
||||
try {
|
||||
secret2 = mapper.readValue(secretJson, AppRoleSecret.class);
|
||||
secret2 = assertDoesNotThrow(() -> mapper.readValue(secretJson4, AppRoleSecret.class), "Deserialization failed");
|
||||
assertThat(secret2.getAccessor(), is("TEST_ACCESSOR"));
|
||||
assertThat(secret2.getCreationTime(), is("TEST_CREATION"));
|
||||
assertThat(secret2.getExpirationTime(), is("TEST_EXPIRATION"));
|
||||
assertThat(secret2.getLastUpdatedTime(), is("TEST_LASTUPDATE"));
|
||||
assertThat(secret2.getNumUses(), is(678));
|
||||
assertThat(secret2.getTtl(), is(12345));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail("Deserialization failed");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -21,13 +21,13 @@ import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import de.stklcode.jvault.connector.model.AppRole;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link AppRoleResponse} model.
|
||||
@ -81,12 +81,11 @@ class AppRoleResponseTest {
|
||||
assertThat("Initial data should be empty", res.getRole(), is(nullValue()));
|
||||
|
||||
// Parsing invalid auth data map should fail.
|
||||
try {
|
||||
res.setData(INVALID_DATA);
|
||||
fail("Parsing invalid data succeeded");
|
||||
} catch (Exception e) {
|
||||
assertThat(e, is(instanceOf(InvalidResponseException.class)));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setData(INVALID_DATA),
|
||||
"Parsing invalid data succeeded"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,8 +93,10 @@ class AppRoleResponseTest {
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
try {
|
||||
AppRoleResponse res = new ObjectMapper().readValue(RES_JSON, AppRoleResponse.class);
|
||||
AppRoleResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(RES_JSON, AppRoleResponse.class),
|
||||
"AuthResponse deserialization failed."
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
// Extract role data.
|
||||
AppRole role = res.getRole();
|
||||
@ -113,8 +114,5 @@ class AppRoleResponseTest {
|
||||
assertThat("Incorrect role bind secret ID flag", role.getBindSecretId(), is(ROLE_BIND_SECRET));
|
||||
assertThat("Incorrect bound CIDR list", role.getTokenBoundCidrs(), is(nullValue()));
|
||||
assertThat("Incorrect bound CIDR list string", role.getTokenBoundCidrsString(), is(emptyString()));
|
||||
} catch (IOException e) {
|
||||
fail("AuthResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,13 +22,13 @@ import de.stklcode.jvault.connector.model.AuthBackend;
|
||||
import de.stklcode.jvault.connector.model.response.embedded.AuthMethod;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link AuthMethodsResponse} model.
|
||||
@ -79,12 +79,11 @@ class AuthMethodsResponseTest {
|
||||
assertThat("Initial method map should be empty", res.getSupportedMethods(), is(anEmptyMap()));
|
||||
|
||||
// Parsing invalid data map should fail.
|
||||
try {
|
||||
res.setData(INVALID_DATA);
|
||||
fail("Parsing invalid data succeeded");
|
||||
} catch (Exception e) {
|
||||
assertThat(e, is(instanceOf(InvalidResponseException.class)));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setData(INVALID_DATA),
|
||||
"Parsing invalid data succeeded"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,8 +91,10 @@ class AuthMethodsResponseTest {
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
try {
|
||||
AuthMethodsResponse res = new ObjectMapper().readValue(RES_JSON, AuthMethodsResponse.class);
|
||||
AuthMethodsResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(RES_JSON, AuthMethodsResponse.class),
|
||||
"AuthResponse deserialization failed"
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
// Extract auth data.
|
||||
Map<String, AuthMethod> supported = res.getSupportedMethods();
|
||||
@ -117,9 +118,6 @@ class AuthMethodsResponseTest {
|
||||
assertThat("Unexpected config size for Token", method.getConfig().keySet(), hasSize(2));
|
||||
assertThat("Incorrect lease TTL config", method.getConfig().get("default_lease_ttl"), is(TK_LEASE_TTL.toString()));
|
||||
assertThat("Incorrect max lease TTL config", method.getConfig().get("max_lease_ttl"), is(TK_MAX_LEASE_TTL.toString()));
|
||||
} catch (IOException e) {
|
||||
fail("AuthResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static class Dummy {
|
||||
|
@ -21,13 +21,13 @@ import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import de.stklcode.jvault.connector.model.response.embedded.AuthData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link AuthResponse} model.
|
||||
@ -87,12 +87,11 @@ class AuthResponseTest {
|
||||
assertThat("Initial data should be empty", res.getData(), is(nullValue()));
|
||||
|
||||
// Parsing invalid auth data map should fail.
|
||||
try {
|
||||
res.setAuth(INVALID_AUTH_DATA);
|
||||
fail("Parsing invalid auth data succeeded");
|
||||
} catch (Exception e) {
|
||||
assertThat(e, is(instanceOf(InvalidResponseException.class)));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setAuth(INVALID_AUTH_DATA),
|
||||
"Parsing invalid auth data succeeded"
|
||||
);
|
||||
|
||||
// Data method should be agnostic.
|
||||
res.setData(INVALID_AUTH_DATA);
|
||||
@ -104,8 +103,10 @@ class AuthResponseTest {
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
try {
|
||||
AuthResponse res = new ObjectMapper().readValue(RES_JSON, AuthResponse.class);
|
||||
AuthResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(RES_JSON, AuthResponse.class),
|
||||
"AuthResponse deserialization failed."
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
// Extract auth data.
|
||||
AuthData data = res.getAuth();
|
||||
@ -123,9 +124,5 @@ class AuthResponseTest {
|
||||
assertThat("Incorrect token policies", data.getTokenPolicies(), containsInRelativeOrder(AUTH_POLICY_2, AUTH_POLICY_1));
|
||||
assertThat("Incorrect auth metadata size", data.getMetadata().entrySet(), hasSize(1));
|
||||
assertThat("Incorrect auth metadata", data.getMetadata().get(AUTH_META_KEY), is(AUTH_META_VALUE));
|
||||
|
||||
} catch (IOException e) {
|
||||
fail("AuthResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,12 +19,10 @@ package de.stklcode.jvault.connector.model.response;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link AuthResponse} model.
|
||||
@ -56,13 +54,16 @@ class HealthResponseTest {
|
||||
" \"replication_dr_mode\": \"" + REPL_DR_MODE + "\",\n" +
|
||||
" \"performance_standby\": " + PERF_STANDBY + "\n" +
|
||||
"}";
|
||||
|
||||
/**
|
||||
* Test creation from JSON value as returned by Vault (JSON example copied from Vault documentation).
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
try {
|
||||
HealthResponse res = new ObjectMapper().readValue(RES_JSON, HealthResponse.class);
|
||||
HealthResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(RES_JSON, HealthResponse.class),
|
||||
"Health deserialization failed."
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
assertThat("Incorrect cluster ID", res.getClusterID(), is(CLUSTER_ID));
|
||||
assertThat("Incorrect cluster name", res.getClusterName(), is(CLUSTER_NAME));
|
||||
@ -74,8 +75,5 @@ class HealthResponseTest {
|
||||
assertThat("Incorrect performance standby state", res.isPerformanceStandby(), is(PERF_STANDBY));
|
||||
assertThat("Incorrect replication perf mode", res.getReplicationPerfMode(), is(REPL_PERF_MODE));
|
||||
assertThat("Incorrect replication DR mode", res.getReplicationDrMode(), is(REPL_DR_MODE));
|
||||
} catch (IOException e) {
|
||||
fail("Health deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,16 +17,12 @@
|
||||
package de.stklcode.jvault.connector.model.response;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link MetadataResponse} model.
|
||||
@ -74,8 +70,10 @@ class MetadataResponseTest {
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
try {
|
||||
MetadataResponse res = new ObjectMapper().readValue(META_JSON, MetadataResponse.class);
|
||||
MetadataResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(META_JSON, MetadataResponse.class),
|
||||
"MetadataResponse deserialization failed."
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
assertThat("Parsed metadata is NULL", res.getMetadata(), is(notNullValue()));
|
||||
assertThat("Incorrect created time", res.getMetadata().getCreatedTimeString(), is(V1_TIME));
|
||||
@ -92,9 +90,5 @@ class MetadataResponseTest {
|
||||
assertThat("Incorrect version 2 created time", res.getMetadata().getVersions().get(2).getCreatedTimeString(), is(V2_TIME));
|
||||
assertThat("Parsing version created failed", res.getMetadata().getVersions().get(2).getCreatedTime(), is(notNullValue()));
|
||||
assertThat("Incorrect version 3 destroyed state", res.getMetadata().getVersions().get(3).isDestroyed(), is(false));
|
||||
|
||||
} catch (IOException e) {
|
||||
fail("MetadataResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +19,9 @@ package de.stklcode.jvault.connector.model.response;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link SealResponse} model.
|
||||
@ -72,8 +70,10 @@ class SealResponseTest {
|
||||
@Test
|
||||
void jsonRoundtripSealed() {
|
||||
// First test sealed Vault's response.
|
||||
try {
|
||||
SealResponse res = new ObjectMapper().readValue(RES_SEALED, SealResponse.class);
|
||||
SealResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(RES_SEALED, SealResponse.class),
|
||||
"TokenResponse deserialization failed."
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
assertThat("Incorrect seal type", res.getType(), is(TYPE));
|
||||
assertThat("Incorrect seal status", res.isSealed(), is(true));
|
||||
@ -86,14 +86,13 @@ class SealResponseTest {
|
||||
// And the fields, that should not be filled.
|
||||
assertThat("Cluster name should not be populated", res.getClusterName(), is(nullValue()));
|
||||
assertThat("Cluster ID should not be populated", res.getClusterId(), is(nullValue()));
|
||||
} catch (IOException e) {
|
||||
fail("TokenResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
// Not test unsealed Vault's response.
|
||||
try {
|
||||
SealResponse res = new ObjectMapper().readValue(RES_UNSEALED, SealResponse.class);
|
||||
res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(RES_UNSEALED, SealResponse.class),
|
||||
"TokenResponse deserialization failed."
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
assertThat("Incorrect seal type", res.getType(), is(TYPE));
|
||||
assertThat("Incorrect seal status", res.isSealed(), is(false));
|
||||
@ -105,8 +104,5 @@ class SealResponseTest {
|
||||
assertThat("Incorrect version", res.getVersion(), is(VERSION));
|
||||
assertThat("Incorrect cluster name", res.getClusterName(), is(CLUSTER_NAME));
|
||||
assertThat("Incorrect cluster ID", res.getClusterId(), is(CLUSTER_ID));
|
||||
} catch (IOException e) {
|
||||
fail("TokenResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link SecretListResponse} model.
|
||||
@ -56,14 +56,13 @@ class SecretListResponseTest {
|
||||
assertThat("Keys should be null without initialization", res.getKeys(), is(nullValue()));
|
||||
|
||||
// Provoke internal ClassCastException.
|
||||
try {
|
||||
Map<String, Object> invalidData = new HashMap<>();
|
||||
invalidData.put("keys", "some string");
|
||||
res.setData(invalidData);
|
||||
fail("Setting incorrect class succeeded");
|
||||
} catch (Exception e) {
|
||||
assertThat("Unexpected exception type", e, instanceOf(InvalidResponseException.class));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setData(invalidData),
|
||||
"Setting incorrect class succeeded"
|
||||
);
|
||||
|
||||
// Fill correct data.
|
||||
res.setData(DATA);
|
||||
|
@ -20,14 +20,14 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link SecretResponse} model.
|
||||
@ -137,19 +137,18 @@ class SecretResponseTest {
|
||||
assertThat("Non-Null returned on unknown key", res.get(KEY_UNKNOWN), is(nullValue()));
|
||||
|
||||
// Try explicit JSON conversion.
|
||||
final List list = res.get(KEY_LIST, List.class);
|
||||
final List<?> list = res.get(KEY_LIST, List.class);
|
||||
assertThat("JSON parsing of list failed", list, is(notNullValue()));
|
||||
assertThat("JSON parsing of list returned incorrect size", list.size(), is(2));
|
||||
assertThat("JSON parsing of list returned incorrect elements", (List<Object>)list, contains("first", "second"));
|
||||
assertThat("JSON parsing of list returned incorrect elements", list, contains("first", "second"));
|
||||
assertThat("Non-Null returned on unknown key", res.get(KEY_UNKNOWN, Object.class), is(nullValue()));
|
||||
|
||||
// Requesting invalid class should result in Exception.
|
||||
try {
|
||||
res.get(KEY_LIST, Double.class);
|
||||
fail("JSON parsing to incorrect type succeeded.");
|
||||
} catch (Exception e) {
|
||||
assertThat(e, is(instanceOf(InvalidResponseException.class)));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.get(KEY_LIST, Double.class),
|
||||
"JSON parsing to incorrect type succeeded."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -157,15 +156,17 @@ class SecretResponseTest {
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
try {
|
||||
assertSecretData(new ObjectMapper().readValue(SECRET_JSON, SecretResponse.class));
|
||||
} catch (IOException e) {
|
||||
fail("SecretResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
SecretResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON, SecretResponse.class),
|
||||
"SecretResponse deserialization failed."
|
||||
);
|
||||
assertSecretData(res);
|
||||
|
||||
// KV v2 secret.
|
||||
try {
|
||||
SecretResponse res = new ObjectMapper().readValue(SECRET_JSON_V2, SecretResponse.class);
|
||||
res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON_V2, SecretResponse.class),
|
||||
"SecretResponse deserialization failed."
|
||||
);
|
||||
assertSecretData(res);
|
||||
assertThat("SecretResponse does not contain metadata", res.getMetadata(), is(notNullValue()));
|
||||
assertThat("Incorrect creation date string", res.getMetadata().getCreatedTimeString(), is(SECRET_META_CREATED));
|
||||
@ -174,13 +175,12 @@ class SecretResponseTest {
|
||||
assertThat("Incorrect deletion date", res.getMetadata().getDeletionTime(), is(nullValue()));
|
||||
assertThat("Secret destroyed when not expected", res.getMetadata().isDestroyed(), is(false));
|
||||
assertThat("Incorrect secret version", res.getMetadata().getVersion(), is(1));
|
||||
} catch (IOException e) {
|
||||
fail("SecretResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
|
||||
// Deleted KV v2 secret.
|
||||
try {
|
||||
SecretResponse res = new ObjectMapper().readValue(SECRET_JSON_V2_2, SecretResponse.class);
|
||||
res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON_V2_2, SecretResponse.class),
|
||||
"SecretResponse deserialization failed."
|
||||
);
|
||||
assertSecretData(res);
|
||||
assertThat("SecretResponse does not contain metadata", res.getMetadata(), is(notNullValue()));
|
||||
assertThat("Incorrect creation date string", res.getMetadata().getCreatedTimeString(), is(SECRET_META_CREATED));
|
||||
@ -189,9 +189,6 @@ class SecretResponseTest {
|
||||
assertThat("Incorrect deletion date", res.getMetadata().getDeletionTime(), is(notNullValue()));
|
||||
assertThat("Secret destroyed when not expected", res.getMetadata().isDestroyed(), is(true));
|
||||
assertThat("Incorrect secret version", res.getMetadata().getVersion(), is(2));
|
||||
} catch (IOException e) {
|
||||
fail("SecretResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void assertSecretData(SecretResponse res) {
|
||||
|
@ -19,12 +19,10 @@ package de.stklcode.jvault.connector.model.response;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link SecretVersionResponse} model.
|
||||
@ -51,16 +49,15 @@ class SecretVersionResponseTest {
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
try {
|
||||
SecretVersionResponse res = new ObjectMapper().readValue(META_JSON, SecretVersionResponse.class);
|
||||
SecretVersionResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(META_JSON, SecretVersionResponse.class),
|
||||
"SecretVersionResponse deserialization failed"
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
assertThat("Parsed metadata is NULL", res.getMetadata(), is(notNullValue()));
|
||||
assertThat("Incorrect created time", res.getMetadata().getCreatedTimeString(), is(CREATION_TIME));
|
||||
assertThat("Incorrect deletion time", res.getMetadata().getDeletionTimeString(), is(DELETION_TIME));
|
||||
assertThat("Incorrect destroyed state", res.getMetadata().isDestroyed(), is(false));
|
||||
assertThat("Incorrect version", res.getMetadata().getVersion(), is(VERSION));
|
||||
} catch (IOException e) {
|
||||
fail("SecretVersionResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,14 +21,14 @@ import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import de.stklcode.jvault.connector.model.response.embedded.TokenData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link TokenResponse} model.
|
||||
@ -107,12 +107,11 @@ class TokenResponseTest {
|
||||
assertThat("Initial data should be empty", res.getData(), is(nullValue()));
|
||||
|
||||
// Parsing invalid data map should fail.
|
||||
try {
|
||||
res.setData(INVALID_TOKEN_DATA);
|
||||
fail("Parsing invalid token data succeeded");
|
||||
} catch (Exception e) {
|
||||
assertThat(e, is(instanceOf(InvalidResponseException.class)));
|
||||
}
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setData(INVALID_TOKEN_DATA),
|
||||
"Parsing invalid token data succeeded"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -120,8 +119,10 @@ class TokenResponseTest {
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
try {
|
||||
TokenResponse res = new ObjectMapper().readValue(RES_JSON, TokenResponse.class);
|
||||
TokenResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(RES_JSON, TokenResponse.class),
|
||||
"TokenResponse deserialization failed."
|
||||
);
|
||||
assertThat("Parsed response is NULL", res, is(notNullValue()));
|
||||
assertThat("Incorrect lease duration", res.getLeaseDuration(), is(RES_LEASE_DURATION));
|
||||
assertThat("Incorrect response renewable flag", res.isRenewable(), is(RES_RENEWABLE));
|
||||
@ -150,8 +151,5 @@ class TokenResponseTest {
|
||||
assertThat("Incorrect token renewable flag", data.isRenewable(), is(TOKEN_RENEWABLE));
|
||||
assertThat("Incorrect token TTL", data.getTtl(), is(RES_TTL));
|
||||
assertThat("Incorrect token type", data.getType(), is(TOKEN_TYPE));
|
||||
} catch (IOException e) {
|
||||
fail("TokenResponse deserialization failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user