2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.streams.listeners;
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;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Lists;
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;
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;
49 public class NotificationListenerTest {
50 private static final QNameModule MODULE;
54 MODULE = QNameModule.create(URI.create("notifi:mod"),
55 SimpleDateFormatUtil.getRevisionFormat().parse("2016-11-23"));
56 } catch (final ParseException e) {
57 throw new ExceptionInInitializerError(e);
61 private SchemaContext schmeaCtx;
64 public void init() throws Exception {
65 MockitoAnnotations.initMocks(this);
66 ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications"));
67 this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema();
71 public void notifi_leafTest() throws Exception {
72 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
74 final DOMNotification notificationData = mock(DOMNotification.class);
76 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
77 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
79 when(notificationData.getType()).thenReturn(schemaPathNotifi);
80 when(notificationData.getBody()).thenReturn(notifiBody);
82 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
91 public void notifi_cont_leafTest() throws Exception {
92 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
94 final DOMNotification notificationData = mock(DOMNotification.class);
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);
100 when(notificationData.getType()).thenReturn(schemaPathNotifi);
101 when(notificationData.getBody()).thenReturn(notifiBody);
103 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
113 public void notifi_list_Test() throws Exception {
114 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
116 final DOMNotification notificationData = mock(DOMNotification.class);
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);
124 when(notificationData.getType()).thenReturn(schemaPathNotifi);
125 when(notificationData.getBody()).thenReturn(notifiBody);
127 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
137 public void notifi_grpTest() throws Exception {
138 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
140 final DOMNotification notificationData = mock(DOMNotification.class);
142 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
143 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
145 when(notificationData.getType()).thenReturn(schemaPathNotifi);
146 when(notificationData.getBody()).thenReturn(notifiBody);
148 final String result = prepareJson(notificationData, schemaPathNotifi);
150 assertTrue(result.contains("ietf-restconf:notification"));
151 assertTrue(result.contains("event-time"));
152 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
156 public void notifi_augmTest() throws Exception {
157 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
159 final DOMNotification notificationData = mock(DOMNotification.class);
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);
165 when(notificationData.getType()).thenReturn(schemaPathNotifi);
166 when(notificationData.getBody()).thenReturn(notifiBody);
168 final String result = prepareJson(notificationData, schemaPathNotifi);
170 assertTrue(result.contains("ietf-restconf:notification"));
171 assertTrue(result.contains("event-time"));
172 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
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);
180 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
183 when(augm.getValue()).thenReturn(childs);
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));
195 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
198 when(entry.getValue()).thenReturn(childs);
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));
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);
216 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
218 when(cont.getValue()).thenReturn(childs);
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");
230 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
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);