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.restconf.nb.rfc8040.streams.listeners;
10 import static java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
16 import com.google.common.collect.Lists;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Optional;
21 import org.junit.AfterClass;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.dom.api.DOMNotification;
27 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
28 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
29 import org.opendaylight.yangtools.util.SingletonSet;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
43 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
44 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
46 @RunWith(MockitoJUnitRunner.StrictStubs.class)
47 public class NotificationListenerTest {
48 private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23"));
50 private static EffectiveModelContext SCHEMA_CONTEXT;
53 public static void beforeClass() throws Exception {
54 SCHEMA_CONTEXT = TestUtils.loadSchemaContext("/notifications");
58 public static void afterClass() {
59 SCHEMA_CONTEXT = null;
63 public void notifi_leafTest() throws Exception {
64 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
66 final DOMNotification notificationData = mock(DOMNotification.class);
68 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
69 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
71 when(notificationData.getType()).thenReturn(schemaPathNotifi);
72 when(notificationData.getBody()).thenReturn(notifiBody);
74 final String result = prepareJson(notificationData, schemaPathNotifi);
76 assertTrue(result.contains("ietf-restconf:notification"));
77 assertTrue(result.contains("event-time"));
78 assertTrue(result.contains("notifi-module:notifi-leaf"));
79 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
83 public void notifi_cont_leafTest() throws Exception {
84 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
86 final DOMNotification notificationData = mock(DOMNotification.class);
88 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
89 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
90 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
92 when(notificationData.getType()).thenReturn(schemaPathNotifi);
93 when(notificationData.getBody()).thenReturn(notifiBody);
95 final String result = prepareJson(notificationData, schemaPathNotifi);
97 assertTrue(result.contains("ietf-restconf:notification"));
98 assertTrue(result.contains("event-time"));
99 assertTrue(result.contains("notifi-module:notifi-cont"));
100 assertTrue(result.contains("cont"));
101 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
105 public void notifi_list_Test() throws Exception {
106 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
108 final DOMNotification notificationData = mock(DOMNotification.class);
110 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
111 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
112 final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
113 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
114 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
116 when(notificationData.getType()).thenReturn(schemaPathNotifi);
117 when(notificationData.getBody()).thenReturn(notifiBody);
119 final String result = prepareJson(notificationData, schemaPathNotifi);
121 assertTrue(result.contains("ietf-restconf:notification"));
122 assertTrue(result.contains("event-time"));
123 assertTrue(result.contains("notifi-module:notifi-list"));
124 assertTrue(result.contains("lst"));
125 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
129 public void notifi_grpTest() throws Exception {
130 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
132 final DOMNotification notificationData = mock(DOMNotification.class);
134 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
135 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
137 when(notificationData.getType()).thenReturn(schemaPathNotifi);
138 when(notificationData.getBody()).thenReturn(notifiBody);
140 final String result = prepareJson(notificationData, schemaPathNotifi);
142 assertTrue(result.contains("ietf-restconf:notification"));
143 assertTrue(result.contains("event-time"));
144 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
148 public void notifi_augmTest() throws Exception {
149 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
151 final DOMNotification notificationData = mock(DOMNotification.class);
153 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
154 final AugmentationNode augm = mockAugm(leaf);
155 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), augm);
157 when(notificationData.getType()).thenReturn(schemaPathNotifi);
158 when(notificationData.getBody()).thenReturn(notifiBody);
160 final String result = prepareJson(notificationData, schemaPathNotifi);
162 assertTrue(result.contains("ietf-restconf:notification"));
163 assertTrue(result.contains("event-time"));
164 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
167 private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
168 final AugmentationNode augm = mock(AugmentationNode.class);
169 final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
170 when(augm.getIdentifier()).thenReturn(augmId);
172 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
175 when(augm.getValue()).thenReturn(childs);
179 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
180 final MapEntryNode entry = mock(MapEntryNode.class);
181 final NodeIdentifierWithPredicates nodeId = NodeIdentifierWithPredicates.of(leaf.getNodeType(),
182 leaf.getNodeType(), "value");
183 when(entry.getIdentifier()).thenReturn(nodeId);
184 when(entry.getChild(any())).thenReturn(Optional.of(leaf));
186 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
189 when(entry.getValue()).thenReturn(childs);
193 private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
194 final MapNode list = mock(MapNode.class);
195 when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
196 when(list.getValue()).thenReturn(Lists.newArrayList(entries));
200 private static ContainerNode mockCont(final QName contQName,
201 final DataContainerChild<? extends PathArgument, ?> child) {
202 final ContainerNode cont = mock(ContainerNode.class);
203 when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
205 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
207 when(cont.getValue()).thenReturn(childs);
211 private static LeafNode<String> mockLeaf(final QName leafQName) {
212 final LeafNode<String> child = mock(LeafNode.class);
213 when(child.getNodeType()).thenReturn(leafQName);
214 when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
215 when(child.getValue()).thenReturn("value");
219 private static String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi) {
220 final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
221 schemaPathNotifi, "stream-name", NotificationOutputType.JSON);
222 return requireNonNull(notifiAdapter.prepareJson(SCHEMA_CONTEXT, notificationData));