Integrate AbstractCommonSubscriber more tightly
[netconf.git] / restconf / restconf-nb-rfc8040 / 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 java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Set;
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;
44
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class JsonNotificationListenerTest {
47     private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class);
48
49     private static final QNameModule MODULE =
50         QNameModule.create(XMLNamespace.of("notifi:mod"), Revision.of("2016-11-23"));
51
52     private static EffectiveModelContext SCHEMA_CONTEXT;
53
54     @BeforeClass
55     public static void beforeClass() throws Exception {
56         SCHEMA_CONTEXT = TestUtils.loadSchemaContext("/notifications");
57     }
58
59     @AfterClass
60     public static void afterClass() {
61         SCHEMA_CONTEXT = null;
62     }
63
64     @Test
65     public void notifi_leafTest() throws Exception {
66         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
67
68         final DOMNotification notificationData = mock(DOMNotification.class);
69
70         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
71         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
72
73         when(notificationData.getType()).thenReturn(schemaPathNotifi);
74         when(notificationData.getBody()).thenReturn(notifiBody);
75
76         final String result = prepareJson(notificationData, schemaPathNotifi);
77
78         LOG.info("json result: {}", result);
79
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"));
84     }
85
86     @Test
87     public void notifi_cont_leafTest() throws Exception {
88         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
89
90         final DOMNotification notificationData = mock(DOMNotification.class);
91
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);
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-cont"));
104         assertTrue(result.contains("cont"));
105         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
106     }
107
108     @Test
109     public void notifi_list_Test() throws Exception {
110         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
111
112         final DOMNotification notificationData = mock(DOMNotification.class);
113
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")))
118             .withChild(entry)
119             .build());
120
121         when(notificationData.getType()).thenReturn(schemaPathNotifi);
122         when(notificationData.getBody()).thenReturn(notifiBody);
123
124         final String result = prepareJson(notificationData, schemaPathNotifi);
125
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"));
131     }
132
133     @Test
134     public void notifi_grpTest() throws Exception {
135         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
136
137         final DOMNotification notificationData = mock(DOMNotification.class);
138
139         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
140         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
141
142         when(notificationData.getType()).thenReturn(schemaPathNotifi);
143         when(notificationData.getBody()).thenReturn(notifiBody);
144
145         final String result = prepareJson(notificationData, schemaPathNotifi);
146
147         assertTrue(result.contains("ietf-restconf:notification"));
148         assertTrue(result.contains("event-time"));
149         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
150     }
151
152     @Test
153     public void notifi_augmTest() throws Exception {
154         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
155
156         final DOMNotification notificationData = mock(DOMNotification.class);
157
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);
161
162         when(notificationData.getType()).thenReturn(schemaPathNotifi);
163         when(notificationData.getBody()).thenReturn(notifiBody);
164
165         final String result = prepareJson(notificationData, schemaPathNotifi);
166
167         assertTrue(result.contains("ietf-restconf:notification"));
168         assertTrue(result.contains("event-time"));
169         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
170     }
171
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);
176
177         final Collection<DataContainerChild> childs = new ArrayList<>();
178         childs.add(leaf);
179
180         when(augm.body()).thenReturn(childs);
181         return augm;
182     }
183
184     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
185         return Builders.mapEntryBuilder()
186             .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.getIdentifier().getNodeType(),
187                 leaf.body()))
188             .withChild(leaf)
189             .build();
190     }
191
192     private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
193         return Builders.containerBuilder()
194             .withNodeIdentifier(NodeIdentifier.create(contQName))
195             .withChild(child)
196             .build();
197     }
198
199     private static LeafNode<String> mockLeaf(final QName leafQName) {
200         return ImmutableNodes.leafNode(leafQName, "value");
201     }
202
203     private static String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi)
204             throws Exception {
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();
208     }
209 }