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