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