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.Mockito.mock;
12 import static org.mockito.Mockito.when;
14 import java.time.Instant;
15 import java.util.ArrayList;
16 import java.util.Collection;
18 import org.junit.AfterClass;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.mdsal.dom.api.DOMNotification;
24 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
25 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.Revision;
29 import org.opendaylight.yangtools.yang.common.XMLNamespace;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
41 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class JsonNotificationListenerTest {
47 private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class);
49 private static final QNameModule MODULE =
50 QNameModule.create(XMLNamespace.of("notifi:mod"), Revision.of("2016-11-23"));
52 private static EffectiveModelContext SCHEMA_CONTEXT;
55 public static void beforeClass() throws Exception {
56 SCHEMA_CONTEXT = TestUtils.loadSchemaContext("/notifications");
60 public static void afterClass() {
61 SCHEMA_CONTEXT = null;
65 public void notifi_leafTest() throws Exception {
66 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
68 final DOMNotification notificationData = mock(DOMNotification.class);
70 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
71 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
73 when(notificationData.getType()).thenReturn(schemaPathNotifi);
74 when(notificationData.getBody()).thenReturn(notifiBody);
76 final String result = prepareJson(notificationData, schemaPathNotifi);
78 LOG.info("json result: {}", result);
80 assertTrue(result.contains("ietf-restconf:notification"));
81 assertTrue(result.contains("event-time"));
82 assertTrue(result.contains("notifi-module:notifi-leaf"));
83 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
87 public void notifi_cont_leafTest() throws Exception {
88 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
90 final DOMNotification notificationData = mock(DOMNotification.class);
92 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
93 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
94 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
96 when(notificationData.getType()).thenReturn(schemaPathNotifi);
97 when(notificationData.getBody()).thenReturn(notifiBody);
99 final String result = prepareJson(notificationData, schemaPathNotifi);
101 assertTrue(result.contains("ietf-restconf:notification"));
102 assertTrue(result.contains("event-time"));
103 assertTrue(result.contains("notifi-module:notifi-cont"));
104 assertTrue(result.contains("cont"));
105 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
109 public void notifi_list_Test() throws Exception {
110 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
112 final DOMNotification notificationData = mock(DOMNotification.class);
114 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
115 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
116 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), Builders.mapBuilder()
117 .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
121 when(notificationData.getType()).thenReturn(schemaPathNotifi);
122 when(notificationData.getBody()).thenReturn(notifiBody);
124 final String result = prepareJson(notificationData, schemaPathNotifi);
126 assertTrue(result.contains("ietf-restconf:notification"));
127 assertTrue(result.contains("event-time"));
128 assertTrue(result.contains("notifi-module:notifi-list"));
129 assertTrue(result.contains("lst"));
130 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
134 public void notifi_grpTest() throws Exception {
135 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
137 final DOMNotification notificationData = mock(DOMNotification.class);
139 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
140 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
142 when(notificationData.getType()).thenReturn(schemaPathNotifi);
143 when(notificationData.getBody()).thenReturn(notifiBody);
145 final String result = prepareJson(notificationData, schemaPathNotifi);
147 assertTrue(result.contains("ietf-restconf:notification"));
148 assertTrue(result.contains("event-time"));
149 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
153 public void notifi_augmTest() throws Exception {
154 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
156 final DOMNotification notificationData = mock(DOMNotification.class);
158 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
159 final AugmentationNode augm = mockAugm(leaf);
160 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), augm);
162 when(notificationData.getType()).thenReturn(schemaPathNotifi);
163 when(notificationData.getBody()).thenReturn(notifiBody);
165 final String result = prepareJson(notificationData, schemaPathNotifi);
167 assertTrue(result.contains("ietf-restconf:notification"));
168 assertTrue(result.contains("event-time"));
169 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
172 private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
173 final AugmentationNode augm = mock(AugmentationNode.class);
174 final AugmentationIdentifier augmId = new AugmentationIdentifier(Set.of(leaf.getIdentifier().getNodeType()));
175 when(augm.getIdentifier()).thenReturn(augmId);
177 final Collection<DataContainerChild> childs = new ArrayList<>();
180 when(augm.body()).thenReturn(childs);
184 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
185 return Builders.mapEntryBuilder()
186 .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.getIdentifier().getNodeType(),
192 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
193 return Builders.containerBuilder()
194 .withNodeIdentifier(NodeIdentifier.create(contQName))
199 private static LeafNode<String> mockLeaf(final QName leafQName) {
200 return ImmutableNodes.leafNode(leafQName, "value");
203 private static String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi)
205 final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
206 schemaPathNotifi, "json-stream", NotificationOutputType.JSON);
207 return notifiAdapter.formatter().eventData(SCHEMA_CONTEXT, notificationData, Instant.now(), false, false).get();