Eliminate unnecessary blocking checks
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / NotificationListenerTest.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 java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import com.google.common.collect.Lists;
17 import java.net.URI;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Optional;
21 import org.junit.AfterClass;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.dom.api.DOMNotification;
27 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
28 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
29 import org.opendaylight.yangtools.util.SingletonSet;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
43 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
44 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
45
46 @RunWith(MockitoJUnitRunner.StrictStubs.class)
47 public class NotificationListenerTest {
48     private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23"));
49
50     private static EffectiveModelContext SCHEMA_CONTEXT;
51
52     @BeforeClass
53     public static void beforeClass() throws Exception {
54         SCHEMA_CONTEXT = TestUtils.loadSchemaContext("/notifications");
55     }
56
57     @AfterClass
58     public static void afterClass() {
59         SCHEMA_CONTEXT = null;
60     }
61
62     @Test
63     public void notifi_leafTest() throws Exception {
64         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
65
66         final DOMNotification notificationData = mock(DOMNotification.class);
67
68         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
69         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
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-leaf"));
79         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
80     }
81
82     @Test
83     public void notifi_cont_leafTest() throws Exception {
84         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
85
86         final DOMNotification notificationData = mock(DOMNotification.class);
87
88         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
89         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
90         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
91
92         when(notificationData.getType()).thenReturn(schemaPathNotifi);
93         when(notificationData.getBody()).thenReturn(notifiBody);
94
95         final String result = prepareJson(notificationData, schemaPathNotifi);
96
97         assertTrue(result.contains("ietf-restconf:notification"));
98         assertTrue(result.contains("event-time"));
99         assertTrue(result.contains("notifi-module:notifi-cont"));
100         assertTrue(result.contains("cont"));
101         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
102     }
103
104     @Test
105     public void notifi_list_Test() throws Exception {
106         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
107
108         final DOMNotification notificationData = mock(DOMNotification.class);
109
110         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
111         final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
112         final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
113         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
114         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
115
116         when(notificationData.getType()).thenReturn(schemaPathNotifi);
117         when(notificationData.getBody()).thenReturn(notifiBody);
118
119         final String result = prepareJson(notificationData, schemaPathNotifi);
120
121         assertTrue(result.contains("ietf-restconf:notification"));
122         assertTrue(result.contains("event-time"));
123         assertTrue(result.contains("notifi-module:notifi-list"));
124         assertTrue(result.contains("lst"));
125         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
126     }
127
128     @Test
129     public void notifi_grpTest() throws Exception {
130         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
131
132         final DOMNotification notificationData = mock(DOMNotification.class);
133
134         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
135         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
136
137         when(notificationData.getType()).thenReturn(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" + '"' + ":" + '"' + "value"));
145     }
146
147     @Test
148     public void notifi_augmTest() throws Exception {
149         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
150
151         final DOMNotification notificationData = mock(DOMNotification.class);
152
153         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
154         final AugmentationNode augm = mockAugm(leaf);
155         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), augm);
156
157         when(notificationData.getType()).thenReturn(schemaPathNotifi);
158         when(notificationData.getBody()).thenReturn(notifiBody);
159
160         final String result = prepareJson(notificationData, schemaPathNotifi);
161
162         assertTrue(result.contains("ietf-restconf:notification"));
163         assertTrue(result.contains("event-time"));
164         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
165     }
166
167     private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
168         final AugmentationNode augm = mock(AugmentationNode.class);
169         final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
170         when(augm.getIdentifier()).thenReturn(augmId);
171
172         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
173         childs.add(leaf);
174
175         when(augm.getValue()).thenReturn(childs);
176         return augm;
177     }
178
179     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
180         final MapEntryNode entry = mock(MapEntryNode.class);
181         final NodeIdentifierWithPredicates nodeId = NodeIdentifierWithPredicates.of(leaf.getNodeType(),
182             leaf.getNodeType(), "value");
183         when(entry.getIdentifier()).thenReturn(nodeId);
184         when(entry.getChild(any())).thenReturn(Optional.of(leaf));
185
186         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
187         childs.add(leaf);
188
189         when(entry.getValue()).thenReturn(childs);
190         return entry;
191     }
192
193     private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
194         final MapNode list = mock(MapNode.class);
195         when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
196         when(list.getValue()).thenReturn(Lists.newArrayList(entries));
197         return list;
198     }
199
200     private static ContainerNode mockCont(final QName contQName,
201                                           final DataContainerChild<? extends PathArgument, ?> child) {
202         final ContainerNode cont = mock(ContainerNode.class);
203         when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
204
205         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
206         childs.add(child);
207         when(cont.getValue()).thenReturn(childs);
208         return cont;
209     }
210
211     private static LeafNode<String> mockLeaf(final QName leafQName) {
212         final LeafNode<String> child = mock(LeafNode.class);
213         when(child.getNodeType()).thenReturn(leafQName);
214         when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
215         when(child.getValue()).thenReturn("value");
216         return child;
217     }
218
219     private static String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi) {
220         final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
221                 schemaPathNotifi, "stream-name", NotificationOutputType.JSON);
222         return requireNonNull(notifiAdapter.prepareJson(SCHEMA_CONTEXT, notificationData));
223     }
224 }