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;
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
14 import com.google.common.collect.ImmutableSet;
15 import java.time.Instant;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.junit.MockitoJUnitRunner;
19 import org.opendaylight.mdsal.dom.api.DOMNotification;
20 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class JsonNotificationListenerTest extends AbstractNotificationListenerTest {
36 private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class);
38 private final ListenersBroker listenersBroker = new ListenersBroker.ServerSentEvents();
41 public void notifi_leafTest() throws Exception {
42 final QName schemaPathNotifi = QName.create(MODULE, "notifi-leaf");
44 final DOMNotification notificationData = mock(DOMNotification.class);
46 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
47 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
49 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
50 when(notificationData.getBody()).thenReturn(notifiBody);
52 final String result = prepareJson(notificationData, schemaPathNotifi);
54 LOG.info("json result: {}", result);
56 assertTrue(result.contains("ietf-restconf:notification"));
57 assertTrue(result.contains("event-time"));
58 assertTrue(result.contains("notifi-module:notifi-leaf"));
59 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
63 public void notifi_cont_leafTest() throws Exception {
64 final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
66 final DOMNotification notificationData = mock(DOMNotification.class);
68 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
69 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
70 final ContainerNode notifiBody = mockCont(schemaPathNotifi, cont);
72 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
73 when(notificationData.getBody()).thenReturn(notifiBody);
75 final String result = prepareJson(notificationData, schemaPathNotifi);
77 assertTrue(result.contains("ietf-restconf:notification"));
78 assertTrue(result.contains("event-time"));
79 assertTrue(result.contains("notifi-module:notifi-cont"));
80 assertTrue(result.contains("cont"));
81 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
85 public void notifi_list_Test() throws Exception {
86 final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
88 final DOMNotification notificationData = mock(DOMNotification.class);
90 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
91 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
92 final ContainerNode notifiBody = mockCont(schemaPathNotifi, Builders.mapBuilder()
93 .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
97 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
98 when(notificationData.getBody()).thenReturn(notifiBody);
100 final String result = prepareJson(notificationData, schemaPathNotifi);
102 assertTrue(result.contains("ietf-restconf:notification"));
103 assertTrue(result.contains("event-time"));
104 assertTrue(result.contains("notifi-module:notifi-list"));
105 assertTrue(result.contains("lst"));
106 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
110 public void notifi_grpTest() throws Exception {
111 final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
113 final DOMNotification notificationData = mock(DOMNotification.class);
115 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
116 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
118 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
119 when(notificationData.getBody()).thenReturn(notifiBody);
121 final String result = prepareJson(notificationData, schemaPathNotifi);
123 assertTrue(result.contains("ietf-restconf:notification"));
124 assertTrue(result.contains("event-time"));
125 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
129 public void notifi_augmTest() throws Exception {
130 final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
132 final DOMNotification notificationData = mock(DOMNotification.class);
134 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
135 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
137 when(notificationData.getType()).thenReturn(Absolute.of(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-augm" + '"' + ":" + '"' + "value"));
147 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
148 return Builders.mapEntryBuilder()
149 .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
154 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
155 return Builders.containerBuilder()
156 .withNodeIdentifier(NodeIdentifier.create(contQName))
161 private static LeafNode<String> mockLeaf(final QName leafQName) {
162 return ImmutableNodes.leafNode(leafQName, "value");
165 private String prepareJson(final DOMNotification notificationData, final QName schemaPathNotifi)
167 final var notifiAdapter = listenersBroker.registerNotificationListener(MODEL_CONTEXT,
168 ImmutableSet.of(schemaPathNotifi), NotificationOutputType.JSON);
169 return notifiAdapter.formatter().eventData(MODEL_CONTEXT, notificationData, Instant.now()).orElseThrow();