Merge "BUG-1601 Fix null pointer in netconf connector for empty schemas node"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / NormalizedNodeToNodeCodecTest.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore.node;
12
13 import junit.framework.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
17 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
18 import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
19 import org.opendaylight.controller.cluster.datastore.util.TestModel;
20 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
21 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26
27 import java.util.List;
28
29 import static junit.framework.Assert.assertEquals;
30 import static junit.framework.Assert.assertNotNull;
31
32 public class NormalizedNodeToNodeCodecTest {
33
34
35
36   private SchemaContext schemaContext;
37
38   @Before
39   public void setUp() {
40     schemaContext = TestModel.createTestContext();
41     assertNotNull("Schema context must not be null.", schemaContext);
42   }
43
44   private YangInstanceIdentifier instanceIdentifierFromString(String s) {
45       return PathUtils.toYangInstanceIdentifier(s);
46   }
47
48   @Test
49   public void testNormalizeNodeAttributesToProtoBuffNode() {
50     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
51     String id =
52         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
53             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
54             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=2}]"
55             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
56
57     NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
58     new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
59         PathUtils.toString(YangInstanceIdentifier.builder().build()), documentOne);
60
61     // Validate the value of id can be retrieved from the normalized node
62     NormalizedNode output = normalizedNodeGetter.getOutput();
63     assertNotNull(output);
64
65
66     NormalizedNodeToNodeCodec codec =
67         new NormalizedNodeToNodeCodec(schemaContext);
68     long start = System.currentTimeMillis();
69     Container container =
70         codec.encode(instanceIdentifierFromString(id), output);
71     long end = System.currentTimeMillis();
72
73     System.out.println("Timetaken to encode :"+(end-start));
74
75     assertNotNull(container);
76     assertEquals(id, container.getParentPath() + "/"
77         + container.getNormalizedNode().getPath());
78
79     // Decode the normalized node from the ProtocolBuffer form
80     // first get the node representation of normalized node
81     final Node node = container.getNormalizedNode();
82
83     start = System.currentTimeMillis();
84     NormalizedNode<?, ?> normalizedNode =
85         codec.decode(instanceIdentifierFromString(id), node);
86     end = System.currentTimeMillis();
87
88     System.out.println("Timetaken to decode :"+(end-start));
89
90     assertEquals(normalizedNode.getValue().toString(), output.getValue()
91         .toString());
92   }
93
94   @Test
95   public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
96       throws Exception {
97     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
98
99     final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
100         new NormalizedNodeToNodeCodec(schemaContext);
101
102     Container container =
103         normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
104             .build(), documentOne);
105
106
107     final NormalizedNode<?, ?> decode =
108         normalizedNodeToNodeCodec
109             .decode(
110                 instanceIdentifierFromString("/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"),
111                 container.getNormalizedNode());
112     assertNotNull(decode != null);
113
114     // let us ensure that the return decode normalized node encode returns same container
115     Container containerResult =
116         normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
117             .build(), decode);
118
119     assertEquals(container.getParentPath(), containerResult.getParentPath());
120     assertEquals(container.getNormalizedNode().getChildCount(), container
121         .getNormalizedNode().getChildCount());
122
123     Assert.assertEquals(containerResult.getNormalizedNode().getChildCount(),
124         container.getNormalizedNode().getChildCount());
125
126     // check first level children are proper
127     List<Node> childrenResult =
128         containerResult.getNormalizedNode().getChildList();
129     List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
130
131     System.out.println("-------------------------------------------------");
132
133     System.out.println(childrenOriginal.toString());
134
135     System.out.println("-------------------------------------------------");
136
137     System.out.println(childrenResult.toString());
138
139     boolean bFound;
140     for (Node resultChild : childrenResult) {
141       bFound = false;
142       for (Node originalChild : childrenOriginal) {
143         if (originalChild.getPath().equals(resultChild.getPath())
144             && resultChild.getType().equals(resultChild.getType())) {
145           bFound = true;
146           break;
147         }
148       }
149       Assert.assertTrue(bFound);
150     }
151
152   }
153
154   @Test
155   public void addAugmentations() {
156     String stringId =
157         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
158             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
159             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=1}]";
160
161     YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
162
163     MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
164
165     NormalizedNodeToNodeCodec codec =
166         new NormalizedNodeToNodeCodec(schemaContext);
167
168     Container encode = codec.encode(identifier, uno);
169
170     System.out.println(encode.getNormalizedNode());
171
172     codec.decode(identifier, encode.getNormalizedNode());
173   }
174
175 }