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