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