Bug 8153: Enforce check-style rules for netconf - sal-netconf-connector
[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.ArrayList;
18 import java.util.List;
19 import java.util.Set;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.util.xml.XmlUtil;
23 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
24 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
25 import org.opendaylight.netconf.api.NetconfMessage;
26 import org.opendaylight.netconf.notifications.NetconfNotification;
27 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 import org.w3c.dom.Document;
33
34 public class NetconfToNotificationTest {
35
36     NetconfMessageTransformer messageTransformer;
37
38     NetconfMessage userNotification;
39
40     @Before
41     public void setup() throws Exception {
42         InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
43         assertNotNull(notifyPayloadStream);
44
45         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
46         assertNotNull(doc);
47         userNotification = new NetconfMessage(doc);
48     }
49
50     static SchemaContext getNotificationSchemaContext(final Class<?> loadClass,
51                                                       final boolean getExceptionTest) throws Exception {
52         final List<InputStream> modelsToParse = new ArrayList<>();
53
54         if (getExceptionTest) {
55             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification4.yang"));
56             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification3.yang"));
57         } else {
58             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification.yang"));
59             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification2.yang"));
60         }
61
62         final SchemaContext context = YangParserTestUtils.parseYangStreams(modelsToParse);
63         final Set<Module> modules = context.getModules();
64         assertTrue(!modules.isEmpty());
65         assertNotNull(context);
66         return context;
67     }
68
69     @Test(expected =  IllegalStateException.class)
70     public void testMostRecentWrongYangModel() throws Exception {
71         final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), true);
72         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
73         messageTransformer.toNotification(userNotification);
74     }
75
76     @Test
77     public void testToNotificationFunction() throws Exception {
78         final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), false);
79         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
80         final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
81         final ContainerNode root = domNotification.getBody();
82         assertNotNull(root);
83         assertEquals(6, Iterables.size(root.getValue()));
84         assertEquals("user-visited-page", root.getNodeType().getLocalName());
85         assertEquals(NetconfNotification.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00"),
86                 ((DOMEvent) domNotification).getEventTime());
87     }
88 }