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