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 org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.junit.MockitoJUnitRunner;
18 import org.opendaylight.mdsal.dom.api.DOMNotification;
19 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class JsonNotificationListenerTest extends AbstractNotificationListenerTest {
35 private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class);
38 public void notifi_leafTest() throws Exception {
39 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
41 final DOMNotification notificationData = mock(DOMNotification.class);
43 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
44 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
46 when(notificationData.getType()).thenReturn(schemaPathNotifi);
47 when(notificationData.getBody()).thenReturn(notifiBody);
49 final String result = prepareJson(notificationData, schemaPathNotifi);
51 LOG.info("json result: {}", result);
53 assertTrue(result.contains("ietf-restconf:notification"));
54 assertTrue(result.contains("event-time"));
55 assertTrue(result.contains("notifi-module:notifi-leaf"));
56 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
60 public void notifi_cont_leafTest() throws Exception {
61 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
63 final DOMNotification notificationData = mock(DOMNotification.class);
65 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
66 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
67 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
69 when(notificationData.getType()).thenReturn(schemaPathNotifi);
70 when(notificationData.getBody()).thenReturn(notifiBody);
72 final String result = prepareJson(notificationData, schemaPathNotifi);
74 assertTrue(result.contains("ietf-restconf:notification"));
75 assertTrue(result.contains("event-time"));
76 assertTrue(result.contains("notifi-module:notifi-cont"));
77 assertTrue(result.contains("cont"));
78 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
82 public void notifi_list_Test() throws Exception {
83 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
85 final DOMNotification notificationData = mock(DOMNotification.class);
87 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
88 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
89 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), Builders.mapBuilder()
90 .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
94 when(notificationData.getType()).thenReturn(schemaPathNotifi);
95 when(notificationData.getBody()).thenReturn(notifiBody);
97 final String result = prepareJson(notificationData, schemaPathNotifi);
99 assertTrue(result.contains("ietf-restconf:notification"));
100 assertTrue(result.contains("event-time"));
101 assertTrue(result.contains("notifi-module:notifi-list"));
102 assertTrue(result.contains("lst"));
103 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
107 public void notifi_grpTest() throws Exception {
108 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
110 final DOMNotification notificationData = mock(DOMNotification.class);
112 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
113 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
115 when(notificationData.getType()).thenReturn(schemaPathNotifi);
116 when(notificationData.getBody()).thenReturn(notifiBody);
118 final String result = prepareJson(notificationData, schemaPathNotifi);
120 assertTrue(result.contains("ietf-restconf:notification"));
121 assertTrue(result.contains("event-time"));
122 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
126 public void notifi_augmTest() throws Exception {
127 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
129 final DOMNotification notificationData = mock(DOMNotification.class);
131 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
132 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
134 when(notificationData.getType()).thenReturn(schemaPathNotifi);
135 when(notificationData.getBody()).thenReturn(notifiBody);
137 final String result = prepareJson(notificationData, schemaPathNotifi);
139 assertTrue(result.contains("ietf-restconf:notification"));
140 assertTrue(result.contains("event-time"));
141 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
144 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
145 return Builders.mapEntryBuilder()
146 .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
151 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
152 return Builders.containerBuilder()
153 .withNodeIdentifier(NodeIdentifier.create(contQName))
158 private static LeafNode<String> mockLeaf(final QName leafQName) {
159 return ImmutableNodes.leafNode(leafQName, "value");
162 private static String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi)
164 final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
165 schemaPathNotifi, "json-stream", NotificationOutputType.JSON);
166 return notifiAdapter.formatter()
167 .eventData(SCHEMA_CONTEXT, notificationData, Instant.now(), false, false, false, false).orElseThrow();