Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
feat: added Junit tests and improved coverage (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
RVelp1 authored Mar 26, 2021
1 parent a57f0aa commit 746c69f
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -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<String, String> 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";
Expand All @@ -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<String, String> defaultHeaders = new HashMap<>();
defaultHeaders.put(headerKey, headerValue);
CerberusClientFactory.getClient(url, credentialsProvider, 100, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}

Original file line number Diff line number Diff line change
@@ -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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class StsCerberusCredentialsProviderTest {

private String cerberusUrl;
private AWSCredentialsProviderChain chain;
private AWSCredentials credentials;
private AWSCredentials credentials ;

@Before
public void setUp() {
Expand Down

0 comments on commit 746c69f

Please sign in to comment.