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