Bump upstream versions
[netconf.git] / netconf / callhome-provider / src / test / java / org / opendaylight / netconf / callhome / mount / ContextKeyTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.callhome.mount;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.net.InetAddresses;
18 import java.net.Inet4Address;
19 import java.net.Inet6Address;
20 import java.net.InetSocketAddress;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.netconf.client.NetconfClientSession;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
29 import org.opendaylight.yangtools.yang.common.Uint16;
30
31 public class ContextKeyTest {
32     private IpAddress address1;
33     private IpAddress address2;
34
35     private PortNumber port1;
36     private PortNumber port2;
37
38     private NetconfNode mockNode;
39     private NetconfClientSession mockSession;
40
41     private ContextKey instance1;
42     private ContextKey instance2;
43     private ContextKey instance3;
44     private ContextKey instance4;
45
46     @Before
47     public void setup() {
48         address1 = new IpAddress(new Ipv4Address("1.2.3.4"));
49         address2 = new IpAddress(new Ipv4Address("5.6.7.8"));
50
51         port1 = new PortNumber(Uint16.valueOf(123));
52         port2 = new PortNumber(Uint16.valueOf(456));
53
54         mockNode = mock(NetconfNode.class);
55         mockSession = mock(NetconfClientSession.class);
56
57         instance1 = new ContextKey(address1, port1);
58         instance2 = new ContextKey(address2, port2);
59         instance3 = new ContextKey(address1, port2);
60         instance4 = new ContextKey(address2, port1);
61
62         Host mockHost = mock(Host.class);
63         when(mockHost.getIpAddress()).thenReturn(address1);
64         when(mockNode.getHost()).thenReturn(mockHost);
65
66         when(mockNode.getPort()).thenReturn(port1);
67     }
68
69     @Test
70     public void hashCodesForDifferentKeysAreDifferent() {
71         // expect
72         assertNotEquals(instance1.hashCode(), instance2.hashCode());
73         assertNotEquals(instance1.hashCode(), 0);
74         assertNotEquals(instance2.hashCode(), 0);
75     }
76
77     @Test
78     public void variousFlavorsOfEqualWork() {
79         // expect
80         assertTrue(instance1.equals(instance1));
81         assertFalse(instance1.equals(null));
82         assertFalse(instance1.equals(123456L));
83         assertFalse(instance1.equals(instance2));
84         assertFalse(instance1.equals(instance3));
85         assertFalse(instance1.equals(instance4));
86     }
87
88     @Test
89     public void newContextCanBeCreatedFromASocketAddress() {
90         // given
91         Inet4Address someAddressIpv4 = (Inet4Address) InetAddresses.forString("1.2.3.4");
92         Inet6Address someAddressIpv6 = (Inet6Address) InetAddresses.forString("::1");
93         // and
94         ContextKey key1 = ContextKey.from(new InetSocketAddress(someAddressIpv4, 123));
95         ContextKey key2 = ContextKey.from(new InetSocketAddress(someAddressIpv6, 123));
96         // expect
97         assertNotNull(key1);
98         assertNotNull(key1.toString());
99         assertNotNull(key2);
100         assertNotNull(key2.toString());
101     }
102
103     @Test
104     public void newContextCanBeCreatedFromANetconfNode() {
105         // expect
106         assertNotNull(ContextKey.from(mockNode));
107     }
108 }