Skip to content

Commit

Permalink
Merge pull request #6 from bancolombia/feature/test
Browse files Browse the repository at this point in the history
New test for AWS Client
  • Loading branch information
santitigaga authored Aug 4, 2020
2 parents fb2f3f7 + fa919a8 commit f9696d6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 88 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ dependencies {

testCompileOnly 'org.projectlombok:lombok:1.18.12'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
testCompile 'org.mockito:mockito-core:2.28.2'
testCompile group: 'org.mockito', name: 'mockito-core', version: '3.4.6'

testImplementation 'junit:junit:4.12'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.7'

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class AWSSecretManagerConnector extends AbstractConnector {

public AWSSecretManagerConnector(String region) {
setRegion(region);
endpoint = Optional.empty();
}

/**
Expand All @@ -34,7 +35,7 @@ public AWSSecretManagerConnector(String region) {
* @param endpoint : String uri connection
* @param region : Dummy region for Amazon SDK Client
*/
public AWSSecretManagerConnector(String endpoint, String region) {
public AWSSecretManagerConnector(String region, String endpoint) {
this.endpoint = Optional.of(URI.create(endpoint));
this.region = Region.of(region);
}
Expand All @@ -45,10 +46,11 @@ private void setRegion(String region) {

@Override
public String getSecret(String secretName) throws SecretException {
SecretsManagerClientBuilder clientBuilder = SecretsManagerClient.builder().region(region);
endpoint.ifPresent(clientBuilder::endpointOverride);
SecretsManagerClient client = clientBuilder.build();
SecretsManagerClient client = buildClient();
return getSecret(secretName, client);
}

private String getSecret(String secretName, SecretsManagerClient client) throws SecretException {
GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder().secretId(secretName).build();
GetSecretValueResponse getSecretValueResult = null;

Expand All @@ -64,4 +66,10 @@ public String getSecret(String secretName) throws SecretException {
}
}

private SecretsManagerClient buildClient() {
SecretsManagerClientBuilder clientBuilder = SecretsManagerClient.builder().region(region);
endpoint.ifPresent(clientBuilder::endpointOverride);
return clientBuilder.build();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,67 @@
import co.com.bancolombia.commons.secretsmanager.exceptions.SecretException;
import co.com.bancolombia.commons.secretsmanager.manager.GenericManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/** Represents an AWS Connector Test. It lets you to test AWS Secrets Manager Connector Object.
/**
* Represents an AWS Connector Test. It lets you to test AWS Secrets Manager Connector Object.
*
* @author <a href="mailto:andmagom@bancolombia.com.co">Andrés Mauricio Gómez P.</a>
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({SecretsManagerClient.class})
public class AWSSecretManagerConnectorTest {
@Test
public void conversionOk() throws SecretException {
AWSSecretManagerConnector mockConnector = mock(AWSSecretManagerConnector.class);
when(mockConnector.getSecret("SecretDBMock"))
.thenReturn("{\"username\":\"root\",\"password\":\"123456789\","
+ "\"engine\":\"oracle\",\""
+ "host\":\"jdbc:oracle:thin:@oauth-oracle.cufapur4ayuj"
+ ".us-east-1.rds.amazonaws.com:1521:ORCL\","
+ "\"port\":\"3306\",\"dbname\":\"ROOT\"}");
GenericManager manager = new GenericManager(mockConnector);
AWSSecretDBModel model = manager.getSecretModel("SecretDBMock", AWSSecretDBModel.class);
assertNotNull(model);
}

@Test(expected = SecretException.class)
public void conversionFail() throws Exception {
AWSSecretManagerConnector mockConnector = mock(AWSSecretManagerConnector.class);
when(mockConnector.getSecret("SecretDBFailMock")).thenReturn("test");
GenericManager manager = new GenericManager(mockConnector);
AWSSecretDBModel model = manager.getSecretModel("SecretDBFailMock", AWSSecretDBModel.class);
}
@Test
public void shouldConversionOk() throws SecretException {
AWSSecretManagerConnector mockConnector = mock(AWSSecretManagerConnector.class);
when(mockConnector.getSecret("SecretDBMock"))
.thenReturn("{\"username\":\"root\",\"password\":\"123456789\","
+ "\"engine\":\"oracle\",\""
+ "host\":\"jdbc:oracle:thin:@oauth-oracle.cufapur4ayuj"
+ ".us-east-1.rds.amazonaws.com:1521:ORCL\","
+ "\"port\":\"3306\",\"dbname\":\"ROOT\"}");
GenericManager manager = new GenericManager(mockConnector);
AWSSecretDBModel model = manager.getSecretModel("SecretDBMock", AWSSecretDBModel.class);
assertNotNull(model);
}

@Test(expected = SecretException.class)
public void shouldConversionFail() throws Exception {
AWSSecretManagerConnector mockConnector = mock(AWSSecretManagerConnector.class);
when(mockConnector.getSecret("SecretDBFailMock")).thenReturn("test");
GenericManager manager = new GenericManager(mockConnector);
AWSSecretDBModel model = manager.getSecretModel("SecretDBFailMock", AWSSecretDBModel.class);
}

@Test
public void shouldGetStringSecret() throws SecretException {
AWSSecretManagerConnector secret = new AWSSecretManagerConnector("us-east-1");
SecretsManagerClient client = mock(SecretsManagerClient.class);

GetSecretValueResponse response = GetSecretValueResponse.builder().secretString("SecretValue").build();

when(client.getSecretValue(Mockito.any(GetSecretValueRequest.class))).thenReturn(response);

PowerMockito.mockStatic(SecretsManagerClient.class);
SecretsManagerClientBuilder builder = mock(SecretsManagerClientBuilder.class);
when(SecretsManagerClient.builder()).thenReturn(builder);
when(builder.region(Mockito.any())).thenReturn(builder);
when(builder.build()).thenReturn(client);

String secretValue = secret.getSecret("secretName");
assertEquals(secretValue, "SecretValue");
}
}

This file was deleted.

0 comments on commit f9696d6

Please sign in to comment.