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