Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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.k8snode.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 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static junit.framework.TestCase.assertEquals; |
| 27 | import static org.onosproject.k8snode.api.K8sHostState.COMPLETE; |
| 28 | import static org.onosproject.k8snode.api.K8sHostState.INIT; |
| 29 | |
| 30 | /** |
| 31 | * Unit test for DefaultK8sHost. |
| 32 | */ |
| 33 | public final class DefaultK8sHostTest { |
| 34 | |
| 35 | private static final IpAddress HOST_IP_1 = IpAddress.valueOf("192.168.200.3"); |
| 36 | private static final IpAddress HOST_IP_2 = IpAddress.valueOf("192.168.200.4"); |
| 37 | |
| 38 | private static final Set<String> NODE_NAMES_1 = ImmutableSet.of("1", "2"); |
| 39 | private static final Set<String> NODE_NAMES_2 = ImmutableSet.of("3", "4"); |
| 40 | |
| 41 | private K8sHost refHost; |
| 42 | |
| 43 | private static final K8sHost K8S_HOST_1 = createHost( |
| 44 | HOST_IP_1, |
| 45 | NODE_NAMES_1, |
| 46 | INIT |
| 47 | ); |
| 48 | |
| 49 | private static final K8sHost K8S_HOST_2 = createHost( |
| 50 | HOST_IP_1, |
| 51 | NODE_NAMES_1, |
| 52 | INIT |
| 53 | ); |
| 54 | |
| 55 | private static final K8sHost K8S_HOST_3 = createHost( |
| 56 | HOST_IP_2, |
| 57 | NODE_NAMES_2, |
| 58 | COMPLETE |
| 59 | ); |
| 60 | |
| 61 | /** |
| 62 | * Initial setup for this unit test. |
| 63 | */ |
| 64 | @Before |
| 65 | public void setUp() { |
| 66 | refHost = DefaultK8sHost.builder() |
| 67 | .hostIp(HOST_IP_1) |
| 68 | .nodeNames(NODE_NAMES_1) |
| 69 | .state(INIT) |
| 70 | .build(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Checks equals method works as expected. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testEquality() { |
| 78 | new EqualsTester().addEqualityGroup(K8S_HOST_1, K8S_HOST_2) |
| 79 | .addEqualityGroup(K8S_HOST_3) |
| 80 | .testEquals(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Test object construction. |
| 85 | */ |
| 86 | @Test |
| 87 | public void testConstruction() { |
| 88 | assertEquals(refHost.hostIp(), HOST_IP_1); |
| 89 | assertEquals(refHost.nodeNames(), NODE_NAMES_1); |
| 90 | assertEquals(refHost.state(), INIT); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Checks the functionality of update state method. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testUpdateState() { |
| 98 | K8sHost updatedHost = refHost.updateState(COMPLETE); |
| 99 | |
| 100 | assertEquals(updatedHost.hostIp(), HOST_IP_1); |
| 101 | assertEquals(updatedHost.nodeNames(), NODE_NAMES_1); |
| 102 | assertEquals(updatedHost.state(), COMPLETE); |
| 103 | } |
| 104 | |
| 105 | private static K8sHost createHost(IpAddress hostIp, Set<String> nodeNames, K8sHostState state) { |
| 106 | return DefaultK8sHost.builder() |
| 107 | .hostIp(hostIp) |
| 108 | .nodeNames(nodeNames) |
| 109 | .state(state) |
| 110 | .build(); |
| 111 | } |
| 112 | } |