@Test
public void storeSecrets() throws Exception {

    //GIVEN
    final TypedSecret typedSecret = new TypedSecret();
    typedSecret.setSecretType(SecretType.E_METER_MASTER_KEY);
    typedSecret.setSecret(HexUtils.toHexString("$3cr3t".getBytes()));
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setEncryptionProviderType(EncryptionProviderType.JRE);
    keyReference.setReference("keyReferenceString");
    final EncryptedSecret encryptedSecret = new EncryptedSecret(EncryptionProviderType.JRE, "$3cr3t".getBytes());
    final DbEncryptedSecret dbEncryptedSecret = new DbEncryptedSecret();
    //WHEN
    when(this.secretRepository.findIdOfValidMostRecent(any(), any(), any())).thenReturn(null);
    when(this.keyRepository.findByTypeAndValid(any(), any(), any())).thenReturn(
            new PageImpl<>(Arrays.asList(keyReference)));
    when(this.encryptionDelegate.encrypt(any(), any(), anyString())).thenReturn(encryptedSecret);
    this.service.storeSecrets("SOME_DEVICE", Arrays.asList(typedSecret));
    //THEN
    final ArgumentCaptor<DbEncryptedSecret> secretArgumentCaptor = ArgumentCaptor.forClass(DbEncryptedSecret.class);
    verify(this.secretRepository).save(secretArgumentCaptor.capture());
    final DbEncryptedSecret savedSecret = secretArgumentCaptor.getValue();
    assertThat(savedSecret).isNotNull();
    assertThat(savedSecret.getDeviceIdentification()).isEqualTo("SOME_DEVICE");
    assertThat(savedSecret.getSecretType()).isEqualTo(typedSecret.getSecretType());
    assertThat(savedSecret.getEncodedSecret()).isEqualTo(HexUtils.toHexString(encryptedSecret.getSecret()));
    assertThat(savedSecret.getEncryptionKeyReference()).isEqualTo(keyReference);
    assertThat(savedSecret.getCreationTime()).isNotNull();
}