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