### Question:
MatchingKeyVaultConfigsForKeyDataValidator implements ConstraintValidator<MatchingKeyVaultConfigsForKeyData, KeyConfiguration> { @Override public boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext cvc) { if (keyConfiguration == null) { return true; } List<Boolean> outcomes = Stream.of(KeyVaultType.values()) .filter(k -> { if (Optional.ofNullable(keyConfiguration.getKeyData()).isPresent()) { return keyConfiguration.getKeyData().stream() .map(KeyDataUtil::getKeyPairTypeFor) .anyMatch(keyType -> Objects.equals(k.getKeyPairType(),keyType)); } return false; }) .filter(k -> !keyConfiguration.getKeyVaultConfig(k).isPresent()) .map( k -> { cvc.disableDefaultConstraintViolation(); String messageKey = String.format( "{MatchingKeyVaultConfigsForKeyData.%s.message}", k.name().toLowerCase()); cvc.buildConstraintViolationWithTemplate(messageKey).addConstraintViolation(); return false; }) .collect(Collectors.toList()); return outcomes.isEmpty(); }  @Override boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext cvc);  }### Answer:
@Test public void isvalid() { KeyConfiguration keyConfiguration = new KeyConfiguration(); KeyData keyData = new KeyData(); keyConfiguration.setKeyData(Collections.singletonList(keyData)); boolean result = validator.isValid(keyConfiguration, constraintValidatorContext); assertThat(result).isTrue(); }
@Test public void nullKeyConfigurationIsIgnored() { assertThat(validator.isValid(null, constraintValidatorContext)).isTrue(); }
@Test public void nullKeyDataIsIgnored() { KeyConfiguration keyConfiguration = new KeyConfiguration(); assertThat(validator.isValid(keyConfiguration, constraintValidatorContext)).isTrue(); }
@Test public void keyDataDoesnotMatchVaultConfig() { ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder = mock(ConstraintValidatorContext.ConstraintViolationBuilder.class); when(constraintValidatorContext.buildConstraintViolationWithTemplate(anyString())) .thenReturn(constraintViolationBuilder); KeyConfiguration keyConfiguration = new KeyConfiguration(); KeyData keyData = new KeyData(); keyData.setAzureVaultPublicKeyId("AzureVaultPublicKeyId"); keyData.setAzureVaultPrivateKeyId("AzureVaultPrivateKeyId"); keyConfiguration.setKeyData(Arrays.asList(keyData)); DefaultKeyVaultConfig keyVaultConfig = new DefaultKeyVaultConfig(); keyVaultConfig.setKeyVaultType(KeyVaultType.HASHICORP); keyConfiguration.addKeyVaultConfig(keyVaultConfig); assertThat(validator.isValid(keyConfiguration, constraintValidatorContext)).isFalse(); verify(constraintValidatorContext).disableDefaultConstraintViolation(); verify(constraintValidatorContext).buildConstraintViolationWithTemplate(anyString()); }
@Test public void keyDataDoesMatchVaultConfig() { KeyConfiguration keyConfiguration = new KeyConfiguration(); KeyData keyData = new KeyData(); keyData.setAzureVaultPublicKeyId("AzureVaultPublicKeyId"); keyData.setAzureVaultPrivateKeyId("AzureVaultPrivateKeyId"); keyConfiguration.setKeyData(Arrays.asList(keyData)); DefaultKeyVaultConfig keyVaultConfig = new DefaultKeyVaultConfig(); keyVaultConfig.setKeyVaultType(KeyVaultType.AZURE); keyConfiguration.addKeyVaultConfig(keyVaultConfig); assertThat(validator.isValid(keyConfiguration, constraintValidatorContext)).isTrue(); }