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.assertTrue;
14 import com.google.common.collect.Iterables;
15 import java.io.InputStream;
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.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 import org.w3c.dom.Document;
31 public class NetconfToNotificationTest {
33 NetconfMessageTransformer messageTransformer;
35 NetconfMessage userNotification;
38 public void setup() throws Exception {
39 InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
40 assertNotNull(notifyPayloadStream);
42 final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
44 userNotification = new NetconfMessage(doc);
47 static SchemaContext getNotificationSchemaContext(final Class<?> loadClass, final boolean getExceptionTest) {
48 final SchemaContext 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 Set<Module> modules = context.getModules();
58 assertTrue(!modules.isEmpty());
59 assertNotNull(context);
63 @Test(expected = IllegalArgumentException.class)
64 public void testMostRecentWrongYangModel() throws Exception {
65 final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), true);
66 messageTransformer = new NetconfMessageTransformer(schemaContext, true);
67 messageTransformer.toNotification(userNotification);
71 public void testToNotificationFunction() throws Exception {
72 final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), false);
73 messageTransformer = new NetconfMessageTransformer(schemaContext, true);
74 final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
75 final ContainerNode root = domNotification.getBody();
77 assertEquals(6, Iterables.size(root.getValue()));
78 assertEquals("user-visited-page", root.getNodeType().getLocalName());
79 assertEquals(NetconfNotification.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00").toInstant(),
80 ((DOMEvent) domNotification).getEventInstant());