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.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.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 @ExtendWith(MockitoExtension.class)
32 class XMLNotificationFormatterTest extends AbstractNotificationListenerTest {
34 private DOMNotification notificationData;
37 void notifi_leafTest() throws Exception {
38 final QName schemaPathNotifi = QName.create(MODULE, "notifi-leaf");
40 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
41 final var notifiBody = mockCont(schemaPathNotifi, leaf);
43 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
44 when(notificationData.getBody()).thenReturn(notifiBody);
46 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
47 <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>""");
53 void notifi_cont_leafTest() throws Exception {
54 final QName schemaPathNotifi = QName.create(MODULE, "notifi-cont");
56 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
57 final var cont = mockCont(QName.create(MODULE, "cont"), leaf);
58 final var notifiBody = mockCont(schemaPathNotifi, cont);
60 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
61 when(notificationData.getBody()).thenReturn(notifiBody);
63 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
64 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
65 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-cont xmlns="notifi:mod">\
66 <cont><lf>value</lf></cont></notifi-cont></notification>""");
70 void notifi_list_Test() throws Exception {
71 final QName schemaPathNotifi = QName.create(MODULE, "notifi-list");
73 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
74 final var entry = mockMapEntry(QName.create(MODULE, "lst"), leaf);
75 final var notifiBody = mockCont(schemaPathNotifi, Builders.mapBuilder()
76 .withNodeIdentifier(NodeIdentifier.create(QName.create(MODULE, "lst")))
80 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
81 when(notificationData.getBody()).thenReturn(notifiBody);
83 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
84 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
85 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-list xmlns="notifi:mod">\
86 <lst><lf>value</lf></lst></notifi-list></notification>""");
90 void notifi_grpTest() throws Exception {
91 final QName schemaPathNotifi = QName.create(MODULE, "notifi-grp");
93 final var leaf = mockLeaf(QName.create(MODULE, "lf"));
94 final var notifiBody = mockCont(schemaPathNotifi, leaf);
96 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
97 when(notificationData.getBody()).thenReturn(notifiBody);
99 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
100 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
101 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-grp xmlns="notifi:mod">\
102 <lf>value</lf></notifi-grp></notification>""");
106 void notifi_augmTest() throws Exception {
107 final QName schemaPathNotifi = QName.create(MODULE, "notifi-augm");
109 final var leaf = mockLeaf(QName.create(MODULE, "lf-augm"));
110 final var notifiBody = mockCont(schemaPathNotifi, leaf);
112 when(notificationData.getType()).thenReturn(Absolute.of(schemaPathNotifi));
113 when(notificationData.getBody()).thenReturn(notifiBody);
115 assertXmlMatches(prepareXmlResult(schemaPathNotifi), """
116 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">\
117 <eventTime>2020-06-29T14:23:46.086855+02:00</eventTime><notifi-augm xmlns="notifi:mod">\
118 <lf-augm>value</lf-augm></notifi-augm></notification>""");
121 private static void assertXmlMatches(final String result, final String control) {
122 XmlAssert.assertThat(result).and(control)
123 // text values have localName null but we want to compare those, ignore only nodes that have localName
124 // with eventTime value
125 .withNodeFilter(node -> node.getLocalName() == null || !node.getLocalName().equals("eventTime"))
129 private static MapEntryNode mockMapEntry(final QName entryQName, final LeafNode<String> leaf) {
130 return Builders.mapEntryBuilder()
131 .withNodeIdentifier(NodeIdentifierWithPredicates.of(entryQName, leaf.name().getNodeType(), leaf.body()))
136 private static ContainerNode mockCont(final QName contQName, final DataContainerChild child) {
137 return Builders.containerBuilder()
138 .withNodeIdentifier(NodeIdentifier.create(contQName))
143 private static LeafNode<String> mockLeaf(final QName leafQName) {
144 return ImmutableNodes.leafNode(leafQName, "value");
147 private String prepareXmlResult(final QName schemaPathNotifi) throws Exception {
148 final var ret = XMLNotificationFormatter.EMPTY.eventData(MODEL_CONTEXT, notificationData, Instant.now());