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.listeners;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
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;
32 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class XmlNotificationListenerTest extends AbstractNotificationListenerTest {
34 private final ListenersBroker listenersBroker = new ListenersBroker.ServerSentEvents();
37 public void notifi_leafTest() throws Exception {
38 final QName schemaPathNotifi = QName.create(MODULE, "notifi-leaf");
40 final DOMNotification notificationData = mock(DOMNotification.class);
42 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
43 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
45 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
46 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
55 public void notifi_cont_leafTest() throws Exception {
56 final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
58 final DOMNotification notificationData = mock(DOMNotification.class);
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);
64 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
65 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
74 public void notifi_list_Test() throws Exception {
75 final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
77 final DOMNotification notificationData = mock(DOMNotification.class);
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")))
86 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
87 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
96 public void notifi_grpTest() throws Exception {
97 final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
99 final DOMNotification notificationData = mock(DOMNotification.class);
101 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
102 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
104 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
105 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
114 public void notifi_augmTest() throws Exception {
115 final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
117 final DOMNotification notificationData = mock(DOMNotification.class);
119 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
120 final ContainerNode notifiBody = mockCont(schemaPathNotifi, leaf);
122 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
123 when(notificationData.getBody()).thenReturn(notifiBody);
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>""");
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"))
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()))
146 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
147 return Builders.containerBuilder()
148 .withNodeIdentifier(NodeIdentifier.create(contQName))
153 private static LeafNode<String> mockLeaf(final QName leafQName) {
154 return ImmutableNodes.leafNode(leafQName, "value");
157 private String prepareXmlResult(final DOMNotification notificationData, final QName schemaPathNotifi)
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();