netconf-notifications-api unit tests added 25/45725/6
authorRudolf Brisuda <rudolf.brisuda@pantheon.tech>
Fri, 16 Sep 2016 11:05:24 +0000 (13:05 +0200)
committerRudolf Brisuda <rbrisuda@cisco.com>
Mon, 17 Oct 2016 13:47:23 +0000 (13:47 +0000)
Change-Id: I02d2bbabb6e5fabb8f45a89c9233a0e763636596
Signed-off-by: Rudolf Brisuda <rudolf.brisuda@pantheon.tech>
netconf/netconf-notifications-api/src/test/java/org/opendaylight/netconf/notifications/NetconfNotificationTest.java [new file with mode: 0644]

diff --git a/netconf/netconf-notifications-api/src/test/java/org/opendaylight/netconf/notifications/NetconfNotificationTest.java b/netconf/netconf-notifications-api/src/test/java/org/opendaylight/netconf/notifications/NetconfNotificationTest.java
new file mode 100644 (file)
index 0000000..345b37a
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.netconf.notifications;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+public class NetconfNotificationTest {
+
+    @Test
+    public void testWrapNotification() throws Exception {
+        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
+        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
+
+        final Document document = docBuilder.newDocument();
+
+        final Element rootElement = document.createElement("test-root");
+        document.appendChild(rootElement);
+
+        final Date eventTime = new Date();
+        eventTime.setTime(10000000);
+
+        final NetconfNotification netconfNotification = new NetconfNotification(document, eventTime);
+        final Document resultDoc = netconfNotification.getDocument();
+        final NodeList nodeList = resultDoc.getElementsByTagNameNS(NetconfNotification.NOTIFICATION_NAMESPACE,
+                NetconfNotification.NOTIFICATION);
+
+        assertNotNull(nodeList);
+        // expected only the one NOTIFICATION tag
+        assertEquals(1, nodeList.getLength());
+
+        final Element entireNotification = (Element) nodeList.item(0);
+        final NodeList childNodes = entireNotification.getElementsByTagNameNS(
+                NetconfNotification.NOTIFICATION_NAMESPACE, NetconfNotification.EVENT_TIME);
+
+        assertNotNull(childNodes);
+        // expected only the one EVENT_TIME tag
+        assertEquals(1, childNodes.getLength());
+
+        final Element eventTimeElement = (Element) childNodes.item(0);
+
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+        assertEquals(eventTime.getTime(), sdf.parse(eventTimeElement.getTextContent()).getTime());
+
+        assertEquals(eventTime, netconfNotification.getEventTime());
+
+    }
+}