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