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.sal.connect.netconf;
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.mdsal.dom.api.DOMNotification;
21 import org.opendaylight.netconf.api.NetconfMessage;
22 import org.opendaylight.netconf.api.messages.NotificationMessage;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
25 import org.opendaylight.yangtools.rfc8528.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;
32 public class NetconfToNotificationTest extends AbstractBaseSchemasTest {
34 NetconfMessageTransformer messageTransformer;
36 NetconfMessage userNotification;
39 public void setup() throws Exception {
40 InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
41 assertNotNull(notifyPayloadStream);
43 final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
45 userNotification = new NetconfMessage(doc);
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");
55 context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification.yang",
56 "/schemas/user-notification2.yang");
59 final Collection<? extends Module> modules = context.getModules();
60 assertTrue(!modules.isEmpty());
61 assertNotNull(context);
66 public void testMostRecentWrongYangModel() throws Exception {
67 final EffectiveModelContext schemaContext = getNotificationSchemaContext(getClass(), true);
68 messageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(schemaContext), true,
69 BASE_SCHEMAS.getBaseSchema());
70 assertThrows(IllegalArgumentException.class, () -> messageTransformer.toNotification(userNotification));
74 public void testToNotificationFunction() throws Exception {
75 final EffectiveModelContext schemaContext = getNotificationSchemaContext(getClass(), false);
76 messageTransformer = new NetconfMessageTransformer(new EmptyMountPointContext(schemaContext), true,
77 BASE_SCHEMAS.getBaseSchema());
78 final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
79 final ContainerNode root = domNotification.getBody();
81 assertEquals(6, root.body().size());
82 assertEquals("user-visited-page", root.getIdentifier().getNodeType().getLocalName());
83 assertEquals(NotificationMessage.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00"),
84 ((DOMEvent) domNotification).getEventInstant());