model: eliminate double-mapping of generic data in response classes
Explicitly declare mapping of the "data" field in response classes. Therefore, the JSON setter setData() is no longer used. SecretResponse is split into subclasses for secret with and without metadata.
This commit is contained in:
@ -17,13 +17,11 @@
|
||||
package de.stklcode.jvault.connector.model.response;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import de.stklcode.jvault.connector.model.AppRole;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@ -63,8 +61,6 @@ class AppRoleResponseTest {
|
||||
" \"lease_id\": \"\"\n" +
|
||||
"}";
|
||||
|
||||
private static final Map<String, Object> INVALID_DATA = Map.of("token_policies", "fancy-policy");
|
||||
|
||||
/**
|
||||
* Test getter, setter and get-methods for response data.
|
||||
*/
|
||||
@ -73,13 +69,6 @@ class AppRoleResponseTest {
|
||||
// Create empty Object.
|
||||
AppRoleResponse res = new AppRoleResponse();
|
||||
assertNull(res.getRole(), "Initial data should be empty");
|
||||
|
||||
// Parsing invalid auth data map should fail.
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setData(INVALID_DATA),
|
||||
"Parsing invalid data succeeded"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,8 +63,6 @@ class AuthMethodsResponseTest {
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
private static final Map<String, Object> INVALID_DATA = Map.of("dummy/", new Dummy());
|
||||
|
||||
/**
|
||||
* Test getter, setter and get-methods for response data.
|
||||
*/
|
||||
@ -73,13 +71,6 @@ class AuthMethodsResponseTest {
|
||||
// Create empty Object.
|
||||
AuthMethodsResponse res = new AuthMethodsResponse();
|
||||
assertEquals(Collections.emptyMap(), res.getSupportedMethods(), "Initial method map should be empty");
|
||||
|
||||
// Parsing invalid data map should fail.
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setData(INVALID_DATA),
|
||||
"Parsing invalid data succeeded"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,7 +17,6 @@
|
||||
package de.stklcode.jvault.connector.model.response;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import de.stklcode.jvault.connector.model.response.embedded.AuthData;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -69,8 +68,6 @@ class AuthResponseTest {
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
private static final Map<String, Object> INVALID_AUTH_DATA = Map.of("policies", "fancy-policy");
|
||||
|
||||
/**
|
||||
* Test getter, setter and get-methods for response data.
|
||||
*/
|
||||
@ -78,18 +75,8 @@ class AuthResponseTest {
|
||||
void getDataRoundtrip() {
|
||||
// Create empty Object.
|
||||
AuthResponse res = new AuthResponse();
|
||||
assertNull(res.getData(), "Initial data should be empty");
|
||||
|
||||
// Parsing invalid auth data map should fail.
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setAuth(INVALID_AUTH_DATA),
|
||||
"Parsing invalid auth data succeeded"
|
||||
);
|
||||
|
||||
// Data method should be agnostic.
|
||||
res.setData(INVALID_AUTH_DATA);
|
||||
assertEquals(INVALID_AUTH_DATA, res.getData(), "Data not passed through");
|
||||
// TODO
|
||||
// assertNull(res.getData(), "Initial data should be empty");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,14 +16,12 @@
|
||||
|
||||
package de.stklcode.jvault.connector.model.response;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link CredentialsResponse} model.
|
||||
@ -34,10 +32,17 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
class CredentialsResponseTest {
|
||||
private static final String VAL_USER = "testUserName";
|
||||
private static final String VAL_PASS = "5up3r5ecr3tP455";
|
||||
private static final Map<String, Object> DATA = Map.of(
|
||||
"username", VAL_USER,
|
||||
"password", VAL_PASS
|
||||
);
|
||||
private static final String JSON = "{\n" +
|
||||
" \"request_id\": \"68315073-6658-e3ff-2da7-67939fb91bbd\",\n" +
|
||||
" \"lease_id\": \"\",\n" +
|
||||
" \"lease_duration\": 2764800,\n" +
|
||||
" \"renewable\": false,\n" +
|
||||
" \"data\": {\n" +
|
||||
" \"username\": \"" + VAL_USER + "\",\n" +
|
||||
" \"password\": \"" + VAL_PASS + "\"\n" +
|
||||
" },\n" +
|
||||
" \"warnings\": null\n" +
|
||||
"}";
|
||||
|
||||
/**
|
||||
* Test getter, setter and get-methods for response data.
|
||||
@ -51,8 +56,10 @@ class CredentialsResponseTest {
|
||||
assertNull(res.getUsername(), "Username not present in data map should not return anything");
|
||||
assertNull(res.getPassword(), "Password not present in data map should not return anything");
|
||||
|
||||
// Fill data map.
|
||||
res.setData(DATA);
|
||||
res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(JSON, CredentialsResponse.class),
|
||||
"Deserialization of CredentialsResponse failed"
|
||||
);
|
||||
assertEquals(VAL_USER, res.getUsername(), "Incorrect username");
|
||||
assertEquals(VAL_PASS, res.getPassword(), "Incorrect password");
|
||||
}
|
||||
|
@ -16,16 +16,14 @@
|
||||
|
||||
package de.stklcode.jvault.connector.model.response;
|
||||
|
||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* JUnit Test for {@link SecretListResponse} model.
|
||||
@ -36,33 +34,30 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
class SecretListResponseTest {
|
||||
private static final String KEY1 = "key1";
|
||||
private static final String KEY2 = "key-2";
|
||||
private static final List<String> KEYS = Arrays.asList(KEY1, KEY2);
|
||||
private static final Map<String, Object> DATA = Map.of("keys", KEYS);
|
||||
private static final String JSON = "{\n" +
|
||||
" \"auth\": null,\n" +
|
||||
" \"data\": {\n" +
|
||||
" \"keys\": [" +
|
||||
" \"" + KEY1 + "\",\n" +
|
||||
" \"" + KEY2 + "\"\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" \"lease_duration\": 2764800,\n" +
|
||||
" \"lease_id\": \"\",\n" +
|
||||
" \"renewable\": false\n" +
|
||||
"}";
|
||||
|
||||
/**
|
||||
* Test getter, setter and get-methods for response data.
|
||||
*
|
||||
* @throws InvalidResponseException Should not occur
|
||||
* Test JSON deserialization and key getter.
|
||||
*/
|
||||
@Test
|
||||
void getKeysTest() throws InvalidResponseException {
|
||||
// Create empty Object.
|
||||
SecretListResponse res = new SecretListResponse();
|
||||
assertNull(res.getKeys(), "Keys should be null without initialization");
|
||||
|
||||
// Provoke internal ClassCastException.
|
||||
Map<String, Object> invalidData = Map.of("keys", "some string");
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setData(invalidData),
|
||||
"Setting incorrect class succeeded"
|
||||
void getKeysTest() {
|
||||
SecretListResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(JSON, SecretListResponse.class),
|
||||
"SecretListResponse deserialization failed"
|
||||
);
|
||||
|
||||
// Fill correct data.
|
||||
res.setData(DATA);
|
||||
assertNotNull(res.getKeys(), "Keys should be filled here");
|
||||
assertEquals(2, res.getKeys().size(), "Unexpected number of keys");
|
||||
assertTrue(res.getKeys().containsAll(Set.of(KEY1, KEY2)), "Unexpected keys");
|
||||
assertEquals(List.of(KEY1, KEY2), res.getKeys(), "Unexpected secret keys");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -17,13 +17,10 @@
|
||||
package de.stklcode.jvault.connector.model.response;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@ -34,20 +31,6 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
* @since 0.6.2
|
||||
*/
|
||||
class SecretResponseTest {
|
||||
private static final String KEY_UNKNOWN = "unknown";
|
||||
private static final String KEY_STRING = "test1";
|
||||
private static final String VAL_STRING = "testvalue";
|
||||
private static final String KEY_INTEGER = "test2";
|
||||
private static final Integer VAL_INTEGER = 42;
|
||||
private static final String KEY_LIST = "list";
|
||||
private static final String VAL_LIST = "[\"first\",\"second\"]";
|
||||
|
||||
private static final Map<String, Object> DATA = Map.of(
|
||||
KEY_STRING, VAL_STRING,
|
||||
KEY_INTEGER, VAL_INTEGER,
|
||||
KEY_LIST, VAL_LIST
|
||||
);
|
||||
|
||||
private static final String SECRET_REQUEST_ID = "68315073-6658-e3ff-2da7-67939fb91bbd";
|
||||
private static final String SECRET_LEASE_ID = "";
|
||||
private static final Integer SECRET_LEASE_DURATION = 2764800;
|
||||
@ -109,58 +92,20 @@ class SecretResponseTest {
|
||||
" \"warnings\": " + SECRET_WARNINGS + "\n" +
|
||||
"}";
|
||||
|
||||
/**
|
||||
* Test getter, setter and get-methods for response data.
|
||||
*
|
||||
* @throws InvalidResponseException Should not occur
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void getDataRoundtrip() throws InvalidResponseException {
|
||||
// Create empty Object.
|
||||
SecretResponse res = new SecretResponse();
|
||||
assertNotNull(res.getData(), "Initial data should be Map");
|
||||
assertTrue(res.getData().isEmpty(), "Initial data should be empty");
|
||||
assertNull(res.get(KEY_STRING), "Getter should return NULL on empty data map");
|
||||
|
||||
// Fill data map.
|
||||
res.setData(DATA);
|
||||
assertEquals(DATA, res.getData(), "Data setter/getter not transparent");
|
||||
assertEquals(DATA.size(), res.getData().keySet().size(), "Data size modified");
|
||||
assertTrue(res.getData().keySet().containsAll(Set.of(KEY_STRING, KEY_INTEGER, KEY_LIST)), "Data keys not passed correctly");
|
||||
assertEquals(VAL_STRING, res.get(KEY_STRING), "Data values not passed correctly");
|
||||
assertEquals(VAL_INTEGER, res.get(KEY_INTEGER), "Data values not passed correctly");
|
||||
assertNull(res.get(KEY_UNKNOWN), "Non-Null returned on unknown key");
|
||||
|
||||
// Try explicit JSON conversion.
|
||||
final List<?> list = res.get(KEY_LIST, List.class);
|
||||
assertNotNull(list, "JSON parsing of list failed");
|
||||
assertEquals(2, list.size(), "JSON parsing of list returned incorrect size");
|
||||
assertTrue(list.containsAll(List.of("first", "second")), "JSON parsing of list returned incorrect elements");
|
||||
assertNull(res.get(KEY_UNKNOWN, Object.class), "Non-Null returned on unknown key");
|
||||
|
||||
// Requesting invalid class should result in Exception.
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.get(KEY_LIST, Double.class),
|
||||
"JSON parsing to incorrect type succeeded"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creation from JSON value as returned by Vault (JSON example copied from Vault documentation).
|
||||
*/
|
||||
@Test
|
||||
void jsonRoundtrip() {
|
||||
SecretResponse res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON, SecretResponse.class),
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON, PlainSecretResponse.class),
|
||||
"SecretResponse deserialization failed"
|
||||
);
|
||||
assertSecretData(res);
|
||||
|
||||
// KV v2 secret.
|
||||
res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON_V2, SecretResponse.class),
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON_V2, MetaSecretResponse.class),
|
||||
"SecretResponse deserialization failed"
|
||||
);
|
||||
assertSecretData(res);
|
||||
@ -174,7 +119,7 @@ class SecretResponseTest {
|
||||
|
||||
// Deleted KV v2 secret.
|
||||
res = assertDoesNotThrow(
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON_V2_2, SecretResponse.class),
|
||||
() -> new ObjectMapper().readValue(SECRET_JSON_V2_2, MetaSecretResponse.class),
|
||||
"SecretResponse deserialization failed"
|
||||
);
|
||||
assertSecretData(res);
|
||||
@ -190,6 +135,8 @@ class SecretResponseTest {
|
||||
@Test
|
||||
void testEqualsHashcode() {
|
||||
EqualsVerifier.simple().forClass(SecretResponse.class).verify();
|
||||
EqualsVerifier.simple().forClass(PlainSecretResponse.class).verify();
|
||||
EqualsVerifier.simple().forClass(MetaSecretResponse.class).verify();
|
||||
}
|
||||
|
||||
private void assertSecretData(SecretResponse res) {
|
||||
|
@ -17,7 +17,6 @@
|
||||
package de.stklcode.jvault.connector.model.response;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.stklcode.jvault.connector.exception.InvalidResponseException;
|
||||
import de.stklcode.jvault.connector.model.response.embedded.TokenData;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -89,8 +88,6 @@ class TokenResponseTest {
|
||||
" \"auth\": null\n" +
|
||||
"}";
|
||||
|
||||
private static final Map<String, Object> INVALID_TOKEN_DATA = Map.of("num_uses", "fourtytwo");
|
||||
|
||||
/**
|
||||
* Test getter, setter and get-methods for response data.
|
||||
*/
|
||||
@ -99,13 +96,6 @@ class TokenResponseTest {
|
||||
// Create empty Object.
|
||||
TokenResponse res = new TokenResponse();
|
||||
assertNull(res.getData(), "Initial data should be empty");
|
||||
|
||||
// Parsing invalid data map should fail.
|
||||
assertThrows(
|
||||
InvalidResponseException.class,
|
||||
() -> res.setData(INVALID_TOKEN_DATA),
|
||||
"Parsing invalid token data succeeded"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user