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.ParseException;
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.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;
53 public class NotificationListenerTest {
54 private static final QNameModule MODULE;
57 MODULE = QNameModule.create(URI.create("notifi:mod"),
58 SimpleDateFormatUtil.getRevisionFormat().parse("2016-11-23"));
59 } catch (ParseException e) {
60 throw new ExceptionInInitializerError(e);
64 private SchemaContext schmeaCtx;
67 public void init() throws Exception {
68 MockitoAnnotations.initMocks(this);
69 ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications"));
70 this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema();
74 public void notifi_leafTest() throws Exception {
75 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
77 final DOMNotification notificationData = mock(DOMNotification.class);
79 final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf"));
80 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
82 when(notificationData.getType()).thenReturn(schemaPathNotifi);
83 when(notificationData.getBody()).thenReturn(notifiBody);
85 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
94 public void notifi_cont_leafTest() throws Exception {
95 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
97 final DOMNotification notificationData = mock(DOMNotification.class);
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);
103 when(notificationData.getType()).thenReturn(schemaPathNotifi);
104 when(notificationData.getBody()).thenReturn(notifiBody);
106 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
116 public void notifi_list_Test() throws Exception {
117 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
119 final DOMNotification notificationData = mock(DOMNotification.class);
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);
127 when(notificationData.getType()).thenReturn(schemaPathNotifi);
128 when(notificationData.getBody()).thenReturn(notifiBody);
130 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
140 public void notifi_grpTest() throws Exception {
141 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
143 final DOMNotification notificationData = mock(DOMNotification.class);
145 final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf"));
146 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
148 when(notificationData.getType()).thenReturn(schemaPathNotifi);
149 when(notificationData.getBody()).thenReturn(notifiBody);
151 final String result = prepareJson(notificationData, schemaPathNotifi);
153 assertTrue(result.contains("ietf-restconf:notification"));
154 assertTrue(result.contains("event-time"));
155 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
159 public void notifi_augmTest() throws Exception {
160 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
162 final DOMNotification notificationData = mock(DOMNotification.class);
164 final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
165 final AugmentationNode augm = mockAugm(leaf);
166 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
168 when(notificationData.getType()).thenReturn(schemaPathNotifi);
169 when(notificationData.getBody()).thenReturn(notifiBody);
171 final String result = prepareJson(notificationData, schemaPathNotifi);
173 assertTrue(result.contains("ietf-restconf:notification"));
174 assertTrue(result.contains("event-time"));
175 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
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);
183 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
186 when(augm.getValue()).thenReturn(childs);
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));
198 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
201 when(entry.getValue()).thenReturn(childs);
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));
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);
218 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
220 when(cont.getValue()).thenReturn(childs);
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");
232 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
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);
240 final Class<?> vars[] = {};
241 final Method prepareJsonM = notifi.getClass().getDeclaredMethod("prepareJson", vars);
242 prepareJsonM.setAccessible(true);
244 final Field notification = notifi.getClass().getDeclaredField("notification");
245 notification.setAccessible(true);
246 notification.set(notifi, notificationData);
248 final Field schema = notifi.getClass().getDeclaredField("schemaContext");
249 schema.setAccessible(true);
250 schema.set(notifi, this.schmeaCtx);
252 final String result = (String) prepareJsonM.invoke(notifi, null);
253 Preconditions.checkNotNull(result);