X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Ftest%2FCnSnToXmlAndJsonInstanceIdentifierTest.java;h=18b5ce490ff137f7da978052a09819a6a5b4e1d1;hp=9723af7671e41dc75fd5de5521bee3fa6606817a;hb=749b6d5fdcba6cf6043d771f4cfc844e43dc7521;hpb=6d607f131104ec8b8096cb6aea015674ba1e466c diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CnSnToXmlAndJsonInstanceIdentifierTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CnSnToXmlAndJsonInstanceIdentifierTest.java index 9723af7671..18b5ce490f 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CnSnToXmlAndJsonInstanceIdentifierTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CnSnToXmlAndJsonInstanceIdentifierTest.java @@ -32,41 +32,83 @@ import org.junit.Ignore; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider; -import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper; -import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; +import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode; +import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode; +import org.opendaylight.yangtools.yang.data.impl.NodeFactory; public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialize() { - dataLoad("/instanceidentifier/yang", 3, "instance-identifier-module", "cont"); + dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont"); } @Test - public void saveCnSnToXml() throws WebApplicationException, IOException, URISyntaxException, XMLStreamException { - CompositeNode cnSn = prepareCnSn(); + public void saveCnSnToXmlTest() throws WebApplicationException, IOException, URISyntaxException, XMLStreamException { + CompositeNode cnSn = prepareCnSn(createInstanceIdentifier()); String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode, StructuredDataToXmlProvider.INSTANCE); - validateXmlOutput(output); + //uncomment for debug // System.out.println(output); + validateXmlOutput(output); } @Ignore @Test - public void saveCnSnToJson() throws WebApplicationException, IOException, URISyntaxException { - CompositeNode cnSn = prepareCnSn(); + public void saveCnSnWithLeafListInstIdentifierToXmlTest() throws WebApplicationException, IOException, + URISyntaxException, XMLStreamException { + CompositeNode cnSn = prepareCnSn(createInstanceIdentifierWithLeafList()); + String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode, + StructuredDataToXmlProvider.INSTANCE); + //uncomment for debug + // System.out.println(output); + validateXmlOutputWithLeafList(output); + } + + @Test + public void saveCnSnToJsonTest() throws WebApplicationException, IOException, URISyntaxException { + CompositeNode cnSn = prepareCnSn(createInstanceIdentifier()); String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode, StructuredDataToJsonProvider.INSTANCE); - assertTrue(output - .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11[augment-module:keyvalue111=\\\"value1\\\"][augment-module:keyvalue112=\\\"value2\\\"]/augment-augment-module:lf112\"")); + boolean strInOutput = false; + strInOutput = output + .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11[augment-module:keyvalue111=\\\"value1\\\"][augment-module:keyvalue112=\\\"value2\\\"]/augment-augment-module:lf112\""); + + if (!strInOutput) { + strInOutput = output + .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11[augment-module:keyvalue111='value1'][augment-module:keyvalue112='value2']/augment-augment-module:lf112\""); + } + //uncomment for debug // System.out.println(output); + assertTrue(strInOutput); + } + + + @Test + public void saveCnSnWithLeafListInstIdentifierToJsonTest() throws WebApplicationException, IOException, + URISyntaxException { + CompositeNode cnSn = prepareCnSn(createInstanceIdentifierWithLeafList()); + String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode, + StructuredDataToJsonProvider.INSTANCE); + //uncomment for debug + // System.out.println(output); + boolean strInOutput = false; + strInOutput = output + .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.='lflst11_1']\""); + if (!strInOutput) { + strInOutput = output + .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\\"lflst11_1\\\"]\""); + } + + assertTrue(strInOutput); } private void validateXmlOutput(String xml) throws XMLStreamException { @@ -106,21 +148,66 @@ public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSch + ":lst11[" + aModulePrefix + ":keyvalue111='value1'][" + aModulePrefix + ":keyvalue112='value2']/" + aaModulePrefix + ":lf112"; -// System.out.println(xml); assertTrue(xml.contains(instanceIdentifierValue)); } - private CompositeNode prepareCnSn() throws URISyntaxException { - CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("instance:identifier:module"), "cont"); - CompositeNodeWrapper cont1 = new CompositeNodeWrapper(new URI("instance:identifier:module"), "cont1"); - CompositeNodeWrapper lst11 = new CompositeNodeWrapper(new URI("augment:module"), "lst11"); - InstanceIdentifier instanceIdentifier = createInstanceIdentifier(); - SimpleNodeWrapper lf111 = new SimpleNodeWrapper(new URI("augment:augment:module"), "lf111", instanceIdentifier); + private void validateXmlOutputWithLeafList(String xml) throws XMLStreamException { + XMLInputFactory xmlInFactory = XMLInputFactory.newInstance(); + XMLEventReader eventReader; - lst11.addValue(lf111); - cont1.addValue(lst11); - cont.addValue(cont1); + eventReader = xmlInFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes())); + String aModuleLfLstPrefix = null; + String iiModulePrefix = null; + while (eventReader.hasNext()) { + XMLEvent nextEvent = eventReader.nextEvent(); + if (nextEvent.isStartElement()) { + StartElement startElement = (StartElement) nextEvent; + if (startElement.getName().getLocalPart().equals("lf111")) { + Iterator prefixes = startElement.getNamespaceContext().getPrefixes("augment:module:leaf:list"); + + while (prefixes.hasNext() && aModuleLfLstPrefix == null) { + String prefix = (String) prefixes.next(); + if (!prefix.isEmpty()) { + aModuleLfLstPrefix = prefix; + } + } + iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module"); + break; + } + } + } + + assertNotNull(aModuleLfLstPrefix); + assertNotNull(iiModulePrefix); + + String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/" + + aModuleLfLstPrefix + ":lflst11[.='lflst11_1']"; + + assertTrue(xml.contains(instanceIdentifierValue)); + + } + + private CompositeNode prepareCnSn(InstanceIdentifier instanceIdentifier) throws URISyntaxException { + MutableCompositeNode cont = NodeFactory.createMutableCompositeNode( + TestUtils.buildQName("cont", "instance:identifier:module", "2014-01-17"), null, null,null,null); + MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode( + TestUtils.buildQName("cont1", "instance:identifier:module", "2014-01-17"), cont, null,null,null); + MutableCompositeNode lst11 = NodeFactory.createMutableCompositeNode( + TestUtils.buildQName("lst11", "augment:module", "2014-01-17"), cont1, null,null,null); + + MutableSimpleNode lf111 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111", "augment:augment:module", "2014-01-17"), + lst11, instanceIdentifier,null,null); + + + lst11.getChildren().add(lf111); + lst11.init(); + + cont1.getChildren().add(lst11); + cont1.init(); + + cont.getChildren().add(cont1); + cont.init(); return cont; } @@ -142,4 +229,13 @@ public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSch return new InstanceIdentifier(pathArguments); } + private InstanceIdentifier createInstanceIdentifierWithLeafList() throws URISyntaxException { + List pathArguments = new ArrayList<>(); + pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont"))); + pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont1"))); + pathArguments.add(new NodeWithValue(new QName(new URI("augment:module:leaf:list"), "lflst11"), "lflst11_1")); + + return new InstanceIdentifier(pathArguments); + } + }