127b0cbfcdd510f569af8b1d71d0e773d9e1f740
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / NetconfToNotificationTest.java
1 package org.opendaylight.controller.sal.connect.netconf;
2
3 import static junit.framework.Assert.assertEquals;
4 import static junit.framework.Assert.assertNotNull;
5 import static junit.framework.Assert.assertTrue;
6
7 import java.io.InputStream;
8 import java.util.Collections;
9 import java.util.List;
10 import java.util.Set;
11
12 import javax.xml.parsers.DocumentBuilderFactory;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
17 import org.opendaylight.controller.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
22 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
23 import org.w3c.dom.Document;
24
25 /**
26  * @author Lukas Sedlak <lsedlak@cisco.com>
27  */
28 public class NetconfToNotificationTest {
29
30     NetconfMessageTransformer messageTransformer;
31
32     NetconfMessage userNotification;
33
34     @SuppressWarnings("deprecation")
35     @Before
36     public void setup() throws Exception {
37         final List<InputStream> modelsToParse = Collections.singletonList(getClass().getResourceAsStream("/schemas/user-notification.yang"));
38         final YangContextParser parser = new YangParserImpl();
39         final Set<Module> modules = parser.parseYangModelsFromStreams(modelsToParse);
40         assertTrue(!modules.isEmpty());
41         final SchemaContext schemaContext = parser.resolveSchemaContext(modules);
42         assertNotNull(schemaContext);
43
44         messageTransformer = new NetconfMessageTransformer();
45         messageTransformer.onGlobalContextUpdated(schemaContext);
46
47         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
48         factory.setNamespaceAware(true);
49         InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
50         assertNotNull(notifyPayloadStream);
51
52         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
53         assertNotNull(doc);
54         userNotification = new NetconfMessage(doc);
55     }
56
57     @Test
58     public void test() throws Exception {
59         final CompositeNode root = messageTransformer.toNotification(userNotification);
60
61         assertNotNull(root);
62         assertEquals(6, root.size());
63         assertEquals("user-visited-page", root.getKey().getLocalName());
64     }
65 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.