Bump MRI upstreams
[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.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
47
48 @RunWith(MockitoJUnitRunner.StrictStubs.class)
49 public class NotificationListenerTest {
50     private static final QNameModule MODULE = QNameModule.create(XMLNamespace.of("notifi:mod"),
51         Revision.of("2016-11-23"));
52
53     private static EffectiveModelContext schemaContext;
54
55     private ControllerContext controllerContext;
56
57     @BeforeClass
58     public static void staticInit() throws FileNotFoundException {
59         schemaContext = TestUtils.loadSchemaContext("/notifications");
60     }
61
62     @Before
63     public void init() {
64         controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
65     }
66
67     @Test
68     public void notifi_leafTest() {
69         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
70
71         final DOMNotification notificationData = mock(DOMNotification.class);
72
73         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
74         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
75
76         when(notificationData.getType()).thenReturn(schemaPathNotifi);
77         when(notificationData.getBody()).thenReturn(notifiBody);
78
79         final String result = prepareJson(notificationData, schemaPathNotifi);
80
81         assertTrue(result.contains("ietf-restconf:notification"));
82         assertTrue(result.contains("event-time"));
83         assertTrue(result.contains("notifi-module:notifi-leaf"));
84         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
85     }
86
87     @Test
88     public void notifi_cont_leafTest() {
89         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
90
91         final DOMNotification notificationData = mock(DOMNotification.class);
92
93         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
94         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
95         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
96
97         when(notificationData.getType()).thenReturn(schemaPathNotifi);
98         when(notificationData.getBody()).thenReturn(notifiBody);
99
100         final String result = prepareJson(notificationData, schemaPathNotifi);
101
102         assertTrue(result.contains("ietf-restconf:notification"));
103         assertTrue(result.contains("event-time"));
104         assertTrue(result.contains("notifi-module:notifi-cont"));
105         assertTrue(result.contains("cont"));
106         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
107     }
108
109     @Test
110     public void notifi_list_Test() {
111         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
112
113         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), ImmutableNodes.mapNodeBuilder()
114             .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
115             .withChild(mockMapEntry(QName.create(MODULE, "lst"), mockLeaf(QName.create(MODULE, "lf"))))
116             .build());
117
118         final DOMNotification notificationData = mock(DOMNotification.class);
119         when(notificationData.getType()).thenReturn(schemaPathNotifi);
120         when(notificationData.getBody()).thenReturn(notifiBody);
121
122         final String result = prepareJson(notificationData, schemaPathNotifi);
123
124         assertTrue(result.contains("ietf-restconf:notification"));
125         assertTrue(result.contains("event-time"));
126         assertTrue(result.contains("notifi-module:notifi-list"));
127         assertTrue(result.contains("lst"));
128         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
129     }
130
131     @Test
132     public void notifi_grpTest() {
133         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
134
135         final DOMNotification notificationData = mock(DOMNotification.class);
136
137         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
138         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
139
140         when(notificationData.getType()).thenReturn(schemaPathNotifi);
141         when(notificationData.getBody()).thenReturn(notifiBody);
142
143         final String result = prepareJson(notificationData, schemaPathNotifi);
144
145         assertTrue(result.contains("ietf-restconf:notification"));
146         assertTrue(result.contains("event-time"));
147         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
148     }
149
150     @Test
151     public void notifi_augmTest() {
152         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
153
154         final DOMNotification notificationData = mock(DOMNotification.class);
155
156         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
157         final AugmentationNode augm = mockAugm(leaf);
158         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), augm);
159
160         when(notificationData.getType()).thenReturn(schemaPathNotifi);
161         when(notificationData.getBody()).thenReturn(notifiBody);
162
163         final String result = prepareJson(notificationData, schemaPathNotifi);
164
165         assertTrue(result.contains("ietf-restconf:notification"));
166         assertTrue(result.contains("event-time"));
167         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
168     }
169
170     private static AugmentationNode mockAugm(final LeafNode<String> leaf) {
171         final AugmentationNode augm = mock(AugmentationNode.class);
172         final AugmentationIdentifier augmId = new AugmentationIdentifier(Set.of(leaf.getIdentifier().getNodeType()));
173         when(augm.getIdentifier()).thenReturn(augmId);
174
175         final Collection<DataContainerChild> childs = new ArrayList<>();
176         childs.add(leaf);
177
178         when(augm.body()).thenReturn(childs);
179         return augm;
180     }
181
182     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
183         return Builders.mapEntryBuilder()
184             .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.getIdentifier().getNodeType(),
185                 leaf.body()))
186             .withChild(leaf)
187             .build();
188     }
189
190     private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
191         return Builders.containerBuilder()
192             .withNodeIdentifier(NodeIdentifier.create(contQName))
193             .withChild(child)
194             .build();
195     }
196
197     private static LeafNode<String> mockLeaf(final QName leafQName) {
198         return ImmutableNodes.leafNode(leafQName, "value");
199     }
200
201     private String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi) {
202         final List<SchemaPath> paths = new ArrayList<>();
203         paths.add(schemaPathNotifi.asSchemaPath());
204         final List<NotificationListenerAdapter> listNotifi =
205                 Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString(),
206                         controllerContext);
207         final NotificationListenerAdapter notifi = listNotifi.get(0);
208         return requireNonNull(notifi.prepareJson(schemaContext, notificationData));
209     }
210 }