2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.streams.listeners;
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;
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;
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;
51 public class NotificationListenerTest {
53 private SchemaContext schmeaCtx;
56 public void init() throws Exception {
57 MockitoAnnotations.initMocks(this);
58 ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications"));
59 this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema();
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"));
68 final DOMNotification notificationData = mock(DOMNotification.class);
70 final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf"));
71 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
73 when(notificationData.getType()).thenReturn(schemaPathNotifi);
74 when(notificationData.getBody()).thenReturn(notifiBody);
76 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
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"));
89 final SchemaPath schemaPathNotifi =
90 SchemaPath.create(false, QName.create(moduleQName, "notifi-cont"));
92 final DOMNotification notificationData = mock(DOMNotification.class);
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);
98 when(notificationData.getType()).thenReturn(schemaPathNotifi);
99 when(notificationData.getBody()).thenReturn(notifiBody);
101 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
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"));
115 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-list"));
117 final DOMNotification notificationData = mock(DOMNotification.class);
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);
125 when(notificationData.getType()).thenReturn(schemaPathNotifi);
126 when(notificationData.getBody()).thenReturn(notifiBody);
128 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
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"));
142 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-grp"));
144 final DOMNotification notificationData = mock(DOMNotification.class);
146 final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf"));
147 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
149 when(notificationData.getType()).thenReturn(schemaPathNotifi);
150 when(notificationData.getBody()).thenReturn(notifiBody);
152 final String result = prepareJson(notificationData, schemaPathNotifi);
154 assertTrue(result.contains("ietf-restconf:notification"));
155 assertTrue(result.contains("event-time"));
156 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
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"));
164 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-augm"));
166 final DOMNotification notificationData = mock(DOMNotification.class);
168 final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf-augm"));
169 final AugmentationNode augm = mockAugm(leaf);
170 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
172 when(notificationData.getType()).thenReturn(schemaPathNotifi);
173 when(notificationData.getBody()).thenReturn(notifiBody);
175 final String result = prepareJson(notificationData, schemaPathNotifi);
177 assertTrue(result.contains("ietf-restconf:notification"));
178 assertTrue(result.contains("event-time"));
179 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
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);
187 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
190 when(augm.getValue()).thenReturn(childs);
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));
202 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
205 when(entry.getValue()).thenReturn(childs);
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));
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);
222 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
224 when(cont.getValue()).thenReturn(childs);
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");
236 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
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);
244 final Class<?> vars[] = {};
245 final Method prepareJsonM = notifi.getClass().getDeclaredMethod("prepareJson", vars);
246 prepareJsonM.setAccessible(true);
248 final Field notification = notifi.getClass().getDeclaredField("notification");
249 notification.setAccessible(true);
250 notification.set(notifi, notificationData);
252 final Field schema = notifi.getClass().getDeclaredField("schemaContext");
253 schema.setAccessible(true);
254 schema.set(notifi, this.schmeaCtx);
256 final String result = (String) prepareJsonM.invoke(notifi, null);
257 Preconditions.checkNotNull(result);