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