Remove useless warning about rounding
[netconf.git] / protocol / netconf-api / src / test / java / org / opendaylight / netconf / api / messages / NetconfMessageTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech s.r.o. 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.api.messages;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13
14 import com.google.common.collect.ImmutableMap;
15 import java.time.Instant;
16 import java.util.List;
17 import java.util.Optional;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.netconf.api.DocumentedException;
20 import org.opendaylight.yangtools.util.xml.UntrustedXML;
21 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.common.ErrorType;
24 import org.w3c.dom.Document;
25
26 class NetconfMessageTest {
27     private static final String MESSAGE_ID = "1014";
28
29     @Test
30     void testOfHello() throws Exception {
31         final var expected = HelloMessage.createClientHello(List.of(), Optional.empty());
32         final var msg = assertInstanceOf(HelloMessage.class, NetconfMessage.of(expected.getDocument()));
33         assertEquals("""
34             <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
35                 <capabilities/>
36             </hello>
37             """, msg.toString());
38     }
39
40     @Test
41     void testOfNotification() throws Exception {
42         final var expected = NotificationMessage.ofNotificationContent(getTestElement(), Instant.ofEpochSecond(42));
43         final var msg = assertInstanceOf(NotificationMessage.class, NetconfMessage.of(expected.getDocument()));
44         assertEquals("""
45             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
46                 <test-root/>
47                 <eventTime>1970-01-01T00:00:42Z</eventTime>
48             </notification>
49             """, msg.toString());
50     }
51
52     @Test
53     void testOfNotificationNanos() throws Exception {
54         final var eventTime = Instant.ofEpochSecond(42, 123456789);
55         final var expected = NotificationMessage.ofNotificationContent(getTestElement(), eventTime);
56         final var msg = assertInstanceOf(NotificationMessage.class, NetconfMessage.of(expected.getDocument()));
57         assertEquals("""
58             <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
59                 <test-root/>
60                 <eventTime>1970-01-01T00:00:42.123456789Z</eventTime>
61             </notification>
62             """, msg.toString());
63         assertEquals(eventTime, msg.getEventTime());
64     }
65
66     @Test
67     void testOfRpc() throws Exception {
68         final var expected = RpcMessage.ofOperation(MESSAGE_ID, getTestElement());
69         final var msg = assertInstanceOf(RpcMessage.class, NetconfMessage.of(expected.getDocument()));
70         assertEquals("""
71             <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1014">
72                 <test-root/>
73             </rpc>
74             """, msg.toString());
75     }
76
77     @Test
78     void testOfRpcRply() throws Exception {
79         final var expected = RpcReplyMessage.ofOperation(MESSAGE_ID, getTestElement());
80         final var msg = assertInstanceOf(RpcReplyMessage.class, NetconfMessage.of(expected.getDocument()));
81         assertEquals("""
82             <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1014">
83                 <test-root/>
84             </rpc-reply>
85             """, msg.toString());
86     }
87
88     void testOfInvalid() {
89         final var ex = assertThrows(DocumentedException.class, () -> NetconfMessage.of(getTestElement()));
90         assertEquals("", ex.getMessage());
91         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
92         assertEquals(ErrorType.PROTOCOL, ex.getErrorType());
93         assertEquals(ErrorTag.UNKNOWN_ELEMENT, ex.getErrorTag());
94         assertEquals(ImmutableMap.of("bad-element", "test-root"), ex.getErrorInfo());
95     }
96
97     private static Document getTestElement() {
98         final var document = UntrustedXML.newDocumentBuilder().newDocument();
99         final var rootElement = document.createElement("test-root");
100         document.appendChild(rootElement);
101         return document;
102     }
103 }