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.Preconditions;
16 import com.google.common.collect.Lists;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.List;
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;
48 public class NotificationListenerTest {
49 private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23"));
51 private SchemaContext schmeaCtx;
54 public void init() throws Exception {
55 MockitoAnnotations.initMocks(this);
56 ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications"));
57 this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema();
61 public void notifi_leafTest() throws Exception {
62 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
64 final DOMNotification notificationData = mock(DOMNotification.class);
66 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
67 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
69 when(notificationData.getType()).thenReturn(schemaPathNotifi);
70 when(notificationData.getBody()).thenReturn(notifiBody);
72 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
81 public void notifi_cont_leafTest() throws Exception {
82 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
84 final DOMNotification notificationData = mock(DOMNotification.class);
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);
90 when(notificationData.getType()).thenReturn(schemaPathNotifi);
91 when(notificationData.getBody()).thenReturn(notifiBody);
93 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
103 public void notifi_list_Test() throws Exception {
104 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
106 final DOMNotification notificationData = mock(DOMNotification.class);
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);
114 when(notificationData.getType()).thenReturn(schemaPathNotifi);
115 when(notificationData.getBody()).thenReturn(notifiBody);
117 final String result = prepareJson(notificationData, schemaPathNotifi);
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"));
127 public void notifi_grpTest() throws Exception {
128 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
130 final DOMNotification notificationData = mock(DOMNotification.class);
132 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
133 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
135 when(notificationData.getType()).thenReturn(schemaPathNotifi);
136 when(notificationData.getBody()).thenReturn(notifiBody);
138 final String result = prepareJson(notificationData, schemaPathNotifi);
140 assertTrue(result.contains("ietf-restconf:notification"));
141 assertTrue(result.contains("event-time"));
142 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
146 public void notifi_augmTest() throws Exception {
147 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
149 final DOMNotification notificationData = mock(DOMNotification.class);
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);
155 when(notificationData.getType()).thenReturn(schemaPathNotifi);
156 when(notificationData.getBody()).thenReturn(notifiBody);
158 final String result = prepareJson(notificationData, schemaPathNotifi);
160 assertTrue(result.contains("ietf-restconf:notification"));
161 assertTrue(result.contains("event-time"));
162 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
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);
170 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
173 when(augm.getValue()).thenReturn(childs);
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));
185 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
188 when(entry.getValue()).thenReturn(childs);
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));
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);
206 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
208 when(cont.getValue()).thenReturn(childs);
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");
220 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
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);