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