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.server.mdsal.streams.notif;
10 import static org.junit.jupiter.api.Assertions.assertNotNull;
11 import static org.mockito.Mockito.when;
13 import java.time.Instant;
14 import org.junit.jupiter.api.Test;
15 import org.junit.jupiter.api.extension.ExtendWith;
16 import org.mockito.Mock;
17 import org.mockito.junit.jupiter.MockitoExtension;
18 import org.opendaylight.mdsal.dom.api.DOMNotification;
19 import org.opendaylight.restconf.nb.rfc8040.streams.AbstractNotificationListenerTest;
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 @ExtendWith(MockitoExtension.class)
33 class XMLNotificationFormatterTest extends AbstractNotificationListenerTest {
35 private DOMNotification notificationData;
38 void notifi_leafTest() throws Exception {
39 final QName schemaPathNotifi = QName.create(MODULE, "notifi-leaf");
41 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
42 final var notifiBody = mockCont(schemaPathNotifi, leaf);
44 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
45 when(notificationData.getBody()).thenReturn(notifiBody);
47 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
48 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
49 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-leaf xmlns="notifi:mod">\
50 <lf>value</lf></notifi-leaf></notification>""");
54 void notifi_cont_leafTest() throws Exception {
55 final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
57 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
58 final var cont = mockCont(QName.create(MODULE, "cont"), leaf);
59 final var notifiBody = mockCont(schemaPathNotifi, cont);
61 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
62 when(notificationData.getBody()).thenReturn(notifiBody);
64 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
65 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
66 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-cont xmlns="notifi:mod">\
67 <cont><lf>value</lf></cont></notifi-cont></notification>""");
71 void notifi_list_Test() throws Exception {
72 final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
74 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
75 final var entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
76 final var notifiBody = mockCont(schemaPathNotifi, Builders.mapBuilder()
77 .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
81 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
82 when(notificationData.getBody()).thenReturn(notifiBody);
84 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
85 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
86 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-list xmlns="notifi:mod">\
87 <lst><lf>value</lf></lst></notifi-list></notification>""");
91 void notifi_grpTest() throws Exception {
92 final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
94 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
95 final var notifiBody = mockCont(schemaPathNotifi, leaf);
97 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
98 when(notificationData.getBody()).thenReturn(notifiBody);
100 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
101 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
102 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-grp xmlns="notifi:mod">\
103 <lf>value</lf></notifi-grp></notification>""");
107 void notifi_augmTest() throws Exception {
108 final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
110 final var leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
111 final var notifiBody = mockCont(schemaPathNotifi, leaf);
113 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
114 when(notificationData.getBody()).thenReturn(notifiBody);
116 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
117 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
118 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-augm xmlns="notifi:mod">\
119 <lf-augm>value</lf-augm></notifi-augm></notification>""");
122 private static void assertXmlMatches(final String result, final String control) {
123 XmlAssert.assertThat(result).and(control)
124 // text values have localName null but we want to compare those, ignore only nodes that have localName
125 // with eventTime value
126 .withNodeFilter(node -> node.getLocalName() == null || !node.getLocalName().equals("eventTime"))
130 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
131 return Builders.mapEntryBuilder()
132 .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
137 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
138 return Builders.containerBuilder()
139 .withNodeIdentifier(NodeIdentifier.create(contQName))
144 private static LeafNode<String> mockLeaf(final QName leafQName) {
145 return ImmutableNodes.leafNode(leafQName, "value");
148 private String prepareXmlResult(final QName schemaPathNotifi) throws Exception {
149 final var ret = XMLNotificationFormatter.EMPTY.eventData(MODEL_CONTEXT, notificationData, Instant.now());