732c327864ed61da91a1cf659aa2ed103c25ab4e
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / netconf / sal / streams / listeners / NotificationListenerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.streams.listeners;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.io.FileNotFoundException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Set;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
26 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
27 import org.opendaylight.mdsal.dom.api.DOMNotification;
28 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
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.common.XMLNamespace;
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.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.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
46
47 @RunWith(MockitoJUnitRunner.StrictStubs.class)
48 public class NotificationListenerTest {
49     private static final QNameModule MODULE = QNameModule.create(XMLNamespace.of("notifi:mod"),
50         Revision.of("2016-11-23"));
51
52     private static EffectiveModelContext schemaContext;
53
54     private ControllerContext controllerContext;
55
56     @BeforeClass
57     public static void staticInit() throws FileNotFoundException {
58         schemaContext = TestUtils.loadSchemaContext("/notifications");
59     }
60
61     @Before
62     public void init() {
63         controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
64     }
65
66     @Test
67     public void notifi_leafTest() {
68         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
69
70         final DOMNotification notificationData = mock(DOMNotification.class);
71
72         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
73         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
74
75         when(notificationData.getType()).thenReturn(schemaPathNotifi);
76         when(notificationData.getBody()).thenReturn(notifiBody);
77
78         final String result = prepareJson(notificationData, schemaPathNotifi);
79
80         assertTrue(result.contains("ietf-restconf:notification"));
81         assertTrue(result.contains("event-time"));
82         assertTrue(result.contains("notifi-module:notifi-leaf"));
83         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
84     }
85
86     @Test
87     public void notifi_cont_leafTest() {
88         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
89
90         final DOMNotification notificationData = mock(DOMNotification.class);
91
92         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
93         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
94         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
95
96         when(notificationData.getType()).thenReturn(schemaPathNotifi);
97         when(notificationData.getBody()).thenReturn(notifiBody);
98
99         final String result = prepareJson(notificationData, schemaPathNotifi);
100
101         assertTrue(result.contains("ietf-restconf:notification"));
102         assertTrue(result.contains("event-time"));
103         assertTrue(result.contains("notifi-module:notifi-cont"));
104         assertTrue(result.contains("cont"));
105         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
106     }
107
108     @Test
109     public void notifi_list_Test() {
110         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
111
112         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), ImmutableNodes.mapNodeBuilder()
113             .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
114             .withChild(mockMapEntry(QName.create(MODULE, "lst"), mockLeaf(QName.create(MODULE, "lf"))))
115             .build());
116
117         final DOMNotification notificationData = mock(DOMNotification.class);
118         when(notificationData.getType()).thenReturn(schemaPathNotifi);
119         when(notificationData.getBody()).thenReturn(notifiBody);
120
121         final String result = prepareJson(notificationData, schemaPathNotifi);
122
123         assertTrue(result.contains("ietf-restconf:notification"));
124         assertTrue(result.contains("event-time"));
125         assertTrue(result.contains("notifi-module:notifi-list"));
126         assertTrue(result.contains("lst"));
127         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
128     }
129
130     @Test
131     public void notifi_grpTest() {
132         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
133
134         final DOMNotification notificationData = mock(DOMNotification.class);
135
136         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
137         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
138
139         when(notificationData.getType()).thenReturn(schemaPathNotifi);
140         when(notificationData.getBody()).thenReturn(notifiBody);
141
142         final String result = prepareJson(notificationData, schemaPathNotifi);
143
144         assertTrue(result.contains("ietf-restconf:notification"));
145         assertTrue(result.contains("event-time"));
146         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
147     }
148
149     @Test
150     public void notifi_augmTest() {
151         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
152
153         final DOMNotification notificationData = mock(DOMNotification.class);
154
155         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
156         final AugmentationNode augm = mockAugm(leaf);
157         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), augm);
158
159         when(notificationData.getType()).thenReturn(schemaPathNotifi);
160         when(notificationData.getBody()).thenReturn(notifiBody);
161
162         final String result = prepareJson(notificationData, schemaPathNotifi);
163
164         assertTrue(result.contains("ietf-restconf:notification"));
165         assertTrue(result.contains("event-time"));
166         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
167     }
168
169     private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
170         final AugmentationNode augm = mock(AugmentationNode.class);
171         final AugmentationIdentifier augmId = new AugmentationIdentifier(Set.of(leaf.getIdentifier().getNodeType()));
172         when(augm.getIdentifier()).thenReturn(augmId);
173
174         final Collection<DataContainerChild> childs = new ArrayList<>();
175         childs.add(leaf);
176
177         when(augm.body()).thenReturn(childs);
178         return augm;
179     }
180
181     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
182         return Builders.mapEntryBuilder()
183             .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.getIdentifier().getNodeType(),
184                 leaf.body()))
185             .withChild(leaf)
186             .build();
187     }
188
189     private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
190         return Builders.containerBuilder()
191             .withNodeIdentifier(NodeIdentifier.create(contQName))
192             .withChild(child)
193             .build();
194     }
195
196     private static LeafNode<String> mockLeaf(final QName leafQName) {
197         return ImmutableNodes.leafNode(leafQName, "value");
198     }
199
200     private String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi) {
201         final List<NotificationListenerAdapter> listNotifi = Notificator.createNotificationListener(
202             List.of(schemaPathNotifi), "stream-name", NotificationOutputType.JSON.toString(), controllerContext);
203         final NotificationListenerAdapter notifi = listNotifi.get(0);
204         return requireNonNull(notifi.prepareJson(schemaContext, notificationData));
205     }
206 }