OpenImmo_1_2_5 extends XmlConverter<OpenImmoDocument, OpenImmoVersion> { @SuppressWarnings("Duplicates") protected void upgradeEnergiepassElements(Document doc) throws JaxenException { Map<String, String> fields = new HashMap<>(); fields.put("hwbwert", "user_defined_simplefield[@feldname='epass_hwbwert']"); fields.put("hwbklasse", "user_defined_simplefield[@feldname='epass_hwbklasse']"); fields.put("fgeewert", "user_defined_simplefield[@feldname='epass_fgeewert']"); fields.put("fgeeklasse", "user_defined_simplefield[@feldname='epass_fgeeklasse']"); List nodes = XmlUtils.newXPath( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben", doc).selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; Element energiepassNode = (Element) XmlUtils.newXPath( "io:energiepass", doc).selectSingleNode(node); if (energiepassNode == null) { energiepassNode = doc.createElementNS(StringUtils.EMPTY, "energiepass"); } for (Map.Entry<String, String> entry : fields.entrySet()) { boolean fieldProcessed = false; List childNodes = XmlUtils.newXPath(entry.getValue(), doc) .selectNodes(node); for (Object childItem : childNodes) { Node childNode = (Node) childItem; if (!fieldProcessed) { String value = StringUtils.trimToNull(childNode.getTextContent()); if (value != null) { Element newElement = doc.createElementNS(StringUtils.EMPTY, entry.getKey()); newElement.setTextContent(value); energiepassNode.appendChild(newElement); fieldProcessed = true; } } node.removeChild(childNode); } } if (energiepassNode.getParentNode() == null && energiepassNode.hasChildNodes()) { node.appendChild(energiepassNode); } } }  @Override OpenImmoVersion getVersion(); @Override void downgradeToPreviousVersion(OpenImmoDocument doc); @Override @SuppressWarnings("Duplicates") void upgradeFromPreviousVersion(OpenImmoDocument doc);  }
@Test public void testUpgradeEnergiepassElements() { try { Document doc = buildTransferDocumentForUpgrade(); int count; count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_hwbwert']", doc); Assert.assertEquals( "2 convertable <user_defined_simplefield feldname=\"epass_hwbwert\"> elements available before conversion.", 2, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_hwbklasse']", doc); Assert.assertEquals( "2 convertable <user_defined_simplefield feldname=\"epass_hwbklasse\"> elements available before conversion.", 2, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_fgeewert']", doc); Assert.assertEquals( "2 convertable <user_defined_simplefield feldname=\"epass_fgeewert\"> elements available before conversion.", 2, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_fgeeklasse']", doc); Assert.assertEquals( "2 convertable <user_defined_simplefield feldname=\"epass_fgeeklasse\"> elements available before conversion.", 2, count); new OpenImmo_1_2_5().upgradeEnergiepassElements(doc); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_hwbwert']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_hwbwert\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_hwbklasse']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_hwbklasse\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_fgeewert']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_fgeewert\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_fgeeklasse']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_fgeeklasse\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:hwbwert", doc); Assert.assertEquals( "2 converted <hwbwert> elements available after conversion.", 2, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:hwbklasse", doc); Assert.assertEquals( "2 converted <hwbklasse> elements available after conversion.", 2, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:fgeewert", doc); Assert.assertEquals( "2 converted <fgeewert> elements available after conversion.", 2, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:fgeeklasse", doc); Assert.assertEquals( "2 converted <fgeeklasse> elements available after conversion.", 2, count); } catch (Exception ex) { LOGGER.error("Test of OpenImmo_1_2_5.upgradeEnergiepassElements failed!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); Assert.fail("Test of OpenImmo_1_2_5.upgradeEnergiepassElements failed!"); } }