Migrate netconf-topology tests to JUnit 5
[netconf.git] / apps / netconf-topology / src / test / java / org / opendaylight / netconf / topology / spi / NetconfNodeUtilsTest.java
1 /*
2  * Copyright (c) 2022 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.topology.spi;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12
13 import java.util.NoSuchElementException;
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.DomainName;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.credentials.credentials.LoginPwUnencryptedBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev240120.credentials.credentials.login.pw.unencrypted.LoginPasswordUnencryptedBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNodeBuilder;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
24 import org.opendaylight.yangtools.yang.common.Decimal64;
25 import org.opendaylight.yangtools.yang.common.Uint16;
26 import org.opendaylight.yangtools.yang.common.Uint32;
27
28 class NetconfNodeUtilsTest {
29     @Test
30     void testCreateRemoteDeviceId() {
31         final var host = new Host(new IpAddress(new Ipv4Address("127.0.0.1")));
32         final var id = NetconfNodeUtils.toRemoteDeviceId(new NodeId("testing-node"), new NetconfNodeBuilder()
33             .setHost(host)
34             .setPort(new PortNumber(Uint16.valueOf(9999)))
35             .build());
36
37         assertEquals("testing-node", id.name());
38         assertEquals(host, id.host());
39         assertEquals(9999, id.address().getPort());
40     }
41
42     @Test
43     void toInetSocketAddressRequiresHostPort() {
44         // Creates a netconfNode without specifying a Port and Host.
45         final var builder = new NetconfNodeBuilder()
46             .setReconnectOnChangedSchema(true)
47             .setSchemaless(true)
48             .setTcpOnly(true)
49             .setBackoffMultiplier(Decimal64.valueOf("1.5"))
50             .setConcurrentRpcLimit(Uint16.ONE)
51             // One reconnection attempt
52             .setMaxConnectionAttempts(Uint32.TWO)
53             .setDefaultRequestTimeoutMillis(Uint32.valueOf(1000))
54             .setMinBackoffMillis(Uint16.valueOf(100))
55             .setKeepaliveDelay(Uint32.valueOf(1000))
56             .setConnectionTimeoutMillis(Uint32.valueOf(1000))
57             .setCredentials(new LoginPwUnencryptedBuilder()
58                 .setLoginPasswordUnencrypted(new LoginPasswordUnencryptedBuilder()
59                     .setUsername("testuser")
60                     .setPassword("testpassword")
61                     .build())
62                 .build());
63
64         assertEquals("Value of host is not present", assertThrows(NoSuchElementException.class,
65             () -> NetconfNodeUtils.toInetSocketAddress(builder.build())).getMessage());
66
67         builder.setHost(new Host(new DomainName("testhost")));
68         assertEquals("Value of port is not present", assertThrows(NoSuchElementException.class,
69             () -> NetconfNodeUtils.toInetSocketAddress(builder.build())).getMessage());
70     }
71 }