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