2 * Copyright (c) 2020 Pantheon.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.streams;
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
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;
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class XmlNotificationListenerTest extends AbstractNotificationListenerTest {
35 private final ListenersBroker listenersBroker = new ListenersBroker.ServerSentEvents();
38 public void notifi_leafTest() throws Exception {
39 final QName schemaPathNotifi = QName.create(MODULE, "notifi-leaf");
41 final DOMNotification notificationData = mock(DOMNotification.class);
43 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
44 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
46 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
47 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
56 public void notifi_cont_leafTest() throws Exception {
57 final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
59 final DOMNotification notificationData = mock(DOMNotification.class);
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);
65 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
66 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
75 public void notifi_list_Test() throws Exception {
76 final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
78 final DOMNotification notificationData = mock(DOMNotification.class);
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")))
87 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
88 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
97 public void notifi_grpTest() throws Exception {
98 final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
100 final DOMNotification notificationData = mock(DOMNotification.class);
102 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
103 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
105 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
106 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
115 public void notifi_augmTest() throws Exception {
116 final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
118 final DOMNotification notificationData = mock(DOMNotification.class);
120 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
121 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
123 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
124 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
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"))
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()))
147 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
148 return Builders.containerBuilder()
149 .withNodeIdentifier(NodeIdentifier.create(contQName))
154 private static LeafNode<String> mockLeaf(final QName leafQName) {
155 return ImmutableNodes.leafNode(leafQName, "value");
158 private String prepareXmlResult(final DOMNotification notificationData, final QName schemaPathNotifi)
160 final var ret = listenersBroker.registerNotificationListener(MODEL_CONTEXT, ImmutableSet.of(schemaPathNotifi),
161 NotificationOutputType.XML).formatter().eventData(MODEL_CONTEXT, notificationData, Instant.now());