3255f429be1544898e24c54b82c578543c55784c
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / 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.client.mdsal;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14
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.netconf.api.messages.NetconfMessage;
21 import org.opendaylight.netconf.api.messages.NotificationMessage;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformer;
24 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 import org.w3c.dom.Document;
29
30 public class NetconfToNotificationTest extends AbstractBaseSchemasTest {
31
32     NetconfMessageTransformer messageTransformer;
33
34     NetconfMessage userNotification;
35
36     @Before
37     public void setup() throws Exception {
38         InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
39         assertNotNull(notifyPayloadStream);
40
41         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
42         assertNotNull(doc);
43         userNotification = new NetconfMessage(doc);
44     }
45
46     static EffectiveModelContext getNotificationSchemaContext(final Class<?> loadClass,
47             final boolean getExceptionTest) {
48         final EffectiveModelContext context;
49         if (getExceptionTest) {
50             context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification4.yang",
51                     "/schemas/user-notification3.yang");
52         } else {
53             context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification.yang",
54                 "/schemas/user-notification2.yang");
55         }
56
57         final Collection<? extends Module> modules = context.getModules();
58         assertTrue(!modules.isEmpty());
59         assertNotNull(context);
60         return context;
61     }
62
63     @Test
64     public void testMostRecentWrongYangModel() throws Exception {
65         final var schemaContext = getNotificationSchemaContext(getClass(), true);
66         messageTransformer = new NetconfMessageTransformer(MountPointContext.of(schemaContext), true,
67             BASE_SCHEMAS.baseSchema());
68         assertThrows(IllegalArgumentException.class, () -> messageTransformer.toNotification(userNotification));
69     }
70
71     @Test
72     public void testToNotificationFunction() throws Exception {
73         final var schemaContext = getNotificationSchemaContext(getClass(), false);
74         messageTransformer = new NetconfMessageTransformer(MountPointContext.of(schemaContext), true,
75             BASE_SCHEMAS.baseSchema());
76         final var domNotification = messageTransformer.toNotification(userNotification);
77         final var root = domNotification.getBody();
78         assertNotNull(root);
79         assertEquals(6, root.body().size());
80         assertEquals("user-visited-page", root.name().getNodeType().getLocalName());
81         assertEquals(NotificationMessage.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00"),
82                 ((DOMEvent) domNotification).getEventInstant());
83     }
84 }