@Test
public void mapUuidsToKeys_whenTheActiveKeyIsTheOnlyKey_andThereAreNoCanariesInTheDatabase_andKeyCreationIsEnabled_createsAndSavesACanaryToTheDatabase()
  throws Exception {

  when(encryptionKeysConfiguration.isKeyCreationEnabled()).thenReturn(true);
  when(encryptionKeysConfiguration.getProviders().get(0).getKeys()).thenReturn(asList(activeKeyData));
  when(encryptionKeysConfiguration.getProviders()).thenReturn(asList(activeProvider));
  final List<EncryptionKeyCanary> canaries = newArrayList();
  when(encryptionKeyCanaryDataService.findAll()).thenReturn(canaries);
  when(encryptionKeyCanaryDataService.save(any(EncryptionKeyCanary.class)))
    .thenAnswer(invocation -> {
      canaries.add(activeKeyCanary);
      return activeKeyCanary;
    });
  when(encryptionService.encrypt(any(EncryptionKey.class), eq(CANARY_VALUE))).thenReturn(new EncryptedValue(
    null,
    "fake-encrypted-value",
    "fake-nonce"));
  subject = new EncryptionKeyCanaryMapper(encryptionKeyCanaryDataService,
    encryptionKeysConfiguration, timedRetry, providerFactory);
  subject.mapUuidsToKeys(keySet);
  assertCanaryValueWasEncryptedAndSavedToDatabase();
  assertThat(keySet.get(activeCanaryUuid).getKey(), equalTo(activeKey));
  assertThat(keySet.getActive().getUuid(), equalTo(activeCanaryUuid));
}