Capture ListenersBroker instances
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / JsonNotificationListenerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
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;
32
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class JsonNotificationListenerTest extends AbstractNotificationListenerTest {
35     private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class);
36
37     private final ListenersBroker listenersBroker = ListenersBroker.getInstance();
38
39     @Test
40     public void notifi_leafTest() throws Exception {
41         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
42
43         final DOMNotification notificationData = mock(DOMNotification.class);
44
45         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
46         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
47
48         when(notificationData.getType()).thenReturn(schemaPathNotifi);
49         when(notificationData.getBody()).thenReturn(notifiBody);
50
51         final String result = prepareJson(notificationData, schemaPathNotifi);
52
53         LOG.info("json result: {}", result);
54
55         assertTrue(result.contains("ietf-restconf:notification"));
56         assertTrue(result.contains("event-time"));
57         assertTrue(result.contains("notifi-module:notifi-leaf"));
58         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
59     }
60
61     @Test
62     public void notifi_cont_leafTest() throws Exception {
63         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
64
65         final DOMNotification notificationData = mock(DOMNotification.class);
66
67         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
68         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
69         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
70
71         when(notificationData.getType()).thenReturn(schemaPathNotifi);
72         when(notificationData.getBody()).thenReturn(notifiBody);
73
74         final String result = prepareJson(notificationData, schemaPathNotifi);
75
76         assertTrue(result.contains("ietf-restconf:notification"));
77         assertTrue(result.contains("event-time"));
78         assertTrue(result.contains("notifi-module:notifi-cont"));
79         assertTrue(result.contains("cont"));
80         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
81     }
82
83     @Test
84     public void notifi_list_Test() throws Exception {
85         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
86
87         final DOMNotification notificationData = mock(DOMNotification.class);
88
89         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
90         final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
91         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), Builders.mapBuilder()
92             .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
93             .withChild(entry)
94             .build());
95
96         when(notificationData.getType()).thenReturn(schemaPathNotifi);
97         when(notificationData.getBody()).thenReturn(notifiBody);
98
99         final String result = prepareJson(notificationData, schemaPathNotifi);
100
101         assertTrue(result.contains("ietf-restconf:notification"));
102         assertTrue(result.contains("event-time"));
103         assertTrue(result.contains("notifi-module:notifi-list"));
104         assertTrue(result.contains("lst"));
105         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
106     }
107
108     @Test
109     public void notifi_grpTest() throws Exception {
110         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
111
112         final DOMNotification notificationData = mock(DOMNotification.class);
113
114         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
115         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
116
117         when(notificationData.getType()).thenReturn(schemaPathNotifi);
118         when(notificationData.getBody()).thenReturn(notifiBody);
119
120         final String result = prepareJson(notificationData, schemaPathNotifi);
121
122         assertTrue(result.contains("ietf-restconf:notification"));
123         assertTrue(result.contains("event-time"));
124         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
125     }
126
127     @Test
128     public void notifi_augmTest() throws Exception {
129         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
130
131         final DOMNotification notificationData = mock(DOMNotification.class);
132
133         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
134         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
135
136         when(notificationData.getType()).thenReturn(schemaPathNotifi);
137         when(notificationData.getBody()).thenReturn(notifiBody);
138
139         final String result = prepareJson(notificationData, schemaPathNotifi);
140
141         assertTrue(result.contains("ietf-restconf:notification"));
142         assertTrue(result.contains("event-time"));
143         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
144     }
145
146     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
147         return Builders.mapEntryBuilder()
148             .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
149             .withChild(leaf)
150             .build();
151     }
152
153     private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
154         return Builders.containerBuilder()
155             .withNodeIdentifier(NodeIdentifier.create(contQName))
156             .withChild(child)
157             .build();
158     }
159
160     private static LeafNode<String> mockLeaf(final QName leafQName) {
161         return ImmutableNodes.leafNode(leafQName, "value");
162     }
163
164     private String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi)
165             throws Exception {
166         final var notifiAdapter = listenersBroker.registerNotificationListener(schemaPathNotifi, "json-stream",
167             NotificationOutputType.JSON);
168         return notifiAdapter.formatter().eventData(SCHEMA_CONTEXT, notificationData, Instant.now()).orElseThrow();
169     }
170 }