### Question:
ServerConfigValidator implements ConstraintValidator<ValidServerConfig, ServerConfig> { @Override public boolean isValid(ServerConfig serverConfig, ConstraintValidatorContext constraintContext) { if (serverConfig == null) { return true; } if (serverConfig.getApp() == null || AppType.ADMIN.toString().equals(serverConfig.getApp().toString())) { String supportedAppTypes = Arrays.stream(AppType.values()) .filter(t -> t != AppType.ADMIN) .map(AppType::name) .map(n -> "THIRD_PARTY".equals(n) ? "ThirdParty" : n) .collect(Collectors.joining(", ")); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate( "app must be provided for serverConfig and be one of " + supportedAppTypes) .addConstraintViolation(); return false; } if (!serverConfig.getApp().getAllowedCommunicationTypes().contains(serverConfig.getCommunicationType())) { LOGGER.debug( "Invalid communicationType '" + serverConfig.getCommunicationType() + "' specified for serverConfig with app " + serverConfig.getApp()); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate( "Invalid communicationType '" + serverConfig.getCommunicationType() + "' specified for serverConfig with app " + serverConfig.getApp()) .addConstraintViolation(); return false; } if (serverConfig.getApp() != AppType.THIRD_PARTY) { if (serverConfig.getCrossDomainConfig() != null) { LOGGER.debug("Invalid server config. CrossDomainConfig is only allowed in ThirdParty server"); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate( "Invalid server config. CrossDomainConfig is only allowed in ThirdParty server") .addConstraintViolation(); return false; } } return true; }  @Override boolean isValid(ServerConfig serverConfig, ConstraintValidatorContext constraintContext);  }### Answer:
@Test public void isValidWhenServerConfigIsNull() { assertThat(validator.isValid(null, cvc)).isTrue(); }
@Test public void isValidWhenValidDataIsSupplied() { assertThat(serverConfig.getApp()).isSameAs(AppType.P2P); assertThat(serverConfig.getServerUri()).isNotNull(); assertThat(serverConfig.getCommunicationType()).isSameAs(CommunicationType.REST); assertThat(serverConfig.getSslConfig()).isNull(); assertThat(serverConfig.getInfluxConfig()).isNull(); assertThat(serverConfig.getBindingAddress()).isEqualTo("http: assertThat(validator.isValid(serverConfig, cvc)).isTrue(); }
@Test public void unsupportedCommunicationType() { serverConfig.setCommunicationType(CommunicationType.WEB_SOCKET); assertThat(validator.isValid(serverConfig, cvc)).isFalse(); verify(cvc).disableDefaultConstraintViolation(); verify(cvc).buildConstraintViolationWithTemplate(anyString()); }
@Test public void allowCorsOnlyInThirdPartyServer() { serverConfig.setApp(AppType.P2P); CrossDomainConfig cors = new CrossDomainConfig(); cors.setAllowCredentials(true); serverConfig.setCrossDomainConfig(cors); assertThat(validator.isValid(serverConfig, cvc)).isFalse(); verify(cvc).disableDefaultConstraintViolation(); verify(cvc).buildConstraintViolationWithTemplate(anyString()); }