@Test public void multiConfigsSameTypesInvalid() { ConstraintValidatorContext.ConstraintViolationBuilder builder = mock(ConstraintValidatorContext.ConstraintViolationBuilder.class); String defaultMessageTemplate = "DEFAULT_MESSAGE_TEMPLATE"; when(constraintValidatorContext.getDefaultConstraintMessageTemplate()).thenReturn(defaultMessageTemplate); when(constraintValidatorContext.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); DefaultKeyVaultConfig azure = mock(DefaultKeyVaultConfig.class); when(azure.getKeyVaultType()).thenReturn(KeyVaultType.AZURE); DefaultKeyVaultConfig hashicorp = mock(DefaultKeyVaultConfig.class); when(hashicorp.getKeyVaultType()).thenReturn(KeyVaultType.HASHICORP); DefaultKeyVaultConfig aws = mock(DefaultKeyVaultConfig.class); when(aws.getKeyVaultType()).thenReturn(KeyVaultType.AWS); DefaultKeyVaultConfig azure2 = mock(DefaultKeyVaultConfig.class); when(azure2.getKeyVaultType()).thenReturn(KeyVaultType.AZURE); DefaultKeyVaultConfig hashicorp2 = mock(DefaultKeyVaultConfig.class); when(hashicorp2.getKeyVaultType()).thenReturn(KeyVaultType.HASHICORP); DefaultKeyVaultConfig aws2 = mock(DefaultKeyVaultConfig.class); when(aws2.getKeyVaultType()).thenReturn(KeyVaultType.AWS); List<KeyVaultConfig> configs = Arrays.asList(azure, hashicorp, aws, azure2, hashicorp2, aws2); KeyConfiguration keyConfiguration = mock(KeyConfiguration.class); when(keyConfiguration.getKeyVaultConfigs()).thenReturn(configs); boolean result = validator.isValid(keyConfiguration, constraintValidatorContext); assertThat(result).isFalse(); verify(constraintValidatorContext).buildConstraintViolationWithTemplate("AZURE " + defaultMessageTemplate); verify(constraintValidatorContext).buildConstraintViolationWithTemplate("HASHICORP " + defaultMessageTemplate); verify(constraintValidatorContext).buildConstraintViolationWithTemplate("AWS " + defaultMessageTemplate); }
@Override public boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext constraintValidatorContext) { if(Objects.isNull(keyConfiguration)) { return true; } if(Objects.isNull(keyConfiguration.getKeyVaultConfigs())) { return true; } if(keyConfiguration.getKeyVaultConfigs().isEmpty()) { return true; } final List<KeyVaultConfig> legacyConfigs = new ArrayList<>(); legacyConfigs.add(keyConfiguration.getHashicorpKeyVaultConfig()); legacyConfigs.add(keyConfiguration.getAzureKeyVaultConfig()); List<KeyVaultConfig> configs = keyConfiguration.getKeyVaultConfigs().stream() .map(KeyVaultConfig.class::cast).collect(Collectors.toList()); configs.addAll(legacyConfigs); final Map<KeyVaultType, Integer> typeCount = configs.stream() .filter(Objects::nonNull) .filter(c -> Objects.nonNull(c.getKeyVaultType())) .collect(Collectors.toMap(e -> e.getKeyVaultType(),v -> 1, (l, r) -> l + 1)); typeCount.entrySet().stream() .filter(e -> e.getValue() > 1) .map(e -> e.getKey().name()) .forEach(s -> { String message = String.join(" ",s,constraintValidatorContext.getDefaultConstraintMessageTemplate()); constraintValidatorContext.disableDefaultConstraintViolation(); constraintValidatorContext .buildConstraintViolationWithTemplate(message) .addConstraintViolation(); }); return typeCount.values().stream().allMatch(v -> v == 1); }
NoDuplicateKeyVaultConfigsValidator implements ConstraintValidator<NoDuplicateKeyVaultConfigs, KeyConfiguration> { @Override public boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext constraintValidatorContext) { if(Objects.isNull(keyConfiguration)) { return true; } if(Objects.isNull(keyConfiguration.getKeyVaultConfigs())) { return true; } if(keyConfiguration.getKeyVaultConfigs().isEmpty()) { return true; } final List<KeyVaultConfig> legacyConfigs = new ArrayList<>(); legacyConfigs.add(keyConfiguration.getHashicorpKeyVaultConfig()); legacyConfigs.add(keyConfiguration.getAzureKeyVaultConfig()); List<KeyVaultConfig> configs = keyConfiguration.getKeyVaultConfigs().stream() .map(KeyVaultConfig.class::cast).collect(Collectors.toList()); configs.addAll(legacyConfigs); final Map<KeyVaultType, Integer> typeCount = configs.stream() .filter(Objects::nonNull) .filter(c -> Objects.nonNull(c.getKeyVaultType())) .collect(Collectors.toMap(e -> e.getKeyVaultType(),v -> 1, (l, r) -> l + 1)); typeCount.entrySet().stream() .filter(e -> e.getValue() > 1) .map(e -> e.getKey().name()) .forEach(s -> { String message = String.join(" ",s,constraintValidatorContext.getDefaultConstraintMessageTemplate()); constraintValidatorContext.disableDefaultConstraintViolation(); constraintValidatorContext .buildConstraintViolationWithTemplate(message) .addConstraintViolation(); }); return typeCount.values().stream().allMatch(v -> v == 1); } }
NoDuplicateKeyVaultConfigsValidator implements ConstraintValidator<NoDuplicateKeyVaultConfigs, KeyConfiguration> { @Override public boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext constraintValidatorContext) { if(Objects.isNull(keyConfiguration)) { return true; } if(Objects.isNull(keyConfiguration.getKeyVaultConfigs())) { return true; } if(keyConfiguration.getKeyVaultConfigs().isEmpty()) { return true; } final List<KeyVaultConfig> legacyConfigs = new ArrayList<>(); legacyConfigs.add(keyConfiguration.getHashicorpKeyVaultConfig()); legacyConfigs.add(keyConfiguration.getAzureKeyVaultConfig()); List<KeyVaultConfig> configs = keyConfiguration.getKeyVaultConfigs().stream() .map(KeyVaultConfig.class::cast).collect(Collectors.toList()); configs.addAll(legacyConfigs); final Map<KeyVaultType, Integer> typeCount = configs.stream() .filter(Objects::nonNull) .filter(c -> Objects.nonNull(c.getKeyVaultType())) .collect(Collectors.toMap(e -> e.getKeyVaultType(),v -> 1, (l, r) -> l + 1)); typeCount.entrySet().stream() .filter(e -> e.getValue() > 1) .map(e -> e.getKey().name()) .forEach(s -> { String message = String.join(" ",s,constraintValidatorContext.getDefaultConstraintMessageTemplate()); constraintValidatorContext.disableDefaultConstraintViolation(); constraintValidatorContext .buildConstraintViolationWithTemplate(message) .addConstraintViolation(); }); return typeCount.values().stream().allMatch(v -> v == 1); }  }
NoDuplicateKeyVaultConfigsValidator implements ConstraintValidator<NoDuplicateKeyVaultConfigs, KeyConfiguration> { @Override public boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext constraintValidatorContext) { if(Objects.isNull(keyConfiguration)) { return true; } if(Objects.isNull(keyConfiguration.getKeyVaultConfigs())) { return true; } if(keyConfiguration.getKeyVaultConfigs().isEmpty()) { return true; } final List<KeyVaultConfig> legacyConfigs = new ArrayList<>(); legacyConfigs.add(keyConfiguration.getHashicorpKeyVaultConfig()); legacyConfigs.add(keyConfiguration.getAzureKeyVaultConfig()); List<KeyVaultConfig> configs = keyConfiguration.getKeyVaultConfigs().stream() .map(KeyVaultConfig.class::cast).collect(Collectors.toList()); configs.addAll(legacyConfigs); final Map<KeyVaultType, Integer> typeCount = configs.stream() .filter(Objects::nonNull) .filter(c -> Objects.nonNull(c.getKeyVaultType())) .collect(Collectors.toMap(e -> e.getKeyVaultType(),v -> 1, (l, r) -> l + 1)); typeCount.entrySet().stream() .filter(e -> e.getValue() > 1) .map(e -> e.getKey().name()) .forEach(s -> { String message = String.join(" ",s,constraintValidatorContext.getDefaultConstraintMessageTemplate()); constraintValidatorContext.disableDefaultConstraintViolation(); constraintValidatorContext .buildConstraintViolationWithTemplate(message) .addConstraintViolation(); }); return typeCount.values().stream().allMatch(v -> v == 1); }  @Override void initialize(NoDuplicateKeyVaultConfigs config); @Override boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext constraintValidatorContext); }
NoDuplicateKeyVaultConfigsValidator implements ConstraintValidator<NoDuplicateKeyVaultConfigs, KeyConfiguration> { @Override public boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext constraintValidatorContext) { if(Objects.isNull(keyConfiguration)) { return true; } if(Objects.isNull(keyConfiguration.getKeyVaultConfigs())) { return true; } if(keyConfiguration.getKeyVaultConfigs().isEmpty()) { return true; } final List<KeyVaultConfig> legacyConfigs = new ArrayList<>(); legacyConfigs.add(keyConfiguration.getHashicorpKeyVaultConfig()); legacyConfigs.add(keyConfiguration.getAzureKeyVaultConfig()); List<KeyVaultConfig> configs = keyConfiguration.getKeyVaultConfigs().stream() .map(KeyVaultConfig.class::cast).collect(Collectors.toList()); configs.addAll(legacyConfigs); final Map<KeyVaultType, Integer> typeCount = configs.stream() .filter(Objects::nonNull) .filter(c -> Objects.nonNull(c.getKeyVaultType())) .collect(Collectors.toMap(e -> e.getKeyVaultType(),v -> 1, (l, r) -> l + 1)); typeCount.entrySet().stream() .filter(e -> e.getValue() > 1) .map(e -> e.getKey().name()) .forEach(s -> { String message = String.join(" ",s,constraintValidatorContext.getDefaultConstraintMessageTemplate()); constraintValidatorContext.disableDefaultConstraintViolation(); constraintValidatorContext .buildConstraintViolationWithTemplate(message) .addConstraintViolation(); }); return typeCount.values().stream().allMatch(v -> v == 1); }  @Override void initialize(NoDuplicateKeyVaultConfigs config); @Override boolean isValid(KeyConfiguration keyConfiguration, ConstraintValidatorContext constraintValidatorContext);  }