Skip to content

Commit

Permalink
The JSON representation of a subfield is always a string #348: provid…
Browse files Browse the repository at this point in the history
…e additional tests
  • Loading branch information
pkiraly committed Jun 14, 2024
1 parent c731537 commit 1c2694a
Showing 1 changed file with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,32 +134,32 @@ public void test045R_asJson() throws IOException, URISyntaxException {
if (reader.hasNext())
record = reader.next();
BibliographicRecord bibRecord = MarcFactory.createPicaFromMarc4j(record, schema);
String json = bibRecord.asJson();

for (de.gwdg.metadataqa.marc.dao.DataField field : bibRecord.getDatafields()) {
System.err.println(field.getTagWithOccurrence());
}


/*
List<de.gwdg.metadataqa.marc.dao.DataField> tags = bibRecord.getDatafield("045R");
assertEquals(1, tags.size());
List<de.gwdg.metadataqa.marc.dao.DataField> tags = bibRecord.getDatafieldsByTag("045R");
assertEquals(5, tags.size());
de.gwdg.metadataqa.marc.dao.DataField tag = tags.get(0);
assertEquals(2, tag.getSubfield("k").size());
assertEquals("Psychologie", tag.getSubfield("k").get(0).getValue());
assertEquals("Sozialpsychologie", tag.getSubfield("k").get(1).getValue());
assertEquals(2, tag.getSubfield("J").size());
assertEquals("Psychologie", tag.getSubfield("J").get(0).getValue());
assertEquals("Sozialpsychologie", tag.getSubfield("J").get(1).getValue());

String json = bibRecord.asJson();
// string approach
int i = json.indexOf("\"045R");
String firstTag045R = json.substring(i, i + 182);
assertTrue(firstTag045R.contains("\"N\":[\"CL-CZ\",\"CV\"]"));
assertTrue(firstTag045R.contains("\"J\":[\"Psychologie\",\"Sozialpsychologie\"]"));

// JSON object approach
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> configuration = mapper.readValue(json, new TypeReference<>(){});
assertNotNull(configuration.get("045T"));
assertTrue(configuration.get("045T") instanceof ArrayList);
assertEquals(1, ((List) configuration.get("045T")).size());
assertTrue(((List)configuration.get("045T")).get(0) instanceof Map);
Map<String, Object> tag = (Map) ((List) configuration.get("045T")).get(0);
Map<String, Object> subfields = (Map) tag.get("subfields");
assertTrue(subfields.get("k") instanceof List);
assertEquals("Psychologie", ((List<?>) subfields.get("k")).get(0));
assertEquals("Sozialpsychologie", ((List<?>) subfields.get("k")).get(1));
*/
assertNotNull(configuration.get("045R"));
assertTrue(configuration.get("045R") instanceof ArrayList);
assertEquals(5, ((List) configuration.get("045R")).size());
assertTrue(((List)configuration.get("045R")).get(0) instanceof Map);
Map<String, Object> firstTag = (Map) ((List) configuration.get("045R")).get(0);
Map<String, Object> subfields = (Map) firstTag.get("subfields");
assertTrue(subfields.get("J") instanceof List);
assertEquals("Psychologie", ((List<?>) subfields.get("J")).get(0));
assertEquals("Sozialpsychologie", ((List<?>) subfields.get("J")).get(1));
}
}

0 comments on commit 1c2694a

Please sign in to comment.