Integrate netconf-events-mdsal
[netconf.git] / plugins / netconf-server-mdsal / src / test / java / org / opendaylight / netconf / server / mdsal / notifications / 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.server.mdsal.notifications;
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.Map;
17 import java.util.Set;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.mockito.Answers;
21 import org.opendaylight.mdsal.binding.dom.codec.impl.di.DefaultBindingDOMCodecFactory;
22 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
23 import org.opendaylight.netconf.api.messages.NotificationMessage;
24 import org.opendaylight.netconf.api.xml.XmlUtil;
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.yangtools.yang.binding.EventInstantAware;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
30 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
31 import org.xmlunit.builder.DiffBuilder;
32 import org.xmlunit.diff.DefaultNodeMatcher;
33 import org.xmlunit.diff.ElementSelectors;
34
35 public class NotificationsTransformUtilTest {
36     private static final Instant EVENT_TIME = Instant.now();
37     private static final String INNER_NOTIFICATION = """
38             <netconf-capability-change xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
39                 <deleted-capability>uri4</deleted-capability>
40                 <deleted-capability>uri3</deleted-capability>
41                 <added-capability>uri1</added-capability>
42             </netconf-capability-change>
43         """;
44
45     private static final String EXPECTED_NOTIFICATION =
46         "<notification xmlns=\"urn:ietf:params:netconf:capability:notification:1.0\">\n"
47         + INNER_NOTIFICATION
48         + "    <eventTime>" + NotificationMessage.RFC3339_DATE_FORMATTER.apply(EVENT_TIME) + "</eventTime>\n"
49         + "</notification>\n";
50
51     private static NotificationsTransformUtil UTIL;
52
53     @BeforeClass
54     public static void beforeClass() throws YangParserException {
55         UTIL = new NotificationsTransformUtil(new DefaultYangParserFactory(), new DefaultBindingRuntimeGenerator(),
56             new DefaultBindingDOMCodecFactory());
57     }
58
59     @Test
60     public void testTransform() throws Exception {
61         final var capabilityChange = mock(NetconfCapabilityChange.class,
62             withSettings().extraInterfaces(EventInstantAware.class).defaultAnswer(Answers.CALLS_REAL_METHODS));
63         doReturn(Set.of(new Uri("uri1"))).when(capabilityChange).getAddedCapability();
64         doReturn(Set.of(new Uri("uri4"), new Uri("uri3"))).when(capabilityChange).getDeletedCapability();
65         doReturn(EVENT_TIME).when((EventInstantAware) capabilityChange).eventInstant();
66         doReturn(null).when(capabilityChange).getChangedBy();
67         doReturn(null).when(capabilityChange).getModifiedCapability();
68         doReturn(Map.of()).when(capabilityChange).augmentations();
69
70         final var notification = UTIL.transform(capabilityChange, Absolute.of(NetconfCapabilityChange.QNAME));
71
72         compareXml(EXPECTED_NOTIFICATION, XmlUtil.toString(notification.getDocument()));
73     }
74
75     @Test
76     public void testTransformFromDOM() throws Exception {
77         final var notification = new NotificationMessage(XmlUtil.readXmlToDocument(INNER_NOTIFICATION), EVENT_TIME);
78
79         compareXml(EXPECTED_NOTIFICATION, notification.toString());
80     }
81
82     private static void compareXml(final String expected, final String actual) throws Exception {
83         final var diff = DiffBuilder.compare(expected)
84             .withTest(actual)
85             .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
86             .checkForSimilar()
87             .build();
88
89         assertFalse(diff.toString(), diff.hasDifferences());
90     }
91 }