View Javadoc

1   package com.atlassian.asap.core.keys.publickey;
2   
3   import com.atlassian.asap.api.exception.CannotRetrieveKeyException;
4   import com.atlassian.asap.core.exception.InvalidHeaderException;
5   import com.atlassian.asap.core.exception.PublicKeyNotFoundException;
6   import com.atlassian.asap.core.exception.PublicKeyRetrievalException;
7   import com.atlassian.asap.core.keys.KeyProvider;
8   import com.atlassian.asap.core.validator.ValidatedKeyId;
9   import com.google.common.collect.ImmutableList;
10  import org.junit.Before;
11  import org.junit.Test;
12  import org.junit.runner.RunWith;
13  import org.mockito.Mock;
14  import org.mockito.runners.MockitoJUnitRunner;
15  
16  import java.net.URI;
17  import java.security.PublicKey;
18  import java.util.List;
19  
20  import static org.hamcrest.Matchers.is;
21  import static org.hamcrest.Matchers.sameInstance;
22  import static org.junit.Assert.assertThat;
23  import static org.junit.Assert.fail;
24  import static org.mockito.Mockito.verify;
25  import static org.mockito.Mockito.verifyZeroInteractions;
26  import static org.mockito.Mockito.when;
27  
28  @RunWith(MockitoJUnitRunner.class)
29  public class ChainedKeyProviderTest {
30      public static final String KEY_ID = "key-1";
31      public static final URI KEY_URI = URI.create("test:" + KEY_ID);
32  
33      @Mock
34      private KeyProvider<PublicKey> provider1;
35      @Mock
36      private KeyProvider<PublicKey> provider2;
37      @Mock
38      private PublicKey key1;
39  
40      private ValidatedKeyId validatedKeyId;
41  
42      private ChainedKeyProvider keyProvider;
43  
44      @Before
45      public void initialise() throws InvalidHeaderException {
46          keyProvider = new ChainedKeyProvider(ImmutableList.of(provider1, provider2));
47          validatedKeyId = ValidatedKeyId.validate(KEY_ID);
48      }
49  
50      @Test
51      public void shouldSuccessfullyGetKeyFromFirstProvider() throws Exception {
52          when(provider1.getKey(validatedKeyId)).thenReturn(key1);
53          assertThat(keyProvider.getKey(validatedKeyId), is(key1));
54          verifyZeroInteractions(provider2);
55      }
56  
57      @Test
58      public void shouldSuccessfullyGetKeyFromSecondProvider() throws Exception {
59          when(provider1.getKey(validatedKeyId)).thenThrow(new PublicKeyRetrievalException("", validatedKeyId, KEY_URI));
60          when(provider2.getKey(validatedKeyId)).thenReturn(key1);
61          assertThat(keyProvider.getKey(validatedKeyId), is(key1));
62          verify(provider1).getKey(validatedKeyId);
63          verify(provider2).getKey(validatedKeyId);
64      }
65  
66      @Test
67      public void shouldFailoverIfKeyDoesNotExistInFirstProvider() throws Exception {
68          when(provider1.getKey(validatedKeyId)).thenThrow(new PublicKeyNotFoundException("", validatedKeyId, KEY_URI));
69          when(provider2.getKey(validatedKeyId)).thenReturn(key1);
70          assertThat(keyProvider.getKey(validatedKeyId), is(key1));
71          verify(provider1).getKey(validatedKeyId);
72          verify(provider2).getKey(validatedKeyId);
73      }
74  
75      @Test
76      public void shouldThrowExceptionIfAllProvidersAreDown() throws Exception {
77          when(provider1.getKey(validatedKeyId)).thenThrow(new CannotRetrieveKeyException(""));
78          when(provider2.getKey(validatedKeyId)).thenThrow(new CannotRetrieveKeyException(""));
79          try {
80              keyProvider.getKey(validatedKeyId);
81              fail("Should have thrown");
82          } catch (CannotRetrieveKeyException e) {
83              verify(provider1).getKey(validatedKeyId);
84              verify(provider2).getKey(validatedKeyId);
85          }
86      }
87  
88      @Test
89      public void shouldThrowExceptionIfKeyIsNotFoundAnywhere() throws Exception {
90          when(provider1.getKey(validatedKeyId)).thenThrow(new PublicKeyNotFoundException("", validatedKeyId, KEY_URI));
91          when(provider2.getKey(validatedKeyId)).thenThrow(new PublicKeyNotFoundException("", validatedKeyId, KEY_URI));
92          try {
93              keyProvider.getKey(validatedKeyId);
94              fail("Should have thrown");
95          } catch (CannotRetrieveKeyException e) {
96              verify(provider1).getKey(validatedKeyId);
97              verify(provider2).getKey(validatedKeyId);
98          }
99      }
100 
101     @Test
102     public void shouldNotWrapForSingleProvider() {
103         List<KeyProvider<PublicKey>> providers = ImmutableList.of(provider1);
104         KeyProvider<PublicKey> chainedProvider = ChainedKeyProvider.createChainedKeyProvider(providers);
105         assertThat(chainedProvider, sameInstance(provider1));
106     }
107 
108     @Test(expected = CannotRetrieveKeyException.class)
109     public void shouldFailWhenNoProviders() throws CannotRetrieveKeyException, InvalidHeaderException {
110         KeyProvider<PublicKey> chainedProvider = ChainedKeyProvider.createChainedKeyProvider(ImmutableList.<KeyProvider<PublicKey>>of());
111         chainedProvider.getKey(validatedKeyId);
112     }
113 }