### Question:
EitherServerConfigsOrServerValidator implements ConstraintValidator<ValidEitherServerConfigsOrServer, Config> { @Override public boolean isValid(Config config, ConstraintValidatorContext constraintContext) { if (config == null) { return true; } if (null == config.getServer() && config.isServerConfigsNull()) { LOGGER.debug("One of server/serverConfigs must be provided."); constraintContext.disableDefaultConstraintViolation(); constraintContext.buildConstraintViolationWithTemplate("One of server/serverConfigs must be provided.") .addConstraintViolation(); return false; } if (null != config.getServer() && !config.isServerConfigsNull()) { LOGGER.debug("Either one of server/serverConfigs can be configured (not both)."); constraintContext.disableDefaultConstraintViolation(); constraintContext.buildConstraintViolationWithTemplate("Either one of server/serverConfigs can be configured (not both).") .addConstraintViolation(); return false; } if (Objects.nonNull(config.getServer()) && Objects.isNull(config.getUnixSocketFile())) { LOGGER.debug("Unix socket file must be configured is using deprecated server config"); constraintContext.disableDefaultConstraintViolation(); constraintContext.buildConstraintViolationWithTemplate("Unix socket file must be configured is using deprecated server config. Check config.unixSocketFile") .addConstraintViolation(); return false; } return true; }  @Override boolean isValid(Config config, ConstraintValidatorContext constraintContext);  }### Answer:
@Test public void ignoreNullArg() { boolean outcome = eitherServerConfigsOrServerValidator.isValid(null, constraintContext); assertThat(outcome).isTrue(); }
@Test public void nullServerAndServerConfigs() { Config config = new Config(); boolean outcome = eitherServerConfigsOrServerValidator.isValid(config, constraintContext); assertThat(outcome).isFalse(); verify(constraintContext).disableDefaultConstraintViolation(); verify(constraintContext).buildConstraintViolationWithTemplate(anyString()); }
@Test public void cantHaveBoth() { Config config = new Config(); config.setServer(new DeprecatedServerConfig()); config.setServerConfigs(Collections.emptyList()); boolean outcome = eitherServerConfigsOrServerValidator.isValid(config, constraintContext); assertThat(outcome).isFalse(); verify(constraintContext).disableDefaultConstraintViolation(); verify(constraintContext).buildConstraintViolationWithTemplate(anyString()); }
@Test public void unixFileRequiredWhenDeprecatedServer() { Config config = new Config(); config.setServer(new DeprecatedServerConfig()); config.setUnixSocketFile(null); boolean outcome = eitherServerConfigsOrServerValidator.isValid(config, constraintContext); assertThat(outcome).isFalse(); verify(constraintContext).disableDefaultConstraintViolation(); verify(constraintContext).buildConstraintViolationWithTemplate(anyString()); }
@Test public void validConfigReturnsTrue() throws IOException { final Path socketPath = Files.createTempFile("socket", ".ipc"); Config config = new Config(); config.setServer(new DeprecatedServerConfig()); config.setUnixSocketFile(socketPath); boolean outcome = eitherServerConfigsOrServerValidator.isValid(config, constraintContext); assertThat(outcome).isTrue(); }