Fix CS warnings in sal-clustering-commons and enable enforcement
[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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeGetter;
19 import org.opendaylight.controller.cluster.datastore.node.utils.NormalizedNodeNavigator;
20 import org.opendaylight.controller.cluster.datastore.node.utils.PathUtils;
21 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
22 import org.opendaylight.controller.cluster.datastore.util.TestModel;
23 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
24 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class NormalizedNodeToNodeCodecTest {
33     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeToNodeCodecTest.class);
34
35     private SchemaContext schemaContext;
36
37     @Before
38     public void setUp() {
39         schemaContext = TestModel.createTestContext();
40         assertNotNull("Schema context must not be null.", schemaContext);
41     }
42
43     private static YangInstanceIdentifier instanceIdentifierFromString(String str) {
44         return PathUtils.toYangInstanceIdentifier(str);
45     }
46
47     @Test
48     public void testNormalizeNodeAttributesToProtoBuffNode() {
49         final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
50         String id = "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"
51             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list"
52             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)outer-list["
53             + "{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=2}]"
54             + "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
55
56         NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
57         new NormalizedNodeNavigator(normalizedNodeGetter).navigate(PathUtils.toString(YangInstanceIdentifier.EMPTY),
58                 documentOne);
59
60         // Validate the value of id can be retrieved from the normalized node
61         NormalizedNode<?, ?> output = normalizedNodeGetter.getOutput();
62         assertNotNull(output);
63
64         NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec();
65         long start = System.currentTimeMillis();
66         Container container = codec.encode(output);
67         long end = System.currentTimeMillis();
68
69         LOG.info("Time taken 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 = codec.decode(node);
79         end = System.currentTimeMillis();
80
81         LOG.info("Time taken to decode: {}", end - start);
82
83         assertEquals(normalizedNode.getValue().toString(), output.getValue().toString());
84     }
85
86     @Test
87     public void testThatANormalizedNodeToProtoBuffNodeEncodeDecode() throws Exception {
88         final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
89
90         final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec = new NormalizedNodeToNodeCodec();
91
92         Container container = normalizedNodeToNodeCodec.encode(documentOne);
93
94         final NormalizedNode<?, ?> decode = normalizedNodeToNodeCodec.decode(container.getNormalizedNode());
95         assertNotNull(decode);
96
97         // let us ensure that the return decode normalized node encode returns
98         // same container
99         Container containerResult = normalizedNodeToNodeCodec.encode(decode);
100
101         // check first level children are proper
102         List<Node> childrenResult = containerResult.getNormalizedNode().getChildList();
103         List<Node> childrenOriginal = container.getNormalizedNode().getChildList();
104
105         LOG.info("\n-------------------------------------------------\n" + childrenOriginal
106                 + "\n-------------------------------------------------\n" + childrenResult);
107
108         boolean found;
109         for (Node resultChild : childrenResult) {
110             found = false;
111             for (Node originalChild : childrenOriginal) {
112
113                 YangInstanceIdentifier.PathArgument result = NormalizedNodeSerializer
114                         .deSerialize(containerResult.getNormalizedNode(), resultChild.getPathArgument());
115
116                 YangInstanceIdentifier.PathArgument original = NormalizedNodeSerializer
117                         .deSerialize(container.getNormalizedNode(), originalChild.getPathArgument());
118
119                 if (original.equals(result) && resultChild.getIntType() == resultChild.getIntType()) {
120                     found = true;
121                     break;
122                 }
123             }
124             assertTrue(found);
125         }
126
127     }
128
129     @Test
130     public void addAugmentations() {
131         MapEntryNode uno = TestModel.createAugmentedListEntry(1, "Uno");
132
133         NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec();
134
135         Container encode = codec.encode(uno);
136
137         LOG.info(encode.getNormalizedNode().toString());
138
139         codec.decode(encode.getNormalizedNode());
140     }
141 }