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