86bc60010d5150ed1bf7513f9fbcba692ef8c2ea
[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 testOfRpc() throws Exception {
54         final var expected = RpcMessage.ofOperation(MESSAGE_ID, getTestElement());
55         final var msg = assertInstanceOf(RpcMessage.class, NetconfMessage.of(expected.getDocument()));
56         assertEquals("""
57             <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1014">
58                 <test-root/>
59             </rpc>
60             """, msg.toString());
61     }
62
63     @Test
64     void testOfRpcRply() throws Exception {
65         final var expected = RpcReplyMessage.ofOperation(MESSAGE_ID, getTestElement());
66         final var msg = assertInstanceOf(RpcReplyMessage.class, NetconfMessage.of(expected.getDocument()));
67         assertEquals("""
68             <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1014">
69                 <test-root/>
70             </rpc-reply>
71             """, msg.toString());
72     }
73
74     void testOfInvalid() {
75         final var ex = assertThrows(DocumentedException.class, () -> NetconfMessage.of(getTestElement()));
76         assertEquals("", ex.getMessage());
77         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
78         assertEquals(ErrorType.PROTOCOL, ex.getErrorType());
79         assertEquals(ErrorTag.UNKNOWN_ELEMENT, ex.getErrorTag());
80         assertEquals(ImmutableMap.of("bad-element", "test-root"), ex.getErrorInfo());
81     }
82
83     private static Document getTestElement() {
84         final var document = UntrustedXML.newDocumentBuilder().newDocument();
85         final var rootElement = document.createElement("test-root");
86         document.appendChild(rootElement);
87         return document;
88     }
89 }