2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. 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.client;
10 import static org.assertj.core.api.Assertions.assertThat;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import java.net.ConnectException;
15 import java.net.InetAddress;
16 import java.net.ServerSocket;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.jupiter.api.AfterAll;
20 import org.junit.jupiter.api.BeforeAll;
21 import org.junit.jupiter.api.Test;
22 import org.junit.jupiter.api.extension.ExtendWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.jupiter.MockitoExtension;
25 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
26 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
27 import org.opendaylight.netconf.common.impl.DefaultNetconfTimer;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.client.rev240208.netconf.client.initiate.stack.grouping.transport.tls.tls.TcpClientParametersBuilder;
32 import org.opendaylight.yangtools.yang.common.Uint16;
34 @ExtendWith(MockitoExtension.class)
36 private static DefaultNetconfTimer TIMER;
37 private static NetconfClientFactoryImpl FACTORY;
40 private NetconfClientSessionListener sessionListener;
43 static void beforeAll() {
44 TIMER = new DefaultNetconfTimer();
45 FACTORY = new NetconfClientFactoryImpl(TIMER);
49 static void afterAll() {
55 void tcpClientNoServer() throws Exception {
56 // Find a free local port
58 try (var socket = new ServerSocket(0)) {
59 localPort = socket.getLocalPort();
62 final var clientConfig = NetconfClientConfigurationBuilder.create()
63 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP)
64 .withTcpParameters(new TcpClientParametersBuilder()
65 .setRemoteAddress(new Host(IetfInetUtil.ipAddressFor(InetAddress.getLoopbackAddress())))
66 .setRemotePort(new PortNumber(Uint16.valueOf(localPort)))
68 .withSessionListener(sessionListener)
70 final var future = FACTORY.createClient(clientConfig);
72 final var ex = assertThrows(ExecutionException.class, () -> future.get(1, TimeUnit.SECONDS)).getCause();
73 assertInstanceOf(ConnectException.class, ex);
74 assertThat(ex.getMessage()).startsWith("Connection refused: ");