Further warnings mitigation
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / server / mdsal / streams / notif / XMLNotificationFormatterTest.java
1 /*
2  * Copyright (c) 2020 Pantheon.tech, s.r.o. 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.mockito.Mockito.when;
12
13 import java.time.Instant;
14 import org.junit.jupiter.api.Test;
15 import org.junit.jupiter.api.extension.ExtendWith;
16 import org.mockito.Mock;
17 import org.mockito.junit.jupiter.MockitoExtension;
18 import org.opendaylight.mdsal.dom.api.DOMNotification;
19 import org.opendaylight.restconf.nb.rfc8040.streams.AbstractNotificationListenerTest;
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.spi.node.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
29 import org.xmlunit.assertj.XmlAssert;
30
31 @ExtendWith(MockitoExtension.class)
32 class XMLNotificationFormatterTest 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         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
47             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
48             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-leaf xmlns="notifi:mod">\
49             <lf>value</lf></notifi-leaf></notification>""");
50     }
51
52     @Test
53     void notifi_cont_leafTest() throws Exception {
54         final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
55
56         final var leaf = mockLeaf(QName.create(MODULE, "lf"));
57         final var cont = mockCont(QName.create(MODULE, "cont"), leaf);
58         final var notifiBody = mockCont(schemaPathNotifi, cont);
59
60         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
61         when(notificationData.getBody()).thenReturn(notifiBody);
62
63         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
64             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
65             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-cont xmlns="notifi:mod">\
66             <cont><lf>value</lf></cont></notifi-cont></notification>""");
67     }
68
69     @Test
70     void notifi_list_Test() throws Exception {
71         final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
72
73         final var leaf = mockLeaf(QName.create(MODULE, "lf"));
74         final var entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
75         final var notifiBody = mockCont(schemaPathNotifi, ImmutableNodes.newSystemMapBuilder()
76             .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
77             .withChild(entry)
78             .build());
79
80         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
81         when(notificationData.getBody()).thenReturn(notifiBody);
82
83         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
84             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
85             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-list xmlns="notifi:mod">\
86             <lst><lf>value</lf></lst></notifi-list></notification>""");
87     }
88
89     @Test
90     void notifi_grpTest() throws Exception {
91         final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
92
93         final var leaf = mockLeaf(QName.create(MODULE, "lf"));
94         final var notifiBody = mockCont(schemaPathNotifi, leaf);
95
96         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
97         when(notificationData.getBody()).thenReturn(notifiBody);
98
99         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
100             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
101             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-grp xmlns="notifi:mod">\
102             <lf>value</lf></notifi-grp></notification>""");
103     }
104
105     @Test
106     void notifi_augmTest() throws Exception {
107         final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
108
109         final var leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
110         final var notifiBody = mockCont(schemaPathNotifi, leaf);
111
112         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
113         when(notificationData.getBody()).thenReturn(notifiBody);
114
115         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
116             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
117             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-augm xmlns="notifi:mod">\
118             <lf-augm>value</lf-augm></notifi-augm></notification>""");
119     }
120
121     private static void assertXmlMatches(final String result, final String control) {
122         XmlAssert.assertThat(result).and(control)
123                 // text values have localName null but we want to compare those, ignore only nodes that have localName
124                 // with eventTime value
125                 .withNodeFilter(node -> node.getLocalName() == null || !node.getLocalName().equals("eventTime"))
126                 .areSimilar();
127     }
128
129     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
130         return ImmutableNodes.newMapEntryBuilder()
131             .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
132             .withChild(leaf)
133             .build();
134     }
135
136     private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
137         return ImmutableNodes.newContainerBuilder()
138             .withNodeIdentifier(NodeIdentifier.create(contQName))
139             .withChild(child)
140             .build();
141     }
142
143     private static LeafNode<String> mockLeaf(final QName leafQName) {
144         return ImmutableNodes.leafNode(leafQName, "value");
145     }
146
147     private String prepareXmlResult(final QName schemaPathNotifi) throws Exception {
148         final var ret = XMLNotificationFormatter.EMPTY.eventData(MODEL_CONTEXT, notificationData, Instant.now());
149         assertNotNull(ret);
150         return ret;
151     }
152 }