Rework BaseScheams
[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 java.util.Set;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.dom.api.DOMEvent;
21 import org.opendaylight.netconf.api.messages.NetconfMessage;
22 import org.opendaylight.netconf.api.messages.NotificationMessage;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
25 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformer;
26 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
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 extends AbstractBaseSchemasTest {
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
66     public void testMostRecentWrongYangModel() throws Exception {
67         final var schemaContext = getNotificationSchemaContext(getClass(), true);
68         messageTransformer = new NetconfMessageTransformer(MountPointContext.of(schemaContext), true,
69             BASE_SCHEMAS.baseSchemaForCapabilities(NetconfSessionPreferences.fromStrings(Set.of())));
70         assertThrows(IllegalArgumentException.class, () -> messageTransformer.toNotification(userNotification));
71     }
72
73     @Test
74     public void testToNotificationFunction() throws Exception {
75         final var schemaContext = getNotificationSchemaContext(getClass(), false);
76         messageTransformer = new NetconfMessageTransformer(MountPointContext.of(schemaContext), true,
77             BASE_SCHEMAS.baseSchemaForCapabilities(NetconfSessionPreferences.fromStrings(Set.of())));
78         final var domNotification = messageTransformer.toNotification(userNotification);
79         final var root = domNotification.getBody();
80         assertNotNull(root);
81         assertEquals(6, root.body().size());
82         assertEquals("user-visited-page", root.name().getNodeType().getLocalName());
83         assertEquals(NotificationMessage.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00"),
84                 ((DOMEvent) domNotification).getEventInstant());
85     }
86 }