OpenImmo_1_2_7 extends XmlConverter<OpenImmoDocument, OpenImmoVersion> { @SuppressWarnings("Duplicates") protected void upgradeEnergiepassElements(Document doc) throws JaxenException { Map<String, String> fields = new HashMap<>(); fields.put("stromwert", "user_defined_simplefield[@feldname='epass_stromwert']"); fields.put("waermewert", "user_defined_simplefield[@feldname='epass_waermewert']"); fields.put("wertklasse", "user_defined_simplefield[@feldname='epass_wertklasse']"); fields.put("baujahr", "user_defined_simplefield[@feldname='epass_baujahr']"); fields.put("ausstelldatum", "user_defined_simplefield[@feldname='epass_ausstelldatum']"); fields.put("jahrgang", "user_defined_simplefield[@feldname='epass_jahrgang']"); fields.put("gebaeudeart", "user_defined_simplefield[@feldname='epass_gebaeudeart']"); 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 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_waermewert']", doc); Assert.assertEquals( "3 convertable <user_defined_simplefield feldname=\"epass_waermewert\"> elements available before conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_stromwert']", doc); Assert.assertEquals( "3 convertable <user_defined_simplefield feldname=\"epass_stromwert\"> elements available before conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_wertklasse']", doc); Assert.assertEquals( "3 convertable <user_defined_simplefield feldname=\"epass_wertklasse\"> elements available before conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_baujahr']", doc); Assert.assertEquals( "3 convertable <user_defined_simplefield feldname=\"epass_baujahr\"> elements available before conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_ausstelldatum']", doc); Assert.assertEquals( "3 convertable <user_defined_simplefield feldname=\"epass_ausstelldatum\"> elements available before conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_jahrgang']", doc); Assert.assertEquals( "3 convertable <user_defined_simplefield feldname=\"epass_jahrgang\"> elements available before conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_gebaeudeart']", doc); Assert.assertEquals( "3 convertable <user_defined_simplefield feldname=\"epass_gebaeudeart\"> elements available before conversion.", 3, count); new OpenImmo_1_2_7().upgradeEnergiepassElements(doc); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_waermewert']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_waermewert\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_stromwert']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_stromwert\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_wertklasse']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_wertklasse\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_baujahr']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_baujahr\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_ausstelldatum']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_ausstelldatum\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_jahrgang']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_jahrgang\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:user_defined_simplefield[@feldname='epass_gebaeudeart']", doc); Assert.assertEquals( "0 convertable <user_defined_simplefield feldname=\"epass_gebaeudeart\"> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:waermewert", doc); Assert.assertEquals( "3 converted <waermewert> elements available after conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:stromwert", doc); Assert.assertEquals( "3 converted <stromwert> elements available after conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:wertklasse", doc); Assert.assertEquals( "3 converted <wertklasse> elements available after conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:baujahr", doc); Assert.assertEquals( "3 converted <baujahr> elements available after conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:ausstelldatum", doc); Assert.assertEquals( "3 converted <ausstelldatum> elements available after conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:jahrgang", doc); Assert.assertEquals( "3 converted <jahrgang> elements available after conversion.", 3, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:gebaeudeart", doc); Assert.assertEquals( "3 converted <gebaeudeart> elements available after conversion.", 3, count); } catch (Exception ex) { LOGGER.error("Test of OpenImmo_1_2_7.upgradeEnergiepassElements failed!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); Assert.fail("Test of OpenImmo_1_2_7.upgradeEnergiepassElements failed!"); } }