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 testOfRpc() throws Exception {
54 final var expected = RpcMessage.ofOperation(MESSAGE_ID, getTestElement());
55 final var msg = assertInstanceOf(RpcMessage.class, NetconfMessage.of(expected.getDocument()));
57 <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1014">
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()));
68 <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1014">
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());
83 private static Document getTestElement() {
84 final var document = UntrustedXML.newDocumentBuilder().newDocument();
85 final var rootElement = document.createElement("test-root");
86 document.appendChild(rootElement);