Fix usage of StringBuilder when getting parent path
[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.NodeIdentifierFactory;
17 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
18 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
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.ArrayList;
28 import java.util.List;
29
30 import static junit.framework.Assert.assertEquals;
31 import static junit.framework.Assert.assertNotNull;
32
33 public class NormalizedNodeToNodeCodecTest {
34
35
36
37   private SchemaContext schemaContext;
38
39   @Before
40   public void setUp() {
41     schemaContext = TestModel.createTestContext();
42     assertNotNull("Schema context must not be null.", schemaContext);
43   }
44
45   private YangInstanceIdentifier instanceIdentifierFromString(String s) {
46
47     String[] ids = s.split("/");
48
49     List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
50     for (String nodeId : ids) {
51       if (!"".equals(nodeId)) {
52         pathArguments.add(NodeIdentifierFactory.getArgument(nodeId));
53       }
54     }
55     final YangInstanceIdentifier instanceIdentifier =
56         YangInstanceIdentifier.create(pathArguments);
57     return instanceIdentifier;
58   }
59
60
61   @Test
62   public void testNormalizeNodeAttributesToProtoBuffNode() {
63     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
64     String id =
65         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
66             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
67             + "/(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}]"
68             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
69
70     NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
71     new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
72         YangInstanceIdentifier.builder().build().toString(), documentOne);
73
74     // Validate the value of id can be retrieved from the normalized node
75     NormalizedNode output = normalizedNodeGetter.getOutput();
76     assertNotNull(output);
77
78
79     NormalizedNodeToNodeCodec codec =
80         new NormalizedNodeToNodeCodec(schemaContext);
81     long start = System.currentTimeMillis();
82     Container container =
83         codec.encode(instanceIdentifierFromString(id), output);
84     long end = System.currentTimeMillis();
85
86     System.out.println("Timetaken to encode :"+(end-start));
87
88     assertNotNull(container);
89     assertEquals(id, container.getParentPath() + "/"
90         + container.getNormalizedNode().getPath());
91
92     // Decode the normalized node from the ProtocolBuffer form
93     // first get the node representation of normalized node
94     final Node node = container.getNormalizedNode();
95
96     start = System.currentTimeMillis();
97     NormalizedNode<?, ?> normalizedNode =
98         codec.decode(instanceIdentifierFromString(id), node);
99     end = System.currentTimeMillis();
100
101     System.out.println("Timetaken to decode :"+(end-start));
102
103     assertEquals(normalizedNode.getValue().toString(), output.getValue()
104         .toString());
105   }
106
107   @Test
108   public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode()
109       throws Exception {
110     final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
111
112     final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec =
113         new NormalizedNodeToNodeCodec(schemaContext);
114
115     Container container =
116         normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
117             .build(), documentOne);
118
119
120     final NormalizedNode<?, ?> decode =
121         normalizedNodeToNodeCodec
122             .decode(
123                 instanceIdentifierFromString("/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"),
124                 container.getNormalizedNode());
125     assertNotNull(decode != null);
126
127     // let us ensure that the return decode normalized node encode returns same container
128     Container containerResult =
129         normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder()
130             .build(), decode);
131
132     assertEquals(container.getParentPath(), containerResult.getParentPath());
133     assertEquals(container.getNormalizedNode().getChildCount(), container
134         .getNormalizedNode().getChildCount());
135
136     Assert.assertEquals(containerResult.getNormalizedNode().getChildCount(),
137         container.getNormalizedNode().getChildCount());
138
139     // check first level children are proper
140     List<Node> childrenResult =
141         containerResult.getNormalizedNode().getChildList();
142     List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
143
144     System.out.println("-------------------------------------------------");
145
146     System.out.println(childrenOriginal.toString());
147
148     System.out.println("-------------------------------------------------");
149
150     System.out.println(childrenResult.toString());
151
152     boolean bFound;
153     for (Node resultChild : childrenResult) {
154       bFound = false;
155       for (Node originalChild : childrenOriginal) {
156         if (originalChild.getPath().equals(resultChild.getPath())
157             && resultChild.getType().equals(resultChild.getType())) {
158           bFound = true;
159           break;
160         }
161       }
162       Assert.assertTrue(bFound);
163     }
164
165   }
166
167   @Test
168   public void addAugmentations() {
169     String stringId =
170         "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
171             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)augmented-list"
172             + "/(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}]";
173
174     YangInstanceIdentifier identifier = instanceIdentifierFromString(stringId);
175
176     MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
177
178     NormalizedNodeToNodeCodec codec =
179         new NormalizedNodeToNodeCodec(schemaContext);
180
181     Container encode = codec.encode(identifier, uno);
182
183     System.out.println(encode.getNormalizedNode());
184
185     codec.decode(identifier, encode.getNormalizedNode());
186   }
187
188 }

©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.