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