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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.