Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@
import java.util.Set;

import org.apache.xmlrpc.XmlRpcException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.cloud.agent.api.StartupStorageCommand;
import com.cloud.agent.api.StoragePoolInfo;
Expand All @@ -52,13 +51,13 @@
import com.xensource.xenapi.PBD;
import com.xensource.xenapi.SR;
import com.xensource.xenapi.Types.XenAPIException;
import org.mockito.junit.MockitoJUnitRunner;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest({Host.class, Script.class, SR.class})
@RunWith(MockitoJUnitRunner.class)
public class CitrixResourceBaseTest {

@Spy
Expand Down Expand Up @@ -87,12 +86,14 @@ protected String getPatchFilePath() {
final static String publicIp = "10.10.10.10";
final static Integer port = 8080;

MockedStatic<Host> hostMocked;

@Before
public void beforeTest() throws XenAPIException, XmlRpcException {
citrixResourceBase._host.setUuid(hostUuidMock);

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

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

PowerMockito.mockStatic(Script.class);
Mockito.when(Script.findScript("", patch)).thenReturn(null);

citrixResourceBase.getPatchFiles();
try (MockedStatic<Script> ignored = Mockito.mockStatic(Script.class)) {
Mockito.when(Script.findScript("", patch)).thenReturn(null);

citrixResourceBase.getPatchFiles();
}
}

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

PowerMockito.mockStatic(Script.class);
Mockito.when(Script.findScript("", patch)).thenReturn(patch);
try (MockedStatic<Script> ignored = Mockito.mockStatic(Script.class)) {
Mockito.when(Script.findScript("", patch)).thenReturn(patch);

File expected = new File(patch);
String pathExpected = expected.getAbsolutePath();
File expected = new File(patch);
String pathExpected = expected.getAbsolutePath();

List<File> files = citrixResourceBase.getPatchFiles();
String receivedPath = files.get(0).getAbsolutePath();
Assert.assertEquals(receivedPath, pathExpected);
}
}

List<File> files = citrixResourceBase.getPatchFiles();
String receivedPath = files.get(0).getAbsolutePath();
Assert.assertEquals(receivedPath, pathExpected);
@After
public void tearDown() throws Exception {
hostMocked.close();
}

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

PowerMockito.mockStatic(SR.class);
BDDMockito.given(SR.getAllRecords(connectionMock)).willReturn(mapOfSrsRecords);

List<SR> allLocalSrForType = citrixResourceBase.getAllLocalSrForType(connectionMock, SRType.EXT);
List<SR> allLocalSrForType = citrixResourceBase.getAllLocalSrForType(connectionMock, SRType.EXT);

Assert.assertEquals(expectedListOfSrs.size(), allLocalSrForType.size());
Assert.assertEquals(expectedListOfSrs.get(0), allLocalSrForType.get(0));
Assert.assertEquals(expectedListOfSrs.size(), allLocalSrForType.size());
Assert.assertEquals(expectedListOfSrs.get(0), allLocalSrForType.get(0));
}
}

@Test
Expand Down Expand Up @@ -287,9 +294,7 @@ public void createStoragePoolInfoTest() throws XenAPIException, XmlRpcException
String hostUuid = "hostUuid";
citrixResourceBase._host.setUuid(hostUuid);

PowerMockito.mockStatic(Host.class);
PowerMockito.when(Host.getByUuid(connectionMock, hostUuid)).thenReturn(hostMock);

Mockito.when(Host.getByUuid(connectionMock, hostUuid)).thenReturn(hostMock);
String srType = "ext";
String srUuid = "srUuid";
long srPhysicalSize = 100l;
Expand Down Expand Up @@ -432,7 +437,6 @@ public void testGetStatsForNetworkStats() {
CitrixResourceBase citrixResourceBaseSpy = Mockito.spy(CitrixResourceBase.class);
Connection connection = Mockito.mock(Connection.class);

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

Expand Down
Loading