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;
30 import org.opendaylight.yangtools.yang.common.Uint16;
32 public class ContextKeyTest {
33 private IpAddress address1;
34 private IpAddress address2;
36 private PortNumber port1;
37 private PortNumber port2;
39 private NetconfNode mockNode;
40 private NetconfClientSession mockSession;
42 private ContextKey instance1;
43 private ContextKey instance2;
44 private ContextKey instance3;
45 private ContextKey instance4;
49 address1 = IpAddressBuilder.getDefaultInstance("1.2.3.4");
50 address2 = IpAddressBuilder.getDefaultInstance("5.6.7.8");
52 port1 = new PortNumber(Uint16.valueOf(123));
53 port2 = new PortNumber(Uint16.valueOf(456));
55 mockNode = mock(NetconfNode.class);
56 mockSession = mock(NetconfClientSession.class);
58 instance1 = new ContextKey(address1, port1);
59 instance2 = new ContextKey(address2, port2);
60 instance3 = new ContextKey(address1, port2);
61 instance4 = new ContextKey(address2, port1);
63 Host mockHost = mock(Host.class);
64 when(mockHost.getIpAddress()).thenReturn(address1);
65 when(mockNode.getHost()).thenReturn(mockHost);
67 when(mockNode.getPort()).thenReturn(port1);
71 public void hashCodesForDifferentKeysAreDifferent() {
73 assertNotEquals(instance1.hashCode(), instance2.hashCode());
74 assertNotEquals(instance1.hashCode(), 0);
75 assertNotEquals(instance2.hashCode(), 0);
79 public void variousFlavorsOfEqualWork() {
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));
90 public void newContextCanBeCreatedFromASocketAddress() {
92 Inet4Address someAddressIpv4 = (Inet4Address) InetAddresses.forString("1.2.3.4");
93 Inet6Address someAddressIpv6 = (Inet6Address) InetAddresses.forString("::1");
95 ContextKey key1 = ContextKey.from(new InetSocketAddress(someAddressIpv4, 123));
96 ContextKey key2 = ContextKey.from(new InetSocketAddress(someAddressIpv6, 123));
99 assertNotNull(key1.toString());
101 assertNotNull(key2.toString());
105 public void newContextCanBeCreatedFromANetconfNode() {
107 assertNotNull(ContextKey.from(mockNode));