-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ca2dcc
commit cfe6202
Showing
6 changed files
with
205 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...ava/com/consid/bpm/camunda/secrets/provider/kv/GenericKeyValueVaultSecretServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.consid.bpm.camunda.secrets.provider.kv; | ||
|
||
import com.consid.bpm.camunda.secrets.provider.VaultSecretService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.vault.core.VaultTemplate; | ||
import org.springframework.vault.support.VaultResponse; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class GenericKeyValueVaultSecretServiceTest { | ||
|
||
@Mock | ||
private VaultTemplate vaultTemplate; | ||
private VaultSecretService secretService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
secretService = new GenericKeyValueVaultSecretService(vaultTemplate, "path", "backend"); | ||
} | ||
|
||
@Test | ||
public void test_secrets_are_loaded_as_expected() { | ||
// given | ||
Map<String, Object> expectedSecrets = Map.of("apiKey", "0000-0000-0000-0000"); | ||
VaultResponse response = new VaultResponse(); | ||
response.setData(expectedSecrets); | ||
Mockito.when(vaultTemplate.read(anyString())).thenReturn(response); | ||
|
||
// when | ||
Map<String, Object> secrets = secretService.getSecretsFromVault(); | ||
|
||
// then | ||
assertThat(secrets).isEqualTo(expectedSecrets); | ||
verify(vaultTemplate).read(eq("backend/path")); | ||
} | ||
|
||
@Test | ||
public void test_existing_secret_by_key_is_returned_as_expected() { | ||
// given | ||
Map<String, Object> expectedSecrets = Map.of("apiKey", "0000-0000-0000-0000"); | ||
VaultResponse response = new VaultResponse(); | ||
response.setData(expectedSecrets); | ||
Mockito.when(vaultTemplate.read(anyString())).thenReturn(response); | ||
|
||
// when | ||
Optional<Object> secret = secretService.getSecretByKey("apiKey"); | ||
|
||
// then | ||
assertThat(secret.isPresent()).isTrue(); | ||
assertThat(secret.get()).isEqualTo("0000-0000-0000-0000"); | ||
} | ||
|
||
@Test | ||
public void test_non_existing_secret_by_key_is_returned_empty_as_expected() { | ||
// given | ||
Map<String, Object> expectedSecrets = Map.of("apiKey", "0000-0000-0000-0000"); | ||
VaultResponse response = new VaultResponse(); | ||
response.setData(expectedSecrets); | ||
Mockito.when(vaultTemplate.read(anyString())).thenReturn(response); | ||
|
||
// when | ||
Optional<Object> secret = secretService.getSecretByKey("unknown"); | ||
|
||
// then | ||
assertThat(secret.isPresent()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void test_empty_secrets_return_empty_map_as_expected() { | ||
// given | ||
VaultResponse response = new VaultResponse(); | ||
Mockito.when(vaultTemplate.read(anyString())).thenReturn(response); | ||
|
||
// when | ||
Map<String, Object> secrets = secretService.getSecretsFromVault(); | ||
|
||
// then | ||
assertThat(secrets).isEmpty(); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...a/com/consid/bpm/camunda/secrets/provider/kv/VersionedKeyValueVaultSecretServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.consid.bpm.camunda.secrets.provider.kv; | ||
|
||
import com.consid.bpm.camunda.secrets.provider.VaultSecretService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.vault.core.VaultVersionedKeyValueTemplate; | ||
import org.springframework.vault.support.Versioned; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class VersionedKeyValueVaultSecretServiceTest { | ||
|
||
@Mock | ||
private VaultVersionedKeyValueTemplate vaultTemplate; | ||
private VaultSecretService secretService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
secretService = new VersionedKeyValueVaultSecretService(vaultTemplate, "path", 1); | ||
} | ||
|
||
@Test | ||
public void test_secrets_are_loaded_as_expected() { | ||
// given | ||
Map<String, Object> expectedSecrets = Map.of("apiKey", "0000-0000-0000-0000"); | ||
Versioned.Version expectedVersion = Versioned.Version.from(1); | ||
Versioned<Map<String, Object>> versionedSecrets = Versioned.create(expectedSecrets, expectedVersion); | ||
Mockito.when(vaultTemplate.get(anyString(), any(Versioned.Version.class))).thenReturn(versionedSecrets); | ||
|
||
// when | ||
Map<String, Object> secrets = secretService.getSecretsFromVault(); | ||
|
||
// then | ||
assertThat(secrets).isEqualTo(expectedSecrets); | ||
verify(vaultTemplate).get(eq("path"), eq(expectedVersion)); | ||
} | ||
|
||
@Test | ||
public void test_existing_secret_by_key_is_returned_as_expected() { | ||
// given | ||
Map<String, Object> expectedSecrets = Map.of("apiKey", "0000-0000-0000-0000"); | ||
Versioned.Version expectedVersion = Versioned.Version.from(1); | ||
Versioned<Map<String, Object>> versionedSecrets = Versioned.create(expectedSecrets, expectedVersion); | ||
Mockito.when(vaultTemplate.get(anyString(), any(Versioned.Version.class))).thenReturn(versionedSecrets); | ||
|
||
// when | ||
Optional<Object> secret = secretService.getSecretByKey("apiKey"); | ||
|
||
// then | ||
assertThat(secret.isPresent()).isTrue(); | ||
assertThat(secret.get()).isEqualTo("0000-0000-0000-0000"); | ||
} | ||
|
||
@Test | ||
public void test_non_existing_secret_by_key_is_returned_empty_as_expected() { | ||
// given | ||
Versioned<Map<String, Object>> versionedSecrets = Versioned.create(Map.of()); | ||
Mockito.when(vaultTemplate.get(anyString(), any(Versioned.Version.class))).thenReturn(versionedSecrets); | ||
|
||
// when | ||
Optional<Object> secret = secretService.getSecretByKey("unknown"); | ||
|
||
// then | ||
assertThat(secret.isPresent()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void test_empty_secrets_return_empty_map_as_expected() { | ||
// given | ||
Versioned.Version expectedVersion = Versioned.Version.from(1); | ||
Versioned<Map<String, Object>> versionedSecrets = Versioned.create(null, expectedVersion); | ||
Mockito.when(vaultTemplate.get(anyString(), any(Versioned.Version.class))).thenReturn(versionedSecrets); | ||
|
||
// when | ||
Map<String, Object> secrets = secretService.getSecretsFromVault(); | ||
|
||
// then | ||
assertThat(secrets).isEmpty(); | ||
} | ||
|
||
} |