BUG-6859 - Binding generator v1 refactoring
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / 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.netconf.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.ArrayList;
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.netconf.api.NetconfMessage;
28 import org.opendaylight.netconf.notifications.NetconfNotification;
29 import org.opendaylight.netconf.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.test.util.YangParserTestUtils;
34 import org.w3c.dom.Document;
35
36 public class NetconfToNotificationTest {
37
38     NetconfMessageTransformer messageTransformer;
39
40     NetconfMessage userNotification;
41
42     @SuppressWarnings("deprecation")
43     @Before
44     public void setup() throws Exception {
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, boolean getExceptionTest) throws Exception {
56         final List<InputStream> modelsToParse = new ArrayList<>();
57
58         if (getExceptionTest) {
59             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification4.yang"));
60             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification3.yang"));
61         } else {
62             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification.yang"));
63             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification2.yang"));
64         }
65
66         final SchemaContext context = YangParserTestUtils.parseYangStreams(modelsToParse);
67         final Set<Module> modules = context.getModules();
68         assertTrue(!modules.isEmpty());
69         assertNotNull(context);
70         return context;
71     }
72
73     @Test(expected =  IllegalStateException.class)
74     public void testMostRecentWrongYangModel() throws Exception {
75         final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), true);
76         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
77         messageTransformer.toNotification(userNotification);
78     }
79
80     @Test
81     public void testToNotificationFunction() throws Exception {
82         final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), false);
83         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
84         final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
85         final ContainerNode root = domNotification.getBody();
86         assertNotNull(root);
87         assertEquals(6, Iterables.size(root.getValue()));
88         assertEquals("user-visited-page", root.getNodeType().getLocalName());
89         assertEquals(new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT).parse("2015-10-23T09:42:27.67175+00:00"),
90                 ((DOMEvent) domNotification).getEventTime());
91     }
92 }