Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[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.util.Set;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.config.util.xml.XmlUtil;
21 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
22 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
23 import org.opendaylight.netconf.api.NetconfMessage;
24 import org.opendaylight.netconf.notifications.NetconfNotification;
25 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30 import org.w3c.dom.Document;
31
32 public class NetconfToNotificationTest {
33
34     NetconfMessageTransformer messageTransformer;
35
36     NetconfMessage userNotification;
37
38     @Before
39     public void setup() throws Exception {
40         InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
41         assertNotNull(notifyPayloadStream);
42
43         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
44         assertNotNull(doc);
45         userNotification = new NetconfMessage(doc);
46     }
47
48     static SchemaContext getNotificationSchemaContext(final Class<?> loadClass, final boolean getExceptionTest) {
49         final SchemaContext context;
50         if (getExceptionTest) {
51             context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification4.yang",
52                     "/schemas/user-notification3.yang");
53         } else {
54             context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification.yang",
55                 "/schemas/user-notification2.yang");
56         }
57
58         final Set<Module> modules = context.getModules();
59         assertTrue(!modules.isEmpty());
60         assertNotNull(context);
61         return context;
62     }
63
64     @Test(expected =  IllegalArgumentException.class)
65     public void testMostRecentWrongYangModel() throws Exception {
66         final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), true);
67         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
68         messageTransformer.toNotification(userNotification);
69     }
70
71     @Test
72     public void testToNotificationFunction() throws Exception {
73         final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), false);
74         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
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(NetconfNotification.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00"),
81                 ((DOMEvent) domNotification).getEventTime());
82     }
83 }