Merge "Fix missing dependencies in opendaylight-startup archetype"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / util / InstanceIdentifierUtilsTest.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.util;
12
13 import com.google.common.collect.ImmutableSet;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.QNameDeSerializationContext;
17 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.QNameDeSerializationContextImpl;
18 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.QNameSerializationContextImpl;
19 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 public class InstanceIdentifierUtilsTest {
27
28     private static QName TEST_QNAME = QName
29             .create("(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test");
30     private static QName NODE_WITH_VALUE_QNAME = QName
31             .create("(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)value");
32     private static QName NODE_WITH_PREDICATES_QNAME = QName
33             .create("(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)pred");
34     private static QName NAME_QNAME = QName
35             .create("(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)name");
36
37     @Test
38     public void testSerializationOfNodeIdentifier() {
39         YangInstanceIdentifier.PathArgument p1 = new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME);
40
41         List<YangInstanceIdentifier.PathArgument> arguments = new ArrayList<>();
42
43         arguments.add(p1);
44
45         YangInstanceIdentifier expected = YangInstanceIdentifier.create(arguments);
46
47         NormalizedNodeMessages.InstanceIdentifier instanceIdentifier =
48                 InstanceIdentifierUtils.toSerializable(expected);
49
50         YangInstanceIdentifier actual = InstanceIdentifierUtils.fromSerializable(instanceIdentifier);
51
52         Assert.assertEquals(expected.getLastPathArgument(), actual.getLastPathArgument());
53     }
54
55     @Test
56     public void testSerializationOfNodeWithValue() {
57
58         withValue((short) 1);
59         withValue((long) 2);
60         withValue(3);
61         withValue(true);
62
63     }
64
65     private void withValue(Object value) {
66         YangInstanceIdentifier.PathArgument p1 = new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME);
67
68         YangInstanceIdentifier.PathArgument p2 =
69                 new YangInstanceIdentifier.NodeWithValue(NODE_WITH_VALUE_QNAME, value);
70
71         List<YangInstanceIdentifier.PathArgument> arguments = new ArrayList<>();
72
73         arguments.add(p1);
74         arguments.add(p2);
75
76         YangInstanceIdentifier expected = YangInstanceIdentifier.create(arguments);
77
78         NormalizedNodeMessages.InstanceIdentifier instanceIdentifier =
79                 InstanceIdentifierUtils.toSerializable(expected);
80
81         YangInstanceIdentifier actual = InstanceIdentifierUtils.fromSerializable(instanceIdentifier);
82
83         Assert.assertEquals(expected.getLastPathArgument(), actual.getLastPathArgument());
84     }
85
86     @Test
87     public void testSerializationOfNodeIdentifierWithPredicates() {
88
89         withPredicates((short) 1);
90         withPredicates((long) 2);
91         withPredicates(3);
92         withPredicates(true);
93
94     }
95
96     private void withPredicates(Object value) {
97         YangInstanceIdentifier.PathArgument p1 = new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME);
98
99         YangInstanceIdentifier.PathArgument p2 = new YangInstanceIdentifier.NodeIdentifierWithPredicates(
100                 NODE_WITH_PREDICATES_QNAME, NAME_QNAME, value);
101
102         List<YangInstanceIdentifier.PathArgument> arguments = new ArrayList<>();
103
104         arguments.add(p1);
105         arguments.add(p2);
106
107         YangInstanceIdentifier expected = YangInstanceIdentifier.create(arguments);
108
109         NormalizedNodeMessages.InstanceIdentifier instanceIdentifier =
110                 InstanceIdentifierUtils.toSerializable(expected);
111
112         YangInstanceIdentifier actual = InstanceIdentifierUtils.fromSerializable(instanceIdentifier);
113
114         Assert.assertEquals(expected.getLastPathArgument(), actual.getLastPathArgument());
115     }
116
117     @Test
118     public void testAugmentationIdentifier() {
119         YangInstanceIdentifier.PathArgument p1 = new YangInstanceIdentifier.AugmentationIdentifier(
120             ImmutableSet.of(TEST_QNAME));
121
122         List<YangInstanceIdentifier.PathArgument> arguments = new ArrayList<>();
123
124         arguments.add(p1);
125
126         YangInstanceIdentifier expected = YangInstanceIdentifier.create(arguments);
127
128         NormalizedNodeMessages.InstanceIdentifier instanceIdentifier =
129                 InstanceIdentifierUtils.toSerializable(expected);
130
131         YangInstanceIdentifier actual = InstanceIdentifierUtils.fromSerializable(instanceIdentifier);
132
133         Assert.assertEquals(expected.getLastPathArgument(), actual.getLastPathArgument());
134
135     }
136
137     @Test
138     public void testSerializationWithContext() {
139         List<YangInstanceIdentifier.PathArgument> arguments =
140                                                 Arrays.<YangInstanceIdentifier.PathArgument>asList(
141                 new YangInstanceIdentifier.NodeIdentifier(TEST_QNAME),
142                 new YangInstanceIdentifier.NodeWithValue(NODE_WITH_VALUE_QNAME, 1),
143                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(
144                         NODE_WITH_PREDICATES_QNAME, NAME_QNAME, 2));
145
146         YangInstanceIdentifier expected = YangInstanceIdentifier.create(arguments);
147
148         QNameSerializationContextImpl serializationContext = new QNameSerializationContextImpl();
149
150         NormalizedNodeMessages.InstanceIdentifier instanceIdentifier =
151                 InstanceIdentifierUtils.toSerializable(expected, serializationContext);
152
153         QNameDeSerializationContext deserializationContext = new QNameDeSerializationContextImpl(
154                 serializationContext.getCodes());
155
156         YangInstanceIdentifier actual = InstanceIdentifierUtils.fromSerializable(
157                 instanceIdentifier, deserializationContext);
158
159         Assert.assertEquals(expected.getLastPathArgument(), actual.getLastPathArgument());
160     }
161 }

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