View Javadoc

1   package com.atlassian.asap.core.keys.privatekey;
2   
3   import com.atlassian.asap.api.exception.CannotRetrieveKeyException;
4   import com.atlassian.asap.core.keys.KeyReader;
5   import com.atlassian.asap.core.validator.ValidatedKeyId;
6   import org.junit.Test;
7   import org.junit.runner.RunWith;
8   import org.mockito.Mock;
9   import org.mockito.runners.MockitoJUnitRunner;
10  
11  import java.io.Reader;
12  import java.security.PrivateKey;
13  
14  import static org.hamcrest.Matchers.is;
15  import static org.junit.Assert.assertThat;
16  import static org.mockito.Matchers.any;
17  import static org.mockito.Mockito.when;
18  
19  @RunWith(MockitoJUnitRunner.class)
20  public class StringPrivateKeyProviderTest {
21  
22      private static final String KEY_VALUE = "key-value";
23      private static final String KEY_ID = "key-id";
24  
25      @Mock
26      private KeyReader keyReader;
27  
28      @Mock
29      private PrivateKey key;
30  
31      @Test
32      public void shouldReadKey() throws Exception {
33          when(keyReader.readPrivateKey(any(Reader.class))).thenReturn(key);
34  
35          StringPrivateKeyProvider keyProvider = new StringPrivateKeyProvider(keyReader, KEY_VALUE, KEY_ID);
36  
37          PrivateKey actualKey = keyProvider.getKey(ValidatedKeyId.validate(KEY_ID));
38  
39          assertThat(actualKey, is(key));
40      }
41  
42      @Test(expected = CannotRetrieveKeyException.class)
43      public void shouldNotRetrieveADifferentKeyId() throws Exception {
44          when(keyReader.readPrivateKey(any(Reader.class))).thenReturn(key);
45  
46          StringPrivateKeyProvider keyProvider = new StringPrivateKeyProvider(keyReader, KEY_VALUE, KEY_ID);
47  
48          keyProvider.getKey(ValidatedKeyId.validate("not-the-key-id"));
49      }
50  }