This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added Junit tests and improved coverage (#100)
- Loading branch information
Showing
7 changed files
with
218 additions
and
2 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
113 changes: 113 additions & 0 deletions
113
src/test/java/com/nike/cerberus/client/auth/aws/BaseAwsCredentialsProviderTest.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,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"); | ||
} | ||
} | ||
|
33 changes: 33 additions & 0 deletions
33
...ava/com/nike/cerberus/client/auth/aws/DefaultAWSCredentialsProviderChainDebuggerTest.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,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."); | ||
} | ||
} |
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