### Question:
DatabaseVendorAndServiceValidator implements ConstraintValidator<ValidDatabaseVendorAndService, DatabaseV4Request> { @Override public boolean isValid(DatabaseV4Request request, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); Optional<DatabaseVendor> vendorByJdbcUrl = databaseVendorUtil.getVendorByJdbcUrl(request.getConnectionURL()); if (!vendorByJdbcUrl.isPresent()) { ValidatorUtil.addConstraintViolation(context, "Could not determine database vendor from JDBC URL " + request.getConnectionURL(), "connectionURL"); return false; } Optional<SupportedExternalDatabaseServiceEntry> serviceEntry = SupportedDatabaseProvider.supportedExternalDatabases() .stream() .filter(item -> item.getName().equalsIgnoreCase(request.getType()) || item.getDisplayName().equalsIgnoreCase(request.getType())) .findFirst(); if (!serviceEntry.isPresent()) { serviceEntry = SupportedDatabaseProvider.getOthers(); } if (serviceEntry.isPresent() && vendorByJdbcUrl.isPresent()) { Set<SupportedDatabaseEntry> allSupportedDatabases = serviceEntry.get().getDatabases(); boolean supported = allSupportedDatabases.stream() .anyMatch(item -> item.getDatabaseName().equals(vendorByJdbcUrl.get().name())); if (supported) { return true; } String message = String.format("The specified database vendor for service %s is not supported." + " The supported database vendors are: %s", request.getType(), allSupportedDatabases.stream() .map(SupportedDatabaseEntry::getDatabaseName) .collect(Collectors.joining(", "))); ValidatorUtil.addConstraintViolation(context, message, "type"); return false; } ValidatorUtil.addConstraintViolation(context, "Could not find database support information for service " + request.getType(), "type"); return false; }  @Override boolean isValid(DatabaseV4Request request, ConstraintValidatorContext context);  }### Answer:
@Test public void testIsValid() { DatabaseV4Request request = new DatabaseV4Request(); request.setType(serviceName); request.setConnectionURL(new DatabaseCommon().getJdbcConnectionUrl(databaseVendor.jdbcUrlDriverId(), "mydb.example.com", 1234, Optional.of(serviceName))); assertEquals(expectedResult, underTest.isValid(request, context)); int expectedTimes = expectedResult ? 0 : 1; verify(context, times(expectedTimes)).buildConstraintViolationWithTemplate(any(String.class)); }