2 * Copyright (c) 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.notifications.impl.ops;
11 import static org.junit.Assert.assertTrue;
13 import com.google.common.collect.Lists;
14 import java.io.IOException;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import org.custommonkey.xmlunit.DetailedDiff;
18 import org.custommonkey.xmlunit.Diff;
19 import org.custommonkey.xmlunit.XMLUnit;
20 import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.util.xml.XmlUtil;
23 import org.opendaylight.netconf.notifications.NetconfNotification;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChangeBuilder;
27 import org.xml.sax.SAXException;
29 public class NotificationsTransformUtilTest {
31 private static final Date DATE = new Date();
32 private static final String innerNotification = "<netconf-capability-change xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-notifications\">" +
33 "<deleted-capability>uri4</deleted-capability>" +
34 "<deleted-capability>uri3</deleted-capability>" +
35 "<added-capability>uri1</added-capability>" +
36 "</netconf-capability-change>";
38 private static final String expectedNotification = "<notification xmlns=\"urn:ietf:params:netconf:capability:notification:1.0\">" +
40 "<eventTime>" + new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT).format(DATE) + "</eventTime>" +
44 public void testTransform() throws Exception {
45 final NetconfCapabilityChangeBuilder netconfCapabilityChangeBuilder = new NetconfCapabilityChangeBuilder();
47 netconfCapabilityChangeBuilder.setAddedCapability(Lists.newArrayList(new Uri("uri1"), new Uri("uri1")));
48 netconfCapabilityChangeBuilder.setDeletedCapability(Lists.newArrayList(new Uri("uri4"), new Uri("uri3")));
50 final NetconfCapabilityChange capabilityChange = netconfCapabilityChangeBuilder.build();
51 final NetconfNotification transform = NotificationsTransformUtil.transform(capabilityChange, DATE);
53 final String serialized = XmlUtil.toString(transform.getDocument());
55 compareXml(expectedNotification, serialized);
58 static void compareXml(final String expected, final String actual) throws SAXException, IOException {
59 XMLUnit.setIgnoreWhitespace(true);
60 final Diff diff = new Diff(expected, actual);
61 final DetailedDiff detailedDiff = new DetailedDiff(diff);
62 detailedDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
63 assertTrue(detailedDiff.toString(), detailedDiff.similar());
67 public void testTransformFromDOM() throws Exception {
68 final NetconfNotification netconfNotification = new NetconfNotification(XmlUtil.readXmlToDocument(innerNotification), DATE);
70 XMLUnit.setIgnoreWhitespace(true);
71 compareXml(expectedNotification, netconfNotification.toString());