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 MirroredKeyProviderTest {
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> mirror1;
35      @Mock
36      private KeyProvider<PublicKey> mirror2;
37      @Mock
38      private PublicKey key1;
39      private ValidatedKeyId validatedKeyId;
40  
41      private MirroredKeyProvider keyProvider;
42  
43      @Before
44      public void initialise() throws InvalidHeaderException {
45          keyProvider = new MirroredKeyProvider(ImmutableList.of(mirror1, mirror2));
46          validatedKeyId = ValidatedKeyId.validate(KEY_ID);
47      }
48  
49      @Test
50      public void shouldSuccessfullyGetKeyFromFirstMirror() throws Exception {
51          when(mirror1.getKey(validatedKeyId)).thenReturn(key1);
52          assertThat(keyProvider.getKey(validatedKeyId), is(key1));
53          verifyZeroInteractions(mirror2);
54      }
55  
56      @Test
57      public void shouldSuccessfullyGetKeyFromSecondMirror() throws Exception {
58          when(mirror1.getKey(validatedKeyId)).thenThrow(new PublicKeyRetrievalException("", validatedKeyId, KEY_URI));
59          when(mirror2.getKey(validatedKeyId)).thenReturn(key1);
60          assertThat(keyProvider.getKey(validatedKeyId), is(key1));
61          verify(mirror1).getKey(validatedKeyId);
62          verify(mirror2).getKey(validatedKeyId);
63      }
64  
65      @Test
66      public void shouldGiveupIfKeyDoesNotExistInFirstMirror() throws Exception {
67          when(mirror1.getKey(validatedKeyId)).thenThrow(new PublicKeyNotFoundException("", validatedKeyId, KEY_URI));
68          try {
69              keyProvider.getKey(validatedKeyId);
70              fail("Should have thrown");
71          } catch (CannotRetrieveKeyException e) {
72              verify(mirror1).getKey(validatedKeyId);
73              verifyZeroInteractions(mirror2);
74          }
75      }
76  
77      @Test
78      public void shouldThrowExceptionIfAllMirrorsAreDown() throws Exception {
79          when(mirror1.getKey(validatedKeyId)).thenThrow(new CannotRetrieveKeyException(""));
80          when(mirror2.getKey(validatedKeyId)).thenThrow(new CannotRetrieveKeyException(""));
81          try {
82              keyProvider.getKey(validatedKeyId);
83              fail("Should have thrown");
84          } catch (CannotRetrieveKeyException e) {
85              verify(mirror1).getKey(validatedKeyId);
86              verify(mirror2).getKey(validatedKeyId);
87          }
88      }
89  
90      @Test
91      public void shouldNotWrapForSingleMirror() {
92          List<KeyProvider<PublicKey>> providers = ImmutableList.of(mirror1);
93          KeyProvider<PublicKey> mirroredProvider = MirroredKeyProvider.createMirroredKeyProvider(providers);
94          assertThat(mirroredProvider, sameInstance(mirror1));
95      }
96  }