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