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 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;
31 @RunWith(MockitoJUnitRunner.StrictStubs.class)
32 public class XmlNotificationListenerTest extends AbstractNotificationListenerTest {
34 public void notifi_leafTest() throws Exception {
35 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-leaf"));
37 final DOMNotification notificationData = mock(DOMNotification.class);
39 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
40 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
42 when(notificationData.getType()).thenReturn(schemaPathNotifi);
43 when(notificationData.getBody()).thenReturn(notifiBody);
45 final String result = prepareXmlResult(notificationData, schemaPathNotifi);
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>";
51 assertXmlMatches(result, control);
55 public void notifi_cont_leafTest() throws Exception {
56 final Absolute schemaPathNotifi = Absolute.of(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.lastNodeIdentifier(), cont);
64 when(notificationData.getType()).thenReturn(schemaPathNotifi);
65 when(notificationData.getBody()).thenReturn(notifiBody);
67 final String result = prepareXmlResult(notificationData, schemaPathNotifi);
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>";
73 assertXmlMatches(result, control);
77 public void notifi_list_Test() throws Exception {
78 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-list"));
80 final DOMNotification notificationData = mock(DOMNotification.class);
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")))
89 when(notificationData.getType()).thenReturn(schemaPathNotifi);
90 when(notificationData.getBody()).thenReturn(notifiBody);
92 final String result = prepareXmlResult(notificationData, schemaPathNotifi);
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>";
98 assertXmlMatches(result, control);
102 public void notifi_grpTest() throws Exception {
103 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-grp"));
105 final DOMNotification notificationData = mock(DOMNotification.class);
107 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf"));
108 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
110 when(notificationData.getType()).thenReturn(schemaPathNotifi);
111 when(notificationData.getBody()).thenReturn(notifiBody);
113 final String result = prepareXmlResult(notificationData, schemaPathNotifi);
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>";
119 assertXmlMatches(result, control);
123 public void notifi_augmTest() throws Exception {
124 final Absolute schemaPathNotifi = Absolute.of(QName.create(MODULE, "notifi-augm"));
126 final DOMNotification notificationData = mock(DOMNotification.class);
128 final LeafNode<String> leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
129 final ContainerNode notifiBody = mockCont(schemaPathNotifi.lastNodeIdentifier(), leaf);
131 when(notificationData.getType()).thenReturn(schemaPathNotifi);
132 when(notificationData.getBody()).thenReturn(notifiBody);
134 final String result = prepareXmlResult(notificationData, schemaPathNotifi);
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>";
140 assertXmlMatches(result, control);
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"))
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()))
158 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
159 return Builders.containerBuilder()
160 .withNodeIdentifier(NodeIdentifier.create(contQName))
165 private static LeafNode<String> mockLeaf(final QName leafQName) {
166 return ImmutableNodes.leafNode(leafQName, "value");
169 private static String prepareXmlResult(final DOMNotification notificationData, final Absolute schemaPathNotifi)
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, false).orElseThrow();