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