Skip to content

Commit 90443cd

Browse files
authored
Remove powermock from xenserver hypervisor plugin (#7806)
1 parent e3bd2c0 commit 90443cd

10 files changed

Lines changed: 133 additions & 229 deletions

File tree

plugins/hypervisors/xenserver/src/test/java/com/cloud/hypervisor/xenserver/resource/CitrixResourceBaseTest.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,17 @@
2424
import java.util.Set;
2525

2626
import org.apache.xmlrpc.XmlRpcException;
27+
import org.junit.After;
2728
import org.junit.Assert;
2829
import org.junit.Before;
2930
import org.junit.Test;
3031
import org.junit.runner.RunWith;
3132
import org.mockito.BDDMockito;
3233
import org.mockito.InOrder;
3334
import org.mockito.Mock;
35+
import org.mockito.MockedStatic;
3436
import org.mockito.Mockito;
3537
import org.mockito.Spy;
36-
import org.powermock.api.mockito.PowerMockito;
37-
import org.powermock.core.classloader.annotations.PrepareForTest;
38-
import org.powermock.modules.junit4.PowerMockRunner;
3938

4039
import com.cloud.agent.api.StartupStorageCommand;
4140
import com.cloud.agent.api.StoragePoolInfo;
@@ -52,13 +51,13 @@
5251
import com.xensource.xenapi.PBD;
5352
import com.xensource.xenapi.SR;
5453
import com.xensource.xenapi.Types.XenAPIException;
54+
import org.mockito.junit.MockitoJUnitRunner;
5555

5656
import static com.cloud.hypervisor.xenserver.resource.CitrixResourceBase.PLATFORM_CORES_PER_SOCKET_KEY;
5757
import static org.junit.Assert.assertEquals;
5858
import static org.mockito.Mockito.doReturn;
5959

60-
@RunWith(PowerMockRunner.class)
61-
@PrepareForTest({Host.class, Script.class, SR.class})
60+
@RunWith(MockitoJUnitRunner.class)
6261
public class CitrixResourceBaseTest {
6362

6463
@Spy
@@ -87,12 +86,14 @@ protected String getPatchFilePath() {
8786
final static String publicIp = "10.10.10.10";
8887
final static Integer port = 8080;
8988

89+
MockedStatic<Host> hostMocked;
90+
9091
@Before
9192
public void beforeTest() throws XenAPIException, XmlRpcException {
9293
citrixResourceBase._host.setUuid(hostUuidMock);
9394

94-
PowerMockito.mockStatic(Host.class);
95-
PowerMockito.when(Host.getByUuid(connectionMock, hostUuidMock)).thenReturn(hostMock);
95+
hostMocked = Mockito.mockStatic(Host.class);
96+
hostMocked.when(() -> Host.getByUuid(connectionMock, hostUuidMock)).thenReturn(hostMock);
9697

9798
hostRecordMock.softwareVersion = new HashMap<>();
9899
Mockito.when(hostMock.getRecord(connectionMock)).thenReturn(hostRecordMock);
@@ -102,25 +103,31 @@ public void beforeTest() throws XenAPIException, XmlRpcException {
102103
public void testGetPathFilesException() {
103104
String patch = citrixResourceBase.getPatchFilePath();
104105

105-
PowerMockito.mockStatic(Script.class);
106-
Mockito.when(Script.findScript("", patch)).thenReturn(null);
107-
108-
citrixResourceBase.getPatchFiles();
106+
try (MockedStatic<Script> ignored = Mockito.mockStatic(Script.class)) {
107+
Mockito.when(Script.findScript("", patch)).thenReturn(null);
109108

109+
citrixResourceBase.getPatchFiles();
110+
}
110111
}
111112

112113
public void testGetPathFilesListReturned() {
113114
String patch = citrixResourceBase.getPatchFilePath();
114115

115-
PowerMockito.mockStatic(Script.class);
116-
Mockito.when(Script.findScript("", patch)).thenReturn(patch);
116+
try (MockedStatic<Script> ignored = Mockito.mockStatic(Script.class)) {
117+
Mockito.when(Script.findScript("", patch)).thenReturn(patch);
117118

118-
File expected = new File(patch);
119-
String pathExpected = expected.getAbsolutePath();
119+
File expected = new File(patch);
120+
String pathExpected = expected.getAbsolutePath();
121+
122+
List<File> files = citrixResourceBase.getPatchFiles();
123+
String receivedPath = files.get(0).getAbsolutePath();
124+
Assert.assertEquals(receivedPath, pathExpected);
125+
}
126+
}
120127

121-
List<File> files = citrixResourceBase.getPatchFiles();
122-
String receivedPath = files.get(0).getAbsolutePath();
123-
Assert.assertEquals(receivedPath, pathExpected);
128+
@After
129+
public void tearDown() throws Exception {
130+
hostMocked.close();
124131
}
125132

126133
@Test
@@ -233,14 +240,14 @@ public void getAllLocalSrForTypeTest() throws Exception {
233240
Map<SR, SR.Record> mapOfSrsRecords = new HashMap<>();
234241
mapOfSrsRecords.put(srExtShared, srExtSharedRecord);
235242
mapOfSrsRecords.put(srExtNonShared, srExtNonSharedRecord);
243+
try (MockedStatic<SR> ignored = Mockito.mockStatic(SR.class)) {
244+
BDDMockito.given(SR.getAllRecords(connectionMock)).willReturn(mapOfSrsRecords);
236245

237-
PowerMockito.mockStatic(SR.class);
238-
BDDMockito.given(SR.getAllRecords(connectionMock)).willReturn(mapOfSrsRecords);
239-
240-
List<SR> allLocalSrForType = citrixResourceBase.getAllLocalSrForType(connectionMock, SRType.EXT);
246+
List<SR> allLocalSrForType = citrixResourceBase.getAllLocalSrForType(connectionMock, SRType.EXT);
241247

242-
Assert.assertEquals(expectedListOfSrs.size(), allLocalSrForType.size());
243-
Assert.assertEquals(expectedListOfSrs.get(0), allLocalSrForType.get(0));
248+
Assert.assertEquals(expectedListOfSrs.size(), allLocalSrForType.size());
249+
Assert.assertEquals(expectedListOfSrs.get(0), allLocalSrForType.get(0));
250+
}
244251
}
245252

246253
@Test
@@ -287,9 +294,7 @@ public void createStoragePoolInfoTest() throws XenAPIException, XmlRpcException
287294
String hostUuid = "hostUuid";
288295
citrixResourceBase._host.setUuid(hostUuid);
289296

290-
PowerMockito.mockStatic(Host.class);
291-
PowerMockito.when(Host.getByUuid(connectionMock, hostUuid)).thenReturn(hostMock);
292-
297+
Mockito.when(Host.getByUuid(connectionMock, hostUuid)).thenReturn(hostMock);
293298
String srType = "ext";
294299
String srUuid = "srUuid";
295300
long srPhysicalSize = 100l;
@@ -432,7 +437,6 @@ public void testGetStatsForNetworkStats() {
432437
CitrixResourceBase citrixResourceBaseSpy = Mockito.spy(CitrixResourceBase.class);
433438
Connection connection = Mockito.mock(Connection.class);
434439

435-
String args = "-g -l " + publicIp;
436440
doReturn(networkStats[0] + ":" + networkStats[1]).when(citrixResourceBaseSpy).networkUsage(Mockito.any(Connection.class),
437441
Mockito.eq(privateIp), Mockito.eq("get"), Mockito.any(), Mockito.eq(publicIp));
438442

0 commit comments

Comments
 (0)