f4c0f904a03a531838e9467adf652746c912271a
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / NetconfNestedNotificationTest.java
1 /*
2  * Copyright © 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.client.mdsal;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.dom.api.DOMEvent;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.netconf.api.messages.NetconfMessage;
19 import org.opendaylight.netconf.api.messages.NotificationMessage;
20 import org.opendaylight.netconf.api.xml.XmlUtil;
21 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformer;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27 import org.w3c.dom.Document;
28 import org.xml.sax.SAXException;
29
30 public class NetconfNestedNotificationTest extends AbstractBaseSchemasTest {
31     private static final QName INTERFACES_QNAME = QName
32             .create("org:opendaylight:notification:test:ns:yang:nested-notification", "2014-07-08", "interfaces");
33     private static final QName INTERFACE_QNAME = QName.create(INTERFACES_QNAME, "interface");
34     private static final QName INTERFACE_ENABLED_NOTIFICATION_QNAME = QName
35             .create(INTERFACE_QNAME, "interface-enabled");
36
37     @Test
38     public void testNestedNotificationToNotificationFunction() throws Exception {
39         final var context = YangParserTestUtils.parseYangResources(
40             NetconfNestedNotificationTest.class, "/schemas/nested-notification.yang");
41
42         final NetconfMessage notificationMessage = prepareNotification("/nested-notification-payload.xml");
43         NetconfMessageTransformer messageTransformer = new NetconfMessageTransformer(
44             MountPointContext.of(context), true, BASE_SCHEMAS.baseSchema());
45         final DOMNotification domNotification = messageTransformer.toNotification(notificationMessage);
46         final ContainerNode root = domNotification.getBody();
47         assertNotNull(root);
48         assertEquals(1, root.body().size());
49         assertEquals("interface-enabled", root.name().getNodeType().getLocalName());
50         assertEquals(NotificationMessage.RFC3339_DATE_PARSER.apply("2008-07-08T00:01:00Z"),
51                 ((DOMEvent) domNotification).getEventInstant());
52         assertEquals(Absolute.of(INTERFACES_QNAME, INTERFACE_QNAME, INTERFACE_ENABLED_NOTIFICATION_QNAME),
53                 domNotification.getType());
54     }
55
56     private NetconfMessage prepareNotification(final String notificationPayloadPath) throws IOException, SAXException {
57         InputStream notifyPayloadStream = getClass().getResourceAsStream(notificationPayloadPath);
58         assertNotNull(notifyPayloadStream);
59
60         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
61         assertNotNull(doc);
62         return new NetconfMessage(doc);
63     }
64 }