1414import java .io .FileNotFoundException ;
1515import java .io .FileReader ;
1616import java .io .IOException ;
17+ import java .io .StringReader ;
1718import java .lang .reflect .Method ;
1819import java .nio .file .Files ;
1920import 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
2329public 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 {
0 commit comments