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;
53 MODULE = QNameModule.create(URI.create("notifi:mod"),
54 SimpleDateFormatUtil.getRevisionFormat().parse("2016-11-23"));
55 } catch (ParseException e) {
56 throw new ExceptionInInitializerError(e);
60 private SchemaContext schmeaCtx;
63 public void init() throws Exception {
64 MockitoAnnotations.initMocks(this);
65 ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications"));
66 this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema();
70 public void notifi_leafTest() throws Exception {
71 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-leaf"));
73 final DOMNotification notificationData = mock(DOMNotification.class);
75 final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf"));
76 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
78 when(notificationData.getType()).thenReturn(schemaPathNotifi);
79 when(notificationData.getBody()).thenReturn(notifiBody);
81 final String result = prepareJson(notificationData, schemaPathNotifi);
83 assertTrue(result.contains("ietf-restconf:notification"));
84 assertTrue(result.contains("event-time"));
85 assertTrue(result.contains("notifi-module:notifi-leaf"));
86 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
90 public void notifi_cont_leafTest() throws Exception {
91 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-cont"));
93 final DOMNotification notificationData = mock(DOMNotification.class);
95 final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf"));
96 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
97 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
99 when(notificationData.getType()).thenReturn(schemaPathNotifi);
100 when(notificationData.getBody()).thenReturn(notifiBody);
102 final String result = prepareJson(notificationData, schemaPathNotifi);
104 assertTrue(result.contains("ietf-restconf:notification"));
105 assertTrue(result.contains("event-time"));
106 assertTrue(result.contains("notifi-module:notifi-cont"));
107 assertTrue(result.contains("cont"));
108 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
112 public void notifi_list_Test() throws Exception {
113 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-list"));
115 final DOMNotification notificationData = mock(DOMNotification.class);
117 final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf"));
118 final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
119 final MapNode list = mockList(QName.create(MODULE, "lst"), entry);
120 final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), list);
121 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), cont);
123 when(notificationData.getType()).thenReturn(schemaPathNotifi);
124 when(notificationData.getBody()).thenReturn(notifiBody);
126 final String result = prepareJson(notificationData, schemaPathNotifi);
128 assertTrue(result.contains("ietf-restconf:notification"));
129 assertTrue(result.contains("event-time"));
130 assertTrue(result.contains("notifi-module:notifi-list"));
131 assertTrue(result.contains("lst"));
132 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
136 public void notifi_grpTest() throws Exception {
137 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-grp"));
139 final DOMNotification notificationData = mock(DOMNotification.class);
141 final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf"));
142 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), leaf);
144 when(notificationData.getType()).thenReturn(schemaPathNotifi);
145 when(notificationData.getBody()).thenReturn(notifiBody);
147 final String result = prepareJson(notificationData, schemaPathNotifi);
149 assertTrue(result.contains("ietf-restconf:notification"));
150 assertTrue(result.contains("event-time"));
151 assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
155 public void notifi_augmTest() throws Exception {
156 final SchemaPath schemaPathNotifi = SchemaPath.create(false, QName.create(MODULE, "notifi-augm"));
158 final DOMNotification notificationData = mock(DOMNotification.class);
160 final LeafNode leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
161 final AugmentationNode augm = mockAugm(leaf);
162 final ContainerNode notifiBody = mockCont(schemaPathNotifi.getLastComponent(), augm);
164 when(notificationData.getType()).thenReturn(schemaPathNotifi);
165 when(notificationData.getBody()).thenReturn(notifiBody);
167 final String result = prepareJson(notificationData, schemaPathNotifi);
169 assertTrue(result.contains("ietf-restconf:notification"));
170 assertTrue(result.contains("event-time"));
171 assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
174 private static AugmentationNode mockAugm(final LeafNode leaf) {
175 final AugmentationNode augm = mock(AugmentationNode.class);
176 final AugmentationIdentifier augmId = new AugmentationIdentifier(SingletonSet.of(leaf.getNodeType()));
177 when(augm.getIdentifier()).thenReturn(augmId);
179 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
182 when(augm.getValue()).thenReturn(childs);
186 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode leaf) {
187 final MapEntryNode entry = mock(MapEntryNode.class);
188 final Map<QName, Object> keyValues = new HashMap<>();
189 keyValues.put(leaf.getNodeType(), "value");
190 final NodeIdentifierWithPredicates nodeId = new NodeIdentifierWithPredicates(leaf.getNodeType(), keyValues);
191 when(entry.getIdentifier()).thenReturn(nodeId);
192 when(entry.getChild(any())).thenReturn(Optional.of(leaf));
194 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
197 when(entry.getValue()).thenReturn(childs);
201 private static MapNode mockList(final QName listQName, final MapEntryNode... entries) {
202 final MapNode list = mock(MapNode.class);
203 when(list.getIdentifier()).thenReturn(NodeIdentifier.create(listQName));
204 when(list.getNodeType()).thenReturn(listQName);
205 when(list.getValue()).thenReturn(Lists.newArrayList(entries));
209 private static ContainerNode mockCont(final QName contQName, final DataContainerChild<? extends PathArgument, ?> child) {
210 final ContainerNode cont = mock(ContainerNode.class);
211 when(cont.getIdentifier()).thenReturn(NodeIdentifier.create(contQName));
212 when(cont.getNodeType()).thenReturn(contQName);
214 final Collection<DataContainerChild<? extends PathArgument, ?>> childs = new ArrayList<>();
216 when(cont.getValue()).thenReturn(childs);
220 private static LeafNode mockLeaf(final QName leafQName) {
221 final LeafNode child = mock(LeafNode.class);
222 when(child.getNodeType()).thenReturn(leafQName);
223 when(child.getIdentifier()).thenReturn(NodeIdentifier.create(leafQName));
224 when(child.getValue()).thenReturn("value");
228 private String prepareJson(final DOMNotification notificationData, final SchemaPath schemaPathNotifi)
230 final List<SchemaPath> paths = new ArrayList<>();
231 paths.add(schemaPathNotifi);
232 final List<NotificationListenerAdapter> listNotifi =
233 Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString());
234 final NotificationListenerAdapter notifi = listNotifi.get(0);
235 notifi.setNotification(notificationData);
236 notifi.setSchemaContext(this.schmeaCtx);
237 final String result = notifi.prepareJson();
238 return Preconditions.checkNotNull(result);