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;
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.mdsal.dom.api.DOMNotification;
27 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
28 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
29 import org.opendaylight.yangtools.util.SingletonSet;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 public class NotificationListenerTest {
47 private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23"));
49 private SchemaContext schmeaCtx;
52 public void init() throws Exception {
53 MockitoAnnotations.initMocks(this);
54 this.schmeaCtx = TestUtils.loadSchemaContext("/notifications");
58 public void notifi_leafTest() throws Exception {
59 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
61 final DOMNotification notificationData = mock(DOMNotification.class);
63 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
64 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
66 when(notificationData.getType()).thenReturn(schemaPathNotifi);
67 when(notificationData.getBody()).thenReturn(notifiBody);
69 final String result = prepareJson(notificationData, schemaPathNotifi);
71 assertTrue(result.contains("ietf-restconf:notification"));
72 assertTrue(result.contains("event-time"));
73 assertTrue(result.contains("notifi-module:notifi-leaf"));
74 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
78 public void notifi_cont_leafTest() throws Exception {
79 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
81 final DOMNotification notificationData = mock(DOMNotification.class);
83 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
84 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
85 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
87 when(notificationData.getType()).thenReturn(schemaPathNotifi);
88 when(notificationData.getBody()).thenReturn(notifiBody);
90 final String result = prepareJson(notificationData, schemaPathNotifi);
92 assertTrue(result.contains("ietf-restconf:notification"));
93 assertTrue(result.contains("event-time"));
94 assertTrue(result.contains("notifi-module:notifi-cont"));
95 assertTrue(result.contains("cont"));
96 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
100 public void notifi_list_Test() throws Exception {
101 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
103 final DOMNotification notificationData = mock(DOMNotification.class);
105 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
106 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
107 final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
108 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
109 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
111 when(notificationData.getType()).thenReturn(schemaPathNotifi);
112 when(notificationData.getBody()).thenReturn(notifiBody);
114 final String result = prepareJson(notificationData, schemaPathNotifi);
116 assertTrue(result.contains("ietf-restconf:notification"));
117 assertTrue(result.contains("event-time"));
118 assertTrue(result.contains("notifi-module:notifi-list"));
119 assertTrue(result.contains("lst"));
120 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
124 public void notifi_grpTest() throws Exception {
125 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
127 final DOMNotification notificationData = mock(DOMNotification.class);
129 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
130 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
132 when(notificationData.getType()).thenReturn(schemaPathNotifi);
133 when(notificationData.getBody()).thenReturn(notifiBody);
135 final String result = prepareJson(notificationData, schemaPathNotifi);
137 assertTrue(result.contains("ietf-restconf:notification"));
138 assertTrue(result.contains("event-time"));
139 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
143 public void notifi_augmTest() throws Exception {
144 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
146 final DOMNotification notificationData = mock(DOMNotification.class);
148 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
149 final AugmentationNode augm = mockAugm(leaf);
150 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
152 when(notificationData.getType()).thenReturn(schemaPathNotifi);
153 when(notificationData.getBody()).thenReturn(notifiBody);
155 final String result = prepareJson(notificationData, schemaPathNotifi);
157 assertTrue(result.contains("ietf-restconf:notification"));
158 assertTrue(result.contains("event-time"));
159 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
162 private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
163 final AugmentationNode augm = mock(AugmentationNode.class);
164 final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
165 when(augm.getIdentifier()).thenReturn(augmId);
167 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
170 when(augm.getValue()).thenReturn(childs);
174 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
175 final MapEntryNode entry = mock(MapEntryNode.class);
176 final Map<QName, Object> keyValues = new HashMap<>();
177 keyValues.put(leaf.getNodeType(), "value");
178 final NodeIdentifierWithPredicates nodeId = new NodeIdentifierWithPredicates(leaf.getNodeType(), keyValues);
179 when(entry.getIdentifier()).thenReturn(nodeId);
180 when(entry.getChild(any())).thenReturn(Optional.of(leaf));
182 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
185 when(entry.getValue()).thenReturn(childs);
189 private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
190 final MapNode list = mock(MapNode.class);
191 when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
192 when(list.getNodeType()).thenReturn(listQName);
193 when(list.getValue()).thenReturn(Lists.newArrayList(entries));
197 private static ContainerNode mockCont(final QName contQName,
198 final DataContainerChild<? extends PathArgument, ?> child) {
199 final ContainerNode cont = mock(ContainerNode.class);
200 when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
201 when(cont.getNodeType()).thenReturn(contQName);
203 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
205 when(cont.getValue()).thenReturn(childs);
209 private static LeafNode<String> mockLeaf(final QName leafQName) {
210 final LeafNode<String> child = mock(LeafNode.class);
211 when(child.getNodeType()).thenReturn(leafQName);
212 when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
213 when(child.getValue()).thenReturn("value");
217 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi) {
218 final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
219 schemaPathNotifi, "stream-name", NotificationOutputType.JSON);
220 final String result = notifiAdapter.prepareJson(schmeaCtx, notificationData);
221 return Preconditions.checkNotNull(result);