RequestValidator { public static <T> void validate(T obj) { Set<ConstraintViolation<T>> violations = getInstance().validate(obj); if (!violations.isEmpty()) { StringBuilder violationStringBuilder = new StringBuilder(); for (ConstraintViolation<T> violation : violations) { violationStringBuilder.append(violation.getPropertyPath().toString()) .append(" ") .append(violation.getMessage()) .append(System.lineSeparator()); } throw RestUtils.createJsonFormattedException("Malformed " + obj.getClass().getSimpleName() + ": " + violationStringBuilder.toString(), 400); } }  static Validator getInstance(); static void validate(T obj);  }
@Test public void testValidateSuccess() throws IOException { String dataSourceDescription = "{\n" + "\t\"data\":{\n" + "\t\t\"id\":\"osm\",\n" + "\t\t\"type\":\"DataSource\",\n" + "\t\t\"attributes\":{\n" + "\t\t\t\"domainIdKey\":\"/id\",\n" + "\t\t\t\"schema\":{},\n" + "\t\t\t\"metaData\":{\n" + "\t\t\t\t\"name\":\"OSM\",\n" + "\t\t\t\t\"title\":\"OSM\",\n" + "\t\t\t\t\"authorEmail\":\"\",\n" + "\t\t\t\t\"notes\":\"\",\n" + "\t\t\t\t\"author\":\"\",\n" + "\t\t\t\t\"termsOfUse\":\"\",\n" + "\t\t\t\t\"url\":\"\"\n" + "\t\t\t}\n" + "\t\t}\n" + "\t}\n" + "}"; JsonApiRequest request = JsonMapper.readValue(dataSourceDescription, JsonApiRequest.class); DataSourceDescription sourceDescription = JsonMapper.convertValue( request.getAttributes(), DataSourceDescription.class); RequestValidator.validate(sourceDescription); }
@Test(expected = WebApplicationException.class) public void testValidateFail() throws IOException { String dataSourceDescription = "{\n" + "\t\"data\":{\n" + "\t\t\"id\":\"osm\",\n" + "\t\t\"type\":\"DataSource\",\n" + "\t\t\"attributes\":{\n" + "\t\t\t\"domainIdKey\":\"/id\",\n" + "\t\t\t\"schema\":{},\n" + "\t\t\t\"metaData\":{\n" + "\t\t\t\t\"name\":\"OSM\",\n" + "\t\t\t\t\"title\":\"OSM\",\n" + "\t\t\t\t\"notes\":\"\",\n" + "\t\t\t\t\"author\":\"\",\n" + "\t\t\t\t\"termsOfUse\":\"\",\n" + "\t\t\t\t\"url\":\"\"\n" + "\t\t\t}\n" + "\t\t}\n" + "\t}\n" + "}"; JsonApiRequest request = JsonMapper.readValue(dataSourceDescription, JsonApiRequest.class); DataSourceDescription sourceDescription = JsonMapper.convertValue( request.getAttributes(), DataSourceDescription.class); RequestValidator.validate(sourceDescription); }