8b1ede41166f2e2dc9d986fa36cf9d05f3150148
[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 org.junit.Assert.assertTrue;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import com.google.common.base.Preconditions;
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.HashMap;
21 import java.util.Map;
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.MockitoAnnotations;
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.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
45
46 public class NotificationListenerTest {
47     private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23"));
48
49     private SchemaContext schmeaCtx;
50
51     @Before
52     public void init() throws Exception {
53         MockitoAnnotations.initMocks(this);
54         this.schmeaCtx = TestUtils.loadSchemaContext("/notifications");
55     }
56
57     @Test
58     public void notifi_leafTest() throws Exception {
59         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
60
61         final DOMNotification notificationData = mock(DOMNotification.class);
62
63         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
64         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
65
66         when(notificationData.getType()).thenReturn(schemaPathNotifi);
67         when(notificationData.getBody()).thenReturn(notifiBody);
68
69         final String result = prepareJson(notificationData, schemaPathNotifi);
70
71         assertTrue(result.contains("ietf-restconf:notification"));
72         assertTrue(result.contains("event-time"));
73         assertTrue(result.contains("notifi-module:notifi-leaf"));
74         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
75     }
76
77     @Test
78     public void notifi_cont_leafTest() throws Exception {
79         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
80
81         final DOMNotification notificationData = mock(DOMNotification.class);
82
83         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
84         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
85         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
86
87         when(notificationData.getType()).thenReturn(schemaPathNotifi);
88         when(notificationData.getBody()).thenReturn(notifiBody);
89
90         final String result = prepareJson(notificationData, schemaPathNotifi);
91
92         assertTrue(result.contains("ietf-restconf:notification"));
93         assertTrue(result.contains("event-time"));
94         assertTrue(result.contains("notifi-module:notifi-cont"));
95         assertTrue(result.contains("cont"));
96         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
97     }
98
99     @Test
100     public void notifi_list_Test() throws Exception {
101         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
102
103         final DOMNotification notificationData = mock(DOMNotification.class);
104
105         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
106         final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
107         final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
108         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
109         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
110
111         when(notificationData.getType()).thenReturn(schemaPathNotifi);
112         when(notificationData.getBody()).thenReturn(notifiBody);
113
114         final String result = prepareJson(notificationData, schemaPathNotifi);
115
116         assertTrue(result.contains("ietf-restconf:notification"));
117         assertTrue(result.contains("event-time"));
118         assertTrue(result.contains("notifi-module:notifi-list"));
119         assertTrue(result.contains("lst"));
120         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
121     }
122
123     @Test
124     public void notifi_grpTest() throws Exception {
125         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
126
127         final DOMNotification notificationData = mock(DOMNotification.class);
128
129         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
130         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
131
132         when(notificationData.getType()).thenReturn(schemaPathNotifi);
133         when(notificationData.getBody()).thenReturn(notifiBody);
134
135         final String result = prepareJson(notificationData, schemaPathNotifi);
136
137         assertTrue(result.contains("ietf-restconf:notification"));
138         assertTrue(result.contains("event-time"));
139         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
140     }
141
142     @Test
143     public void notifi_augmTest() throws Exception {
144         final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
145
146         final DOMNotification notificationData = mock(DOMNotification.class);
147
148         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
149         final AugmentationNode augm = mockAugm(leaf);
150         final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
151
152         when(notificationData.getType()).thenReturn(schemaPathNotifi);
153         when(notificationData.getBody()).thenReturn(notifiBody);
154
155         final String result = prepareJson(notificationData, schemaPathNotifi);
156
157         assertTrue(result.contains("ietf-restconf:notification"));
158         assertTrue(result.contains("event-time"));
159         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
160     }
161
162     private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
163         final AugmentationNode augm = mock(AugmentationNode.class);
164         final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
165         when(augm.getIdentifier()).thenReturn(augmId);
166
167         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
168         childs.add(leaf);
169
170         when(augm.getValue()).thenReturn(childs);
171         return augm;
172     }
173
174     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
175         final MapEntryNode entry = mock(MapEntryNode.class);
176         final Map<QName, Object> keyValues = new HashMap<>();
177         keyValues.put(leaf.getNodeType(), "value");
178         final NodeIdentifierWithPredicates nodeId = new NodeIdentifierWithPredicates(leaf.getNodeType(), keyValues);
179         when(entry.getIdentifier()).thenReturn(nodeId);
180         when(entry.getChild(any())).thenReturn(Optional.of(leaf));
181
182         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
183         childs.add(leaf);
184
185         when(entry.getValue()).thenReturn(childs);
186         return entry;
187     }
188
189     private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
190         final MapNode list = mock(MapNode.class);
191         when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
192         when(list.getNodeType()).thenReturn(listQName);
193         when(list.getValue()).thenReturn(Lists.newArrayList(entries));
194         return list;
195     }
196
197     private static ContainerNode mockCont(final QName contQName,
198                                           final DataContainerChild<? extends PathArgument, ?> child) {
199         final ContainerNode cont = mock(ContainerNode.class);
200         when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
201         when(cont.getNodeType()).thenReturn(contQName);
202
203         final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
204         childs.add(child);
205         when(cont.getValue()).thenReturn(childs);
206         return cont;
207     }
208
209     private static LeafNode<String> mockLeaf(final QName leafQName) {
210         final LeafNode<String> child = mock(LeafNode.class);
211         when(child.getNodeType()).thenReturn(leafQName);
212         when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
213         when(child.getValue()).thenReturn("value");
214         return child;
215     }
216
217     private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi) {
218         final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
219                 schemaPathNotifi, "stream-name", NotificationOutputType.JSON);
220         final String result = notifiAdapter.prepareJson(schmeaCtx, notificationData);
221         return Preconditions.checkNotNull(result);
222     }
223 }