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 @@ -16,29 +16,29 @@
// under the License.
package org.apache.cloudstack.api.command.test;

import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;

import java.util.Map;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;

import junit.framework.Assert;
import junit.framework.TestCase;
import java.util.Map;

import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.admin.storage.AddImageStoreCmd;
import org.apache.cloudstack.api.response.ImageStoreResponse;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.admin.storage.AddImageStoreCmd;
import org.apache.cloudstack.api.response.ImageStoreResponse;

import com.cloud.storage.ImageStore;
import com.cloud.storage.StorageService;

import junit.framework.TestCase;

public class AddSecondaryStorageCmdTest extends TestCase {

private AddImageStoreCmd addImageStoreCmd;
Expand All @@ -62,25 +62,22 @@ public void testExecuteForResult() throws Exception {

ImageStore store = Mockito.mock(ImageStore.class);

Mockito.when(resourceService.discoverImageStore(anyString(), anyString(), anyString(), anyLong(), (Map)anyObject()))
.thenReturn(store);

Mockito.when(resourceService.discoverImageStore(isNull(), isNull(), isNull(), isNull(), isNull())).thenReturn(store);
ResponseGenerator responseGenerator = Mockito.mock(ResponseGenerator.class);
addImageStoreCmd._responseGenerator = responseGenerator;

ImageStoreResponse responseHost = new ImageStoreResponse();
responseHost.setName("Test");

Mockito.when(responseGenerator.createImageStoreResponse(store)).thenReturn(responseHost);
Mockito.doReturn(responseHost).when(responseGenerator).createImageStoreResponse(store);

addImageStoreCmd.execute();

Mockito.verify(responseGenerator).createImageStoreResponse(store);

ImageStoreResponse actualResponse = (ImageStoreResponse)addImageStoreCmd.getResponseObject();

Assert.assertEquals(responseHost, actualResponse);
Assert.assertEquals("addimagestoreresponse", actualResponse.getResponseName());
assertEquals(responseHost, actualResponse);
assertEquals("addimagestoreresponse", actualResponse.getResponseName());

}

Expand All @@ -96,7 +93,7 @@ public void testExecuteForNullResult() throws Exception {
try {
addImageStoreCmd.execute();
} catch (ServerApiException exception) {
Assert.assertEquals("Failed to add secondary storage", exception.getDescription());
assertEquals("Failed to add secondary storage", exception.getDescription());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@
// under the License.
package org.apache.cloudstack.api.command.test;

import junit.framework.Assert;
import junit.framework.TestCase;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.nullable;

import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.user.vpn.AddVpnUserCmd;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Matchers;
import org.mockito.Mockito;

import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.user.vpn.AddVpnUserCmd;

import com.cloud.network.VpnUser;
import com.cloud.network.vpn.RemoteAccessVpnService;
import com.cloud.user.Account;
import com.cloud.user.AccountService;

import junit.framework.Assert;
import junit.framework.TestCase;
public class AddVpnUserCmdTest extends TestCase {

private AddVpnUserCmd addVpnUserCmd;
Expand Down Expand Up @@ -76,14 +77,15 @@ public void testCreateSuccess() {
AccountService accountService = Mockito.mock(AccountService.class);

Account account = Mockito.mock(Account.class);
Mockito.when(accountService.getAccount(Matchers.anyLong())).thenReturn(account);
Mockito.when(accountService.getAccount(nullable(Long.class))).thenReturn(account);

addVpnUserCmd._accountService = accountService;

RemoteAccessVpnService ravService = Mockito.mock(RemoteAccessVpnService.class);

VpnUser vpnUser = Mockito.mock(VpnUser.class);
Mockito.when(ravService.addVpnUser(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(vpnUser);

Mockito.when(ravService.addVpnUser(anyLong(), isNull(), isNull())).thenReturn(vpnUser);

addVpnUserCmd._ravService = ravService;

Expand All @@ -96,12 +98,13 @@ public void testCreateFailure() {

AccountService accountService = Mockito.mock(AccountService.class);
Account account = Mockito.mock(Account.class);
Mockito.when(accountService.getAccount(Matchers.anyLong())).thenReturn(account);
Mockito.when(accountService.getAccount(nullable(Long.class))).thenReturn(account);

addVpnUserCmd._accountService = accountService;

RemoteAccessVpnService ravService = Mockito.mock(RemoteAccessVpnService.class);
Mockito.when(ravService.addVpnUser(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(null);

Mockito.when(ravService.addVpnUser(anyLong(), isNull(), isNull())).thenReturn(null);

addVpnUserCmd._ravService = ravService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
// under the License.
package org.apache.cloudstack.api.command.test;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
Expand Down Expand Up @@ -92,8 +91,8 @@ public void testCreateSuccess() {
VolumeApiService volumeApiService = Mockito.mock(VolumeApiService.class);
Snapshot snapshot = Mockito.mock(Snapshot.class);
try {
Mockito.when(volumeApiService.takeSnapshot(anyLong(), anyLong(), anyLong(),
any(Account.class), anyBoolean(), isNull(Snapshot.LocationType.class), anyBoolean(), anyObject())).thenReturn(snapshot);
Mockito.when(volumeApiService.takeSnapshot(nullable(Long.class), nullable(Long.class), isNull(),
nullable(Account.class), nullable(Boolean.class), nullable(Snapshot.LocationType.class), nullable(Boolean.class), nullable(Map.class))).thenReturn(snapshot);

} catch (Exception e) {
Assert.fail("Received exception when success expected " + e.getMessage());
Expand Down Expand Up @@ -125,8 +124,8 @@ public void testCreateFailure() {
VolumeApiService volumeApiService = Mockito.mock(VolumeApiService.class);

try {
Mockito.when(volumeApiService.takeSnapshot(anyLong(), anyLong(), anyLong(),
any(Account.class), anyBoolean(), isNull(Snapshot.LocationType.class), anyBoolean(), anyObject())).thenReturn(null);
Mockito.when(volumeApiService.takeSnapshot(nullable(Long.class), nullable(Long.class), nullable(Long.class),
nullable(Account.class), nullable(Boolean.class), nullable(Snapshot.LocationType.class), nullable(Boolean.class), anyObject())).thenReturn(null);
} catch (Exception e) {
Assert.fail("Received exception when success expected " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@
// under the License.
package org.apache.cloudstack.api.command.test;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Matchers;
import org.mockito.Mockito;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;

import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.command.admin.region.AddRegionCmd;
import org.apache.cloudstack.api.response.RegionResponse;
import org.apache.cloudstack.region.Region;
import org.apache.cloudstack.region.RegionService;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;

import junit.framework.TestCase;

public class RegionCmdTest extends TestCase {

Expand Down Expand Up @@ -66,7 +67,8 @@ public void testCreateSuccess() {
RegionService regionService = Mockito.mock(RegionService.class);

Region region = Mockito.mock(Region.class);
Mockito.when(regionService.addRegion(Matchers.anyInt(), Matchers.anyString(), Matchers.anyString())).thenReturn(region);

Mockito.when(regionService.addRegion(anyInt(), anyString(), isNull())).thenReturn(region);

addRegionCmd._regionService = regionService;
responseGenerator = Mockito.mock(ResponseGenerator.class);
Expand All @@ -86,14 +88,15 @@ public void testCreateFailure() {
RegionService regionService = Mockito.mock(RegionService.class);

Region region = Mockito.mock(Region.class);
Mockito.when(regionService.addRegion(Matchers.anyInt(), Matchers.anyString(), Matchers.anyString())).thenReturn(null);

Mockito.when(regionService.addRegion(anyInt(), anyString(), isNull())).thenReturn(null);

addRegionCmd._regionService = regionService;

try {
addRegionCmd.execute();
} catch (ServerApiException exception) {
Assert.assertEquals("Failed to add Region", exception.getDescription());
assertEquals("Failed to add Region", exception.getDescription());
}

}
Expand Down
4 changes: 4 additions & 0 deletions engine/schema/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@
<artifactId>cloud-framework-db</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class DatabaseAccessObjectTest {

@Before
public void setup() {
Whitebox.setInternalState(dao, "s_logger", loggerMock);
Whitebox.setInternalState(dao.getClass(), "s_logger", loggerMock);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
// under the License.
package org.apache.cloudstack.storage.datastore.db;

import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -29,9 +32,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Spy;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;

import com.cloud.storage.ScopeType;
Expand All @@ -41,6 +44,7 @@
import junit.framework.TestCase;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
public class PrimaryDataStoreDaoImplTest extends TestCase {

@Mock
Expand Down Expand Up @@ -77,12 +81,13 @@ public class PrimaryDataStoreDaoImplTest extends TestCase {
private static final Long CLUSTER_ID = null;
private static final ScopeType SCOPE = ScopeType.ZONE;


@Before
public void setup() {
public void setup() throws IOException, ClassNotFoundException, SQLException {
STORAGE_POOL_DETAILS.put(DETAIL_KEY, DETAIL_VALUE);
doReturn(Arrays.asList(storagePoolVO)).when(primaryDataStoreDao).
searchStoragePoolsPreparedStatement(Matchers.anyString(), Matchers.anyLong(), Matchers.anyLong(), Matchers.anyLong(),
Matchers.any(ScopeType.class), Matchers.anyInt());
searchStoragePoolsPreparedStatement(nullable(String.class), nullable(Long.class), nullable(Long.class), nullable(Long.class),
nullable(ScopeType.class), nullable(Integer.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.apache.cloudstack.storage.configdrive;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.times;

import java.io.File;
Expand Down Expand Up @@ -130,8 +133,10 @@ public void buildConfigDriveTestNoVmData() {
public void buildConfigDriveTestIoException() throws Exception {
PowerMockito.mockStatic(ConfigDriveBuilder.class);

Method method1 = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("writeFile")).iterator().next();
Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("writeVendorAndNetworkEmptyJsonFile")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(Mockito.any(File.class)).thenThrow(IOException.class);

PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(File.class)).thenThrow(CloudRuntimeException.class);

//This is odd, but it was necessary to allow us to check if we catch the IOexception and re-throw as a CloudRuntimeException
//We are mocking the class being tested; therefore, we needed to force the execution of the real method we want to test.
Expand Down Expand Up @@ -228,15 +233,20 @@ public void writeVmMetadataTest() throws Exception {
PowerMockito.mockStatic(ConfigDriveBuilder.class);

Method method = getWriteVmMetadataMethod();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(new ArrayList<>(), "metadataFile", new File("folder")).thenCallRealMethod();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(Mockito.anyListOf(String[].class), anyString(), any(File.class)).thenCallRealMethod();

Method createJsonObjectWithVmDataMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("createJsonObjectWithVmData")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, createJsonObjectWithVmDataMethod).withArguments(Mockito.anyListOf(String[].class), Mockito.any(File.class)).thenReturn(new JsonObject());

ConfigDriveBuilder.writeVmMetadata(new ArrayList<>(), "metadataFile", new File("folder"));
PowerMockito.when(ConfigDriveBuilder.class, createJsonObjectWithVmDataMethod).withArguments(Mockito.anyListOf(String[].class), Mockito.anyString()).thenReturn(new JsonObject());

List<String[]> vmData = new ArrayList<>();
vmData.add(new String[] {"dataType", "fileName", "content"});
vmData.add(new String[] {"dataType2", "fileName2", "content2"});

ConfigDriveBuilder.writeVmMetadata(vmData, "metadataFile", new File("folder"));

PowerMockito.verifyStatic(ConfigDriveBuilder.class);
ConfigDriveBuilder.createJsonObjectWithVmData(Mockito.anyListOf(String[].class), Mockito.anyString());
ConfigDriveBuilder.createJsonObjectWithVmData(vmData, "metadataFile");
ConfigDriveBuilder.writeFile(Mockito.any(File.class), Mockito.eq("meta_data.json"), Mockito.eq("{}"));
}

Expand Down Expand Up @@ -306,7 +316,7 @@ public void generateAndRetrieveIsoAsBase64IsoTestGenIsoFailure() throws Exceptio
Mockito.doReturn("scriptMessage").when(scriptMock).execute();

Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(Mockito.any(File.class), Mockito.any(File.class), Mockito.any(File.class)).thenCallRealMethod();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();

Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
Expand All @@ -330,7 +340,7 @@ public void generateAndRetrieveIsoAsBase64IsoTestIsoTooBig() throws Exception {
Mockito.doReturn(64L * 1024L * 1024L + 1l).when(fileMock).length();

Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(Mockito.any(File.class), Mockito.any(File.class), Mockito.any(File.class)).thenCallRealMethod();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();

Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
Expand All @@ -355,7 +365,7 @@ public void generateAndRetrieveIsoAsBase64IsoTest() throws Exception {
Mockito.doReturn(64L * 1024L * 1024L).when(fileMock).length();

Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(Mockito.any(File.class), Mockito.any(File.class), Mockito.any(File.class)).thenCallRealMethod();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();

Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
Expand All @@ -377,7 +387,7 @@ public void generateAndRetrieveIsoAsBase64IsoTest() throws Exception {
inOrder.verify(scriptMock).execute();

PowerMockito.verifyStatic(ConfigDriveBuilder.class);
ConfigDriveBuilder.fileToBase64String(Mockito.any(File.class));
ConfigDriveBuilder.fileToBase64String(nullable(File.class));

}

Expand Down Expand Up @@ -494,4 +504,4 @@ public void getProgramToGenerateIsoTestMkIsoMac() throws Exception {
Mockito.verify(mkIsoProgramInMacOsFileMock, Mockito.times(1)).canExecute();
Mockito.verify(mkIsoProgramInMacOsFileMock, Mockito.times(1)).getCanonicalPath();
}
}
}
Loading