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