7e61f69d3770338a9b5cb7cb2c74d7ed16a12638
[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.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.xmlunit.assertj.XmlAssert;
31
32 @ExtendWith(MockitoExtension.class)
33 class XMLNotificationFormatterTest 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         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
48             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
49             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-leaf xmlns="notifi:mod">\
50             <lf>value</lf></notifi-leaf></notification>""");
51     }
52
53     @Test
54     void notifi_cont_leafTest() throws Exception {
55         final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
56
57         final var leaf = mockLeaf(QName.create(MODULE, "lf"));
58         final var cont = mockCont(QName.create(MODULE, "cont"), leaf);
59         final var notifiBody = mockCont(schemaPathNotifi, cont);
60
61         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
62         when(notificationData.getBody()).thenReturn(notifiBody);
63
64         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
65             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
66             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-cont xmlns="notifi:mod">\
67             <cont><lf>value</lf></cont></notifi-cont></notification>""");
68     }
69
70     @Test
71     void notifi_list_Test() throws Exception {
72         final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
73
74         final var leaf = mockLeaf(QName.create(MODULE, "lf"));
75         final var entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
76         final var notifiBody = mockCont(schemaPathNotifi, Builders.mapBuilder()
77             .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
78             .withChild(entry)
79             .build());
80
81         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
82         when(notificationData.getBody()).thenReturn(notifiBody);
83
84         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
85             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
86             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-list xmlns="notifi:mod">\
87             <lst><lf>value</lf></lst></notifi-list></notification>""");
88     }
89
90     @Test
91     void notifi_grpTest() throws Exception {
92         final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
93
94         final var leaf = mockLeaf(QName.create(MODULE, "lf"));
95         final var notifiBody = mockCont(schemaPathNotifi, leaf);
96
97         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
98         when(notificationData.getBody()).thenReturn(notifiBody);
99
100         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
101             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
102             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-grp xmlns="notifi:mod">\
103             <lf>value</lf></notifi-grp></notification>""");
104     }
105
106     @Test
107     void notifi_augmTest() throws Exception {
108         final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
109
110         final var leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
111         final var notifiBody = mockCont(schemaPathNotifi, leaf);
112
113         when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
114         when(notificationData.getBody()).thenReturn(notifiBody);
115
116         assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
117             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
118             <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-augm xmlns="notifi:mod">\
119             <lf-augm>value</lf-augm></notifi-augm></notification>""");
120     }
121
122     private static void assertXmlMatches(final String result, final String control) {
123         XmlAssert.assertThat(result).and(control)
124                 // text values have localName null but we want to compare those, ignore only nodes that have localName
125                 // with eventTime value
126                 .withNodeFilter(node -> node.getLocalName() == null || !node.getLocalName().equals("eventTime"))
127                 .areSimilar();
128     }
129
130     private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
131         return Builders.mapEntryBuilder()
132             .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
133             .withChild(leaf)
134             .build();
135     }
136
137     private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
138         return Builders.containerBuilder()
139             .withNodeIdentifier(NodeIdentifier.create(contQName))
140             .withChild(child)
141             .build();
142     }
143
144     private static LeafNode<String> mockLeaf(final QName leafQName) {
145         return ImmutableNodes.leafNode(leafQName, "value");
146     }
147
148     private String prepareXmlResult(final QName schemaPathNotifi) throws Exception {
149         final var ret = XMLNotificationFormatter.EMPTY.eventData(MODEL_CONTEXT, notificationData, Instant.now());
150         assertNotNull(ret);
151         return ret;
152     }
153 }