Skip to content

Commit 2d15d86

Browse files
Merge pull request #1022 from psiotwo/1016-fix-json-report
[#1016] Fixes JSON report serialisation
2 parents 0510bbe + 4aa8eec commit 2d15d86

File tree

7 files changed

+151
-7
lines changed

7 files changed

+151
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Add "domain" and "range" support to export [#1061]
1414

1515
### Fixed
16+
- Fixed report serialization in JSON [#1016]
1617
- Fix missing labels in [`diff`] output. [#1026]
1718

1819
## [1.9.0] - 2022-06-16
@@ -329,6 +330,7 @@ First official release of ROBOT!
329330
[#1061]: https://github.com/ontodev/robot/issues/1061
330331
[#1026]: https://github.com/ontodev/robot/issues/1026
331332
[#1017]: https://github.com/ontodev/robot/issues/1017
333+
[#1016]: https://github.com/ontodev/robot/issues/1016
332334
[#1009]: https://github.com/ontodev/robot/issues/1009
333335
[#979]: https://github.com/ontodev/robot/pull/979
334336
[#978]: https://github.com/ontodev/robot/pull/978

robot-core/src/main/java/org/obolibrary/robot/ReportOperation.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.net.URL;
66
import java.net.URLDecoder;
77
import java.nio.charset.Charset;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
811
import java.util.*;
912
import java.util.jar.JarEntry;
1013
import java.util.jar.JarFile;
14+
import java.util.stream.Collectors;
1115
import org.apache.commons.io.FileUtils;
1216
import org.apache.jena.query.*;
1317
import org.apache.jena.tdb.TDBFactory;
@@ -695,20 +699,21 @@ private static Map<String, String> getDefaultQueryStrings(Set<String> rules)
695699
Map<String, String> queries = new HashMap<>();
696700
// Handle simple file path, probably accessed during testing
697701
if (dirURL != null && dirURL.getProtocol().equals("file")) {
698-
String[] queryFilePaths = new File(dirURL.toURI()).list();
699-
if (queryFilePaths == null || queryFilePaths.length == 0) {
702+
final Set<Path> queryFilePaths =
703+
Files.list(Paths.get(dirURL.toURI())).collect(Collectors.toSet());
704+
if (queryFilePaths.size() == 0) {
700705
throw new IOException(
701706
"Cannot access report query files. There are no files in the directory.");
702707
}
703708
Set<String> defaultRuleNames = new HashSet<>();
704-
for (String qPath : queryFilePaths) {
705-
String ruleName = qPath.substring(qPath.lastIndexOf("/")).split(".")[0];
709+
for (final Path qPath : queryFilePaths) {
710+
final String ruleName = qPath.getFileName().toString().split("[.]")[0];
706711
defaultRuleNames.add(ruleName);
707712
// Only add it to the queries if the rule set contains that rule
708713
// If rules == null, include all rules
709714
if (rules == null || rules.contains(ruleName)) {
710715
queries.put(
711-
ruleName, FileUtils.readFileToString(new File(qPath), Charset.defaultCharset()));
716+
ruleName, FileUtils.readFileToString(qPath.toFile(), Charset.defaultCharset()));
712717
}
713718
}
714719
// Check that all the provided rule names are valid defaults

robot-core/src/main/java/org/obolibrary/robot/checks/Report.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ private String yamlHelper(
556556
display =
557557
OntologyHelper.renderManchester(value, provider, RendererType.OBJECT_RENDERER);
558558
}
559-
sb.append(" - \"").append(display.replace("\"", "\\\"")).append("\"");
559+
sb.append(" - \"").append(display).append("\"");
560560
sb.append("\n");
561561
}
562562
}
@@ -576,7 +576,7 @@ private String yamlHelper(
576576
if (value == null) {
577577
value = "";
578578
}
579-
sb.append(" - \"").append(value.replace("\"", "\\\"")).append("\"");
579+
sb.append(" - \"").append(value).append("\"");
580580
sb.append("\n");
581581
}
582582
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.obolibrary.robot;
2+
3+
import java.io.*;
4+
import java.nio.charset.Charset;
5+
import java.nio.charset.StandardCharsets;
6+
import java.util.Collections;
7+
8+
import org.apache.commons.io.IOUtils;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
import org.semanticweb.owlapi.model.OWLOntology;
12+
import org.yaml.snakeyaml.error.YAMLException;
13+
14+
/**
15+
* Tests for the ReportOperation.
16+
*/
17+
public class ReportOperationTest extends CoreTest {
18+
19+
/**
20+
* Test report produces correct JSON.
21+
*/
22+
@Test
23+
public void testReportProducesValidJson() throws Exception {
24+
testReportProducesCorrectOutput("json");
25+
}
26+
27+
/**
28+
* Test report produces correct YAML.
29+
*/
30+
@Test
31+
public void testReportProducesValidYaml() throws Exception {
32+
testReportProducesCorrectOutput("yaml");
33+
}
34+
35+
private void testReportProducesCorrectOutput(String extension) throws Exception {
36+
try {
37+
final OWLOntology ontology = loadOntology("/1016-report-json-failure/input.owl");
38+
final IOHelper iohelper = new IOHelper();
39+
final File outputFile =
40+
File.createTempFile("1016-report-json-failure-output", "." + extension);
41+
ReportOperation.report(ontology, iohelper, outputFile.toString(), Collections.emptyMap());
42+
final String output = IOUtils.toString(new FileInputStream(outputFile), Charset.defaultCharset()).trim();
43+
final InputStream expected = getClass().getResourceAsStream("/1016-report-json-failure/output." + extension);
44+
assert expected != null;
45+
final String expectedOutput =
46+
IOUtils.toString(expected, StandardCharsets.UTF_8.name()).trim();
47+
Assert.assertEquals(expectedOutput, output);
48+
} catch (YAMLException e) {
49+
e.printStackTrace();
50+
Assert.fail();
51+
}
52+
}
53+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<rdf:RDF
2+
xmlns:obo="http://purl.obolibrary.org/obo/"
3+
xmlns:owl="http://www.w3.org/2002/07/owl#"
4+
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
5+
xmlns:xml="http://www.w3.org/XML/1998/namespace">
6+
<owl:Ontology rdf:about="http://test.org/test"/>
7+
8+
<owl:ObjectProperty rdf:about="http://test.org/test#A">
9+
<obo:IAO_0000115>skos:Concept interferes with &quot;quoted text&quot;</obo:IAO_0000115>
10+
</owl:ObjectProperty>
11+
</rdf:RDF>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[
2+
{
3+
"level": "ERROR",
4+
"violations": [
5+
{
6+
"missing_label": [
7+
{
8+
"subject": "http://test.org/test#A"
9+
}
10+
]
11+
},
12+
{
13+
"missing_ontology_description": [
14+
{
15+
"subject": "http://test.org/test"
16+
}
17+
]
18+
},
19+
{
20+
"missing_ontology_license": [
21+
{
22+
"subject": "http://test.org/test"
23+
}
24+
]
25+
},
26+
{
27+
"missing_ontology_title": [
28+
{
29+
"subject": "http://test.org/test"
30+
}
31+
]
32+
}
33+
]
34+
},
35+
{
36+
"level": "WARN"
37+
},
38+
{
39+
"level": "INFO",
40+
"violations": [
41+
{
42+
"lowercase_definition": [
43+
{
44+
"subject": "http://test.org/test#A",
45+
"property": "IAO:0000115",
46+
"values": [
47+
"skos:Concept interferes with \"quoted text\""
48+
]
49+
}
50+
]
51+
}
52+
]
53+
}
54+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
- level: 'ERROR'
2+
violations:
3+
- missing_label:
4+
- subject: "http://test.org/test#A"
5+
- missing_ontology_description:
6+
- subject: "http://test.org/test"
7+
- missing_ontology_license:
8+
- subject: "http://test.org/test"
9+
- missing_ontology_title:
10+
- subject: "http://test.org/test"
11+
- level: 'WARN'
12+
violations:
13+
- level: 'INFO'
14+
violations:
15+
- lowercase_definition:
16+
- subject: "http://test.org/test#A"
17+
property: "IAO:0000115"
18+
values:
19+
- "skos:Concept interferes with \"quoted text\""

0 commit comments

Comments
 (0)