From 746c69fe77bacc293c03af95a2749969fa66c7c2 Mon Sep 17 00:00:00 2001 From: RVelp1 <80131455+RVelp1@users.noreply.github.com> Date: Fri, 26 Mar 2021 10:43:50 -0700 Subject: [PATCH] feat: added Junit tests and improved coverage (#100) --- gradle.properties | 2 +- .../client/CerberusClientFactoryTest.java | 28 +++++ .../DefaultCerberusClientFactoryTest.java | 24 ++++ ...tCerberusCredentialsProviderChainTest.java | 18 +++ .../aws/BaseAwsCredentialsProviderTest.java | 113 ++++++++++++++++++ ...SCredentialsProviderChainDebuggerTest.java | 33 +++++ .../StsCerberusCredentialsProviderTest.java | 2 +- 7 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/nike/cerberus/client/auth/aws/BaseAwsCredentialsProviderTest.java create mode 100644 src/test/java/com/nike/cerberus/client/auth/aws/DefaultAWSCredentialsProviderChainDebuggerTest.java diff --git a/gradle.properties b/gradle.properties index b10b57a..6c7f6d9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,6 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -version=7.4.5 +version=7.4.6 groupId=com.nike artifactId=cerberus-client diff --git a/src/test/java/com/nike/cerberus/client/CerberusClientFactoryTest.java b/src/test/java/com/nike/cerberus/client/CerberusClientFactoryTest.java index 021cd0d..6cdf5f4 100644 --- a/src/test/java/com/nike/cerberus/client/CerberusClientFactoryTest.java +++ b/src/test/java/com/nike/cerberus/client/CerberusClientFactoryTest.java @@ -19,6 +19,7 @@ import com.nike.cerberus.client.auth.TokenCerberusCredentials; import com.nike.cerberus.client.auth.CerberusCredentials; import com.nike.cerberus.client.auth.CerberusCredentialsProvider; +import org.apache.commons.logging.Log; import org.junit.Test; import java.util.HashMap; @@ -52,6 +53,15 @@ public void test_get_client_uses_url_and_creds_provider() { assertThat(client.getCredentialsProvider().getCredentials().getToken()).isEqualTo(TOKEN); } + @Test + public void test_get_client_uses_url_and_creds_provider_max_requests_per_host() { + final CerberusClient client = CerberusClientFactory.getClient(url, credentialsProvider, 3); + assertThat(client).isNotNull(); + assertThat(client.getCerberusUrl().url().toString()).isEqualTo(url); + assertThat(client.getCredentialsProvider()).isNotNull(); + assertThat(client.getCredentialsProvider().getCredentials().getToken()).isEqualTo(TOKEN); + } + @Test public void test_get_client_uses_default_headers() { final String headerKey = "HeaderKey"; @@ -67,6 +77,15 @@ public void test_get_client_uses_default_headers() { assertThat(client.getDefaultHeaders().get(headerKey)).isEqualTo(headerValue); } + @Test(expected = IllegalArgumentException.class) + public void test_get_client_uses_default_as_null_throws_illegalArgument_exception() { + final String headerKey = "HeaderKey"; + final String headerValue = "header value"; + final Map defaultHeaders = new HashMap<>(); + defaultHeaders.put(headerKey, headerValue); + CerberusClientFactory.getClient(url, credentialsProvider, null); + } + @Test public void test_get_admin_client_uses_all_parameters() { final String headerKey = "HeaderKey"; @@ -82,4 +101,13 @@ public void test_get_admin_client_uses_all_parameters() { assertThat(client.getDefaultHeaders().get(headerKey)).isEqualTo(headerValue); } + @Test(expected = IllegalArgumentException.class) + public void test_get_admin_client_null_default_headers_throws_illegal_argument_exception() { + final String headerKey = "HeaderKey"; + final String headerValue = "header value"; + final Map defaultHeaders = new HashMap<>(); + defaultHeaders.put(headerKey, headerValue); + CerberusClientFactory.getClient(url, credentialsProvider, 100, null); + } + } \ No newline at end of file diff --git a/src/test/java/com/nike/cerberus/client/DefaultCerberusClientFactoryTest.java b/src/test/java/com/nike/cerberus/client/DefaultCerberusClientFactoryTest.java index 65b6c57..d8eba2b 100644 --- a/src/test/java/com/nike/cerberus/client/DefaultCerberusClientFactoryTest.java +++ b/src/test/java/com/nike/cerberus/client/DefaultCerberusClientFactoryTest.java @@ -18,11 +18,19 @@ import org.junit.Test; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + import static org.junit.Assert.assertEquals; /** * Tests the DefaultCerberusClientFactory class */ + public class DefaultCerberusClientFactoryTest { @Test @@ -35,4 +43,20 @@ public void test_that_getClient_adds_client_version_as_a_default_header() { result.getDefaultHeaders().get(ClientVersion.CERBERUS_CLIENT_HEADER)); } + @Test + public void test_that_getClient_adds_client_version_as_a_default_header_and_returns_CerberusClientFactory() throws NoSuchAlgorithmException, KeyStoreException { + String region = "us-west-2"; + String url = "url"; + SSLSocketFactory sslSocketFactory =(SSLSocketFactory) SSLSocketFactory.getDefault(); + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + trustManagerFactory.init((KeyStore) null); + + CerberusClient result = DefaultCerberusClientFactory.getClient(url, region, sslSocketFactory, (X509TrustManager)trustManagerFactory.getTrustManagers()[0]); + + assertEquals( + ClientVersion.getClientHeaderValue(), + result.getDefaultHeaders().get(ClientVersion.CERBERUS_CLIENT_HEADER)); + } + + } \ No newline at end of file diff --git a/src/test/java/com/nike/cerberus/client/auth/DefaultCerberusCredentialsProviderChainTest.java b/src/test/java/com/nike/cerberus/client/auth/DefaultCerberusCredentialsProviderChainTest.java index dbac529..3b7c626 100644 --- a/src/test/java/com/nike/cerberus/client/auth/DefaultCerberusCredentialsProviderChainTest.java +++ b/src/test/java/com/nike/cerberus/client/auth/DefaultCerberusCredentialsProviderChainTest.java @@ -16,6 +16,8 @@ package com.nike.cerberus.client.auth; +import okhttp3.OkHttpClient; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,6 +45,10 @@ public class DefaultCerberusCredentialsProviderChainTest { private static final String url = "url"; + private static final String xCerberusClientOverride = "xCerberusClientOverride"; + + private static final OkHttpClient httpClient = new OkHttpClient(); + private DefaultCerberusCredentialsProviderChain credentialsProviderChain; @Before @@ -73,4 +79,16 @@ public void sys_value_set_if_env_is_not_set() { assertThat(credentials).isNotNull(); assertThat(credentials.getToken()).isEqualTo(SYS_VALUE); } + + @Test + public void testCreatDefaultCerberusCredentialsProviderChainReturnsValidObject() { + DefaultCerberusCredentialsProviderChain credentialsProviderChain = new DefaultCerberusCredentialsProviderChain(url, region, xCerberusClientOverride); + Assert.assertNotNull(credentialsProviderChain); + } + + @Test + public void testCreatDefaultCerberusCredentialsProviderChainWithHttpClientReturnsValidObject() { + DefaultCerberusCredentialsProviderChain credentialsProviderChain = new DefaultCerberusCredentialsProviderChain(url, region, httpClient); + Assert.assertNotNull(credentialsProviderChain); + } } \ No newline at end of file diff --git a/src/test/java/com/nike/cerberus/client/auth/aws/BaseAwsCredentialsProviderTest.java b/src/test/java/com/nike/cerberus/client/auth/aws/BaseAwsCredentialsProviderTest.java new file mode 100644 index 0000000..50a0592 --- /dev/null +++ b/src/test/java/com/nike/cerberus/client/auth/aws/BaseAwsCredentialsProviderTest.java @@ -0,0 +1,113 @@ + +package com.nike.cerberus.client.auth.aws; + +import com.nike.cerberus.client.CerberusServerException; +import com.nike.cerberus.client.auth.CerberusCredentials; +import com.nike.cerberus.client.auth.TokenCerberusCredentials; +import okhttp3.*; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.net.ssl.*") +public class BaseAwsCredentialsProviderTest { + + private TokenCerberusCredentials credentials; + private OkHttpClient httpClient; + private Call call; + + + @Before + public void setup(){ + credentials = Mockito.mock(TokenCerberusCredentials.class); + httpClient = Mockito.mock(OkHttpClient.class); + call = Mockito.mock(Call.class); + } + + @Test + public void test_getCredentials(){ + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl") { + @Override + protected void authenticate() { + this.credentials = new TokenCerberusCredentials("token-value"); + } + }; + CerberusCredentials credentials = baseAwsCredentialsProvider.getCredentials(); + Assert.assertNotNull(credentials); + assertThat(credentials.getToken()).isEqualTo("token-value"); + } + @Test + public void test_getCredentials_not_empty(){ + Mockito.when(credentials.getToken()).thenReturn("test-token"); + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl","test-value") { + @Override + protected void authenticate() { + + } + }; + baseAwsCredentialsProvider.credentials = credentials; + + CerberusCredentials credentials = baseAwsCredentialsProvider.getCredentials(); + Assert.assertNotNull(credentials); + assertThat(credentials.getToken()).isEqualTo("test-token"); + } + + @Test + public void test_executeRequestWithRetry() throws Exception{ + Mockito.when(httpClient.newCall(Mockito.any())).thenReturn(call); + Request request = new Request.Builder().url("http://testurl").build(); + + Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_0).code(200).message("response").build(); + Mockito.when(call.execute()).thenReturn(response); + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl",httpClient) { + @Override + protected void authenticate() { + + } + }; + response = baseAwsCredentialsProvider.executeRequestWithRetry(null, 1,1); + Assert.assertNotNull(response); + } + + @Test(expected = IOException.class) + public void test_executeRequestWithRetry_exception() throws Exception{ + Mockito.when(httpClient.newCall(Mockito.any())).thenReturn(call); + Request request = new Request.Builder().url("http://testurl").build(); + + Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_0).code(200).message("response").build(); + Mockito.doThrow(new IOException()).when(call).execute(); + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl",httpClient) { + @Override + protected void authenticate() { + + } + }; + baseAwsCredentialsProvider.executeRequestWithRetry(request, 1,1); + + } + @Test(expected = CerberusServerException.class) + public void test_parseAndThrowErrorResponse() throws Exception{ + Mockito.when(httpClient.newCall(Mockito.any())).thenReturn(call); + Request request = new Request.Builder().url("http://testurl").build(); + + Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_0).code(200).message("response").build(); + //Mockito.doThrow(new IOException()).when(call).execute(); + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl",httpClient) { + @Override + protected void authenticate() { + + } + }; + baseAwsCredentialsProvider.parseAndThrowErrorResponse(200,"response"); + } +} + diff --git a/src/test/java/com/nike/cerberus/client/auth/aws/DefaultAWSCredentialsProviderChainDebuggerTest.java b/src/test/java/com/nike/cerberus/client/auth/aws/DefaultAWSCredentialsProviderChainDebuggerTest.java new file mode 100644 index 0000000..a95226c --- /dev/null +++ b/src/test/java/com/nike/cerberus/client/auth/aws/DefaultAWSCredentialsProviderChainDebuggerTest.java @@ -0,0 +1,33 @@ +package com.nike.cerberus.client.auth.aws; + +import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper; +import com.amazonaws.auth.EnvironmentVariableCredentialsProvider; +import com.amazonaws.auth.SystemPropertiesCredentialsProvider; +import com.amazonaws.auth.profile.ProfileCredentialsProvider; +import com.nike.cerberus.client.auth.EnvironmentCerberusCredentialsProvider; +import com.nike.cerberus.client.auth.SystemPropertyCerberusCredentialsProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static com.amazonaws.SDKGlobalConfiguration.ACCESS_KEY_ENV_VAR; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({EnvironmentVariableCredentialsProvider.class, SystemPropertiesCredentialsProvider.class, ProfileCredentialsProvider.class, EC2ContainerCredentialsProviderWrapper.class}) +public class DefaultAWSCredentialsProviderChainDebuggerTest { + + @Test + public void test_logExtraDebuggingIfAppropriate(){ + mockStatic(System.class); + when(System.getenv("AWS_ACCESS_KEY_ID")).thenReturn("TOKEN"); + when(System.getenv("AWS_SECRET_KEY")).thenReturn("secretKey"); + when(System.getenv("aws.accessKeyId")).thenReturn("accesskey"); + when(System.getenv("aws.secretKey")).thenReturn("aws_secretKey"); + DefaultAWSCredentialsProviderChainDebugger defaultAWSCredentialsProviderChainDebugger = new DefaultAWSCredentialsProviderChainDebugger(); + defaultAWSCredentialsProviderChainDebugger.logExtraDebuggingIfAppropriate("The security token included in the request is expired."); + } +} diff --git a/src/test/java/com/nike/cerberus/client/auth/aws/StsCerberusCredentialsProviderTest.java b/src/test/java/com/nike/cerberus/client/auth/aws/StsCerberusCredentialsProviderTest.java index 42c29ab..93a9e3a 100644 --- a/src/test/java/com/nike/cerberus/client/auth/aws/StsCerberusCredentialsProviderTest.java +++ b/src/test/java/com/nike/cerberus/client/auth/aws/StsCerberusCredentialsProviderTest.java @@ -51,7 +51,7 @@ public class StsCerberusCredentialsProviderTest { private String cerberusUrl; private AWSCredentialsProviderChain chain; - private AWSCredentials credentials; + private AWSCredentials credentials ; @Before public void setUp() {