Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / 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.config.util.xml.XmlUtil;
25 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
26 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
27 import org.opendaylight.controller.netconf.api.NetconfMessage;
28 import org.opendaylight.controller.netconf.notifications.NetconfNotification;
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 public class NetconfToNotificationTest {
38
39     NetconfMessageTransformer messageTransformer;
40
41     NetconfMessage userNotification;
42
43     @SuppressWarnings("deprecation")
44     @Before
45     public void setup() throws Exception {
46         final SchemaContext schemaContext = getNotificationSchemaContext(getClass());
47
48         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
49
50         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51         factory.setNamespaceAware(true);
52         InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
53         assertNotNull(notifyPayloadStream);
54
55         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
56         assertNotNull(doc);
57         userNotification = new NetconfMessage(doc);
58     }
59
60     static SchemaContext getNotificationSchemaContext(Class<?> loadClass) {
61         final List<InputStream> modelsToParse = Collections.singletonList(loadClass.getResourceAsStream("/schemas/user-notification.yang"));
62         final YangContextParser parser = new YangParserImpl();
63         final Set<Module> modules = parser.parseYangModelsFromStreams(modelsToParse);
64         assertTrue(!modules.isEmpty());
65         final SchemaContext schemaContext = parser.resolveSchemaContext(modules);
66         assertNotNull(schemaContext);
67         return schemaContext;
68     }
69
70     @Test
71     public void test() throws Exception {
72         final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
73         final ContainerNode root = domNotification.getBody();
74         assertNotNull(root);
75         assertEquals(6, Iterables.size(root.getValue()));
76         assertEquals("user-visited-page", root.getNodeType().getLocalName());
77         assertEquals(new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT).parse("2007-07-08T00:01:00Z"),
78                 ((DOMEvent) domNotification).getEventTime());
79     }
80 }