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
9 package org.opendaylight.netconf.sal.connect.netconf;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.Iterables;
16 import java.io.InputStream;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
22 import org.opendaylight.netconf.api.NetconfMessage;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24 import org.opendaylight.netconf.notifications.NetconfNotification;
25 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30 import org.w3c.dom.Document;
32 public class NetconfToNotificationTest {
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 SchemaContext getNotificationSchemaContext(final Class<?> loadClass, final boolean getExceptionTest) {
49 final SchemaContext context;
50 if (getExceptionTest) {
51 context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification4.yang",
52 "/schemas/user-notification3.yang");
54 context = YangParserTestUtils.parseYangResources(loadClass, "/schemas/user-notification.yang",
55 "/schemas/user-notification2.yang");
58 final Set<Module> modules = context.getModules();
59 assertTrue(!modules.isEmpty());
60 assertNotNull(context);
64 @Test(expected = IllegalArgumentException.class)
65 public void testMostRecentWrongYangModel() throws Exception {
66 final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), true);
67 messageTransformer = new NetconfMessageTransformer(schemaContext, true);
68 messageTransformer.toNotification(userNotification);
72 public void testToNotificationFunction() throws Exception {
73 final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), false);
74 messageTransformer = new NetconfMessageTransformer(schemaContext, true);
75 final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
76 final ContainerNode root = domNotification.getBody();
78 assertEquals(6, Iterables.size(root.getValue()));
79 assertEquals("user-visited-page", root.getNodeType().getLocalName());
80 assertEquals(NetconfNotification.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00"),
81 ((DOMEvent) domNotification).getEventTime());