Skip to content

Commit c5e6558

Browse files
committed
more informative assert messages, another NPE
1 parent 6873483 commit c5e6558

File tree

4 files changed

+124
-24
lines changed

4 files changed

+124
-24
lines changed

src/main/java/edu/harvard/iq/dataverse/DatasetFieldServiceBean.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,12 @@ private JsonObject filterResponse(JsonObject cvocEntry, JsonObject readObject, S
698698
logger.fine("Added #id pattern: " + filterKey + ": " + termUri);
699699
job.add(filterKey, termUri);
700700
} else if (pattern.contains("{")) {
701-
if (!vals.isEmpty()) {
701+
if (vals.isEmpty()) {
702+
if (nrOfNotFound == 0) {
703+
logger.warning("External Vocabulary: " + termUri + " - No value found for " + filterKey);
704+
}
705+
}
706+
else {
702707
if (pattern.equals("{0}")) {
703708
if (vals.get(0) instanceof JsonArray) {
704709
job.add(filterKey, (JsonArray) vals.get(0));
@@ -716,8 +721,6 @@ else if (vals.get(0) instanceof JsonObject) {
716721
job.add(filterKey, result);
717722
logger.fine("Added : " + filterKey + ": " + result);
718723
}
719-
} else if (nrOfNotFound == 0) {
720-
logger.warning("External Vocabulary: " + termUri + " - No value found for " + filterKey);
721724
}
722725
} else {
723726
logger.fine("Added hardcoded pattern: " + filterKey + ": " + pattern);
@@ -755,14 +758,16 @@ Object processPathSegment(int index, String[] pathParts, JsonValue curPath, Stri
755758
if (expected.equals("@id")) {
756759
expected = termUri;
757760
}
758-
for (int k = 0; k < arr.size(); k++) {
759-
JsonObject jo = arr.getJsonObject(k);
760-
if (!jo.isEmpty()) {
761-
JsonValue val = jo.get(keyVal[0]);
762-
if (val != null && val.getValueType() == ValueType.STRING && val.toString().equals(expected)) {
763-
logger.fine("Found: " + jo.toString());
764-
curPath = jo;
765-
return processPathSegment(index + 1, pathParts, curPath, termUri);
761+
if (arr != null) {
762+
for (int k = 0; k < arr.size(); k++) {
763+
JsonObject jo = arr.getJsonObject(k);
764+
if (!jo.isEmpty()) {
765+
JsonValue val = jo.get(keyVal[0]);
766+
if (val != null && val.getValueType() == ValueType.STRING && val.toString().equals(expected)) {
767+
logger.fine("Found: " + jo.toString());
768+
curPath = jo;
769+
return processPathSegment(index + 1, pathParts, curPath, termUri);
770+
}
766771
}
767772
}
768773
}
@@ -785,7 +790,7 @@ Object processPathSegment(int index, String[] pathParts, JsonValue curPath, Stri
785790

786791
} else {
787792
curPath = ((JsonObject) curPath).get(pathParts[index]);
788-
logger.fine("Found next Path object " + curPath.toString());
793+
logger.fine("Found next Path object " + curPath);
789794
return processPathSegment(index + 1, pathParts, curPath, termUri);
790795
}
791796
} else {

src/test/java/edu/harvard/iq/dataverse/DatasetFieldServiceBeanDansTest.java

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
import java.io.FileNotFoundException;
1515
import java.io.FileReader;
1616
import java.io.IOException;
17+
import java.io.StringReader;
1718
import java.lang.reflect.Method;
1819
import java.nio.file.Files;
1920
import java.nio.file.Paths;
21+
import java.util.List;
22+
import java.util.logging.ConsoleHandler;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
25+
import java.util.stream.Collectors;
2026

21-
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
import static org.assertj.core.api.Assertions.assertThat;
2228

2329
public class DatasetFieldServiceBeanDansTest {
2430

@@ -32,36 +38,118 @@ static String getCvocJson(String pathToJsonFile) throws IOException {
3238
@BeforeEach
3339
void setUp() {
3440
this.datasetFieldServiceBean = Mockito.spy(new DatasetFieldServiceBean());
41+
42+
// Logger rootLogger = Logger.getLogger(DatasetFieldServiceBean.class.getCanonicalName());
43+
// rootLogger.setLevel(Level.FINE);
44+
//
45+
// // Ensure all handlers respect the FINE level
46+
// for (var handler : rootLogger.getHandlers()) {
47+
// handler.setLevel(Level.FINE);
48+
// }
49+
//
50+
// // Add a ConsoleHandler if none exists
51+
// if (rootLogger.getHandlers().length == 0) {
52+
// ConsoleHandler consoleHandler = new ConsoleHandler();
53+
// rootLogger.addHandler(consoleHandler);
54+
// }
3555
}
3656

3757
@AfterEach
3858
void tearDown() {
3959
this.datasetFieldServiceBean = null;
4060
}
4161

62+
@Test
63+
void interceptedResultForAudience() { // TODO remove when assertTermNameValues succeeds in another test
64+
JsonObject result = Json.createReader(new StringReader("""
65+
{
66+
"@id": "https://www.narcis.nl/classification/D18100",
67+
"termName": [
68+
{
69+
"lang": "nl",
70+
"value": "Exploitatie en beheer van het fysieke milieu"
71+
},
72+
{
73+
"lang": "en",
74+
"value": "Exploitation and management of the physical environment"
75+
}
76+
],
77+
"vocabularyUri": "https://www.narcis.nl/classification/"
78+
}
79+
""")).readObject();
80+
List<String> expectedValues = List.of(
81+
"Exploitation and management of the physical environment",
82+
"Exploitatie en beheer van het fysieke milieu"
83+
);
84+
assertThat(result.getString("@id")).isEqualTo("https://www.narcis.nl/classification/D18100");
85+
assertTermNameValues(result, expectedValues);
86+
assertThat(result.keySet()).containsExactlyInAnyOrder("@id", "termName", "vocabularyUri");
87+
// TODO shouldn't we also get the vocabularyName?
88+
}
89+
4290
@Test
4391
void getIndexableStringsForAudience() throws Exception {
44-
String termURI = "https://www.narcis.nl/classification/D13000";
92+
String termURI = "https://www.narcis.nl/classification/D18100";
4593
JsonObject cvocEntry = createMocks("dansAudience", termURI, "audience.json");
4694
JsonObject readObject = readObject("src/test/resources/json/cvoc-dans-value/audience.json");
4795

48-
String result = reflectFilterResponse().invoke(datasetFieldServiceBean, cvocEntry, readObject, termURI).toString();
96+
JsonObject result = (JsonObject) reflectFilterResponse().invoke(datasetFieldServiceBean, cvocEntry, readObject, termURI);
97+
98+
List<String> expectedValues = List.of(
99+
"Exploitation and management of the physical environment",
100+
"Exploitatie en beheer van het fysieke milieu"
101+
);
102+
assertThat(result.getString("@id")).isEqualTo(termURI);
103+
assertTermNameValues(result, expectedValues);
104+
assertThat(result.keySet()).containsExactlyInAnyOrder("@id", "termName", "vocabularyUri");
105+
}
106+
107+
@Test
108+
void getIndexableStringsForAbrPeriod1() throws Exception {
109+
String termURI = "https://data.cultureelerfgoed.nl/term/id/abr/19679187-0ac4-4127-b4cd-09a348400585";
110+
JsonObject cvocEntry = createMocks("dansAbrPeriod", termURI, "abrPeriod.json");
111+
JsonObject readObject = readObject("src/test/resources/json/cvoc-dans-value/abrPeriod-1.json");
49112

50-
assertTrue(result.contains(termURI));
51-
assertTrue(result.contains("Exploitation and management of the physical environment"));
52-
assertTrue(result.contains("Exploitatie en beheer van het fysieke milieu"));
113+
JsonObject result = (JsonObject) reflectFilterResponse().invoke(datasetFieldServiceBean, cvocEntry, readObject, termURI);
114+
115+
List<String> expectedValues = List.of(
116+
"Vroege Middeleeuwen D"
117+
);
118+
assertThat(result.getString("@id")).isEqualTo(termURI);
119+
assertTermNameValues(result, expectedValues);
120+
assertThat(result.keySet()).containsExactlyInAnyOrder("@id", "termName", "vocabularyUri");
53121
}
54122

55123
@Test
56-
void getIndexableStringsForAbrPeriod() throws Exception {
57-
String termURI = "https://data.cultureelerfgoed.nl/term/id/abr/02aea074-a4a9-4335-9698-bec9a188964e";
124+
void getIndexableStringsForAbrPeriod2() throws Exception {
125+
String termURI = "https://data.cultureelerfgoed.nl/term/id/abr/19679187-0ac4-4127-b4cd-09a348400585";
58126
JsonObject cvocEntry = createMocks("dansAbrPeriod", termURI, "abrPeriod.json");
59-
JsonObject readObject = readObject("src/test/resources/json/cvoc-dans-value/abrPeriod.json");
127+
JsonObject readObject = readObject("src/test/resources/json/cvoc-dans-value/abrPeriod-2.json");
128+
129+
JsonObject result = (JsonObject) reflectFilterResponse().invoke(datasetFieldServiceBean, cvocEntry, readObject, termURI);
60130

61-
String result = reflectFilterResponse().invoke(datasetFieldServiceBean, cvocEntry, readObject, termURI).toString();
131+
List<String> expectedValues = List.of(
132+
"Eerste Wereldoorlog"
133+
);
134+
assertThat(result.getString("@id")).isEqualTo(termURI);
135+
assertTermNameValues(result, expectedValues);
136+
assertThat(result.keySet()).containsExactlyInAnyOrder("@id", "termName", "vocabularyUri");
137+
}
138+
139+
private void assertTermNameValues(JsonObject result, List<String> expectedValues) {
140+
assertThat(termNameValues(result))
141+
.withFailMessage("Expected result with termName values: %s but got: %s", expectedValues, result)
142+
.containsExactlyInAnyOrderElementsOf(expectedValues);
143+
}
62144

63-
assertTrue(result.contains(termURI));
64-
assertTrue(result.contains("Vroege Middeleeuwen D"));
145+
private @NotNull List<String> termNameValues(JsonObject result) {
146+
var termName = result.getJsonArray("termName");
147+
if (termName == null) {
148+
return List.of();
149+
}
150+
return termName.stream()
151+
.map(jsonValue -> ((JsonObject) jsonValue).getString("value"))
152+
.collect(Collectors.toList());
65153
}
66154

67155
private JsonObject readObject(String pathname) throws FileNotFoundException {

src/test/resources/json/cvoc-dans-value/abrPeriod.json renamed to src/test/resources/json/cvoc-dans-value/abrPeriod-1.json

File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"@id": "https://data.cultureelerfgoed.nl/term/id/abr/19679187-0ac4-4127-b4cd-09a348400585",
3+
"termName": {
4+
"lang": "nl",
5+
"value": "Eerste Wereldoorlog"
6+
}
7+
}

0 commit comments

Comments
 (0)