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

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