Merge "BUG 1082 Migrate sal-rest-connector to Async Data Broker API"
[controller.git] / opendaylight / md-sal / sal-protocolbuffer-encoding / src / test / java / org / opendaylight / controller / cluster / datastore / util / NormalizedNodeXmlConverterTest.java
index 45e3ea5be32f6a62f8a394cd2afb0fc5680ff06a..853b3e264bdc9b4b58eed7e1ca95363eaa64e6be 100644 (file)
@@ -1,9 +1,11 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
  *
- * This program and the accompanying materials are made available under the terms of the Eclipse
- * Public License v1.0 which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
+ *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ *  This program and the accompanying materials are made available under the
+ *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ *  and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
  */
 package org.opendaylight.controller.cluster.datastore.util;
 
@@ -14,9 +16,9 @@ import com.google.common.collect.Sets;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
 import org.junit.Test;
-import org.opendaylight.controller.cluster.datastore.common.SimpleNormalizedNodeMessage;
+import org.opendaylight.controller.protobuff.messages.common.SimpleNormalizedNodeMessage;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
@@ -62,6 +64,7 @@ import java.net.URI;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
@@ -70,8 +73,8 @@ import java.util.Set;
 
 
 /**
- * Two of the testcases in the yangtools/yang-data-impl are leveraged (with  modification) to
- * create the serialization of NormalizedNode using the ProtocolBuffer
+ * Two of the testcases in the yangtools/yang-data-impl are leveraged (with modification) to create
+ * the serialization of NormalizedNode using the ProtocolBuffer
  *
  * @syedbahm
  *
@@ -96,8 +99,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 +114,14 @@ public class NormalizedNodeXmlConverterTest {
         + childNodeName);
   }
 
-  static DataSchemaNode findChildNode(Set<DataSchemaNode> children, String name) {
+  static DataSchemaNode findChildNode(
+      final Collection<DataSchemaNode> children, final String name) {
     List<DataNodeContainer> 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) {
@@ -134,27 +139,27 @@ public class NormalizedNodeXmlConverterTest {
     return null;
   }
 
-  private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(
-      String localName) {
-    return new InstanceIdentifier.NodeIdentifier(new QName(
+  public static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(
+      final String localName) {
+    return new YangInstanceIdentifier.NodeIdentifier(QName.create(
         URI.create(NAMESPACE), revision, localName));
   }
 
-  public static InstanceIdentifier.AugmentationIdentifier getAugmentIdentifier(
-      String... childNames) {
+  public static YangInstanceIdentifier.AugmentationIdentifier getAugmentIdentifier(
+      final String... childNames) {
     Set<QName> qn = Sets.newHashSet();
 
     for (String childName : childNames) {
       qn.add(getNodeIdentifier(childName).getNodeType());
     }
 
-    return new InstanceIdentifier.AugmentationIdentifier(qn);
+    return new YangInstanceIdentifier.AugmentationIdentifier(qn);
   }
 
 
-  private static ContainerNode augmentChoiceExpectedNode() {
+  public static ContainerNode augmentChoiceExpectedNode() {
 
-    DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> b =
+    DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> b =
         Builders.containerBuilder();
     b.withNodeIdentifier(getNodeIdentifier("container"));
 
@@ -233,8 +238,8 @@ public class NormalizedNodeXmlConverterTest {
 
 
 
-  public void init(String yangPath, String xmlPath, ContainerNode expectedNode)
-      throws Exception {
+  public void init(final String yangPath, final String xmlPath,
+      final ContainerNode expectedNode) throws Exception {
     SchemaContext schema = parseTestSchema(yangPath);
     this.xmlPath = xmlPath;
     this.containerNode =
@@ -242,7 +247,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 +273,9 @@ public class NormalizedNodeXmlConverterTest {
             .parse(Collections.singletonList(doc.getDocumentElement()),
                 containerNode);
 
-    if (expectedNode != null)
+    if (expectedNode != null) {
       junit.framework.Assert.assertEquals(expectedNode, built);
+    }
 
     logger.info("{}", built);
 
@@ -287,14 +293,12 @@ 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() {
-    DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> b =
+    DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> b =
         Builders.containerBuilder();
     b.withNodeIdentifier(getNodeIdentifier("container"));
 
@@ -304,11 +308,11 @@ public class NormalizedNodeXmlConverterTest {
     Map<QName, Object> predicates = Maps.newHashMap();
     predicates.put(getNodeIdentifier("uint32InList").getNodeType(), 3L);
 
-    DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> list1Builder =
+    DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> list1Builder =
         Builders.mapEntryBuilder().withNodeIdentifier(
-            new InstanceIdentifier.NodeIdentifierWithPredicates(
+            new YangInstanceIdentifier.NodeIdentifierWithPredicates(
                 getNodeIdentifier("list").getNodeType(), predicates));
-    NormalizedNodeBuilder<InstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> uint32InListBuilder =
+    NormalizedNodeBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> uint32InListBuilder =
         Builders.leafBuilder().withNodeIdentifier(
             getNodeIdentifier("uint32InList"));
 
@@ -317,7 +321,7 @@ public class NormalizedNodeXmlConverterTest {
     listBuilder.withChild(list1Builder.build());
     b.withChild(listBuilder.build());
 
-    NormalizedNodeBuilder<InstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> booleanBuilder =
+    NormalizedNodeBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> booleanBuilder =
         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("boolean"));
     booleanBuilder.withValue(false);
     b.withChild(booleanBuilder.build());
@@ -326,10 +330,10 @@ public class NormalizedNodeXmlConverterTest {
         Builders.leafSetBuilder().withNodeIdentifier(
             getNodeIdentifier("leafList"));
 
-    NormalizedNodeBuilder<InstanceIdentifier.NodeWithValue, Object, LeafSetEntryNode<Object>> leafList1Builder =
+    NormalizedNodeBuilder<YangInstanceIdentifier.NodeWithValue, Object, LeafSetEntryNode<Object>> leafList1Builder =
         Builders.leafSetEntryBuilder().withNodeIdentifier(
-            new InstanceIdentifier.NodeWithValue(getNodeIdentifier("leafList")
-                .getNodeType(), "a"));
+            new YangInstanceIdentifier.NodeWithValue(getNodeIdentifier(
+                "leafList").getNodeType(), "a"));
 
     leafList1Builder.withValue("a");
 
@@ -353,8 +357,9 @@ public class NormalizedNodeXmlConverterTest {
             .parse(Collections.singletonList(doc.getDocumentElement()),
                 containerNode);
 
-    if (expectedNode != null)
+    if (expectedNode != null) {
       junit.framework.Assert.assertEquals(expectedNode, built);
+    }
 
     logger.info("{}", built);
 
@@ -372,14 +377,12 @@ 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.
@@ -479,24 +480,4 @@ public class NormalizedNodeXmlConverterTest {
 
   }
 
-  @Test
-  public void testInMemoryTestModelProtoBuffEncoding() throws Exception {
-
-    SimpleNormalizedNodeMessage.NormalizedNodeXml nnXml =
-        EncoderDecoderUtil.encode(parseTestSchema("/odl-datastore-test.yang"),
-            TestModel.createFamily());
-
-    Document convertedDoc =
-        EncoderDecoderUtil.factory.newDocumentBuilder().parse(
-            new ByteArrayInputStream(nnXml.getXmlString().getBytes("utf-8")));
-    System.out.println(toString(convertedDoc.getDocumentElement()));
-
-    // now we will try to convert xml back to normalize node.
-    ContainerNode cn =
-        (ContainerNode) EncoderDecoderUtil.decode(
-            parseTestSchema("/odl-datastore-test.yang"), nnXml);
-    junit.framework.Assert.assertEquals(TestModel.createFamily(), cn);
-
-
-  }
 }