Eliminate SubscribeToStreamUtil
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / streams / 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;
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 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;
33
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class JsonNotificationListenerTest extends AbstractNotificationListenerTest {
36     private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class);
37
38     private final ListenersBroker listenersBroker = new ListenersBroker.ServerSentEvents();
39
40     @Test
41     public void notifi_leafTest() throws Exception {
42         final QName schemaPathNotifi = QName.create(MODULE, "notifi-leaf");
43
44         final DOMNotification notificationData = mock(DOMNotification.class);
45
46         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
47         final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
48
49         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
50         when(notificationData.getBody()).thenReturn(notifiBody);
51
52         final String result = prepareJson(notificationData, schemaPathNotifi);
53
54         LOG.info("json result: {}", result);
55
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"));
60     }
61
62     @Test
63     public void notifi_cont_leafTest() throws Exception {
64         final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
65
66         final DOMNotification notificationData = mock(DOMNotification.class);
67
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);
71
72         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
73         when(notificationData.getBody()).thenReturn(notifiBody);
74
75         final String result = prepareJson(notificationData, schemaPathNotifi);
76
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"));
82     }
83
84     @Test
85     public void notifi_list_Test() throws Exception {
86         final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
87
88         final DOMNotification notificationData = mock(DOMNotification.class);
89
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")))
94             .withChild(entry)
95             .build());
96
97         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
98         when(notificationData.getBody()).thenReturn(notifiBody);
99
100         final String result = prepareJson(notificationData, schemaPathNotifi);
101
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"));
107     }
108
109     @Test
110     public void notifi_grpTest() throws Exception {
111         final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
112
113         final DOMNotification notificationData = mock(DOMNotification.class);
114
115         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
116         final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
117
118         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
119         when(notificationData.getBody()).thenReturn(notifiBody);
120
121         final String result = prepareJson(notificationData, schemaPathNotifi);
122
123         assertTrue(result.contains("ietf-restconf:notification"));
124         assertTrue(result.contains("event-time"));
125         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
126     }
127
128     @Test
129     public void notifi_augmTest() throws Exception {
130         final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
131
132         final DOMNotification notificationData = mock(DOMNotification.class);
133
134         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
135         final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
136
137         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
138         when(notificationData.getBody()).thenReturn(notifiBody);
139
140         final String result = prepareJson(notificationData, schemaPathNotifi);
141
142         assertTrue(result.contains("ietf-restconf:notification"));
143         assertTrue(result.contains("event-time"));
144         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
145     }
146
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()))
150             .withChild(leaf)
151             .build();
152     }
153
154     private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
155         return Builders.containerBuilder()
156             .withNodeIdentifier(NodeIdentifier.create(contQName))
157             .withChild(child)
158             .build();
159     }
160
161     private static LeafNode<String> mockLeaf(final QName leafQName) {
162         return ImmutableNodes.leafNode(leafQName, "value");
163     }
164
165     private String prepareJson(final DOMNotification notificationData, final QName schemaPathNotifi)
166             throws Exception {
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();
170     }
171 }