Mass conversion to static methods
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / streams / listeners / NotificationListenerTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.controller.sal.streams.listeners;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Lists;
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Method;
20 import java.net.URI;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
31 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
32 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
33 import org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter;
34 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
35 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
36 import org.opendaylight.yangtools.util.SingletonSet;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.QNameModule;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
43 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
46 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
51
52 public class NotificationListenerTest {
53
54     private SchemaContext schmeaCtx;
55
56     @Before
57     public void init() throws Exception {
58         MockitoAnnotations.initMocks(this);
59         ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications"));
60         this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema();
61     }
62
63     @Test
64     public void notifi_leafTest() throws Exception {
65         final QNameModule moduleQName =
66                 QNameModule.create(new URI("notifi:mod"), new SimpleDateFormat("yyyy-MM-dd").parse("2016-11-23"));
67         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-leaf"));
68
69         final DOMNotification notificationData = mock(DOMNotification.class);
70
71         final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf"));
72         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
73
74         when(notificationData.getType()).thenReturn(schemaPathNotifi);
75         when(notificationData.getBody()).thenReturn(notifiBody);
76
77         final String result = prepareJson(notificationData, schemaPathNotifi);
78
79         assertTrue(result.contains("ietf-restconf:notification"));
80         assertTrue(result.contains("event-time"));
81         assertTrue(result.contains("notifi-module:notifi-leaf"));
82         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
83     }
84
85     @Test
86     public void notifi_cont_leafTest() throws Exception {
87         final QNameModule moduleQName =
88                 QNameModule.create(new URI("notifi:mod"), new SimpleDateFormat("yyyy-MM-dd").parse("2016-11-23"));
89
90         final SchemaPath schemaPathNotifi =
91                 SchemaPath.create(false, QName.create(moduleQName, "notifi-cont"));
92
93         final DOMNotification notificationData = mock(DOMNotification.class);
94
95         final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf"));
96         final ContainerNode cont = mockCont(QName.create(moduleQName, "cont"), leaf);
97         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
98
99         when(notificationData.getType()).thenReturn(schemaPathNotifi);
100         when(notificationData.getBody()).thenReturn(notifiBody);
101
102         final String result = prepareJson(notificationData, schemaPathNotifi);
103
104         assertTrue(result.contains("ietf-restconf:notification"));
105         assertTrue(result.contains("event-time"));
106         assertTrue(result.contains("notifi-module:notifi-cont"));
107         assertTrue(result.contains("cont"));
108         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
109     }
110
111     @Test
112     public void notifi_list_Test() throws Exception {
113         final QNameModule moduleQName =
114                 QNameModule.create(new URI("notifi:mod"), new SimpleDateFormat("yyyy-MM-dd").parse("2016-11-23"));
115
116         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-list"));
117
118         final DOMNotification notificationData = mock(DOMNotification.class);
119
120         final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf"));
121         final MapEntryNode entry = mockMapEntry(QName.create(moduleQName, "lst"), leaf);
122         final MapNode list = mockList(QName.create(moduleQName, "lst"), entry);
123         final ContainerNode cont = mockCont(QName.create(moduleQName, "cont"), list);
124         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
125
126         when(notificationData.getType()).thenReturn(schemaPathNotifi);
127         when(notificationData.getBody()).thenReturn(notifiBody);
128
129         final String result = prepareJson(notificationData, schemaPathNotifi);
130
131         assertTrue(result.contains("ietf-restconf:notification"));
132         assertTrue(result.contains("event-time"));
133         assertTrue(result.contains("notifi-module:notifi-list"));
134         assertTrue(result.contains("lst"));
135         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
136     }
137
138     @Test
139     public void notifi_grpTest() throws Exception {
140         final QNameModule moduleQName =
141                 QNameModule.create(new URI("notifi:mod"), new SimpleDateFormat("yyyy-MM-dd").parse("2016-11-23"));
142
143         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-grp"));
144
145         final DOMNotification notificationData = mock(DOMNotification.class);
146
147         final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf"));
148         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
149
150         when(notificationData.getType()).thenReturn(schemaPathNotifi);
151         when(notificationData.getBody()).thenReturn(notifiBody);
152
153         final String result = prepareJson(notificationData, schemaPathNotifi);
154
155         assertTrue(result.contains("ietf-restconf:notification"));
156         assertTrue(result.contains("event-time"));
157         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
158     }
159
160     @Test
161     public void notifi_augmTest() throws Exception {
162         final QNameModule moduleQName =
163                 QNameModule.create(new URI("notifi:mod"), new SimpleDateFormat("yyyy-MM-dd").parse("2016-11-23"));
164
165         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-augm"));
166
167         final DOMNotification notificationData = mock(DOMNotification.class);
168
169         final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf-augm"));
170         final AugmentationNode augm = mockAugm(leaf);
171         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
172
173         when(notificationData.getType()).thenReturn(schemaPathNotifi);
174         when(notificationData.getBody()).thenReturn(notifiBody);
175
176         final String result = prepareJson(notificationData, schemaPathNotifi);
177
178         assertTrue(result.contains("ietf-restconf:notification"));
179         assertTrue(result.contains("event-time"));
180         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
181     }
182
183     private static AugmentationNode mockAugm(final LeafNode leaf) {
184         final AugmentationNode augm = mock(AugmentationNode.class);
185         final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
186         when(augm.getIdentifier()).thenReturn(augmId);
187
188         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
189         childs.add(leaf);
190
191         when(augm.getValue()).thenReturn(childs);
192         return augm;
193     }
194
195     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode leaf) {
196         final MapEntryNode entry = mock(MapEntryNode.class);
197         final Map<QName, Object> keyValues = new HashMap<>();
198         keyValues.put(leaf.getNodeType(), "value");
199         final NodeIdentifierWithPredicates nodeId = new NodeIdentifierWithPredicates(leaf.getNodeType(), keyValues);
200         when(entry.getIdentifier()).thenReturn(nodeId);
201         when(entry.getChild(any())).thenReturn(Optional.of(leaf));
202
203         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
204         childs.add(leaf);
205
206         when(entry.getValue()).thenReturn(childs);
207         return entry;
208     }
209
210     private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
211         final MapNode list = mock(MapNode.class);
212         when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
213         when(list.getNodeType()).thenReturn(listQName);
214         when(list.getValue()).thenReturn(Lists.newArrayList(entries));
215         return list;
216     }
217
218     private static ContainerNode mockCont(final QName contQName, final DataContainerChild<? extends PathArgument, ?> child) {
219         final ContainerNode cont = mock(ContainerNode.class);
220         when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
221         when(cont.getNodeType()).thenReturn(contQName);
222
223         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
224         childs.add(child);
225         when(cont.getValue()).thenReturn(childs);
226         return cont;
227     }
228
229     private static LeafNode mockLeaf(final QName leafQName) {
230         final LeafNode child = mock(LeafNode.class);
231         when(child.getNodeType()).thenReturn(leafQName);
232         when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
233         when(child.getValue()).thenReturn("value");
234         return child;
235     }
236
237     private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
238             throws Exception {
239         final List<SchemaPath> paths = new ArrayList<>();
240         paths.add(schemaPathNotifi);
241         final List<NotificationListenerAdapter> listNotifi =
242                 Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString());
243         final NotificationListenerAdapter notifi = listNotifi.get(0);
244
245         final Class<?> vars[] = {};
246         final Method prepareJsonM = notifi.getClass().getDeclaredMethod("prepareJson", vars);
247         prepareJsonM.setAccessible(true);
248
249         final Field notification = notifi.getClass().getDeclaredField("notification");
250         notification.setAccessible(true);
251         notification.set(notifi, notificationData);
252
253         final Field schema = notifi.getClass().getDeclaredField("schemaContext");
254         schema.setAccessible(true);
255         schema.set(notifi, this.schmeaCtx);
256
257         final String result = (String) prepareJsonM.invoke(notifi, null);
258         Preconditions.checkNotNull(result);
259         return result;
260     }
261 }