Merge "Bug 9092: revert to org.json temporarily"
[netconf.git] / restconf / sal-rest-connector / 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.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Lists;
18 import java.net.URI;
19 import java.text.ParseException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
29 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
32 import org.opendaylight.yangtools.util.SingletonSet;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
48
49 public class NotificationListenerTest {
50     private static final QNameModule MODULE;
51
52     static {
53         try {
54             MODULE = QNameModule.create(URI.create("notifi:mod"),
55                 SimpleDateFormatUtil.getRevisionFormat().parse("2016-11-23"));
56         } catch (ParseException e) {
57             throw new ExceptionInInitializerError(e);
58         }
59     }
60
61     private SchemaContext schmeaCtx;
62
63     @Before
64     public void init() throws Exception {
65         MockitoAnnotations.initMocks(this);
66         ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications"));
67         this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema();
68     }
69
70     @Test
71     public void notifi_leafTest() throws Exception {
72         final SchemaPath schemaPathNotifi = SchemaPath.create(false, 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.getLastComponent(), 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 SchemaPath schemaPathNotifi = SchemaPath.create(false, 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.getLastComponent(), 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 SchemaPath schemaPathNotifi = SchemaPath.create(false, 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.getLastComponent(), 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 SchemaPath schemaPathNotifi = SchemaPath.create(false, 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.getLastComponent(), 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 SchemaPath schemaPathNotifi = SchemaPath.create(false, 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.getLastComponent(), 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 = new NodeIdentifierWithPredicates(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 SchemaPath schemaPathNotifi)
231             throws Exception {
232         final List<SchemaPath> paths = new ArrayList<>();
233         paths.add(schemaPathNotifi);
234         final List<NotificationListenerAdapter> listNotifi =
235                 Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString());
236         final NotificationListenerAdapter notifi = listNotifi.get(0);
237         notifi.setNotification(notificationData);
238         notifi.setSchemaContext(this.schmeaCtx);
239         final String result = notifi.prepareJson();
240         return Preconditions.checkNotNull(result);
241     }
242 }