Jian Li | b964283 | 2021-01-08 03:18:35 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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 | package org.onosproject.kubevirtnetworking.api; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | |
| 27 | import static junit.framework.TestCase.assertEquals; |
| 28 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 29 | |
| 30 | /** |
| 31 | * Unit tests for the default kubevirt instance class. |
| 32 | */ |
| 33 | public class DefaultKubevirtInstanceTest { |
| 34 | |
| 35 | private static final String UID_1 = "1"; |
| 36 | private static final String UID_2 = "2"; |
| 37 | private static final String NAME_1 = "instance-1"; |
| 38 | private static final String NAME_2 = "instance-2"; |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 39 | private static final String NETWORK_ID_1 = "net-1"; |
| 40 | private static final String NETWORK_ID_2 = "net-2"; |
Jian Li | b964283 | 2021-01-08 03:18:35 +0900 | [diff] [blame] | 41 | private static final MacAddress MAC_1 = MacAddress.valueOf("11:22:33:44:55:66"); |
| 42 | private static final MacAddress MAC_2 = MacAddress.valueOf("66:55:44:33:22:11"); |
| 43 | private static final IpAddress IP_1 = IpAddress.valueOf("10.10.10.10"); |
| 44 | private static final IpAddress IP_2 = IpAddress.valueOf("20.20.20.20"); |
| 45 | private static final DeviceId DID_1 = DeviceId.deviceId("did1"); |
| 46 | private static final DeviceId DID_2 = DeviceId.deviceId("did2"); |
| 47 | private static final PortNumber PN_1 = PortNumber.portNumber(1); |
| 48 | private static final PortNumber PN_2 = PortNumber.portNumber(2); |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 49 | private static final KubevirtPort PORT_1 = createPort(NETWORK_ID_1, MAC_1, IP_1, DID_1, PN_1); |
| 50 | private static final KubevirtPort PORT_2 = createPort(NETWORK_ID_2, MAC_2, IP_2, DID_2, PN_2); |
Jian Li | b964283 | 2021-01-08 03:18:35 +0900 | [diff] [blame] | 51 | |
| 52 | private KubevirtInstance instance1; |
| 53 | private KubevirtInstance sameAsInstance1; |
| 54 | private KubevirtInstance instance2; |
| 55 | |
| 56 | /** |
| 57 | * Tests class immutability. |
| 58 | */ |
| 59 | @Test |
| 60 | public void testImmutability() { |
| 61 | assertThatClassIsImmutable(DefaultKubevirtInstance.class); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Initial setup for this unit test. |
| 66 | */ |
| 67 | @Before |
| 68 | public void setUp() { |
| 69 | instance1 = DefaultKubevirtInstance.builder() |
| 70 | .uid(UID_1) |
| 71 | .name(NAME_1) |
| 72 | .ports(ImmutableSet.of(PORT_1)) |
| 73 | .build(); |
| 74 | |
| 75 | sameAsInstance1 = DefaultKubevirtInstance.builder() |
| 76 | .uid(UID_1) |
| 77 | .name(NAME_1) |
| 78 | .ports(ImmutableSet.of(PORT_1)) |
| 79 | .build(); |
| 80 | |
| 81 | instance2 = DefaultKubevirtInstance.builder() |
| 82 | .uid(UID_2) |
| 83 | .name(NAME_2) |
| 84 | .ports(ImmutableSet.of(PORT_2)) |
| 85 | .build(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Tests object equality. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testEquality() { |
| 93 | new EqualsTester().addEqualityGroup(instance1, sameAsInstance1) |
| 94 | .addEqualityGroup(instance2) |
| 95 | .testEquals(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Test object construction. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testConstruction() { |
| 103 | KubevirtInstance instance = instance1; |
| 104 | |
| 105 | assertEquals(UID_1, instance1.uid()); |
| 106 | assertEquals(NAME_1, instance1.name()); |
| 107 | assertEquals(ImmutableSet.of(PORT_1), instance1.ports()); |
| 108 | } |
| 109 | |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 110 | static KubevirtPort createPort(String networkId, MacAddress mac, IpAddress ip, |
| 111 | DeviceId did, PortNumber pn) { |
Jian Li | b964283 | 2021-01-08 03:18:35 +0900 | [diff] [blame] | 112 | return DefaultKubevirtPort.builder() |
Jian Li | 3ba5c58 | 2021-01-14 11:30:36 +0900 | [diff] [blame] | 113 | .networkId(networkId) |
Jian Li | b964283 | 2021-01-08 03:18:35 +0900 | [diff] [blame] | 114 | .macAddress(mac) |
| 115 | .ipAddress(ip) |
| 116 | .deviceId(did) |
| 117 | .portNumber(pn) |
| 118 | .build(); |
| 119 | } |
| 120 | } |