Clean up NotificationMessage
[netconf.git] / netconf / mdsal-netconf-notification / src / test / java / org / opendaylight / netconf / mdsal / notification / impl / 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;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.withSettings;
14
15 import java.time.Instant;
16 import java.util.Set;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.mockito.Answers;
20 import org.opendaylight.mdsal.binding.dom.codec.impl.di.DefaultBindingDOMCodecFactory;
21 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
22 import org.opendaylight.netconf.api.messages.NotificationMessage;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
26 import org.opendaylight.yangtools.yang.binding.EventInstantAware;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
28 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
29 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
30 import org.xmlunit.builder.DiffBuilder;
31 import org.xmlunit.diff.DefaultNodeMatcher;
32 import org.xmlunit.diff.ElementSelectors;
33
34 public class NotificationsTransformUtilTest {
35     private static final Instant EVENT_TIME = Instant.now();
36     private static final String INNER_NOTIFICATION = """
37             <netconf-capability-change xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
38                 <deleted-capability>uri4</deleted-capability>
39                 <deleted-capability>uri3</deleted-capability>
40                 <added-capability>uri1</added-capability>
41             </netconf-capability-change>
42         """;
43
44     private static final String EXPECTED_NOTIFICATION =
45         "<notification xmlns=\"urn:ietf:params:netconf:capability:notification:1.0\">\n"
46         + INNER_NOTIFICATION
47         + "    <eventTime>" + NotificationMessage.RFC3339_DATE_FORMATTER.apply(EVENT_TIME) + "</eventTime>\n"
48         + "</notification>\n";
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 var capabilityChange = mock(NetconfCapabilityChange.class,
61             withSettings().extraInterfaces(EventInstantAware.class).defaultAnswer(Answers.CALLS_REAL_METHODS));
62         doReturn(Set.of(new Uri("uri1"))).when(capabilityChange).getAddedCapability();
63         doReturn(Set.of(new Uri("uri4"), new Uri("uri3"))).when(capabilityChange).getDeletedCapability();
64         doReturn(EVENT_TIME).when((EventInstantAware) capabilityChange).eventInstant();
65
66         final var notification = UTIL.transform(capabilityChange, Absolute.of(NetconfCapabilityChange.QNAME));
67
68         compareXml(EXPECTED_NOTIFICATION, XmlUtil.toString(notification.getDocument()));
69     }
70
71     @Test
72     public void testTransformFromDOM() throws Exception {
73         final var notification = new NotificationMessage(XmlUtil.readXmlToDocument(INNER_NOTIFICATION), EVENT_TIME);
74
75         compareXml(EXPECTED_NOTIFICATION, notification.toString());
76     }
77
78     private static void compareXml(final String expected, final String actual) throws Exception {
79         final var diff = DiffBuilder.compare(expected)
80             .withTest(actual)
81             .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
82             .checkForSimilar()
83             .build();
84
85         assertFalse(diff.toString(), diff.hasDifferences());
86     }
87 }