Skip to content

Commit 38f0286

Browse files
authored
Fix getRepair method in checkVolume command (#8840)
* Fix getRepair method in checkVolume command * Add License
1 parent f7603dc commit 38f0286

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/command/user/volume/CheckAndRepairVolumeCmd.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.cloudstack.api.ServerApiException;
3030
import org.apache.cloudstack.api.response.VolumeResponse;
3131
import org.apache.cloudstack.context.CallContext;
32+
import org.apache.commons.lang3.EnumUtils;
3233
import org.apache.log4j.Logger;
3334

3435
import com.cloud.exception.ResourceAllocationException;
@@ -71,9 +72,9 @@ public Long getId() {
7172

7273
public String getRepair() {
7374
if (org.apache.commons.lang3.StringUtils.isNotEmpty(repair)) {
74-
RepairValues repairType = Enum.valueOf(RepairValues.class, repair.toUpperCase());
75+
RepairValues repairType = EnumUtils.getEnumIgnoreCase(RepairValues.class, repair);
7576
if (repairType == null) {
76-
throw new InvalidParameterValueException(String.format("Repair parameter can only take the following values: %s" + Arrays.toString(RepairValues.values())));
77+
throw new InvalidParameterValueException(String.format("Repair parameter can only take the following values: %s", Arrays.toString(RepairValues.values())));
7778
}
7879
return repair.toLowerCase();
7980
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
18+
package org.apache.cloudstack.api.command.user.volume;
19+
20+
import com.cloud.exception.InvalidParameterValueException;
21+
import junit.framework.TestCase;
22+
import org.junit.After;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.MockitoAnnotations;
27+
import org.mockito.junit.MockitoJUnitRunner;
28+
import org.springframework.test.util.ReflectionTestUtils;
29+
30+
@RunWith(MockitoJUnitRunner.class)
31+
public class CheckAndRepairVolumeCmdTest extends TestCase {
32+
private CheckAndRepairVolumeCmd checkAndRepairVolumeCmd;
33+
private AutoCloseable closeable;
34+
35+
@Before
36+
public void setup() {
37+
closeable = MockitoAnnotations.openMocks(this);
38+
checkAndRepairVolumeCmd = new CheckAndRepairVolumeCmd();
39+
}
40+
41+
@After
42+
public void tearDown() throws Exception {
43+
closeable.close();
44+
}
45+
46+
@Test
47+
public void testGetRepair() {
48+
ReflectionTestUtils.setField(checkAndRepairVolumeCmd, "repair", "all");
49+
assertEquals("all", checkAndRepairVolumeCmd.getRepair());
50+
51+
ReflectionTestUtils.setField(checkAndRepairVolumeCmd, "repair", "LEAKS");
52+
assertEquals("leaks", checkAndRepairVolumeCmd.getRepair());
53+
54+
ReflectionTestUtils.setField(checkAndRepairVolumeCmd, "repair", null);
55+
assertNull(checkAndRepairVolumeCmd.getRepair());
56+
}
57+
58+
@Test(expected = InvalidParameterValueException.class)
59+
public void testGetRepairInvalid() {
60+
ReflectionTestUtils.setField(checkAndRepairVolumeCmd, "repair", "RANDOM STRING");
61+
checkAndRepairVolumeCmd.getRepair();
62+
}
63+
}

0 commit comments

Comments
 (0)