-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for the vault module. (#48)
* Added the unit tests for the vault module. * Update VaultConfigTest.java Added the read method failure scenario test. * Update VaultConfigTest.java Removing unnecessary comment. * Added unit tests for the private methods. * Added unit test for the class VaultClient. * Updated the vaultServiceConfigTest to read data from a dummy json, and added the dummy json. --------- Co-authored-by: Parth Jindal <[email protected]> Co-authored-by: Parth Jindal <[email protected]>
- Loading branch information
1 parent
f060a65
commit 270fc85
Showing
8 changed files
with
418 additions
and
3 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
58 changes: 58 additions & 0 deletions
58
internal/vault/src/test/java/com/instana/vault/VaultServiceConfigTest.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,58 @@ | ||
package com.instana.vault; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class VaultServiceConfigTest { | ||
|
||
private VaultServiceConfig config; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
config = new VaultServiceConfig(); | ||
File file = new File(getClass().getClassLoader().getResource("vault_service_config.json").toURI()); | ||
String json = new String(java.nio.file.Files.readAllBytes(file.toPath())); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
config = mapper.readValue(json, VaultServiceConfig.class); | ||
} | ||
|
||
@Test | ||
public void testGetConnectionURL() { | ||
assertEquals("http://localhost:8200", config.getConnectionURL()); | ||
} | ||
|
||
@Test | ||
public void testGetAuthConfig() { | ||
Map<String, Object> expectedAuthConfig = new HashMap<>(); | ||
expectedAuthConfig.put("token", "s.1234567890"); | ||
|
||
assertEquals(expectedAuthConfig, config.getAuthConfig()); | ||
} | ||
|
||
@Test | ||
public void testGetSecretRefreshRate() { | ||
assertEquals(2, config.getSecretRefreshRate()); | ||
} | ||
|
||
@Test | ||
public void testGetKvVersion() { | ||
assertEquals(2, config.getKvVersion()); | ||
} | ||
|
||
@Test | ||
public void testGetPathToPEMFile() { | ||
assertTrue(config.getPathToPEMFile().isPresent()); | ||
assertEquals("/path/to/pemfile.pem", config.getPathToPEMFile().get()); | ||
} | ||
|
||
@Test | ||
public void testIsPathToPEMFilePresent() { | ||
assertTrue(config.isPathToPEMFilePresent()); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
internal/vault/src/test/java/com/instana/vault/VaultUtilTest.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,53 @@ | ||
package com.instana.vault; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import com.bettercloud.vault.VaultException; | ||
import com.instana.vault.services.vault.VaultConfig; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
|
||
|
||
public class VaultUtilTest { | ||
|
||
@Mock | ||
private final VaultService vaultService = mock(VaultService.class); | ||
|
||
@Test | ||
void testIsVaultConfigured_ConfigPresent() throws VaultException { | ||
// Arrange | ||
when(vaultService.isVaultServiceConfigPresent()).thenReturn(true); | ||
|
||
try (MockedStatic<VaultConfig> mockedVaultConfig = mockStatic(VaultConfig.class)) { | ||
mockedVaultConfig.when(() -> VaultConfig.executeStep(vaultService)).thenReturn(vaultService); | ||
|
||
// Act | ||
VaultService result = VaultUtil.isVaultConfigured(vaultService); | ||
|
||
// Assert | ||
assertNotNull(result); | ||
assertSame(vaultService, result); | ||
verify(vaultService).isVaultServiceConfigPresent(); | ||
mockedVaultConfig.verify(() -> VaultConfig.executeStep(vaultService)); | ||
} | ||
} | ||
|
||
@Test | ||
void testIsVaultConfigured_ConfigNotPresent() throws VaultException { | ||
// Arrange | ||
when(vaultService.isVaultServiceConfigPresent()).thenReturn(false); | ||
|
||
try (MockedStatic<VaultConfig> mockedVaultConfig = mockStatic(VaultConfig.class)) { | ||
|
||
// Act | ||
VaultService result = VaultUtil.isVaultConfigured(vaultService); | ||
|
||
// Assert | ||
assertNotNull(result); | ||
assertSame(vaultService, result); | ||
verify(vaultService).isVaultServiceConfigPresent(); | ||
mockedVaultConfig.verify(() -> VaultConfig.executeStep(vaultService), never()); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
internal/vault/src/test/java/com/instana/vault/services/vault/VaultClientTest.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,64 @@ | ||
package com.instana.vault.services.vault; | ||
|
||
import com.bettercloud.vault.SslConfig; | ||
import com.bettercloud.vault.Vault; | ||
import com.bettercloud.vault.VaultConfig; | ||
import com.bettercloud.vault.VaultException; | ||
import com.instana.vault.VaultServiceConfig; | ||
import com.instana.vault.services.vault.VaultClient; | ||
import com.instana.vault.services.vault.auth.AuthenticationFactory; | ||
import com.instana.vault.services.vault.auth.strategy.VaultAuthenticationStrategy; | ||
import com.instana.vault.services.vault.util.Constant; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.io.File; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class VaultClientTest { | ||
|
||
@Mock | ||
private VaultServiceConfig vaultServiceConfig = mock(VaultServiceConfig.class); | ||
|
||
@Mock | ||
private VaultConfig vaultConfig = mock(VaultConfig.class); | ||
|
||
@Mock | ||
private VaultAuthenticationStrategy vaultAuthenticationStrategy = mock(VaultAuthenticationStrategy.class); | ||
|
||
@Test | ||
public void testCreateVaultClient() throws Exception { | ||
// Arrange | ||
String mockURL = "http://mockurl.com"; | ||
when(vaultServiceConfig.getConnectionURL()).thenReturn(mockURL); | ||
when(vaultServiceConfig.getKvVersion()).thenReturn(2); | ||
|
||
VaultConfig mockVaultConfig = mock(VaultConfig.class); | ||
when(mockVaultConfig.address(mockURL)).thenReturn(mockVaultConfig); | ||
when(mockVaultConfig.sslConfig(any(SslConfig.class))).thenReturn(mockVaultConfig); | ||
when(mockVaultConfig.token(anyString())).thenReturn(mockVaultConfig); | ||
when(mockVaultConfig.build()).thenReturn(mockVaultConfig); | ||
|
||
when(vaultServiceConfig.isPathToPEMFilePresent()).thenReturn(true); | ||
when(vaultServiceConfig.getPathToPEMFile()).thenReturn(Optional.of("path/to/pem")); | ||
|
||
try (MockedStatic<AuthenticationFactory> mockedFactory = mockStatic(AuthenticationFactory.class)) { | ||
mockedFactory.when(() -> AuthenticationFactory.getVaultAuthStrategyFromConfig(vaultServiceConfig)) | ||
.thenReturn(vaultAuthenticationStrategy); | ||
when(vaultAuthenticationStrategy.token()).thenReturn(Optional.of("mockToken")); | ||
|
||
// Act | ||
Vault result = VaultClient.createVaultClient(vaultServiceConfig); | ||
|
||
// Assert | ||
assertNotNull(result); | ||
} | ||
} | ||
} |
162 changes: 162 additions & 0 deletions
162
internal/vault/src/test/java/com/instana/vault/services/vault/VaultConfigTest.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,162 @@ | ||
package com.instana.vault.services.vault; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.bettercloud.vault.Vault; | ||
import com.bettercloud.vault.VaultException; | ||
import com.bettercloud.vault.api.Logical; | ||
import com.bettercloud.vault.response.LogicalResponse; | ||
import com.instana.vault.VaultService; | ||
import com.instana.vault.VaultServiceConfig; | ||
import com.instana.vault.services.vault.util.Constant; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.logging.Logger; | ||
|
||
public class VaultConfigTest { | ||
|
||
@Mock | ||
private VaultServiceConfig vaultServiceConfig = mock(VaultServiceConfig.class); | ||
|
||
@Mock | ||
private VaultService vaultService = mock(VaultService.class); | ||
|
||
@Mock | ||
private Vault vault = mock(Vault.class); | ||
|
||
@Mock | ||
private Logical logical = mock(Logical.class); | ||
|
||
@Mock | ||
private LogicalResponse logicalResponse = mock(LogicalResponse.class); | ||
|
||
@Mock | ||
private Logger logger; | ||
|
||
@BeforeEach | ||
public void setup() throws Exception { | ||
Field vaultClientField = VaultConfig.class.getDeclaredField("vaultClient"); | ||
vaultClientField.setAccessible(true); | ||
vaultClientField.set(null, vault); | ||
} | ||
|
||
@Test | ||
void testExecuteStep() throws VaultException { | ||
// Arrange | ||
when(vaultService.getVaultServiceConfig()).thenReturn(vaultServiceConfig); | ||
try (MockedStatic<VaultClient> mockedVaultClient = mockStatic(VaultClient.class)) { | ||
mockedVaultClient.when(() -> VaultClient.createVaultClient(vaultServiceConfig)).thenReturn(vault); | ||
|
||
// Act | ||
VaultService result = VaultConfig.executeStep(vaultService); | ||
|
||
// Assert | ||
assertNotNull(result); | ||
verify(vaultService).getVaultServiceConfig(); | ||
mockedVaultClient.verify(() -> VaultClient.createVaultClient(vaultServiceConfig)); | ||
} | ||
} | ||
|
||
@Test | ||
void testFetchSecret() throws Exception { | ||
// Arrange | ||
Map<String, Object> vaultSecrets = new HashMap<>(); | ||
vaultSecrets.put(Constant.VAULT_SECRET_KEY, "someKey"); | ||
vaultSecrets.put(Constant.VAULT_SECRET_PATH, "some/path"); | ||
String secret = "secret"; | ||
|
||
when(vault.logical()).thenReturn(logical); | ||
when(logical.read("some/path")).thenReturn(logicalResponse); | ||
Map<String, String> responseData = new HashMap<>(); | ||
responseData.put("someKey", secret); | ||
when(logicalResponse.getData()).thenReturn(responseData); | ||
|
||
Method fetchSecretMethod = VaultConfig.class.getDeclaredMethod("fetchSecret", Map.class, VaultServiceConfig.class); | ||
fetchSecretMethod.setAccessible(true); | ||
|
||
// Act | ||
String result = (String) fetchSecretMethod.invoke(null, vaultSecrets, vaultServiceConfig); | ||
|
||
// Assert | ||
assertNotNull(result); | ||
assertEquals(Base64.getEncoder().encodeToString(secret.getBytes()), result); | ||
} | ||
|
||
@Test | ||
void testUpdateVaultSecretConfig() throws Exception { | ||
// Arrange | ||
Map<String, Object> vaultSecrets = new HashMap<>(); | ||
vaultSecrets.put(Constant.VAULT_SECRET_KEY, "someKey"); | ||
vaultSecrets.put(Constant.VAULT_SECRET_PATH, "some/path"); | ||
|
||
ConcurrentHashMap<String, Object> instance = new ConcurrentHashMap<>(); | ||
instance.put("someProperty", vaultSecrets); | ||
instance.put("db.address", "testAddress"); | ||
|
||
List<ConcurrentHashMap<String, Object>> instances = Arrays.asList(instance); | ||
|
||
when(vaultService.getInstances()).thenReturn(instances); | ||
when(vaultService.getVaultServiceConfig()).thenReturn(vaultServiceConfig); | ||
|
||
when(vault.logical()).thenReturn(logical); | ||
when(logical.read("some/path")).thenReturn(logicalResponse); | ||
Map<String, String> responseData = new HashMap<>(); | ||
responseData.put("someKey", "secret"); | ||
when(logicalResponse.getData()).thenReturn(responseData); | ||
|
||
Method updateVaultSecretConfigMethod = VaultConfig.class.getDeclaredMethod("updateVaultSecretConfig", VaultService.class); | ||
updateVaultSecretConfigMethod.setAccessible(true); | ||
|
||
// Act | ||
VaultService result = (VaultService) updateVaultSecretConfigMethod.invoke(null, vaultService); | ||
|
||
// Assert | ||
assertNotNull(result); | ||
assertEquals(Base64.getEncoder().encodeToString("secret".getBytes()), instance.get("someProperty")); | ||
} | ||
|
||
@Test | ||
void testRead_Success() throws VaultException { | ||
// Arrange | ||
String path = "some/path"; | ||
String keyName = "someKey"; | ||
String secret = "secret"; | ||
|
||
Map<String, String> data = new HashMap<>(); | ||
data.put(keyName, secret); | ||
|
||
when(vault.logical()).thenReturn(logical); | ||
when(logical.read(path)).thenReturn(logicalResponse); | ||
when(logicalResponse.getData()).thenReturn(data); | ||
|
||
// Act | ||
String result = VaultConfig.read(path, keyName); | ||
|
||
// Assert | ||
assertNotNull(result); | ||
assertEquals(secret, result); | ||
} | ||
|
||
@Test | ||
public void testRead_Failure() throws VaultException { | ||
String path = "invalid/path"; | ||
String keyName = "invalidKey"; | ||
|
||
when(vault.logical()).thenReturn(logical); | ||
when(logical.read(path)).thenThrow(new VaultException("Mocked VaultException")); | ||
|
||
String result = VaultConfig.read(path, keyName); | ||
|
||
assertNull(result); | ||
} | ||
} | ||
|
||
|
32 changes: 32 additions & 0 deletions
32
.../vault/src/test/java/com/instana/vault/services/vault/auth/AuthenticationFactoryTest.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,32 @@ | ||
package com.instana.vault.services.vault.auth; | ||
|
||
import com.instana.vault.VaultServiceConfig; | ||
import com.instana.vault.VaultServiceConfigTest; | ||
import com.instana.vault.services.vault.auth.strategy.TokenAuthenticationStrategy; | ||
import com.instana.vault.services.vault.auth.strategy.VaultAuthenticationStrategy; | ||
import com.instana.vault.services.vault.util.Constant; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class AuthenticationFactoryTest { | ||
|
||
@Mock | ||
private VaultServiceConfig vaultServiceConfig = mock(VaultServiceConfig.class); | ||
|
||
@Test | ||
public void testGetVaultAuthStrategyFromConfig() { | ||
Map<String, Object> authConfig = new HashMap<>(); | ||
authConfig.put(Constant.TOKEN, "my-token"); | ||
when(vaultServiceConfig.getAuthConfig()).thenReturn(authConfig); | ||
|
||
VaultAuthenticationStrategy strategy = AuthenticationFactory.getVaultAuthStrategyFromConfig(vaultServiceConfig); | ||
assertInstanceOf(TokenAuthenticationStrategy.class, strategy); | ||
} | ||
} |
Oops, something went wrong.