db4c9873f00d76a55e5cd546b4c416933d26ebf9
[netconf.git] / netconf / mdsal-netconf-notification / src / test / java / org / opendaylight / netconf / mdsal / notification / 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 package org.opendaylight.netconf.mdsal.notification.impl.ops;
9
10 import static org.junit.Assert.assertTrue;
11
12 import java.io.IOException;
13 import java.util.Date;
14 import java.util.Set;
15 import org.custommonkey.xmlunit.DetailedDiff;
16 import org.custommonkey.xmlunit.Diff;
17 import org.custommonkey.xmlunit.XMLUnit;
18 import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.dom.codec.impl.DefaultBindingDOMCodecFactory;
22 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24 import org.opendaylight.netconf.notifications.NetconfNotification;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChangeBuilder;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
30 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
31 import org.xml.sax.SAXException;
32
33 public class NotificationsTransformUtilTest {
34     private static final Date DATE = new Date();
35     private static final String INNER_NOTIFICATION =
36             "<netconf-capability-change xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-notifications\">"
37                     + "<deleted-capability>uri4</deleted-capability>"
38                     + "<deleted-capability>uri3</deleted-capability>"
39                     + "<added-capability>uri1</added-capability>"
40                     + "</netconf-capability-change>";
41
42     private static final String EXPECTED_NOTIFICATION =
43             "<notification xmlns=\"urn:ietf:params:netconf:capability:notification:1.0\">"
44                     + INNER_NOTIFICATION
45                     + "<eventTime>"
46                     + NetconfNotification.RFC3339_DATE_FORMATTER.apply(DATE)
47                     + "</eventTime>"
48                     + "</notification>";
49
50     private static NotificationsTransformUtil UTIL;
51
52     @BeforeClass
53     public static void beforeClass() throws YangParserException {
54         UTIL = new NotificationsTransformUtil(new DefaultYangParserFactory(), new DefaultBindingRuntimeGenerator(),
55             new DefaultBindingDOMCodecFactory());
56     }
57
58     @Test
59     public void testTransform() throws Exception {
60         final NetconfCapabilityChangeBuilder netconfCapabilityChangeBuilder = new NetconfCapabilityChangeBuilder();
61
62         netconfCapabilityChangeBuilder.setAddedCapability(Set.of(new Uri("uri1")));
63         netconfCapabilityChangeBuilder.setDeletedCapability(Set.of(new Uri("uri4"), new Uri("uri3")));
64
65         final NetconfCapabilityChange capabilityChange = netconfCapabilityChangeBuilder.build();
66         final NetconfNotification transform = UTIL.transform(capabilityChange, DATE,
67                 SchemaPath.create(true, NetconfCapabilityChange.QNAME));
68
69         final String serialized = XmlUtil.toString(transform.getDocument());
70
71         compareXml(EXPECTED_NOTIFICATION, serialized);
72     }
73
74     static void compareXml(final String expected, final String actual) throws SAXException, IOException {
75         XMLUnit.setIgnoreWhitespace(true);
76         final Diff diff = new Diff(expected, actual);
77         final DetailedDiff detailedDiff = new DetailedDiff(diff);
78         detailedDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
79         assertTrue(detailedDiff.toString(), detailedDiff.similar());
80     }
81
82     @Test
83     public void testTransformFromDOM() throws Exception {
84         final NetconfNotification netconfNotification =
85                 new NetconfNotification(XmlUtil.readXmlToDocument(INNER_NOTIFICATION), DATE);
86
87         XMLUnit.setIgnoreWhitespace(true);
88         compareXml(EXPECTED_NOTIFICATION, netconfNotification.toString());
89     }
90
91 }