From: Tony Tkacik Date: Mon, 21 Jul 2014 11:48:03 +0000 (+0200) Subject: Fixed incorrect XML test suites & Usage of removed API X-Git-Tag: release/helium~464 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=a063cb44fd583f727fb56c36974b99c86d70d1ec Fixed incorrect XML test suites & Usage of removed API Change in underlying YANG Tools codec, unvereded that there were test-suites which did string comparison for testing of XML responses and any change in XML serialization (valid from XML point-of-view) triggered this tests to fail. Reworked offending tests to use XML W3C Document for testing instead of string comparison and regexes. Change-Id: Ib6e35e8d2fe79275666235370d1dda461277a59f Signed-off-by: Tony Tkacik --- diff --git a/opendaylight/md-sal/sal-protocolbuffer-encoding/src/test/java/org/opendaylight/controller/cluster/datastore/util/NormalizedNodeXmlConverterTest.java b/opendaylight/md-sal/sal-protocolbuffer-encoding/src/test/java/org/opendaylight/controller/cluster/datastore/util/NormalizedNodeXmlConverterTest.java index 02deb5a815..28e358a06a 100644 --- a/opendaylight/md-sal/sal-protocolbuffer-encoding/src/test/java/org/opendaylight/controller/cluster/datastore/util/NormalizedNodeXmlConverterTest.java +++ b/opendaylight/md-sal/sal-protocolbuffer-encoding/src/test/java/org/opendaylight/controller/cluster/datastore/util/NormalizedNodeXmlConverterTest.java @@ -7,10 +7,31 @@ */ package org.opendaylight.controller.cluster.datastore.util; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.net.URI; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; import org.junit.Test; @@ -44,29 +65,10 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.TransformerFactoryConfigurationError; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringWriter; -import java.net.URI; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Set; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; /** @@ -96,8 +98,8 @@ public class NormalizedNodeXmlConverterTest { } } - public static DataSchemaNode getSchemaNode(SchemaContext context, - String moduleName, String childNodeName) { + public static DataSchemaNode getSchemaNode(final SchemaContext context, + final String moduleName, final String childNodeName) { for (Module module : context.getModules()) { if (module.getName().equals(moduleName)) { DataSchemaNode found = @@ -111,12 +113,13 @@ public class NormalizedNodeXmlConverterTest { + childNodeName); } - static DataSchemaNode findChildNode(Set children, String name) { + static DataSchemaNode findChildNode(final Set children, final String name) { List containers = Lists.newArrayList(); for (DataSchemaNode dataSchemaNode : children) { - if (dataSchemaNode.getQName().getLocalName().equals(name)) + if (dataSchemaNode.getQName().getLocalName().equals(name)) { return dataSchemaNode; + } if (dataSchemaNode instanceof DataNodeContainer) { containers.add((DataNodeContainer) dataSchemaNode); } else if (dataSchemaNode instanceof ChoiceNode) { @@ -135,13 +138,13 @@ public class NormalizedNodeXmlConverterTest { } private static InstanceIdentifier.NodeIdentifier getNodeIdentifier( - String localName) { - return new InstanceIdentifier.NodeIdentifier(new QName( + final String localName) { + return new InstanceIdentifier.NodeIdentifier(QName.create( URI.create(NAMESPACE), revision, localName)); } public static InstanceIdentifier.AugmentationIdentifier getAugmentIdentifier( - String... childNames) { + final String... childNames) { Set qn = Sets.newHashSet(); for (String childName : childNames) { @@ -233,7 +236,7 @@ public class NormalizedNodeXmlConverterTest { - public void init(String yangPath, String xmlPath, ContainerNode expectedNode) + public void init(final String yangPath, final String xmlPath, final ContainerNode expectedNode) throws Exception { SchemaContext schema = parseTestSchema(yangPath); this.xmlPath = xmlPath; @@ -242,7 +245,7 @@ public class NormalizedNodeXmlConverterTest { this.expectedNode = expectedNode; } - SchemaContext parseTestSchema(String yangPath) throws Exception { + SchemaContext parseTestSchema(final String yangPath) throws Exception { YangParserImpl yangParserImpl = new YangParserImpl(); InputStream stream = @@ -268,8 +271,9 @@ public class NormalizedNodeXmlConverterTest { .parse(Collections.singletonList(doc.getDocumentElement()), containerNode); - if (expectedNode != null) - junit.framework.Assert.assertEquals(expectedNode, built); + if (expectedNode != null) { + junit.framework.Assert.assertEquals(expectedNode, built); + } logger.info("{}", built); @@ -287,10 +291,9 @@ public class NormalizedNodeXmlConverterTest { System.out.println(toString(doc.getDocumentElement())); System.out.println(toString(el)); - boolean diff = - new Diff( - XMLUnit.buildControlDocument(toString(doc.getDocumentElement())), - XMLUnit.buildTestDocument(toString(el))).similar(); + new Diff( + XMLUnit.buildControlDocument(toString(doc.getDocumentElement())), + XMLUnit.buildTestDocument(toString(el))).similar(); } private static ContainerNode listLeafListWithAttributes() { @@ -353,8 +356,9 @@ public class NormalizedNodeXmlConverterTest { .parse(Collections.singletonList(doc.getDocumentElement()), containerNode); - if (expectedNode != null) - junit.framework.Assert.assertEquals(expectedNode, built); + if (expectedNode != null) { + junit.framework.Assert.assertEquals(expectedNode, built); + } logger.info("{}", built); @@ -372,14 +376,13 @@ public class NormalizedNodeXmlConverterTest { System.out.println(toString(doc.getDocumentElement())); System.out.println(toString(el)); - boolean diff = - new Diff( - XMLUnit.buildControlDocument(toString(doc.getDocumentElement())), - XMLUnit.buildTestDocument(toString(el))).similar(); + new Diff( + XMLUnit.buildControlDocument(toString(doc.getDocumentElement())), + XMLUnit.buildTestDocument(toString(el))).similar(); } - private Document loadDocument(String xmlPath) throws Exception { + private Document loadDocument(final String xmlPath) throws Exception { InputStream resourceAsStream = NormalizedNodeXmlConverterTest.class.getResourceAsStream(xmlPath); @@ -399,7 +402,7 @@ public class NormalizedNodeXmlConverterTest { BUILDERFACTORY = factory; } - private Document readXmlToDocument(InputStream xmlContent) + private Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException { DocumentBuilder dBuilder; try { @@ -413,7 +416,7 @@ public class NormalizedNodeXmlConverterTest { return doc; } - public static String toString(Element xml) { + public static String toString(final Element xml) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); @@ -442,11 +445,10 @@ public class NormalizedNodeXmlConverterTest { System.out.println(toString(convertedDoc.getDocumentElement())); XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); - boolean diff = - new Diff(XMLUnit.buildControlDocument(toString(expectedDoc - .getDocumentElement())), - XMLUnit.buildTestDocument(toString(convertedDoc - .getDocumentElement()))).similar(); + new Diff(XMLUnit.buildControlDocument(toString(expectedDoc + .getDocumentElement())), + XMLUnit.buildTestDocument(toString(convertedDoc + .getDocumentElement()))).similar(); System.out.println(toString(expectedDoc.getDocumentElement())); } @@ -464,11 +466,10 @@ public class NormalizedNodeXmlConverterTest { System.out.println(toString(convertedDoc.getDocumentElement())); XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); - boolean diff = - new Diff(XMLUnit.buildControlDocument(toString(expectedDoc - .getDocumentElement())), - XMLUnit.buildTestDocument(toString(convertedDoc - .getDocumentElement()))).similar(); + new Diff(XMLUnit.buildControlDocument(toString(expectedDoc + .getDocumentElement())), + XMLUnit.buildTestDocument(toString(convertedDoc + .getDocumentElement()))).similar(); System.out.println(toString(expectedDoc.getDocumentElement())); // now we will try to convert xml back to normalize node. diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java index 536d140cf1..ff4678d36f 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java @@ -10,6 +10,7 @@ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; @@ -17,8 +18,6 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -28,16 +27,20 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; + import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; + import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.junit.BeforeClass; @@ -64,11 +67,16 @@ import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.Node; import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode; import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + public class RestGetOperationTest extends JerseyTest { static class NodeData { @@ -90,6 +98,8 @@ public class RestGetOperationTest extends JerseyTest { private static SchemaContext schemaContextModules; private static SchemaContext schemaContextBehindMountPoint; + private static final String RESTCONF_NS = "urn:ietf:params:xml:ns:yang:ietf-restconf"; + @BeforeClass public static void init() throws FileNotFoundException { schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs"); @@ -241,7 +251,7 @@ public class RestGetOperationTest extends JerseyTest { validateModulesResponseJson(response); response = target(uri).request("application/yang.api+xml").get(); - validateModulesResponseXml(response); + validateModulesResponseXml(response,schemaContextModules); } // /streams/ @@ -257,9 +267,12 @@ public class RestGetOperationTest extends JerseyTest { assertTrue(responseBody.contains("streams")); response = target(uri).request("application/yang.api+xml").get(); - responseBody = response.readEntity(String.class); - assertNotNull(responseBody); - assertTrue(responseBody.contains(" foundOperations = new HashSet<>(); + + NodeList operationsList = operationsElem.getChildNodes(); + for(int i = 0;i < operationsList.getLength();i++) { + org.w3c.dom.Node operation = operationsList.item(i); + + String namespace = operation.getNamespaceURI(); + String name = operation.getLocalName(); + QName opQName = QName.create(URI.create(namespace), null, name); + foundOperations.add(opQName); + } + + for(RpcDefinition schemaOp : schemaContext.getOperations()) { + assertTrue(foundOperations.contains(schemaOp.getQName().withoutRevision())); + } + + } + // /operations/pathToMountPoint/yang-ext:mount @Test public void getOperationsBehindMountPointTest() throws FileNotFoundException, UnsupportedEncodingException { @@ -337,15 +372,13 @@ public class RestGetOperationTest extends JerseyTest { Response response = target(uri).request("application/yang.api+xml").get(); assertEquals(200, response.getStatus()); - String responseBody = response.readEntity(String.class); - assertTrue("Xml response for /operations/mount_point rpc-behind-module1 is incorrect", - validateOperationsResponseXml(responseBody, "rpc-behind-module1", "module:1:behind:mount:point").find()); - assertTrue("Xml response for /operations/mount_point rpc-behind-module2 is incorrect", - validateOperationsResponseXml(responseBody, "rpc-behind-module2", "module:2:behind:mount:point").find()); + + Document responseDoc = response.readEntity(Document.class); + validateOperationsResponseXml(responseDoc, schemaContextBehindMountPoint); response = target(uri).request("application/yang.api+json").get(); assertEquals(200, response.getStatus()); - responseBody = response.readEntity(String.class); + String responseBody = response.readEntity(String.class); assertTrue("Json response for /operations/mount_point rpc-behind-module1 is incorrect", validateOperationsResponseJson(responseBody, "rpc-behind-module1", "module1-behind-mount-point").find()); assertTrue("Json response for /operations/mount_point rpc-behind-module2 is incorrect", @@ -441,15 +474,7 @@ public class RestGetOperationTest extends JerseyTest { response = target(uri).request("application/yang.api+xml").get(); assertEquals(200, response.getStatus()); - responseBody = response.readEntity(String.class); - assertTrue( - "module1-behind-mount-point in json wasn't found", - prepareXmlRegex("module1-behind-mount-point", "2014-02-03", "module:1:behind:mount:point", responseBody) - .find()); - assertTrue( - "module2-behind-mount-point in json wasn't found", - prepareXmlRegex("module2-behind-mount-point", "2014-02-04", "module:2:behind:mount:point", responseBody) - .find()); + validateModulesResponseXml(response, schemaContextBehindMountPoint); } @@ -481,26 +506,83 @@ public class RestGetOperationTest extends JerseyTest { response = target(uri).request("application/yang.api+xml").get(); assertEquals(200, response.getStatus()); - responseBody = response.readEntity(String.class); - assertTrue( - "module1-behind-mount-point in json wasn't found", - prepareXmlRegex("module1-behind-mount-point", "2014-02-03", "module:1:behind:mount:point", responseBody) - .find()); - split = responseBody.split(" 0); + + HashSet foundModules = new HashSet<>(); + + for(int i=0;i < moduleNodes.getLength();i++) { + org.w3c.dom.Node module = moduleNodes.item(i); + + QName name = assertedModuleXmlToModuleQName(module); + foundModules.add(name); + } + + assertAllModules(foundModules,schemaContext); + } + + private void assertAllModules(final Set foundModules, final SchemaContext schemaContext) { + for(Module module : schemaContext.getModules()) { + QName current = QName.create(module.getQNameModule(),module.getName()); + assertTrue("Module not found in response.",foundModules.contains(current)); + } + + } + + private QName assertedModuleXmlToModuleQName(final org.w3c.dom.Node module) { + assertEquals("module", module.getLocalName()); + assertEquals(RESTCONF_NS, module.getNamespaceURI()); + String revision = null; + String namespace = null; + String name = null; + + + NodeList childNodes = module.getChildNodes(); + + for(int i =0;i < childNodes.getLength(); i++) { + org.w3c.dom.Node child = childNodes.item(i); + assertEquals(RESTCONF_NS, child.getNamespaceURI()); + + switch(child.getLocalName()) { + case "name": + assertNull("Name element appeared multiple times",name); + name = child.getTextContent().trim(); + break; + case "revision": + assertNull("Revision element appeared multiple times",revision); + revision = child.getTextContent().trim(); + break; + + case "namespace": + assertNull("Namespace element appeared multiple times",namespace); + namespace = child.getTextContent().trim(); + break; + } + } + + assertNotNull("Revision was not part of xml",revision); + assertNotNull("Module namespace was not part of xml",namespace); + assertNotNull("Module identiffier was not part of xml",name); + + + // TODO Auto-generated method stub + + return QName.create(namespace,revision,name); } private void validateModulesResponseJson(final Response response) { @@ -542,34 +624,6 @@ public class RestGetOperationTest extends JerseyTest { } - private Matcher prepareXmlRegex(final String module, final String revision, final String namespace, - final String searchIn) { - StringBuilder regex = new StringBuilder(); - regex.append("^"); - - regex.append(".*"); - - regex.append(".*"); - regex.append(".*" + module); - regex.append(".*<\\/name>"); - - regex.append(".*"); - regex.append(".*" + revision); - regex.append(".*<\\/revision>"); - - regex.append(".*"); - regex.append(".*" + namespace); - regex.append(".*<\\/namespace>"); - - regex.append(".*<\\/module.*>"); - - regex.append(".*"); - regex.append("$"); - - Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL); - return ptrn.matcher(searchIn); - } private void prepareMockForModulesTest(final ControllerContext mockedControllerContext) throws FileNotFoundException { @@ -626,7 +680,7 @@ public class RestGetOperationTest extends JerseyTest { getDataWithUriIncludeWhiteCharsParameter("operational"); } - private void getDataWithUriIncludeWhiteCharsParameter(String target) throws UnsupportedEncodingException { + private void getDataWithUriIncludeWhiteCharsParameter(final String target) throws UnsupportedEncodingException { mockReadConfigurationDataMethod(); String uri = "/" + target + "/ietf-interfaces:interfaces/interface/eth0"; Response response = target(uri).queryParam("prettyPrint", "false").request("application/xml").get(); diff --git a/opendaylight/netconf/netconf-cli/src/main/java/org/opendaylight/controller/netconf/cli/commands/local/Disconnect.java b/opendaylight/netconf/netconf-cli/src/main/java/org/opendaylight/controller/netconf/cli/commands/local/Disconnect.java index 723e484c9c..e9383886ea 100644 --- a/opendaylight/netconf/netconf-cli/src/main/java/org/opendaylight/controller/netconf/cli/commands/local/Disconnect.java +++ b/opendaylight/netconf/netconf-cli/src/main/java/org/opendaylight/controller/netconf/cli/commands/local/Disconnect.java @@ -7,7 +7,6 @@ */ package org.opendaylight.controller.netconf.cli.commands.local; -import com.google.common.collect.Lists; import org.opendaylight.controller.netconf.cli.NetconfDeviceConnectionManager; import org.opendaylight.controller.netconf.cli.commands.AbstractCommand; import org.opendaylight.controller.netconf.cli.commands.Command; @@ -21,6 +20,8 @@ import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl; import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl; import org.opendaylight.yangtools.yang.model.api.RpcDefinition; +import com.google.common.collect.Lists; + /** * Local disconnect command */ @@ -40,7 +41,7 @@ public class Disconnect extends AbstractCommand { connectionManager.disconnect(); return new Output(new CompositeNodeTOImpl(getCommandId(), null, - Lists.> newArrayList(new SimpleNodeTOImpl<>(new QName(getCommandId(), "status"), null, + Lists.> newArrayList(new SimpleNodeTOImpl<>(QName.create(getCommandId(), "status"), null, "Connection disconnected")))); } diff --git a/opendaylight/netconf/netconf-cli/src/test/java/org/opendaylight/controller/netconf/cli/io/IOUtilTest.java b/opendaylight/netconf/netconf-cli/src/test/java/org/opendaylight/controller/netconf/cli/io/IOUtilTest.java index 2021bf722c..5a2445279b 100644 --- a/opendaylight/netconf/netconf-cli/src/test/java/org/opendaylight/controller/netconf/cli/io/IOUtilTest.java +++ b/opendaylight/netconf/netconf-cli/src/test/java/org/opendaylight/controller/netconf/cli/io/IOUtilTest.java @@ -7,20 +7,23 @@ */ package org.opendaylight.controller.netconf.cli.io; -import com.google.common.collect.Maps; import java.util.Map; + import junit.framework.Assert; + import org.junit.Test; import org.opendaylight.controller.netconf.cli.commands.CommandConstants; import org.opendaylight.yangtools.yang.common.QName; +import com.google.common.collect.Maps; + public class IOUtilTest { @Test public void testQNameFromKeyStringNew() throws Exception { final String s = IOUtil.qNameToKeyString(CommandConstants.HELP_QNAME, "module"); final Map modulesMap = Maps.newHashMap(); - modulesMap.put("module", new QName(CommandConstants.HELP_QNAME, "module")); + modulesMap.put("module", QName.create(CommandConstants.HELP_QNAME, "module")); final QName qName = IOUtil.qNameFromKeyString(s, modulesMap); Assert.assertEquals(CommandConstants.HELP_QNAME, qName); }