768bd51afd2032ba3ec1e425f309a950f5515277
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / JsonNotificationListenerTest.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.restconf.nb.rfc8040.streams.listeners;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import java.time.Instant;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.junit.MockitoJUnitRunner;
18 import org.opendaylight.mdsal.dom.api.DOMNotification;
19 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class JsonNotificationListenerTest extends AbstractNotificationListenerTest {
35     private static final Logger LOG = LoggerFactory.getLogger(JsonNotificationListenerTest.class);
36
37     @Test
38     public void notifi_leafTest() throws Exception {
39         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
40
41         final DOMNotification notificationData = mock(DOMNotification.class);
42
43         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
44         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
45
46         when(notificationData.getType()).thenReturn(schemaPathNotifi);
47         when(notificationData.getBody()).thenReturn(notifiBody);
48
49         final String result = prepareJson(notificationData, schemaPathNotifi);
50
51         LOG.info("json result: {}", result);
52
53         assertTrue(result.contains("ietf-restconf:notification"));
54         assertTrue(result.contains("event-time"));
55         assertTrue(result.contains("notifi-module:notifi-leaf"));
56         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
57     }
58
59     @Test
60     public void notifi_cont_leafTest() throws Exception {
61         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-cont"));
62
63         final DOMNotification notificationData = mock(DOMNotification.class);
64
65         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
66         final ContainerNode cont = mockCont(QName.create(MODULE, "cont"), leaf);
67         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), cont);
68
69         when(notificationData.getType()).thenReturn(schemaPathNotifi);
70         when(notificationData.getBody()).thenReturn(notifiBody);
71
72         final String result = prepareJson(notificationData, schemaPathNotifi);
73
74         assertTrue(result.contains("ietf-restconf:notification"));
75         assertTrue(result.contains("event-time"));
76         assertTrue(result.contains("notifi-module:notifi-cont"));
77         assertTrue(result.contains("cont"));
78         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
79     }
80
81     @Test
82     public void notifi_list_Test() throws Exception {
83         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
84
85         final DOMNotification notificationData = mock(DOMNotification.class);
86
87         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
88         final MapEntryNode entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
89         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), Builders.mapBuilder()
90             .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
91             .withChild(entry)
92             .build());
93
94         when(notificationData.getType()).thenReturn(schemaPathNotifi);
95         when(notificationData.getBody()).thenReturn(notifiBody);
96
97         final String result = prepareJson(notificationData, schemaPathNotifi);
98
99         assertTrue(result.contains("ietf-restconf:notification"));
100         assertTrue(result.contains("event-time"));
101         assertTrue(result.contains("notifi-module:notifi-list"));
102         assertTrue(result.contains("lst"));
103         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
104     }
105
106     @Test
107     public void notifi_grpTest() throws Exception {
108         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
109
110         final DOMNotification notificationData = mock(DOMNotification.class);
111
112         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
113         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
114
115         when(notificationData.getType()).thenReturn(schemaPathNotifi);
116         when(notificationData.getBody()).thenReturn(notifiBody);
117
118         final String result = prepareJson(notificationData, schemaPathNotifi);
119
120         assertTrue(result.contains("ietf-restconf:notification"));
121         assertTrue(result.contains("event-time"));
122         assertTrue(result.contains("lf" + '"' + ":" + '"' + "value"));
123     }
124
125     @Test
126     public void notifi_augmTest() throws Exception {
127         final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
128
129         final DOMNotification notificationData = mock(DOMNotification.class);
130
131         final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
132         final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
133
134         when(notificationData.getType()).thenReturn(schemaPathNotifi);
135         when(notificationData.getBody()).thenReturn(notifiBody);
136
137         final String result = prepareJson(notificationData, schemaPathNotifi);
138
139         assertTrue(result.contains("ietf-restconf:notification"));
140         assertTrue(result.contains("event-time"));
141         assertTrue(result.contains("lf-augm" + '"' + ":" + '"' + "value"));
142     }
143
144     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
145         return Builders.mapEntryBuilder()
146             .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
147             .withChild(leaf)
148             .build();
149     }
150
151     private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
152         return Builders.containerBuilder()
153             .withNodeIdentifier(NodeIdentifier.create(contQName))
154             .withChild(child)
155             .build();
156     }
157
158     private static LeafNode<String> mockLeaf(final QName leafQName) {
159         return ImmutableNodes.leafNode(leafQName, "value");
160     }
161
162     private static String prepareJson(final DOMNotification notificationData, final Absolute schemaPathNotifi)
163             throws Exception {
164         final NotificationListenerAdapter notifiAdapter = ListenersBroker.getInstance().registerNotificationListener(
165                 schemaPathNotifi, "json-stream", NotificationOutputType.JSON);
166         return notifiAdapter.formatter()
167                 .eventData(SCHEMA_CONTEXT, notificationData, Instant.now(), false, false, false).orElseThrow();
168     }
169 }