|
| 1 | + |
| 2 | +package com.nike.cerberus.client.auth.aws; |
| 3 | + |
| 4 | +import com.nike.cerberus.client.CerberusServerException; |
| 5 | +import com.nike.cerberus.client.auth.CerberusCredentials; |
| 6 | +import com.nike.cerberus.client.auth.TokenCerberusCredentials; |
| 7 | +import okhttp3.*; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.mockito.Mockito; |
| 13 | +import org.powermock.core.classloader.annotations.PowerMockIgnore; |
| 14 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +@RunWith(PowerMockRunner.class) |
| 21 | +@PowerMockIgnore("javax.net.ssl.*") |
| 22 | +public class BaseAwsCredentialsProviderTest { |
| 23 | + |
| 24 | + private TokenCerberusCredentials credentials; |
| 25 | + private OkHttpClient httpClient; |
| 26 | + private Call call; |
| 27 | + |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setup(){ |
| 31 | + credentials = Mockito.mock(TokenCerberusCredentials.class); |
| 32 | + httpClient = Mockito.mock(OkHttpClient.class); |
| 33 | + call = Mockito.mock(Call.class); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void test_getCredentials(){ |
| 38 | + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl") { |
| 39 | + @Override |
| 40 | + protected void authenticate() { |
| 41 | + this.credentials = new TokenCerberusCredentials("token-value"); |
| 42 | + } |
| 43 | + }; |
| 44 | + CerberusCredentials credentials = baseAwsCredentialsProvider.getCredentials(); |
| 45 | + Assert.assertNotNull(credentials); |
| 46 | + assertThat(credentials.getToken()).isEqualTo("token-value"); |
| 47 | + } |
| 48 | + @Test |
| 49 | + public void test_getCredentials_not_empty(){ |
| 50 | + Mockito.when(credentials.getToken()).thenReturn("test-token"); |
| 51 | + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl","test-value") { |
| 52 | + @Override |
| 53 | + protected void authenticate() { |
| 54 | + |
| 55 | + } |
| 56 | + }; |
| 57 | + baseAwsCredentialsProvider.credentials = credentials; |
| 58 | + |
| 59 | + CerberusCredentials credentials = baseAwsCredentialsProvider.getCredentials(); |
| 60 | + Assert.assertNotNull(credentials); |
| 61 | + assertThat(credentials.getToken()).isEqualTo("test-token"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void test_executeRequestWithRetry() throws Exception{ |
| 66 | + Mockito.when(httpClient.newCall(Mockito.any())).thenReturn(call); |
| 67 | + Request request = new Request.Builder().url("http://testurl").build(); |
| 68 | + |
| 69 | + Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_0).code(200).message("response").build(); |
| 70 | + Mockito.when(call.execute()).thenReturn(response); |
| 71 | + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl",httpClient) { |
| 72 | + @Override |
| 73 | + protected void authenticate() { |
| 74 | + |
| 75 | + } |
| 76 | + }; |
| 77 | + response = baseAwsCredentialsProvider.executeRequestWithRetry(null, 1,1); |
| 78 | + Assert.assertNotNull(response); |
| 79 | + } |
| 80 | + |
| 81 | + @Test(expected = IOException.class) |
| 82 | + public void test_executeRequestWithRetry_exception() throws Exception{ |
| 83 | + Mockito.when(httpClient.newCall(Mockito.any())).thenReturn(call); |
| 84 | + Request request = new Request.Builder().url("http://testurl").build(); |
| 85 | + |
| 86 | + Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_0).code(200).message("response").build(); |
| 87 | + Mockito.doThrow(new IOException()).when(call).execute(); |
| 88 | + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl",httpClient) { |
| 89 | + @Override |
| 90 | + protected void authenticate() { |
| 91 | + |
| 92 | + } |
| 93 | + }; |
| 94 | + baseAwsCredentialsProvider.executeRequestWithRetry(request, 1,1); |
| 95 | + |
| 96 | + } |
| 97 | + @Test(expected = CerberusServerException.class) |
| 98 | + public void test_parseAndThrowErrorResponse() throws Exception{ |
| 99 | + Mockito.when(httpClient.newCall(Mockito.any())).thenReturn(call); |
| 100 | + Request request = new Request.Builder().url("http://testurl").build(); |
| 101 | + |
| 102 | + Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_0).code(200).message("response").build(); |
| 103 | + //Mockito.doThrow(new IOException()).when(call).execute(); |
| 104 | + BaseAwsCredentialsProvider baseAwsCredentialsProvider = new BaseAwsCredentialsProvider("http://testurl",httpClient) { |
| 105 | + @Override |
| 106 | + protected void authenticate() { |
| 107 | + |
| 108 | + } |
| 109 | + }; |
| 110 | + baseAwsCredentialsProvider.parseAndThrowErrorResponse(200,"response"); |
| 111 | + } |
| 112 | +} |
| 113 | + |
0 commit comments