Fix raw type warnings
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / datastore / node / utils / serialization / PathArgumentSerializerTest.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.utils.serialization;
10
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import org.junit.Rule;
14 import org.junit.Test;
15 import org.junit.rules.ExpectedException;
16 import org.opendaylight.controller.cluster.datastore.util.TestModel;
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.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 import java.net.URI;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyString;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
34
35 public class PathArgumentSerializerTest{
36
37     @Rule
38     public ExpectedException expectedException = ExpectedException.none();
39
40     @Test
41     public void testSerializeNullContext(){
42         expectedException.expect(NullPointerException.class);
43         expectedException.expectMessage("context should not be null");
44
45         PathArgumentSerializer.serialize(null, mock(PathArgument.class));
46     }
47
48     @Test
49     public void testSerializeNullPathArgument(){
50         expectedException.expect(NullPointerException.class);
51         expectedException.expectMessage("pathArgument should not be null");
52
53         PathArgumentSerializer.serialize(mock(QNameSerializationContext.class), null);
54
55     }
56
57     @Test
58     public void testDeSerializeNullContext(){
59         expectedException.expect(NullPointerException.class);
60         expectedException.expectMessage("context should not be null");
61
62         PathArgumentSerializer.deSerialize(null, NormalizedNodeMessages.PathArgument.getDefaultInstance());
63
64     }
65
66     @Test
67     public void testDeSerializeNullPathArgument(){
68         expectedException.expect(NullPointerException.class);
69         expectedException.expectMessage("pathArgument should not be null");
70
71         PathArgumentSerializer.deSerialize(mock(QNameDeSerializationContext.class), null);
72
73     }
74
75     @Test
76     public void testSerializeNodeIdentifier(){
77         QNameSerializationContext serializationContext = mock(QNameSerializationContext.class);
78
79         when(serializationContext.addLocalName(anyString())).thenReturn(5);
80         when(serializationContext.addNamespace(any(URI.class))).thenReturn(10);
81         when(serializationContext.addRevision(any(Date.class))).thenReturn(11);
82
83         NormalizedNodeMessages.PathArgument actual = PathArgumentSerializer
84             .serialize(serializationContext,
85                 new NodeIdentifier(
86                     TestModel.TEST_QNAME));
87
88         assertEquals(PathArgumentType.NODE_IDENTIFIER.ordinal(), actual.getIntType());
89         assertEquals(5, actual.getNodeType().getLocalName());
90         assertEquals(10, actual.getNodeType().getNamespace());
91         assertEquals(11, actual.getNodeType().getRevision());
92
93
94     }
95
96     @Test
97     public void testSerializeNodeIdentifierWithValue(){
98         QNameSerializationContext serializationContext = mock(QNameSerializationContext.class);
99
100         when(serializationContext.addLocalName(anyString())).thenReturn(5);
101         when(serializationContext.addNamespace(any(URI.class))).thenReturn(10);
102         when(serializationContext.addRevision(any(Date.class))).thenReturn(11);
103
104         NormalizedNodeMessages.PathArgument actual = PathArgumentSerializer
105             .serialize(serializationContext,
106                 new NodeWithValue<>(
107                     TestModel.TEST_QNAME, "foo"));
108
109         assertEquals(PathArgumentType.NODE_IDENTIFIER_WITH_VALUE.ordinal(), actual.getIntType());
110         assertEquals(5, actual.getNodeType().getLocalName());
111         assertEquals(10, actual.getNodeType().getNamespace());
112         assertEquals(11, actual.getNodeType().getRevision());
113         assertEquals("foo", actual.getAttribute(0).getValue());
114
115
116     }
117
118     @Test
119     public void testSerializeNodeIdentifierWithPredicates(){
120         QNameSerializationContext serializationContext = mock(QNameSerializationContext.class);
121
122         when(serializationContext.addLocalName("test")).thenReturn(5);
123         when(serializationContext.addLocalName("child-name")).thenReturn(55);
124
125         when(serializationContext.addNamespace(TestModel.TEST_QNAME.getNamespace())).thenReturn(
126             10);
127         when(serializationContext.addNamespace(TestModel.CHILD_NAME_QNAME.getNamespace())).thenReturn(66);
128
129         when(serializationContext.addRevision(TestModel.TEST_QNAME.getRevision())).thenReturn(
130             11);
131         when(serializationContext.addRevision(TestModel.CHILD_NAME_QNAME.getRevision())).thenReturn(77);
132
133         Map<QName, Object> predicates = new HashMap<>();
134
135         predicates.put(TestModel.CHILD_NAME_QNAME, "foobar");
136
137         NormalizedNodeMessages.PathArgument actual = PathArgumentSerializer
138             .serialize(serializationContext,
139                 new NodeIdentifierWithPredicates(TestModel.TEST_QNAME, predicates));
140
141         assertEquals(PathArgumentType.NODE_IDENTIFIER_WITH_PREDICATES.ordinal(), actual.getIntType());
142         assertEquals(5, actual.getNodeType().getLocalName());
143         assertEquals(10, actual.getNodeType().getNamespace());
144         assertEquals(11, actual.getNodeType().getRevision());
145
146         assertEquals(55, actual.getAttribute(0).getName().getLocalName());
147         assertEquals(66, actual.getAttribute(0).getName().getNamespace());
148         assertEquals(77, actual.getAttribute(0).getName().getRevision());
149
150         assertEquals("foobar", actual.getAttribute(0).getValue());
151
152
153     }
154
155     @Test
156     public void testSerializeAugmentationIdentifier(){
157         QNameSerializationContext serializationContext = mock(QNameSerializationContext.class);
158
159         when(serializationContext.addLocalName(anyString())).thenReturn(55);
160         when(serializationContext.addNamespace(any(URI.class))).thenReturn(66);
161         when(serializationContext.addRevision(any(Date.class))).thenReturn(77);
162
163         NormalizedNodeMessages.PathArgument actual = PathArgumentSerializer
164             .serialize(serializationContext,
165                 new AugmentationIdentifier(ImmutableSet.of(TestModel.TEST_QNAME)));
166
167         assertEquals(PathArgumentType.AUGMENTATION_IDENTIFIER.ordinal(), actual.getIntType());
168
169         assertEquals(55, actual.getAttribute(0).getName().getLocalName());
170         assertEquals(66, actual.getAttribute(0).getName().getNamespace());
171         assertEquals(77, actual.getAttribute(0).getName().getRevision());
172
173     }
174
175     @Test
176     public void testDeSerializeNodeIdentifier(){
177
178         NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
179         NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
180         NormalizedNodeMessages.QName.Builder qNameBuilder = NormalizedNodeMessages.QName.newBuilder();
181
182         qNameBuilder.setNamespace(0);
183         qNameBuilder.setRevision(1);
184         qNameBuilder.setLocalName(2);
185
186         pathBuilder.setNodeType(qNameBuilder);
187         pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER.ordinal());
188
189         nodeBuilder.addCode(TestModel.TEST_QNAME.getNamespace().toString());
190         nodeBuilder.addCode(TestModel.TEST_QNAME.getFormattedRevision());
191         nodeBuilder.addCode(TestModel.TEST_QNAME.getLocalName());
192
193         PathArgument pathArgument = NormalizedNodeSerializer.deSerialize(nodeBuilder.build(), pathBuilder.build());
194
195         assertEquals(new NodeIdentifier(TestModel.TEST_QNAME), pathArgument);
196
197     }
198
199     @Test
200     public void testDeSerializeNodeWithValue(){
201         NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
202         NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
203         NormalizedNodeMessages.QName.Builder qNameBuilder = NormalizedNodeMessages.QName.newBuilder();
204
205         qNameBuilder.setNamespace(0);
206         qNameBuilder.setRevision(1);
207         qNameBuilder.setLocalName(2);
208
209         pathBuilder.setNodeType(qNameBuilder);
210         pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER_WITH_VALUE.ordinal());
211         pathBuilder.addAttribute(
212             NormalizedNodeMessages.PathArgumentAttribute.newBuilder()
213                 .setValue("foo").setType(ValueType.STRING_TYPE.ordinal()));
214
215         nodeBuilder.addCode(TestModel.TEST_QNAME.getNamespace().toString());
216         nodeBuilder.addCode(TestModel.TEST_QNAME.getFormattedRevision());
217         nodeBuilder.addCode(TestModel.TEST_QNAME.getLocalName());
218
219         PathArgument pathArgument = NormalizedNodeSerializer.deSerialize(nodeBuilder.build(), pathBuilder.build());
220
221         assertEquals(new NodeWithValue<>(TestModel.TEST_QNAME, "foo"), pathArgument);
222
223     }
224     @Test
225     public void testDeSerializeNodeIdentifierWithPredicates(){
226         NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
227         NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
228         NormalizedNodeMessages.QName.Builder qNameBuilder = NormalizedNodeMessages.QName.newBuilder();
229
230         qNameBuilder.setNamespace(0);
231         qNameBuilder.setRevision(1);
232         qNameBuilder.setLocalName(2);
233
234         pathBuilder.setNodeType(qNameBuilder);
235         pathBuilder.setIntType(PathArgumentType.NODE_IDENTIFIER_WITH_PREDICATES.ordinal());
236         pathBuilder.addAttribute(NormalizedNodeMessages.PathArgumentAttribute.newBuilder().setName(qNameBuilder).setValue(
237             "foo").setType(ValueType.STRING_TYPE.ordinal()));
238
239         nodeBuilder.addCode(TestModel.TEST_QNAME.getNamespace().toString());
240         nodeBuilder.addCode(TestModel.TEST_QNAME.getFormattedRevision());
241         nodeBuilder.addCode(TestModel.TEST_QNAME.getLocalName());
242
243         PathArgument pathArgument = NormalizedNodeSerializer.deSerialize(nodeBuilder.build(), pathBuilder.build());
244
245         assertEquals(new NodeIdentifierWithPredicates(TestModel.TEST_QNAME,
246             ImmutableMap.<QName, Object>of(TestModel.TEST_QNAME, "foo")), pathArgument);
247
248     }
249     @Test
250     public void testDeSerializeNodeAugmentationIdentifier(){
251         NormalizedNodeMessages.Node.Builder nodeBuilder = NormalizedNodeMessages.Node.newBuilder();
252         NormalizedNodeMessages.PathArgument.Builder pathBuilder = NormalizedNodeMessages.PathArgument.newBuilder();
253         NormalizedNodeMessages.QName.Builder qNameBuilder = NormalizedNodeMessages.QName.newBuilder();
254
255         qNameBuilder.setNamespace(0);
256         qNameBuilder.setRevision(1);
257         qNameBuilder.setLocalName(2);
258
259         pathBuilder.setIntType(PathArgumentType.AUGMENTATION_IDENTIFIER.ordinal());
260         pathBuilder.addAttribute(NormalizedNodeMessages.PathArgumentAttribute.newBuilder().setName(qNameBuilder).setType(ValueType.STRING_TYPE.ordinal()));
261
262         nodeBuilder.addCode(TestModel.TEST_QNAME.getNamespace().toString());
263         nodeBuilder.addCode(TestModel.TEST_QNAME.getFormattedRevision());
264         nodeBuilder.addCode(TestModel.TEST_QNAME.getLocalName());
265
266         PathArgument pathArgument = NormalizedNodeSerializer.deSerialize(nodeBuilder.build(), pathBuilder.build());
267
268         assertEquals(new AugmentationIdentifier(ImmutableSet.of(TestModel.TEST_QNAME)), pathArgument);
269     }
270 }