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;
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;
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;
33 public class ContextKeyTest {
34 private IpAddress address1;
35 private IpAddress address2;
37 private PortNumber port1;
38 private PortNumber port2;
40 private NetconfNode mockNode;
41 private NetconfClientSession mockSession;
43 private ContextKey instance1;
44 private ContextKey instance2;
45 private ContextKey instance3;
46 private ContextKey instance4;
50 address1 = IpAddressBuilder.getDefaultInstance("1.2.3.4");
51 address2 = IpAddressBuilder.getDefaultInstance("5.6.7.8");
53 port1 = new PortNumber(123);
54 port2 = new PortNumber(456);
56 mockNode = mock(NetconfNode.class);
57 mockSession = mock(NetconfClientSession.class);
59 instance1 = new ContextKey(address1, port1);
60 instance2 = new ContextKey(address2, port2);
61 instance3 = new ContextKey(address1, port2);
62 instance4 = new ContextKey(address2, port1);
64 Host mockHost = mock(Host.class);
65 when(mockHost.getIpAddress()).thenReturn(address1);
66 when(mockNode.getHost()).thenReturn(mockHost);
68 when(mockNode.getPort()).thenReturn(port1);
72 public void hashCodesForDifferentKeysAreDifferent() {
74 assertNotEquals(instance1.hashCode(), instance2.hashCode());
75 assertNotEquals(instance1.hashCode(), 0);
76 assertNotEquals(instance2.hashCode(), 0);
80 public void variousFlavorsOfEqualWork() {
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));
91 public void newContextCanBeCreatedFromASocketAddress() {
93 Inet4Address someAddressIpv4 = (Inet4Address) InetAddresses.forString("1.2.3.4");
94 Inet6Address someAddressIpv6 = (Inet6Address) InetAddresses.forString("::1");
96 ContextKey key1 = ContextKey.from(new InetSocketAddress(someAddressIpv4, 123));
97 ContextKey key2 = ContextKey.from(new InetSocketAddress(someAddressIpv6, 123));
100 assertNotNull(key1.toString());
102 assertNotNull(key2.toString());
106 public void newContextCanBeCreatedFromANetconfNode() {
108 assertNotNull(ContextKey.from(mockNode));