2 * Copyright (c) 2023 PANTHEON.tech s.r.o. All rights reserved.
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
8 package org.opendaylight.netconf.api.messages;
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;
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;
26 class NetconfMessageTest {
27 private static final String MESSAGE_ID = "1014";
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()));
34 <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
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()));
45 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
47 <eventTime>1970-01-01T00:00:42Z</eventTime>
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()));
58 <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
60 <eventTime>1970-01-01T00:00:42.123456789Z</eventTime>
63 assertEquals(eventTime, msg.getEventTime());
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()));
71 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1014">
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()));
82 <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1014">
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());
97 private static Document getTestElement() {
98 final var document = UntrustedXML.newDocumentBuilder().newDocument();
99 final var rootElement = document.createElement("test-root");
100 document.appendChild(rootElement);