Update MRI projects for Aluminium
[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 package org.opendaylight.netconf.sal.connect.netconf;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.Iterables;
15 import java.io.InputStream;
16 import java.util.Collection;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.dom.api.DOMEvent;
20 import org.opendaylight.mdsal.dom.api.DOMNotification;
21 import org.opendaylight.netconf.api.NetconfMessage;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.notifications.NetconfNotification;
24 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
25 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.Module;
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 EffectiveModelContext getNotificationSchemaContext(final Class<?> loadClass,
49             final boolean getExceptionTest) {
50         final EffectiveModelContext context;
51         if (getExceptionTest) {
52             context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification4.yang",
53                     "/schemas/user-notification3.yang");
54         } else {
55             context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification.yang",
56                 "/schemas/user-notification2.yang");
57         }
58
59         final Collection<? extends Module> modules = context.getModules();
60         assertTrue(!modules.isEmpty());
61         assertNotNull(context);
62         return context;
63     }
64
65     @Test(expected =  IllegalArgumentException.class)
66     public void testMostRecentWrongYangModel() throws Exception {
67         final EffectiveModelContext schemaContext = getNotificationSchemaContext(getClass(), true);
68         messageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(schemaContext), true);
69         messageTransformer.toNotification(userNotification);
70     }
71
72     @Test
73     public void testToNotificationFunction() throws Exception {
74         final EffectiveModelContext schemaContext = getNotificationSchemaContext(getClass(), false);
75         messageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(schemaContext), true);
76         final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
77         final ContainerNode root = domNotification.getBody();
78         assertNotNull(root);
79         assertEquals(6, Iterables.size(root.getValue()));
80         assertEquals("user-visited-page", root.getNodeType().getLocalName());
81         assertEquals(NetconfNotification.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00").toInstant(),
82                 ((DOMEvent) domNotification).getEventInstant());
83     }
84 }