Clean up NotificationMessage
[netconf.git] / plugins / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / 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.sal.connect.netconf;
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.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
25 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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
37     private static final QName INTERFACES_QNAME = QName
38             .create("org:opendaylight:notification:test:ns:yang:nested-notification", "2014-07-08", "interfaces");
39     private static final QName INTERFACE_QNAME = QName.create(INTERFACES_QNAME, "interface");
40     private static final QName INTERFACE_ENABLED_NOTIFICATION_QNAME = QName
41             .create(INTERFACE_QNAME, "interface-enabled");
42
43     @Test
44     public void testNestedNotificationToNotificationFunction() throws Exception {
45         final EffectiveModelContext schemaContext =
46                 getNotificationSchemaContext(Collections.singleton("/schemas/nested-notification.yang"));
47         final NetconfMessage notificationMessage = prepareNotification("/nested-notification-payload.xml");
48         NetconfMessageTransformer messageTransformer = new NetconfMessageTransformer(
49             new EmptyMountPointContext(schemaContext), true, BASE_SCHEMAS.getBaseSchema());
50         final DOMNotification domNotification = messageTransformer.toNotification(notificationMessage);
51         final ContainerNode root = domNotification.getBody();
52         assertNotNull(root);
53         assertEquals(1, root.body().size());
54         assertEquals("interface-enabled", root.getIdentifier().getNodeType().getLocalName());
55         assertEquals(NotificationMessage.RFC3339_DATE_PARSER.apply("2008-07-08T00:01:00Z"),
56                 ((DOMEvent) domNotification).getEventInstant());
57         assertEquals(Absolute.of(INTERFACES_QNAME, INTERFACE_QNAME, INTERFACE_ENABLED_NOTIFICATION_QNAME),
58                 domNotification.getType());
59     }
60
61     private EffectiveModelContext getNotificationSchemaContext(final Collection<String> yangResources) {
62         final EffectiveModelContext context = YangParserTestUtils.parseYangResources(getClass(), yangResources);
63         final Collection<? extends Module> modules = context.getModules();
64         assertFalse(modules.isEmpty());
65         return context;
66     }
67
68     private NetconfMessage prepareNotification(final String notificationPayloadPath) throws IOException, SAXException {
69         InputStream notifyPayloadStream = getClass().getResourceAsStream(notificationPayloadPath);
70         assertNotNull(notifyPayloadStream);
71
72         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
73         assertNotNull(doc);
74         return new NetconfMessage(doc);
75     }
76 }