e6a7502ac71cdd45c55a2168161e785071a89b49
[netconf.git] / netconf / netconf-notifications-impl / src / test / java / org / opendaylight / netconf / notifications / impl / ops / NotificationsTransformUtilTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.notifications.impl.ops;
10
11 import static org.junit.Assert.assertTrue;
12
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.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.xml.sax.SAXException;
29
30 public class NotificationsTransformUtilTest {
31
32     private static final Date DATE = new Date();
33     private static final String innerNotification = "<netconf-capability-change xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-notifications\">" +
34             "<deleted-capability>uri4</deleted-capability>" +
35             "<deleted-capability>uri3</deleted-capability>" +
36             "<added-capability>uri1</added-capability>" +
37             "</netconf-capability-change>";
38
39     private static final String expectedNotification = "<notification xmlns=\"urn:ietf:params:netconf:capability:notification:1.0\">" +
40             innerNotification +
41             "<eventTime>" + new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT).format(DATE) + "</eventTime>" +
42             "</notification>";
43
44     @Test
45     public void testTransform() throws Exception {
46         final NetconfCapabilityChangeBuilder netconfCapabilityChangeBuilder = new NetconfCapabilityChangeBuilder();
47
48         netconfCapabilityChangeBuilder.setAddedCapability(Lists.newArrayList(new Uri("uri1"), new Uri("uri1")));
49         netconfCapabilityChangeBuilder.setDeletedCapability(Lists.newArrayList(new Uri("uri4"), new Uri("uri3")));
50
51         final NetconfCapabilityChange capabilityChange = netconfCapabilityChangeBuilder.build();
52         final NetconfNotification transform = NotificationsTransformUtil.transform(capabilityChange, DATE, SchemaPath.create(true, NetconfCapabilityChange.QNAME));
53
54         final String serialized = XmlUtil.toString(transform.getDocument());
55
56         compareXml(expectedNotification, serialized);
57     }
58
59     static void compareXml(final String expected, final String actual) throws SAXException, IOException {
60         XMLUnit.setIgnoreWhitespace(true);
61         final Diff diff = new Diff(expected, actual);
62         final DetailedDiff detailedDiff = new DetailedDiff(diff);
63         detailedDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
64         assertTrue(detailedDiff.toString(), detailedDiff.similar());
65     }
66
67     @Test
68     public void testTransformFromDOM() throws Exception {
69         final NetconfNotification netconfNotification = new NetconfNotification(XmlUtil.readXmlToDocument(innerNotification), DATE);
70
71         XMLUnit.setIgnoreWhitespace(true);
72         compareXml(expectedNotification, netconfNotification.toString());
73     }
74
75 }