@Test
public void storeSecrets_existingIdenticalSecret() {

    //GIVEN
    final TypedSecret newTypedSecret = new TypedSecret();
    newTypedSecret.setSecretType(SecretType.E_METER_MASTER_KEY);
    newTypedSecret.setSecret(HexUtils.toHexString("$3cr3t".getBytes()));
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setEncryptionProviderType(EncryptionProviderType.JRE);
    keyReference.setReference("keyReferenceString");
    final EncryptedSecret newEncryptedSecret = new EncryptedSecret(EncryptionProviderType.JRE, "$3cr3t".getBytes());
    final DbEncryptedSecret existingDbSecret = new DbEncryptedSecret();
    existingDbSecret.setCreationTime(new Date());
    existingDbSecret.setSecretType(SecretType.E_METER_MASTER_KEY);
    existingDbSecret.setEncodedSecret("1234567890ABCDEF");  //value doesnt matter because of decryption mock
    existingDbSecret.setDeviceIdentification("SOME_DEVICE");
    existingDbSecret.setEncryptionKeyReference(keyReference);
    //WHEN
    when(this.secretRepository.findIdOfValidMostRecent(any(), any(), any())).thenReturn(1L);
    when(this.secretRepository.findById(1L)).thenReturn(Optional.of(existingDbSecret));
    when(this.encryptionDelegate.decrypt(any(), any())).thenReturn(
            new Secret(HexUtils.fromHexString(newTypedSecret.getSecret())));    //identical secrets
    //THEN
    assertThatIllegalArgumentException().isThrownBy(
            () -> this.service.storeSecrets("SOME_DEVICE", Arrays.asList(newTypedSecret))).withMessageContaining(
            "identical to current");
}