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 org.junit.Assert.assertTrue;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import com.google.common.collect.Lists;
17 import java.time.Instant;
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;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 @RunWith(MockitoJUnitRunner.StrictStubs.class)
49 public class JsonNotificationListenerTest {
50 private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class);
52 private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23"));
54 private static EffectiveModelContext SCHEMA_CONTEXT;
57 public static void beforeClass() throws Exception {
58 SCHEMA_CONTEXT = TestUtils.loadSchemaContext("/notifications");
62 public static void afterClass() {
63 SCHEMA_CONTEXT = null;
67 public void notifi_leafTest() throws Exception {
68 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
70 final DOMNotification notificationData = mock(DOMNotification.class);
72 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
73 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
75 when(notificationData.getType()).thenReturn(schemaPathNotifi);
76 when(notificationData.getBody()).thenReturn(notifiBody);
78 final String result = prepareJson(notificationData, schemaPathNotifi);
80 LOG.info("json result: {}", result);
82 assertTrue(result.contains("ietf-restconf:notification"));
83 assertTrue(result.contains("event-time"));
84 assertTrue(result.contains("notifi-module:notifi-leaf"));
85 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
89 public void notifi_cont_leafTest() throws Exception {
90 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
92 final DOMNotification notificationData = mock(DOMNotification.class);
94 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
95 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
96 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), 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 Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
114 final DOMNotification notificationData = mock(DOMNotification.class);
116 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
117 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
118 final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
119 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
120 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
122 when(notificationData.getType()).thenReturn(schemaPathNotifi);
123 when(notificationData.getBody()).thenReturn(notifiBody);
125 final String result = prepareJson(notificationData, schemaPathNotifi);
127 assertTrue(result.contains("ietf-restconf:notification"));
128 assertTrue(result.contains("event-time"));
129 assertTrue(result.contains("notifi-module:notifi-list"));
130 assertTrue(result.contains("lst"));
131 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
135 public void notifi_grpTest() throws Exception {
136 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
138 final DOMNotification notificationData = mock(DOMNotification.class);
140 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
141 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
143 when(notificationData.getType()).thenReturn(schemaPathNotifi);
144 when(notificationData.getBody()).thenReturn(notifiBody);
146 final String result = prepareJson(notificationData, schemaPathNotifi);
148 assertTrue(result.contains("ietf-restconf:notification"));
149 assertTrue(result.contains("event-time"));
150 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
154 public void notifi_augmTest() throws Exception {
155 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
157 final DOMNotification notificationData = mock(DOMNotification.class);
159 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
160 final AugmentationNode augm = mockAugm(leaf);
161 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), augm);
163 when(notificationData.getType()).thenReturn(schemaPathNotifi);
164 when(notificationData.getBody()).thenReturn(notifiBody);
166 final String result = prepareJson(notificationData, schemaPathNotifi);
168 assertTrue(result.contains("ietf-restconf:notification"));
169 assertTrue(result.contains("event-time"));
170 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
173 private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
174 final AugmentationNode augm = mock(AugmentationNode.class);
175 final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
176 when(augm.getIdentifier()).thenReturn(augmId);
178 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
181 when(augm.getValue()).thenReturn(childs);
185 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
186 final MapEntryNode entry = mock(MapEntryNode.class);
187 final NodeIdentifierWithPredicates nodeId = NodeIdentifierWithPredicates.of(leaf.getNodeType(),
188 leaf.getNodeType(), "value");
189 when(entry.getIdentifier()).thenReturn(nodeId);
190 when(entry.getChild(any())).thenReturn(Optional.of(leaf));
192 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
195 when(entry.getValue()).thenReturn(childs);
199 private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
200 final MapNode list = mock(MapNode.class);
201 when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
202 when(list.getValue()).thenReturn(Lists.newArrayList(entries));
206 private static ContainerNode mockCont(final QName contQName,
207 final DataContainerChild<? extends PathArgument, ?> child) {
208 final ContainerNode cont = mock(ContainerNode.class);
209 when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
211 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
213 when(cont.getValue()).thenReturn(childs);
217 private static LeafNode<String> mockLeaf(final QName leafQName) {
218 final LeafNode<String> child = mock(LeafNode.class);
219 when(child.getNodeType()).thenReturn(leafQName);
220 when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
221 when(child.getValue()).thenReturn("value");
225 private static String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi)
227 final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
228 schemaPathNotifi, "json-stream", NotificationOutputType.JSON);
229 return notifiAdapter.formatter.eventData(SCHEMA_CONTEXT, notificationData, Instant.now(), false, false).get();