BUG-1092: adjust to YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / sal-protocolbuffer-encoding / src / test / java / org / opendaylight / controller / cluster / datastore / node / NormalizedNodeToNodeCodecTest.java
1 /*
2  * Copyright (c) 2014 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 junit.framework.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Container;
15 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages.Node;
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.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import static junit.framework.Assert.assertEquals;
28 import static junit.framework.Assert.assertNotNull;
29
30 public class NormalizedNodeToNodeCodecTest {
31
32
33
34     private SchemaContext schemaContext;
35
36     @Before
37     public void setUp(){
38         schemaContext = TestModel.createTestContext();
39         assertNotNull("Schema context must not be null.", schemaContext);
40     }
41
42     private YangInstanceIdentifier instanceIdentifierFromString(String s){
43
44         String[] ids = s.split("/");
45
46         List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
47         for(String nodeId : ids){
48             if(!"".equals(nodeId)) {
49                 pathArguments.add(NodeIdentifierFactory.getArgument(nodeId));
50             }
51         }
52         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.create(pathArguments);
53         return instanceIdentifier;
54     }
55
56
57     @Test
58     public void testNormalizeNodeAttributesToProtoBuffNode(){
59         final NormalizedNode<?, ?> documentOne = TestModel.createTestContainer();
60         String id = "/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test/(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)outer-list[{(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id=2}]/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)id";
61
62         NormalizedNodeGetter normalizedNodeGetter = new NormalizedNodeGetter(id);
63         new NormalizedNodeNavigator(normalizedNodeGetter).navigate(
64             YangInstanceIdentifier.builder().build().toString(), documentOne);
65
66         // Validate the value of id can be retrieved from the normalized node
67         NormalizedNode output = normalizedNodeGetter.getOutput();
68         assertNotNull(output);
69
70
71       NormalizedNodeToNodeCodec codec = new NormalizedNodeToNodeCodec(schemaContext);
72       Container container = codec.encode(instanceIdentifierFromString(id),output);
73
74        assertNotNull(container);
75        assertEquals(id, container.getParentPath()+"/"+container.getNormalizedNode().getPath()) ;
76
77       // Decode the normalized node from the ProtocolBuffer form
78       //first get the node representation of normalized node
79       final Node node = container.getNormalizedNode();
80
81       NormalizedNode<?,?> normalizedNode = codec.decode(instanceIdentifierFromString(id),node);
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
91         final NormalizedNodeToNodeCodec normalizedNodeToNodeCodec = new NormalizedNodeToNodeCodec(schemaContext);
92
93         Container container = normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder().build(), documentOne);
94
95
96         final NormalizedNode<?, ?> decode = normalizedNodeToNodeCodec.decode(instanceIdentifierFromString("/(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test"),container.getNormalizedNode());
97         assertNotNull(decode != null);
98
99         //let us ensure that the return decode normalized node encode returns same container
100         Container containerResult =  normalizedNodeToNodeCodec.encode(YangInstanceIdentifier.builder().build(), decode);
101
102         assertEquals(container.getParentPath(),containerResult.getParentPath());
103         assertEquals(container.getNormalizedNode().getChildCount(),container.getNormalizedNode().getChildCount());
104
105         Assert.assertEquals(containerResult.getNormalizedNode().getChildCount(),container.getNormalizedNode().getChildCount());
106
107         //check first level children are proper
108         List<Node>childrenResult = containerResult.getNormalizedNode().getChildList();
109         List<Node>childrenOriginal = container.getNormalizedNode().getChildList();
110        boolean bFound;
111         for(Node resultChild: childrenResult){
112            bFound = false;
113           for(Node originalChild:childrenOriginal){
114             if(originalChild.getPath().equals(resultChild.getPath())
115                 && resultChild.getType().equals(resultChild.getType())){
116                bFound=true;
117                break;
118             }
119           }
120           Assert.assertTrue(bFound);
121         }
122
123
124     }
125
126 }