samueljcc
Committed by Gerrit Code Review

[ONOS-3249] Add the junit test code of DefaultVirtualPort

Change-Id: Ib66c987979e51c05f37d62cc4ca98c294331eda6
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.vtnrsc.virtualport;
18 +
19 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
20 +
21 +import java.util.Map;
22 +import java.util.Set;
23 +
24 +import org.junit.Test;
25 +import org.onlab.packet.IpAddress;
26 +import org.onlab.packet.MacAddress;
27 +import org.onosproject.net.DeviceId;
28 +import org.onosproject.vtnrsc.AllowedAddressPair;
29 +import org.onosproject.vtnrsc.BindingHostId;
30 +import org.onosproject.vtnrsc.DefaultVirtualPort;
31 +import org.onosproject.vtnrsc.FixedIp;
32 +import org.onosproject.vtnrsc.SecurityGroup;
33 +import org.onosproject.vtnrsc.SubnetId;
34 +import org.onosproject.vtnrsc.TenantId;
35 +import org.onosproject.vtnrsc.TenantNetworkId;
36 +import org.onosproject.vtnrsc.VirtualPort;
37 +import org.onosproject.vtnrsc.VirtualPortId;
38 +
39 +import com.google.common.collect.Maps;
40 +import com.google.common.collect.Sets;
41 +import com.google.common.testing.EqualsTester;
42 +
43 +/**
44 + * Unit tests for DefaultVirtualPort class.
45 + */
46 +public class DefaultVirtualPortTest {
47 +
48 + private Set<FixedIp> fixedIps;
49 + private Map<String, String> propertyMap;
50 + private Set<AllowedAddressPair> allowedAddressPairs;
51 + private Set<SecurityGroup> securityGroups;
52 + private VirtualPortId id1;
53 + private VirtualPortId id2;
54 + private String macAddressStr = "fa:12:3e:56:ee:a2";
55 + private String ipAddress = "10.1.1.1";
56 + private String deviceStr = "of:000000000000001";
57 + private String tenantIdStr = "123";
58 + private String portId1 = "1241";
59 + private String portId2 = "1242";
60 + private String tenantNetworkId = "1234567";
61 + private String subnet = "1212";
62 + private String hostIdStr = "fa:e2:3e:56:ee:a2";
63 +
64 + private void initVirtualPortId() {
65 + id1 = VirtualPortId.portId(portId1);
66 + id2 = VirtualPortId.portId(portId2);
67 + }
68 +
69 + private void initFixedIpSet() {
70 + FixedIp fixedIp = FixedIp.fixedIp(SubnetId.subnetId(subnet),
71 + IpAddress.valueOf(ipAddress));
72 + fixedIps = Sets.newHashSet();
73 + fixedIps.add(fixedIp);
74 + }
75 +
76 + private void initPropertyMap() {
77 + String deviceOwner = "james";
78 + propertyMap = Maps.newHashMap();
79 + propertyMap.putIfAbsent("deviceOwner", deviceOwner);
80 + }
81 +
82 + private void initAddressPairSet() {
83 + allowedAddressPairs = Sets.newHashSet();
84 + AllowedAddressPair allowedAddressPair = AllowedAddressPair
85 + .allowedAddressPair(IpAddress.valueOf(ipAddress),
86 + MacAddress.valueOf(macAddressStr));
87 + allowedAddressPairs.add(allowedAddressPair);
88 + }
89 +
90 + private void initSecurityGroupSet() {
91 + securityGroups = Sets.newHashSet();
92 + }
93 +
94 + /**
95 + * Checks that the DefaultVirtualPort class is immutable.
96 + */
97 + @Test
98 + public void testImmutability() {
99 + assertThatClassIsImmutable(SecurityGroup.class);
100 + }
101 +
102 + /**
103 + * Checks the operation of equals().
104 + */
105 + @Test
106 + public void testEquals() {
107 + initVirtualPortId();
108 + initFixedIpSet();
109 + initPropertyMap();
110 + initAddressPairSet();
111 + initSecurityGroupSet();
112 + TenantNetworkId networkId = TenantNetworkId.networkId(tenantNetworkId);
113 + MacAddress macAddress = MacAddress.valueOf(macAddressStr);
114 + TenantId tenantId = TenantId.tenantId(tenantIdStr);
115 + DeviceId deviceId = DeviceId.deviceId(deviceStr);
116 + BindingHostId bindingHostId = BindingHostId.bindingHostId(hostIdStr);
117 +
118 + VirtualPort d1 = new DefaultVirtualPort(id1, networkId, true,
119 + propertyMap,
120 + VirtualPort.State.ACTIVE,
121 + macAddress, tenantId, deviceId,
122 + fixedIps, bindingHostId,
123 + allowedAddressPairs,
124 + securityGroups);
125 + VirtualPort d2 = new DefaultVirtualPort(id1, networkId, true,
126 + propertyMap,
127 + VirtualPort.State.ACTIVE,
128 + macAddress, tenantId, deviceId,
129 + fixedIps, bindingHostId,
130 + allowedAddressPairs,
131 + securityGroups);
132 + VirtualPort d3 = new DefaultVirtualPort(id2, networkId, true,
133 + propertyMap,
134 + VirtualPort.State.ACTIVE,
135 + macAddress, tenantId, deviceId,
136 + fixedIps, bindingHostId,
137 + allowedAddressPairs,
138 + securityGroups);
139 + new EqualsTester().addEqualityGroup(d1, d2).addEqualityGroup(d3)
140 + .testEquals();
141 + }
142 +}