2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
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
9 package org.opendaylight.netconf.callhome.mount;
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;
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;
31 public class ContextKeyTest {
32 private IpAddress address1;
33 private IpAddress address2;
35 private PortNumber port1;
36 private PortNumber port2;
38 private NetconfNode mockNode;
39 private NetconfClientSession mockSession;
41 private ContextKey instance1;
42 private ContextKey instance2;
43 private ContextKey instance3;
44 private ContextKey instance4;
48 address1 = IpAddressBuilder.getDefaultInstance("1.2.3.4");
49 address2 = IpAddressBuilder.getDefaultInstance("5.6.7.8");
51 port1 = new PortNumber(123);
52 port2 = new PortNumber(456);
54 mockNode = mock(NetconfNode.class);
55 mockSession = mock(NetconfClientSession.class);
57 instance1 = new ContextKey(address1, port1);
58 instance2 = new ContextKey(address2, port2);
59 instance3 = new ContextKey(address1, port2);
60 instance4 = new ContextKey(address2, port1);
62 Host mockHost = mock(Host.class);
63 when(mockHost.getIpAddress()).thenReturn(address1);
64 when(mockNode.getHost()).thenReturn(mockHost);
66 when(mockNode.getPort()).thenReturn(port1);
70 public void hashCodesForDifferentKeysAreDifferent() {
72 assertNotEquals(instance1.hashCode(), instance2.hashCode());
73 assertNotEquals(instance1.hashCode(), 0);
74 assertNotEquals(instance2.hashCode(), 0);
78 public void variousFlavorsOfEqualWork() {
80 assertTrue(instance1.equals(instance1));
81 assertFalse(instance1.equals(null));
82 assertFalse(instance1.equals(new Long(123456)));
83 assertFalse(instance1.equals(instance2));
84 assertFalse(instance1.equals(instance3));
85 assertFalse(instance1.equals(instance4));
89 public void newContextCanBeCreatedFromASocketAddress() {
91 Inet4Address someAddressIpv4 = (Inet4Address) InetAddresses.forString("1.2.3.4");
92 Inet6Address someAddressIpv6 = (Inet6Address) InetAddresses.forString("::1");
94 ContextKey key1 = ContextKey.from(new InetSocketAddress(someAddressIpv4, 123));
95 ContextKey key2 = ContextKey.from(new InetSocketAddress(someAddressIpv6, 123));
98 assertNotNull(key1.toString());
100 assertNotNull(key2.toString());
104 public void newContextCanBeCreatedFromANetconfNode() {
106 assertNotNull(ContextKey.from(mockNode));