From e7ec158bf5591335b23e9d2cb08c84ec4de53b34 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 30 Sep 2023 01:29:49 +0200 Subject: [PATCH] Clean up BaseRpcSchemalessTransformerTest Switch to using xmlunit-core and clean up. Change-Id: I5ddd5316939859b131f28949d3de462c9b1929aa Signed-off-by: Robert Varga --- plugins/netconf-client-mdsal/pom.xml | 14 +- .../BaseRpcSchemalessTransformerTest.java | 147 ++++++++---------- 2 files changed, 74 insertions(+), 87 deletions(-) diff --git a/plugins/netconf-client-mdsal/pom.xml b/plugins/netconf-client-mdsal/pom.xml index 22705f8900..00d0af2b75 100644 --- a/plugins/netconf-client-mdsal/pom.xml +++ b/plugins/netconf-client-mdsal/pom.xml @@ -19,6 +19,10 @@ bundle + + com.google.code.gson + gson + com.google.guava guava @@ -199,6 +203,11 @@ org.osgi.service.component.annotations + + + org.xmlunit + xmlunit-core + org.xmlunit xmlunit-legacy @@ -212,15 +221,10 @@ org.opendaylight.netconf netconf-test-util - - com.google.code.gson - gson - org.opendaylight.yangtools yang-test-util - org.mockito diff --git a/plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/impl/BaseRpcSchemalessTransformerTest.java b/plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/impl/BaseRpcSchemalessTransformerTest.java index 8f99b2b7eb..05065e55e9 100644 --- a/plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/impl/BaseRpcSchemalessTransformerTest.java +++ b/plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/impl/BaseRpcSchemalessTransformerTest.java @@ -8,16 +8,11 @@ package org.opendaylight.netconf.client.mdsal.impl; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import java.io.InputStream; import javax.xml.transform.dom.DOMSource; -import org.custommonkey.xmlunit.Diff; -import org.custommonkey.xmlunit.XMLUnit; -import org.junit.Before; import org.junit.Test; -import org.opendaylight.mdsal.dom.api.DOMRpcResult; import org.opendaylight.netconf.api.messages.NetconfMessage; import org.opendaylight.netconf.api.xml.XmlUtil; import org.opendaylight.netconf.client.mdsal.AbstractBaseSchemasTest; @@ -26,98 +21,86 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.re import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.edit.config.input.target.config.target.Candidate; import org.opendaylight.yangtools.yang.common.Empty; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode; -import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode; import org.opendaylight.yangtools.yang.data.impl.schema.Builders; -import org.w3c.dom.Document; +import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.w3c.dom.Element; +import org.xmlunit.builder.DiffBuilder; public class BaseRpcSchemalessTransformerTest extends AbstractBaseSchemasTest { - - static { - XMLUnit.setIgnoreWhitespace(true); - XMLUnit.setIgnoreAttributeOrder(true); - } - - private static final String EXP_RPC = "\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " fred\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + "\n"; - - BaseRpcSchemalessTransformer transformer; - - @Before - public void setUp() throws Exception { - transformer = new BaseRpcSchemalessTransformer(BASE_SCHEMAS, new MessageCounter()); - } + private final BaseRpcSchemalessTransformer transformer = + new BaseRpcSchemalessTransformer(BASE_SCHEMAS, new MessageCounter()); @Test public void toRpcRequest() throws Exception { - final Document doc = - XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/schemaless/edit-config/container.xml")); - final DOMSourceAnyxmlNode xml = Builders.anyXmlBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier( - NetconfMessageTransformUtil.NETCONF_CONFIG_QNAME)) - .withValue(new DOMSource(doc.getDocumentElement())) - .build(); - final ChoiceNode editContent = Builders.choiceBuilder() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(EditContent.QNAME)) - .withChild(xml) - .build(); - final ChoiceNode candidate = Builders.choiceBuilder().withNodeIdentifier( - new YangInstanceIdentifier.NodeIdentifier(ConfigTarget.QNAME)) - .withChild(Builders.leafBuilder().withNodeIdentifier( - new YangInstanceIdentifier.NodeIdentifier(Candidate.QNAME)) - .withValue(Empty.value()).build()) - .build(); - final ContainerNode target = Builders.containerBuilder() - .withNodeIdentifier( - new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_TARGET_QNAME)) - .withChild(candidate) - .build(); - final ContainerNode editConfig = Builders.containerBuilder() - .withNodeIdentifier( - new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)) - .withChild(editContent) - .withChild(target) - .build(); - final NetconfMessage msg = transformer.toRpcRequest( - NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, editConfig); - final Diff diff = XMLUnit.compareXML(EXP_RPC, XmlUtil.toString(msg.getDocument())); - assertTrue(diff.toString(), diff.similar()); + final var msg = transformer.toRpcRequest(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME, + Builders.containerBuilder() + .withNodeIdentifier(new NodeIdentifier(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)) + .withChild(Builders.choiceBuilder() + .withNodeIdentifier(new NodeIdentifier(EditContent.QNAME)) + .withChild(Builders.anyXmlBuilder() + .withNodeIdentifier(new NodeIdentifier(NetconfMessageTransformUtil.NETCONF_CONFIG_QNAME)) + .withValue(new DOMSource(XmlUtil.readXmlToDocument( + BaseRpcSchemalessTransformerTest.class.getResourceAsStream( + "/schemaless/edit-config/container.xml")) + .getDocumentElement())) + .build()) + .build()) + .withChild(Builders.containerBuilder() + .withNodeIdentifier(new NodeIdentifier(NetconfMessageTransformUtil.NETCONF_TARGET_QNAME)) + .withChild(Builders.choiceBuilder() + .withNodeIdentifier(new NodeIdentifier(ConfigTarget.QNAME)) + .withChild(ImmutableNodes.leafNode(Candidate.QNAME, Empty.value())) + .build()) + .build()) + .build()); + + final var diff = DiffBuilder.compare(""" + + + + + + + + + + fred + + + + + + """) + .withTest(msg.getDocument()) + .ignoreWhitespace() + .checkForIdentical() + .build(); + assertFalse(diff.toString(), diff.hasDifferences()); } @Test public void toRpcResult() throws Exception { - final Document doc = XmlUtil.readXmlToDocument( + final var doc = XmlUtil.readXmlToDocument( ""); - final InputStream stream = getClass().getResourceAsStream("/schemaless/get-config/container.xml"); - final Element dataElement = XmlUtil.readXmlToElement(stream); - final Element element = (Element) doc.importNode(dataElement, true); + final var dataElement = XmlUtil.readXmlToElement( + BaseRpcSchemalessTransformerTest.class.getResourceAsStream("/schemaless/get-config/container.xml")); + final var element = (Element) doc.importNode(dataElement, true); doc.getDocumentElement().appendChild(element); - final NetconfMessage msg = new NetconfMessage(doc); - final DOMRpcResult result = transformer.toRpcResult(RpcResultBuilder.success(msg).build(), + final var msg = new NetconfMessage(doc); + final var result = transformer.toRpcResult(RpcResultBuilder.success(msg).build(), NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME); assertNotNull(result.value()); - final ContainerNode rpcReply = result.value(); + final var rpcReply = result.value(); assertEquals(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME, rpcReply.name().getNodeType()); - final DOMSourceAnyxmlNode data = - (DOMSourceAnyxmlNode) rpcReply.getChildByArg(NetconfMessageTransformUtil.NETCONF_DATA_NODEID); - final Diff diff = XMLUnit.compareXML(dataElement.getOwnerDocument(), (Document) data.body().getNode()); - assertTrue(diff.toString(), diff.similar()); + final var data = (DOMSourceAnyxmlNode) rpcReply.getChildByArg(NetconfMessageTransformUtil.NETCONF_DATA_NODEID); + + final var diff = DiffBuilder.compare(dataElement.getOwnerDocument()) + .withTest(data.body().getNode()) + .ignoreWhitespace() + .checkForIdentical() + .build(); + assertFalse(diff.toString(), diff.hasDifferences()); } } -- 2.36.6