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;
17 import java.util.ArrayList;
18 import java.util.List;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.util.xml.XmlUtil;
24 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
25 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
26 import org.opendaylight.netconf.api.NetconfMessage;
27 import org.opendaylight.netconf.notifications.NetconfNotification;
28 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 import org.w3c.dom.Document;
35 public class NetconfToNotificationTest {
37 NetconfMessageTransformer messageTransformer;
39 NetconfMessage userNotification;
41 @SuppressWarnings("deprecation")
43 public void setup() throws Exception {
44 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
45 factory.setNamespaceAware(true);
46 InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
47 assertNotNull(notifyPayloadStream);
49 final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
51 userNotification = new NetconfMessage(doc);
54 static SchemaContext getNotificationSchemaContext(Class<?> loadClass, boolean getExceptionTest) throws Exception {
55 final List<InputStream> modelsToParse = new ArrayList<>();
57 if (getExceptionTest) {
58 modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification4.yang"));
59 modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification3.yang"));
61 modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification.yang"));
62 modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification2.yang"));
65 final SchemaContext context = YangParserTestUtils.parseYangStreams(modelsToParse);
66 final Set<Module> modules = context.getModules();
67 assertTrue(!modules.isEmpty());
68 assertNotNull(context);
72 @Test(expected = IllegalStateException.class)
73 public void testMostRecentWrongYangModel() throws Exception {
74 final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), true);
75 messageTransformer = new NetconfMessageTransformer(schemaContext, true);
76 messageTransformer.toNotification(userNotification);
80 public void testToNotificationFunction() throws Exception {
81 final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), false);
82 messageTransformer = new NetconfMessageTransformer(schemaContext, true);
83 final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
84 final ContainerNode root = domNotification.getBody();
86 assertEquals(6, Iterables.size(root.getValue()));
87 assertEquals("user-visited-page", root.getNodeType().getLocalName());
88 assertEquals(NetconfNotification.RFC3339_DATE_PARSER.apply("2015-10-23T09:42:27.67175+00:00"),
89 ((DOMEvent) domNotification).getEventTime());