NetconfClientFactoryImpl should propagate stack failure
[netconf.git] / protocol / netconf-client / src / test / java / org / opendaylight / netconf / client / NC1252Test.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  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.client;
9
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;
13
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;
33
34 @ExtendWith(MockitoExtension.class)
35 class NC1252Test {
36     private static DefaultNetconfTimer TIMER;
37     private static NetconfClientFactoryImpl FACTORY;
38
39     @Mock
40     private NetconfClientSessionListener sessionListener;
41
42     @BeforeAll
43     static void beforeAll() {
44         TIMER = new DefaultNetconfTimer();
45         FACTORY = new NetconfClientFactoryImpl(TIMER);
46     }
47
48     @AfterAll
49     static void afterAll() {
50         FACTORY.close();
51         TIMER.close();
52     }
53
54     @Test
55     void tcpClientNoServer() throws Exception {
56         // Find a free local port
57         final int localPort;
58         try (var socket = new ServerSocket(0)) {
59             localPort = socket.getLocalPort();
60         }
61
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)))
67                 .build())
68             .withSessionListener(sessionListener)
69             .build();
70         final var future = FACTORY.createClient(clientConfig);
71
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: ");
75     }
76 }