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.Optional;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.mdsal.dom.api.DOMNotification;
25 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
26 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
27 import org.opendaylight.yangtools.util.SingletonSet;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 public class NotificationListenerTest {
45 private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23"));
47 private SchemaContext schmeaCtx;
50 public void init() throws Exception {
51 MockitoAnnotations.initMocks(this);
52 this.schmeaCtx = TestUtils.loadSchemaContext("/notifications");
56 public void notifi_leafTest() throws Exception {
57 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
59 final DOMNotification notificationData = mock(DOMNotification.class);
61 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
62 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
64 when(notificationData.getType()).thenReturn(schemaPathNotifi);
65 when(notificationData.getBody()).thenReturn(notifiBody);
67 final String result = prepareJson(notificationData, schemaPathNotifi);
69 assertTrue(result.contains("ietf-restconf:notification"));
70 assertTrue(result.contains("event-time"));
71 assertTrue(result.contains("notifi-module:notifi-leaf"));
72 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
76 public void notifi_cont_leafTest() throws Exception {
77 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
79 final DOMNotification notificationData = mock(DOMNotification.class);
81 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
82 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
83 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
85 when(notificationData.getType()).thenReturn(schemaPathNotifi);
86 when(notificationData.getBody()).thenReturn(notifiBody);
88 final String result = prepareJson(notificationData, schemaPathNotifi);
90 assertTrue(result.contains("ietf-restconf:notification"));
91 assertTrue(result.contains("event-time"));
92 assertTrue(result.contains("notifi-module:notifi-cont"));
93 assertTrue(result.contains("cont"));
94 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
98 public void notifi_list_Test() throws Exception {
99 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
101 final DOMNotification notificationData = mock(DOMNotification.class);
103 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
104 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
105 final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
106 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
107 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
109 when(notificationData.getType()).thenReturn(schemaPathNotifi);
110 when(notificationData.getBody()).thenReturn(notifiBody);
112 final String result = prepareJson(notificationData, schemaPathNotifi);
114 assertTrue(result.contains("ietf-restconf:notification"));
115 assertTrue(result.contains("event-time"));
116 assertTrue(result.contains("notifi-module:notifi-list"));
117 assertTrue(result.contains("lst"));
118 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
122 public void notifi_grpTest() throws Exception {
123 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
125 final DOMNotification notificationData = mock(DOMNotification.class);
127 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
128 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
130 when(notificationData.getType()).thenReturn(schemaPathNotifi);
131 when(notificationData.getBody()).thenReturn(notifiBody);
133 final String result = prepareJson(notificationData, schemaPathNotifi);
135 assertTrue(result.contains("ietf-restconf:notification"));
136 assertTrue(result.contains("event-time"));
137 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
141 public void notifi_augmTest() throws Exception {
142 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
144 final DOMNotification notificationData = mock(DOMNotification.class);
146 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
147 final AugmentationNode augm = mockAugm(leaf);
148 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
150 when(notificationData.getType()).thenReturn(schemaPathNotifi);
151 when(notificationData.getBody()).thenReturn(notifiBody);
153 final String result = prepareJson(notificationData, schemaPathNotifi);
155 assertTrue(result.contains("ietf-restconf:notification"));
156 assertTrue(result.contains("event-time"));
157 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
160 private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
161 final AugmentationNode augm = mock(AugmentationNode.class);
162 final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
163 when(augm.getIdentifier()).thenReturn(augmId);
165 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
168 when(augm.getValue()).thenReturn(childs);
172 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
173 final MapEntryNode entry = mock(MapEntryNode.class);
174 final NodeIdentifierWithPredicates nodeId = NodeIdentifierWithPredicates.of(leaf.getNodeType(),
175 leaf.getNodeType(), "value");
176 when(entry.getIdentifier()).thenReturn(nodeId);
177 when(entry.getChild(any())).thenReturn(Optional.of(leaf));
179 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
182 when(entry.getValue()).thenReturn(childs);
186 private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
187 final MapNode list = mock(MapNode.class);
188 when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
189 when(list.getNodeType()).thenReturn(listQName);
190 when(list.getValue()).thenReturn(Lists.newArrayList(entries));
194 private static ContainerNode mockCont(final QName contQName,
195 final DataContainerChild<? extends PathArgument, ?> child) {
196 final ContainerNode cont = mock(ContainerNode.class);
197 when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
198 when(cont.getNodeType()).thenReturn(contQName);
200 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
202 when(cont.getValue()).thenReturn(childs);
206 private static LeafNode<String> mockLeaf(final QName leafQName) {
207 final LeafNode<String> child = mock(LeafNode.class);
208 when(child.getNodeType()).thenReturn(leafQName);
209 when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
210 when(child.getValue()).thenReturn("value");
214 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi) {
215 final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
216 schemaPathNotifi, "stream-name", NotificationOutputType.JSON);
217 final String result = notifiAdapter.prepareJson(schmeaCtx, notificationData);
218 return Preconditions.checkNotNull(result);