Skip to content

Commit 7357ad4

Browse files
Add TiltValidator
1 parent f8853ae commit 7357ad4

3 files changed

Lines changed: 64 additions & 23 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.idea/
2+
*.iml
3+
14

25
# Created by https://www.toptal.com/developers/gitignore/api/java,intellij,macos,git,maven
36
# Edit at https://www.toptal.com/developers/gitignore?templates=java,intellij,macos,git,maven
@@ -186,4 +189,4 @@ buildNumber.properties
186189
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
187190
.mvn/wrapper/maven-wrapper.jar
188191

189-
# End of https://www.toptal.com/developers/gitignore/api/java,intellij,macos,git,maven
192+
# End of https://www.toptal.com/developers/gitignore/api/java,intellij,macos,git,maven

src/main/java/TiltValidator.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import org.apache.commons.io.IOUtils;
2+
import org.everit.json.schema.Schema;
3+
import org.everit.json.schema.ValidationException;
4+
import org.everit.json.schema.loader.SchemaLoader;
5+
import org.json.JSONObject;
6+
import org.json.JSONTokener;
7+
8+
import java.io.IOException;
9+
import java.net.URI;
10+
11+
public class TiltValidator {
12+
13+
public static final String TILT_SCHEMA_URL = "https://raw.githubusercontent.com/Transparency-Information-Language/schema/master/tilt-schema.json";
14+
15+
/*
16+
Returns true if document is valid according to given schema
17+
*/
18+
public static boolean validateDocumentFromUrl(String documentUrl, String schemaUrl) {
19+
try {
20+
String schema_content = IOUtils.toString(URI.create(schemaUrl), "utf8");
21+
JSONObject rawSchema = new JSONObject(new JSONTokener(schema_content));
22+
Schema schema = SchemaLoader.load(rawSchema);
23+
24+
String instance_content = IOUtils.toString(URI.create(documentUrl), "utf8");
25+
schema.validate(new JSONObject(instance_content));
26+
27+
System.out.println("Document " + documentUrl + " seems to be valid!\n");
28+
return true;
29+
} catch (ValidationException e) {
30+
for(String s : e.getAllMessages())
31+
System.out.println(s);
32+
//for(Exception ex : e.getCausingExceptions())
33+
// System.out.println(ex.getMessage());
34+
return false;
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
return false;
38+
}
39+
}
40+
41+
/*
42+
Validate by standard tilt schema
43+
*/
44+
public static boolean validateDocumentFromUrl(String documentUrl) {
45+
return validateDocumentFromUrl(documentUrl, TILT_SCHEMA_URL);
46+
}
47+
48+
49+
}

src/test/java/MainTest.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.net.URI;
1818
import java.util.*;
1919

20-
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.*;
2121

2222
public class MainTest {
2323

@@ -158,27 +158,16 @@ public void validateLegacy() throws Exception {
158158
} */
159159

160160
@Test
161-
public void validateDocument() {
162-
try {
163-
String schema_content = IOUtils.toString(URI.create(TILT_SCHEMA_URL), "utf8");
164-
JSONObject rawSchema = new JSONObject(new JSONTokener(schema_content));
165-
Schema schema = SchemaLoader.load(rawSchema);
166-
167-
String instance_content = IOUtils.toString(URI.create(TILT_VALID_DOCUMENT_URL), "utf8");
168-
schema.validate(new JSONObject(instance_content));
169-
System.out.println("Document " + TILT_VALID_DOCUMENT_URL + " seems to be valid!\n");
170-
171-
instance_content = IOUtils.toString(URI.create(TILT_INVALID_DOCUMENT_URL), "utf8");
172-
schema.validate(new JSONObject(instance_content));
173-
System.out.println("Document " + TILT_INVALID_DOCUMENT_URL + "seems to be valid!");
174-
} catch (ValidationException e) {
175-
for(String s : e.getAllMessages())
176-
System.out.println(s);
177-
//for(Exception ex : e.getCausingExceptions())
178-
// System.out.println(ex.getMessage());
179-
} catch (IOException e) {
180-
e.printStackTrace();
181-
}
161+
public void validateDocuments() {
162+
System.out.println("--- Document #1 ---");
163+
boolean validationResult = TiltValidator.validateDocumentFromUrl(TILT_VALID_DOCUMENT_URL, TILT_SCHEMA_URL);
164+
assertTrue(validationResult);
165+
}
166+
167+
@Test public void validateAnotherDocument() {
168+
System.out.println("--- Document #2 ---");
169+
boolean validationResult = TiltValidator.validateDocumentFromUrl(TILT_INVALID_DOCUMENT_URL);
170+
assertFalse(validationResult);
182171
}
183172
}
184173

0 commit comments

Comments
 (0)