Skip to content

Commit 5d696f0

Browse files
committed
cpu test
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent ce43205 commit 5d696f0

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

api/src/main/java/com/cloud/cpu/CPU.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package com.cloud.cpu;
1818

19+
import org.apache.commons.lang3.StringUtils;
20+
1921
public class CPU {
2022
public enum CPUArch {
2123
x86("i686", 32),
@@ -38,16 +40,16 @@ public int getBits() {
3840
return bits;
3941
}
4042

41-
public static CPUArch fromType(String identifier) {
42-
if (identifier == null || identifier.trim().isEmpty()) {
43+
public static CPUArch fromType(String type) {
44+
if (StringUtils.isBlank(type)) {
4345
return amd64; // Default architecture
4446
}
4547
for (CPUArch arch : values()) {
46-
if (arch.type.equals(identifier)) {
48+
if (arch.type.equals(type)) {
4749
return arch;
4850
}
4951
}
50-
throw new IllegalArgumentException("Unsupported arch type: " + identifier);
52+
throw new IllegalArgumentException("Unsupported arch type: " + type);
5153
}
5254

5355
public static String getTypesAsCSV() {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.cpu;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertThrows;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import org.junit.Test;
24+
25+
public class CPUTest {
26+
@Test
27+
public void testCPUArchGetType() {
28+
assertEquals("i686", CPU.CPUArch.x86.getType());
29+
assertEquals("x86_64", CPU.CPUArch.amd64.getType());
30+
assertEquals("aarch64", CPU.CPUArch.arm64.getType());
31+
}
32+
33+
@Test
34+
public void testCPUArchGetBits() {
35+
assertEquals(32, CPU.CPUArch.x86.getBits());
36+
assertEquals(64, CPU.CPUArch.amd64.getBits());
37+
assertEquals(64, CPU.CPUArch.arm64.getBits());
38+
}
39+
40+
@Test
41+
public void testCPUArchFromTypeWithValidValues() {
42+
assertEquals(CPU.CPUArch.x86, CPU.CPUArch.fromType("i686"));
43+
assertEquals(CPU.CPUArch.amd64, CPU.CPUArch.fromType("x86_64"));
44+
assertEquals(CPU.CPUArch.arm64, CPU.CPUArch.fromType("aarch64"));
45+
}
46+
47+
@Test
48+
public void testCPUArchFromTypeWithDefaultForBlankOrNull() {
49+
assertEquals(CPU.CPUArch.amd64, CPU.CPUArch.fromType(""));
50+
assertEquals(CPU.CPUArch.amd64, CPU.CPUArch.fromType(" "));
51+
assertEquals(CPU.CPUArch.amd64, CPU.CPUArch.fromType(null));
52+
}
53+
54+
@Test
55+
public void testCPUArchFromTypeWithInvalidValue() {
56+
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
57+
CPU.CPUArch.fromType("unsupported");
58+
});
59+
assertTrue(exception.getMessage().contains("Unsupported arch type: unsupported"));
60+
}
61+
62+
@Test
63+
public void testCPUArchGetTypesAsCSV() {
64+
String expectedCSV = "i686,x86_64,aarch64";
65+
assertEquals(expectedCSV, CPU.CPUArch.getTypesAsCSV());
66+
}
67+
}

0 commit comments

Comments
 (0)