2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.client.mdsal;
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;
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;
30 public class NetconfToNotificationTest extends AbstractBaseSchemasTest {
32 NetconfMessageTransformer messageTransformer;
34 NetconfMessage userNotification;
37 public void setup() throws Exception {
38 InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
39 assertNotNull(notifyPayloadStream);
41 final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
43 userNotification = new NetconfMessage(doc);
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");
53 context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification.yang",
54 "/schemas/user-notification2.yang");
57 final Collection<? extends Module> modules = context.getModules();
58 assertTrue(!modules.isEmpty());
59 assertNotNull(context);
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));
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();
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());