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