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