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;
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;
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;
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;
52 public class NotificationListenerTest {
54 private SchemaContext schmeaCtx;
57 public void init() throws Exception {
58 MockitoAnnotations.initMocks(this);
59 ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications"));
60 this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema();
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"));
69 final DOMNotification notificationData = mock(DOMNotification.class);
71 final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf"));
72 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
74 when(notificationData.getType()).thenReturn(schemaPathNotifi);
75 when(notificationData.getBody()).thenReturn(notifiBody);
77 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
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"));
90 final SchemaPath schemaPathNotifi =
91 SchemaPath.create(false, QName.create(moduleQName, "notifi-cont"));
93 final DOMNotification notificationData = mock(DOMNotification.class);
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);
99 when(notificationData.getType()).thenReturn(schemaPathNotifi);
100 when(notificationData.getBody()).thenReturn(notifiBody);
102 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
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"));
116 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-list"));
118 final DOMNotification notificationData = mock(DOMNotification.class);
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);
126 when(notificationData.getType()).thenReturn(schemaPathNotifi);
127 when(notificationData.getBody()).thenReturn(notifiBody);
129 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
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"));
143 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-grp"));
145 final DOMNotification notificationData = mock(DOMNotification.class);
147 final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf"));
148 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
150 when(notificationData.getType()).thenReturn(schemaPathNotifi);
151 when(notificationData.getBody()).thenReturn(notifiBody);
153 final String result = prepareJson(notificationData, schemaPathNotifi);
155 assertTrue(result.contains("ietf-restconf:notification"));
156 assertTrue(result.contains("event-time"));
157 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
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"));
165 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(moduleQName, "notifi-augm"));
167 final DOMNotification notificationData = mock(DOMNotification.class);
169 final LeafNode leaf = mockLeaf(QName.create(moduleQName, "lf-augm"));
170 final AugmentationNode augm = mockAugm(leaf);
171 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
173 when(notificationData.getType()).thenReturn(schemaPathNotifi);
174 when(notificationData.getBody()).thenReturn(notifiBody);
176 final String result = prepareJson(notificationData, schemaPathNotifi);
178 assertTrue(result.contains("ietf-restconf:notification"));
179 assertTrue(result.contains("event-time"));
180 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
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);
188 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
191 when(augm.getValue()).thenReturn(childs);
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));
203 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
206 when(entry.getValue()).thenReturn(childs);
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));
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);
223 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
225 when(cont.getValue()).thenReturn(childs);
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");
237 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
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);
245 final Class<?> vars[] = {};
246 final Method prepareJsonM = notifi.getClass().getDeclaredMethod("prepareJson", vars);
247 prepareJsonM.setAccessible(true);
249 final Field notification = notifi.getClass().getDeclaredField("notification");
250 notification.setAccessible(true);
251 notification.set(notifi, notificationData);
253 final Field schema = notifi.getClass().getDeclaredField("schemaContext");
254 schema.setAccessible(true);
255 schema.set(notifi, this.schmeaCtx);
257 final String result = (String) prepareJsonM.invoke(notifi, null);
258 Preconditions.checkNotNull(result);