PageMapper implements Mapping.Mapper<Page> { @Override public Page map(ModsDefinition mods) { NodeLookup nlookup = new NodeLookup(mods); Page page = new Page(); page.setIdentifiers(identMap.map(mods)); page.setNumber(MapperUtils.toString(nlookup.getPageNumber(false))); page.setIndex(MapperUtils.toString(nlookup.getPageIndex(false))); Text note = nlookup.getNote(false); page.setNote(note == null ? null : note.getValue()); PartDefinition part = nlookup.getPart(false); page.setType(part == null ? null : part.getType()); return page; }  @Override Page map(ModsDefinition mods); @Override ModsDefinition map(ModsDefinition mods, Page page); void updatePage(ModsDefinition mods, String pageIndex, String pageNumber, String pageType);  }
@Test public void testRead() { ModsDefinition mods = ModsUtils.unmarshal(PageMapperTest.class.getResource("page_mods.xml"), ModsDefinition.class); PageMapper instance = new PageMapper(); Page result = instance.map(mods); assertNotNull(result); assertEquals("1", result.getIndex()); assertEquals("[1]", result.getNumber()); assertEquals("Blank", result.getType()); assertEquals("note", result.getNote()); List<IdentifierItem> identifiers = result.getIdentifiers(); assertEquals(2, identifiers.size()); assertEquals("issn", identifiers.get(0).getType()); assertEquals("issn value", identifiers.get(0).getValue()); assertEquals("uuid", identifiers.get(1).getType()); assertEquals("1", identifiers.get(1).getValue()); }
@Test public void testReadEmptyDocument() throws Exception { ModsDefinition root = new ModsDefinition(); PageMapper instance = new PageMapper(); Page result = instance.map(root); assertNotNull(result); assertEquals(null, result.getIndex()); assertEquals(null, result.getNumber()); assertEquals(null, result.getType()); assertEquals(null, result.getNote()); assertEquals(Collections.emptyList(), result.getIdentifiers()); }
@Test public void testWriteUpdate() throws Exception { ModsDefinition mods = ModsUtils.unmarshal(PageMapperTest.class.getResource("page_mods.xml"), ModsDefinition.class); Page page = new Page(); page.setNote("note updated"); page.setIndex("2"); page.setNumber("[2]"); page.setType("TitlePage"); page.setIdentifiers(Arrays.asList( new IdentifierItem(1, "uuid", "1 updated"), new IdentifierItem(0, "issn", "issn value updated"), new IdentifierItem("isbn", "isbn value inserted"))); PageMapper instance = new PageMapper(); ModsDefinition result = instance.map(mods, page); String resultXml = ModsUtils.toXml(result, true); ModsDefinition expectedMods = ModsUtils.unmarshal(PageMapperTest.class.getResource("page_mods_updated.xml"), ModsDefinition.class); String expectedXml = ModsUtils.toXml(expectedMods, true); XMLAssert.assertXMLEqual(expectedXml, resultXml); }
@Test public void testWriteRead() throws Exception { Page page = new Page(); page.setIndex("1"); page.setNumber("[1]"); page.setType("normalPage"); page.setNote("note"); page.setIdentifiers(Arrays.asList(new IdentifierItem(null, "uuid", "1"))); PageMapper handler = new PageMapper(); ModsDefinition mods = new ModsDefinition(); handler.map(mods, page); String dump = ModsUtils.toXml(mods, true); ModsDefinition resultMods = ModsUtils.unmarshal(dump, ModsDefinition.class); PageMapper resultHandler = new PageMapper(); Page result = resultHandler.map(resultMods); assertNotNull(result); assertEquals(page.getIndex(), result.getIndex()); assertEquals(page.getNumber(), result.getNumber()); assertEquals(page.getType(), result.getType()); assertEquals(page.getNote(), result.getNote()); assertEquals(Arrays.asList(new IdentifierItem(0, "uuid", "1")), result.getIdentifiers()); }