@Test
public void decrypt_whenTheOperationSucceedsOnlyAfterReconnection() throws Exception {

  when(keySet.get(activeKeyUuid))
    .thenReturn(firstActiveKey)
    .thenReturn(secondActiveKey);
  when(keySet.getActive())
    .thenReturn(firstActiveKey);
  when(encryptionService.isLoggedIn()).thenReturn(true);
  when(firstActiveKey
    .decrypt("fake-encrypted-value".getBytes(UTF_8), "fake-nonce".getBytes(UTF_8)))
    .thenThrow(new IllegalBlockSizeException("test exception"));
  when(firstActiveKey.getProvider()).thenReturn(encryptionService);
  when(secondActiveKey
    .decrypt("fake-encrypted-value".getBytes(UTF_8), "fake-nonce".getBytes(UTF_8)))
    .thenReturn("fake-plaintext");
  when(secondActiveKey.getProvider()).thenReturn(encryptionService);
  assertThat(subject
      .decrypt(new EncryptedValue(activeKeyUuid, "fake-encrypted-value".getBytes(UTF_8), "fake-nonce".getBytes(UTF_8))),
    equalTo("fake-plaintext"));
  verify(encryptionService, times(1))
    .reconnect(any(IllegalBlockSizeException.class));
  verify(keySet, times(1)).reload();
}