BUG-6647 Increase code coverage and clean up III
[bgpcep.git] / testtool-util / src / test / java / org / opendaylight / protocol / util / InetSocketAddressUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.protocol.util;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14
15 import com.google.common.net.HostAndPort;
16 import java.lang.reflect.Constructor;
17 import java.lang.reflect.InvocationTargetException;
18 import java.net.InetSocketAddress;
19 import java.util.Arrays;
20 import java.util.List;
21 import org.junit.Assert;
22 import org.junit.Test;
23
24 public class InetSocketAddressUtilTest {
25     private static final String ADDRESS1 = "1.1.1.1";
26     private static final String ADDRESS2 = "2.2.2.2";
27     private static final int PORT1 = 123;
28     private static final int PORT2 = 321;
29     private static final String ADDRESSES_WO_PORT = ADDRESS1 + "," + ADDRESS2;
30     private static final String ADDRESSES = ADDRESS1 + ":" + PORT1 + "," + ADDRESS2 + ":" + PORT2;
31     private static final int DEFAULT_PORT = 179;
32
33     @Test(expected = UnsupportedOperationException.class)
34     public void testPrivateConstructor() throws Throwable {
35         final Constructor<InetSocketAddressUtil> c = InetSocketAddressUtil.class.getDeclaredConstructor();
36         c.setAccessible(true);
37         try {
38             c.newInstance();
39         } catch (final InvocationTargetException e) {
40             throw e.getCause();
41         }
42     }
43
44     @Test
45     public void parseAddresses() throws Exception {
46         final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES, DEFAULT_PORT);
47         Assert.assertEquals(Arrays.asList(new InetSocketAddress(ADDRESS1, PORT1), new InetSocketAddress(ADDRESS2, PORT2)), actualResult);
48     }
49
50     @Test
51     public void toHostAndPort() throws Exception {
52         final HostAndPort actualResult = InetSocketAddressUtil.toHostAndPort(new InetSocketAddress(ADDRESS2, PORT2));
53         final HostAndPort expected = HostAndPort.fromString("2.2.2.2:321");
54         Assert.assertEquals(expected, actualResult);
55     }
56
57     @Test
58     public void parseAddressesDefaultPort() throws Exception {
59         final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES_WO_PORT, DEFAULT_PORT);
60         final List<InetSocketAddress> expected = Arrays.asList(new InetSocketAddress(ADDRESS1, DEFAULT_PORT), new InetSocketAddress(ADDRESS2, DEFAULT_PORT));
61         Assert.assertEquals(expected, actualResult);
62     }
63
64     @Test
65     public void parseAddressesWithoutPort() throws Exception {
66         final List<InetSocketAddress> actualResult = InetSocketAddressUtil.parseAddresses(ADDRESSES);
67         Assert.assertEquals(Arrays.asList(new InetSocketAddress(ADDRESS1, PORT1), new InetSocketAddress(ADDRESS2, PORT2)), actualResult);
68     }
69
70     @Test
71     public void getInetSocketAddress() throws Exception {
72         assertEquals(new InetSocketAddress(ADDRESS1, PORT1), InetSocketAddressUtil.getInetSocketAddress(ADDRESS1 + ":" + PORT1, DEFAULT_PORT));
73         assertEquals(new InetSocketAddress(ADDRESS1, DEFAULT_PORT), InetSocketAddressUtil.getInetSocketAddress(ADDRESS1, DEFAULT_PORT));
74     }
75
76     @Test
77     public void getRandomLoopbackInetSocketAddressTest() throws Exception {
78         final InetSocketAddress addr1 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
79         final InetSocketAddress addr2 = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
80         assertNotNull(addr1);
81         assertNotNull(addr2);
82         assertNotEquals(addr1, addr2);
83         assertNotEquals(addr1.getHostString(), addr2.getHostString());
84         assertNotEquals(addr1.getPort(), addr2.getPort());
85     }
86 }