96c478f0977c2c963ef14c497bd3e64f9c9a0102
[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 org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import com.google.common.collect.Iterables;
8 import java.io.InputStream;
9 import java.text.SimpleDateFormat;
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.Set;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.netconf.notifications.NetconfNotification;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.opendaylight.controller.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
27 import org.w3c.dom.Document;
28
29 /**
30  * @author Lukas Sedlak <lsedlak@cisco.com>
31  */
32 public class NetconfToNotificationTest {
33
34     NetconfMessageTransformer messageTransformer;
35
36     NetconfMessage userNotification;
37
38     @SuppressWarnings("deprecation")
39     @Before
40     public void setup() throws Exception {
41         final SchemaContext schemaContext = getNotificationSchemaContext(getClass());
42
43         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
44
45         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
46         factory.setNamespaceAware(true);
47         InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
48         assertNotNull(notifyPayloadStream);
49
50         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
51         assertNotNull(doc);
52         userNotification = new NetconfMessage(doc);
53     }
54
55     static SchemaContext getNotificationSchemaContext(Class<?> loadClass) {
56         final List<InputStream> modelsToParse = Collections.singletonList(loadClass.getResourceAsStream("/schemas/user-notification.yang"));
57         final YangContextParser parser = new YangParserImpl();
58         final Set<Module> modules = parser.parseYangModelsFromStreams(modelsToParse);
59         assertTrue(!modules.isEmpty());
60         final SchemaContext schemaContext = parser.resolveSchemaContext(modules);
61         assertNotNull(schemaContext);
62         return schemaContext;
63     }
64
65     @Test
66     public void test() throws Exception {
67         final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
68         final ContainerNode root = domNotification.getBody();
69         assertNotNull(root);
70         assertEquals(6, Iterables.size(root.getValue()));
71         assertEquals("user-visited-page", root.getNodeType().getLocalName());
72         assertEquals(new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT).parse("2007-07-08T00:01:00Z"),
73                 ((DOMEvent) domNotification).getEventTime());
74     }
75 }