OpenImmo_1_2_1 extends XmlConverter<OpenImmoDocument, OpenImmoVersion> { protected void downgradeEnergiepassElements(Document doc) throws JaxenException { List nodes = XmlUtils.newXPath( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass", doc).selectNodes(doc); for (Object item : nodes) { Element parentNode = (Element) item; boolean skalaProcessed = false; String artValue = XmlUtils.newXPath("io:art/text()", doc) .stringValueOf(parentNode); List childNodes = XmlUtils.newXPath("io:mitwarmwasser", doc) .selectNodes(parentNode); for (Object child : childNodes) { Node childNode = (Node) child; childNode.getParentNode().removeChild(childNode); } childNodes = XmlUtils.newXPath("io:energieverbrauchkennwert", doc) .selectNodes(parentNode); for (Object childItem : childNodes) { Node childNode = (Node) childItem; String childValue = StringUtils.trimToNull(childNode.getTextContent()); if (!skalaProcessed && "VERBRAUCH".equalsIgnoreCase(artValue) && childValue != null) { skalaProcessed = true; Element skalaNode = doc.createElementNS(OpenImmoUtils.OLD_NAMESPACE, "skala"); skalaNode.setAttribute("type", "ZAHL"); skalaNode.setTextContent(childValue); parentNode.appendChild(skalaNode); } childNode.getParentNode().removeChild(childNode); } childNodes = XmlUtils.newXPath("io:endenergiebedarf", doc) .selectNodes(parentNode); for (Object childItem : childNodes) { Node childNode = (Node) childItem; String childValue = StringUtils.trimToNull(childNode.getTextContent()); if (!skalaProcessed && "BEDARF".equalsIgnoreCase(artValue) && childValue != null) { skalaProcessed = true; Element skalaNode = doc.createElementNS(OpenImmoUtils.OLD_NAMESPACE, "skala"); skalaNode.setAttribute("type", "ZAHL"); skalaNode.setTextContent(childValue); parentNode.appendChild(skalaNode); Element newNode = doc.createElementNS(OpenImmoUtils.OLD_NAMESPACE, "energiebedarf"); newNode.setTextContent(childValue); parentNode.appendChild(newNode); } childNode.getParentNode().removeChild(childNode); } } }  @Override OpenImmoVersion getVersion(); @Override void downgradeToPreviousVersion(OpenImmoDocument doc); @Override @SuppressWarnings("Duplicates") void upgradeFromPreviousVersion(OpenImmoDocument doc);  }
@Test public void testDowngradeEnergiepassElements() { try { Document doc = buildTransferDocumentForDowngrade(); int count; count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass", doc); Assert.assertEquals( "2 <energiepass> elements available before conversion.", 2, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:mitwarmwasser", doc); Assert.assertEquals( "1 convertable <mitwarmwasser> element available before conversion.", 1, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:energieverbrauchkennwert", doc); Assert.assertEquals( "1 convertable <energieverbrauchkennwert> element available before conversion.", 1, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:endenergiebedarf", doc); Assert.assertEquals( "1 convertable <endenergiebedarf> element available before conversion.", 1, count); new OpenImmo_1_2_1().downgradeEnergiepassElements(doc); new OpenImmo_1_2_1().downgradeXmlNamespace(doc); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass", doc); Assert.assertEquals( "2 <energiepass> elements available after conversion.", 2, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:mitwarmwasser", doc); Assert.assertEquals( "0 convertable <mitwarmwasser> elements available before conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:energieverbrauchkennwert", doc); Assert.assertEquals( "0 convertable <energieverbrauchkennwert> elements available before conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:endenergiebedarf", doc); Assert.assertEquals( "0 convertable <endenergiebedarf> elements available before conversion.", 0, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:energiebedarf", doc); Assert.assertEquals( "1 converted <energiebedarf> element available before conversion.", 1, count); count = XmlUtils.countNodes( "/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:skala[@type='ZAHL']", doc); Assert.assertEquals( "2 converted <skala> elements available before conversion.", 2, count); } catch (Exception ex) { LOGGER.error("Test of OpenImmo_1_2_1.downgradeEnergiepassElements failed!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); Assert.fail("Test of OpenImmo_1_2_1.downgradeEnergiepassElements failed!"); } }