@Test public void isNotValidWhenNoP2PServersAreDefined() { List<ServerConfig> serverConfigList = serverConfigList().stream().filter(s -> s.getApp() != AppType.P2P).collect(Collectors.toList()); Config config = new Config(); config.setServerConfigs(serverConfigList); assertThat(validator.isValid(config, cvc)).isFalse(); verify(cvc).disableDefaultConstraintViolation(); verify(cvc).buildConstraintViolationWithTemplate(eq("Exactly one P2P server must be configured.")); }
@Override public boolean isValid(Config config, ConstraintValidatorContext constraintContext) { if (null == config || config.isServerConfigsNull()) { return true; } final Map<AppType, Integer> counts = config.getServerConfigs().stream() .collect(Collectors.toMap(ServerConfig::getApp, v -> 1, (l, r) -> l + 1)); final int p2PEnabledConfigsCount = counts.getOrDefault(AppType.P2P, 0); final int q2TEnabledConfigsCount = counts.getOrDefault(AppType.Q2T, 0); if (p2PEnabledConfigsCount != 1) { LOGGER.debug("Exactly one P2P server must be configured."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Exactly one P2P server must be configured.") .addConstraintViolation(); return false; } if (config.isBootstrapNode()) { if (q2TEnabledConfigsCount != 0) { LOGGER.debug("Q2T server cannot be specified on a bootstrap node."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Q2T server cannot be specified on a bootstrap node.") .addConstraintViolation(); return false; } } else if (q2TEnabledConfigsCount == 0) { LOGGER.debug("At least one Q2T server must be configured or bootstrap mode enabled."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate( "At least one Q2T server must be configured or bootstrap mode enabled.") .addConstraintViolation(); return false; } return true; }
ServerConfigsValidator implements ConstraintValidator<ValidServerConfigs, Config> { @Override public boolean isValid(Config config, ConstraintValidatorContext constraintContext) { if (null == config || config.isServerConfigsNull()) { return true; } final Map<AppType, Integer> counts = config.getServerConfigs().stream() .collect(Collectors.toMap(ServerConfig::getApp, v -> 1, (l, r) -> l + 1)); final int p2PEnabledConfigsCount = counts.getOrDefault(AppType.P2P, 0); final int q2TEnabledConfigsCount = counts.getOrDefault(AppType.Q2T, 0); if (p2PEnabledConfigsCount != 1) { LOGGER.debug("Exactly one P2P server must be configured."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Exactly one P2P server must be configured.") .addConstraintViolation(); return false; } if (config.isBootstrapNode()) { if (q2TEnabledConfigsCount != 0) { LOGGER.debug("Q2T server cannot be specified on a bootstrap node."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Q2T server cannot be specified on a bootstrap node.") .addConstraintViolation(); return false; } } else if (q2TEnabledConfigsCount == 0) { LOGGER.debug("At least one Q2T server must be configured or bootstrap mode enabled."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate( "At least one Q2T server must be configured or bootstrap mode enabled.") .addConstraintViolation(); return false; } return true; } }
ServerConfigsValidator implements ConstraintValidator<ValidServerConfigs, Config> { @Override public boolean isValid(Config config, ConstraintValidatorContext constraintContext) { if (null == config || config.isServerConfigsNull()) { return true; } final Map<AppType, Integer> counts = config.getServerConfigs().stream() .collect(Collectors.toMap(ServerConfig::getApp, v -> 1, (l, r) -> l + 1)); final int p2PEnabledConfigsCount = counts.getOrDefault(AppType.P2P, 0); final int q2TEnabledConfigsCount = counts.getOrDefault(AppType.Q2T, 0); if (p2PEnabledConfigsCount != 1) { LOGGER.debug("Exactly one P2P server must be configured."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Exactly one P2P server must be configured.") .addConstraintViolation(); return false; } if (config.isBootstrapNode()) { if (q2TEnabledConfigsCount != 0) { LOGGER.debug("Q2T server cannot be specified on a bootstrap node."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Q2T server cannot be specified on a bootstrap node.") .addConstraintViolation(); return false; } } else if (q2TEnabledConfigsCount == 0) { LOGGER.debug("At least one Q2T server must be configured or bootstrap mode enabled."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate( "At least one Q2T server must be configured or bootstrap mode enabled.") .addConstraintViolation(); return false; } return true; }  }
ServerConfigsValidator implements ConstraintValidator<ValidServerConfigs, Config> { @Override public boolean isValid(Config config, ConstraintValidatorContext constraintContext) { if (null == config || config.isServerConfigsNull()) { return true; } final Map<AppType, Integer> counts = config.getServerConfigs().stream() .collect(Collectors.toMap(ServerConfig::getApp, v -> 1, (l, r) -> l + 1)); final int p2PEnabledConfigsCount = counts.getOrDefault(AppType.P2P, 0); final int q2TEnabledConfigsCount = counts.getOrDefault(AppType.Q2T, 0); if (p2PEnabledConfigsCount != 1) { LOGGER.debug("Exactly one P2P server must be configured."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Exactly one P2P server must be configured.") .addConstraintViolation(); return false; } if (config.isBootstrapNode()) { if (q2TEnabledConfigsCount != 0) { LOGGER.debug("Q2T server cannot be specified on a bootstrap node."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Q2T server cannot be specified on a bootstrap node.") .addConstraintViolation(); return false; } } else if (q2TEnabledConfigsCount == 0) { LOGGER.debug("At least one Q2T server must be configured or bootstrap mode enabled."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate( "At least one Q2T server must be configured or bootstrap mode enabled.") .addConstraintViolation(); return false; } return true; }  @Override boolean isValid(Config config, ConstraintValidatorContext constraintContext); }
ServerConfigsValidator implements ConstraintValidator<ValidServerConfigs, Config> { @Override public boolean isValid(Config config, ConstraintValidatorContext constraintContext) { if (null == config || config.isServerConfigsNull()) { return true; } final Map<AppType, Integer> counts = config.getServerConfigs().stream() .collect(Collectors.toMap(ServerConfig::getApp, v -> 1, (l, r) -> l + 1)); final int p2PEnabledConfigsCount = counts.getOrDefault(AppType.P2P, 0); final int q2TEnabledConfigsCount = counts.getOrDefault(AppType.Q2T, 0); if (p2PEnabledConfigsCount != 1) { LOGGER.debug("Exactly one P2P server must be configured."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Exactly one P2P server must be configured.") .addConstraintViolation(); return false; } if (config.isBootstrapNode()) { if (q2TEnabledConfigsCount != 0) { LOGGER.debug("Q2T server cannot be specified on a bootstrap node."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate("Q2T server cannot be specified on a bootstrap node.") .addConstraintViolation(); return false; } } else if (q2TEnabledConfigsCount == 0) { LOGGER.debug("At least one Q2T server must be configured or bootstrap mode enabled."); constraintContext.disableDefaultConstraintViolation(); constraintContext .buildConstraintViolationWithTemplate( "At least one Q2T server must be configured or bootstrap mode enabled.") .addConstraintViolation(); return false; } return true; }  @Override boolean isValid(Config config, ConstraintValidatorContext constraintContext);  }