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.restconf.nb.rfc8040.streams.listeners;
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;
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.mdsal.dom.api.DOMNotification;
28 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
29 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
30 import org.opendaylight.yangtools.util.SingletonSet;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.common.Revision;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
38 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
41 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 public class NotificationListenerTest {
48 private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23"));
50 private SchemaContext schmeaCtx;
53 public void init() throws Exception {
54 MockitoAnnotations.initMocks(this);
55 this.schmeaCtx = TestUtils.loadSchemaContext("/notifications");
59 public void notifi_leafTest() throws Exception {
60 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
62 final DOMNotification notificationData = mock(DOMNotification.class);
64 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
65 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
67 when(notificationData.getType()).thenReturn(schemaPathNotifi);
68 when(notificationData.getBody()).thenReturn(notifiBody);
70 final String result = prepareJson(notificationData, schemaPathNotifi);
72 assertTrue(result.contains("ietf-restconf:notification"));
73 assertTrue(result.contains("event-time"));
74 assertTrue(result.contains("notifi-module:notifi-leaf"));
75 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
79 public void notifi_cont_leafTest() throws Exception {
80 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
82 final DOMNotification notificationData = mock(DOMNotification.class);
84 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
85 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
86 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
88 when(notificationData.getType()).thenReturn(schemaPathNotifi);
89 when(notificationData.getBody()).thenReturn(notifiBody);
91 final String result = prepareJson(notificationData, schemaPathNotifi);
93 assertTrue(result.contains("ietf-restconf:notification"));
94 assertTrue(result.contains("event-time"));
95 assertTrue(result.contains("notifi-module:notifi-cont"));
96 assertTrue(result.contains("cont"));
97 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
101 public void notifi_list_Test() throws Exception {
102 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
104 final DOMNotification notificationData = mock(DOMNotification.class);
106 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
107 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
108 final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
109 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
110 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
112 when(notificationData.getType()).thenReturn(schemaPathNotifi);
113 when(notificationData.getBody()).thenReturn(notifiBody);
115 final String result = prepareJson(notificationData, schemaPathNotifi);
117 assertTrue(result.contains("ietf-restconf:notification"));
118 assertTrue(result.contains("event-time"));
119 assertTrue(result.contains("notifi-module:notifi-list"));
120 assertTrue(result.contains("lst"));
121 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
125 public void notifi_grpTest() throws Exception {
126 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
128 final DOMNotification notificationData = mock(DOMNotification.class);
130 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
131 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
133 when(notificationData.getType()).thenReturn(schemaPathNotifi);
134 when(notificationData.getBody()).thenReturn(notifiBody);
136 final String result = prepareJson(notificationData, schemaPathNotifi);
138 assertTrue(result.contains("ietf-restconf:notification"));
139 assertTrue(result.contains("event-time"));
140 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
144 public void notifi_augmTest() throws Exception {
145 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
147 final DOMNotification notificationData = mock(DOMNotification.class);
149 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
150 final AugmentationNode augm = mockAugm(leaf);
151 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
153 when(notificationData.getType()).thenReturn(schemaPathNotifi);
154 when(notificationData.getBody()).thenReturn(notifiBody);
156 final String result = prepareJson(notificationData, schemaPathNotifi);
158 assertTrue(result.contains("ietf-restconf:notification"));
159 assertTrue(result.contains("event-time"));
160 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
163 private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
164 final AugmentationNode augm = mock(AugmentationNode.class);
165 final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
166 when(augm.getIdentifier()).thenReturn(augmId);
168 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
171 when(augm.getValue()).thenReturn(childs);
175 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
176 final MapEntryNode entry = mock(MapEntryNode.class);
177 final Map<QName, Object> keyValues = new HashMap<>();
178 keyValues.put(leaf.getNodeType(), "value");
179 final NodeIdentifierWithPredicates nodeId = new NodeIdentifierWithPredicates(leaf.getNodeType(), keyValues);
180 when(entry.getIdentifier()).thenReturn(nodeId);
181 when(entry.getChild(any())).thenReturn(Optional.of(leaf));
183 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
186 when(entry.getValue()).thenReturn(childs);
190 private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
191 final MapNode list = mock(MapNode.class);
192 when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
193 when(list.getNodeType()).thenReturn(listQName);
194 when(list.getValue()).thenReturn(Lists.newArrayList(entries));
198 private static ContainerNode mockCont(final QName contQName,
199 final DataContainerChild<? extends PathArgument, ?> child) {
200 final ContainerNode cont = mock(ContainerNode.class);
201 when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
202 when(cont.getNodeType()).thenReturn(contQName);
204 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
206 when(cont.getValue()).thenReturn(childs);
210 private static LeafNode<String> mockLeaf(final QName leafQName) {
211 final LeafNode<String> child = mock(LeafNode.class);
212 when(child.getNodeType()).thenReturn(leafQName);
213 when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
214 when(child.getValue()).thenReturn("value");
218 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
220 final List<SchemaPath> paths = new ArrayList<>();
221 paths.add(schemaPathNotifi);
222 final List<NotificationListenerAdapter> listNotifi =
223 Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString());
224 final NotificationListenerAdapter notifi = listNotifi.get(0);
225 final String result = notifi.prepareJson(schmeaCtx, notificationData);
226 return Preconditions.checkNotNull(result);