Adding some UT test

This commit is contained in:
Pierre Charton 2025-03-12 12:48:51 +01:00
parent 363751e21f
commit e86ccc36b7
2 changed files with 262 additions and 42 deletions

View File

@ -18,6 +18,7 @@ package org.apache.cloudstack.backup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@ -36,6 +37,7 @@ import org.apache.cloudstack.backup.backroll.model.BackrollBackupMetrics;
import org.apache.cloudstack.backup.backroll.model.BackrollOffering;
import org.apache.cloudstack.backup.backroll.model.response.TaskState;
import org.apache.cloudstack.backup.Backup.Metric;
import org.apache.cloudstack.backup.Backup.RestorePoint;
import org.apache.cloudstack.backup.dao.BackupDao;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
@ -90,9 +92,10 @@ public class BackrollBackupProviderTest {
}
@Test
public void listBackupOfferings_Test() throws BackrollApiException, IOException{
public void listBackupOfferings_Test() throws BackrollApiException, IOException {
Mockito.doReturn("dummyUrlToRequest").when(clientMock).getBackupOfferingUrl();
Mockito.doReturn(Arrays.asList(new BackrollOffering("dummyName", "dummyId"))).when(clientMock).getBackupOfferings(Mockito.anyString());
Mockito.doReturn(Arrays.asList(new BackrollOffering("dummyName", "dummyId"))).when(clientMock)
.getBackupOfferings(Mockito.anyString());
List<BackupOffering> results = backupProvider.listBackupOfferings(2L);
assertTrue(results.size() == 1);
}
@ -106,7 +109,8 @@ public class BackrollBackupProviderTest {
vmInstanceVO.setUuid(UUID.randomUUID().toString());
vmInstanceVO.setBackupOfferingId(2L);
Mockito.doReturn("/status/f32092e4-3e8a-461b-8733-ed93e23fa782").when(clientMock).startBackupJob(Mockito.anyString());
Mockito.doReturn("/status/f32092e4-3e8a-461b-8733-ed93e23fa782").when(clientMock)
.startBackupJob(Mockito.anyString());
Mockito.doReturn(new BackupVO()).when(backupDao).persist(Mockito.any(BackupVO.class));
Mockito.doNothing().when(clientMock).triggerTaskStatus(Mockito.anyString());
syncBackups_Test();
@ -116,12 +120,13 @@ public class BackrollBackupProviderTest {
@Test
public void restoreBackedUpVolume_Test() {
try{
backupProvider.restoreBackedUpVolume(new BackupVO(), "dummyString", "dummyString", "dummyString", new Pair<String,VirtualMachine.State>("dummyString", VirtualMachine.State.Shutdown));
try {
backupProvider.restoreBackedUpVolume(new BackupVO(), "dummyString", "dummyString", "dummyString",
new Pair<String, VirtualMachine.State>("dummyString", VirtualMachine.State.Shutdown));
} catch (Exception e) {
assertEquals(CloudRuntimeException.class, e.getClass());
String expected = String.format("Backroll plugin does not support this feature");
assertEquals(expected , e.getMessage());
assertEquals(expected, e.getMessage());
}
}
@ -141,7 +146,7 @@ public class BackrollBackupProviderTest {
}
@Test
public void getBackupMetrics_Test() throws BackrollApiException, IOException{
public void getBackupMetrics_Test() throws BackrollApiException, IOException {
VMInstanceVO vmInstanceVO = new VMInstanceVO();
vmInstanceVO.setInstanceName("test");
vmInstanceVO.setDataCenterId(1l);
@ -152,20 +157,21 @@ public class BackrollBackupProviderTest {
vmInstanceVO2.setDataCenterId(2l);
vmInstanceVO2.setBackupOfferingId(2l);
VMInstanceVO vmInstanceVO3 = new VMInstanceVO();
vmInstanceVO3.setInstanceName("test3");
vmInstanceVO3.setDataCenterId(3l);
vmInstanceVO3.setBackupOfferingId(3l);
List<BackrollVmBackup> backupsFromBackroll = Arrays.asList(
new BackrollVmBackup("OK","OK", new Date()));
new BackrollVmBackup("OK", "OK", new Date()));
BackrollBackupMetrics metrics = new BackrollBackupMetrics(2L, 3L);
Mockito.doReturn(metrics).when(clientMock).getBackupMetrics(Mockito.anyString(), Mockito.anyString());
Mockito.doReturn(backupsFromBackroll).when(clientMock).getAllBackupsfromVirtualMachine(Mockito.anyString());
assertEquals(backupProvider.getBackupMetrics(2L, Arrays.asList(vmInstanceVO, vmInstanceVO2, vmInstanceVO3)).size(), 1);
assertEquals(
backupProvider.getBackupMetrics(2L, Arrays.asList(vmInstanceVO, vmInstanceVO2, vmInstanceVO3)).size(),
1);
Mockito.verify(clientMock, times(3)).getAllBackupsfromVirtualMachine(Mockito.anyString());
Mockito.verify(clientMock, times(3)).getBackupMetrics(Mockito.anyString(), Mockito.anyString());
@ -201,22 +207,22 @@ public class BackrollBackupProviderTest {
} catch (Exception e) {
assertEquals(CloudRuntimeException.class, e.getClass());
String expected = String.format("Failed to restore VM from Backup");
assertEquals(expected , e.getMessage());
assertEquals(expected, e.getMessage());
}
}
@Test
public void getDescription_Test(){
public void getDescription_Test() {
assertEquals("Backroll Backup Plugin", backupProvider.getDescription());
}
@Test
public void isValidProviderOffering_Test(){
public void isValidProviderOffering_Test() {
assertTrue(backupProvider.isValidProviderOffering(2L, "dummyString"));
}
@Test
public void getName_Test(){
public void getName_Test() {
assertEquals("backroll", backupProvider.getName());
}
@ -227,20 +233,18 @@ public class BackrollBackupProviderTest {
assertTrue(backupProvider.assignVMToBackupOffering(vmInstanceVO, backrollOf));
}
@Test
public void removeVMFromBackupOffering_Test(){
public void removeVMFromBackupOffering_Test() {
assertTrue(backupProvider.removeVMFromBackupOffering(new VMInstanceVO()));
}
@Test
public void willDeleteBackupsOnOfferingRemoval_Test(){
public void willDeleteBackupsOnOfferingRemoval_Test() {
assertFalse(backupProvider.willDeleteBackupsOnOfferingRemoval());
}
@Test
public void syncBackups_Test() throws BackrollApiException, IOException {
public void syncBackups_Test() throws BackrollApiException, IOException {
VMInstanceVO vmInstanceVO = new VMInstanceVO();
vmInstanceVO.setInstanceName("test");
vmInstanceVO.setDataCenterId(2l);
@ -277,7 +281,7 @@ public class BackrollBackupProviderTest {
BackrollBackupMetrics metrics = new BackrollBackupMetrics(2L, 3L);
List<BackrollVmBackup> backupsFromBackroll = Arrays.asList(
new BackrollVmBackup("OK","OK", new Date()));
new BackrollVmBackup("OK", "OK", new Date()));
List<Backup> backupsInDb = Arrays.asList(backupBackingUp, backupBackedUp, backupFailed);
Mockito.doReturn(backupsInDb).when(backupDao).listByVmId(Mockito.anyLong(), Mockito.anyLong());
@ -290,7 +294,6 @@ public class BackrollBackupProviderTest {
backupProvider.syncBackups(vmInstanceVO, metric);
}
@Test
public void getClient_Test() {
BackrollClient client = backupProvider.getClient(2L);
@ -326,7 +329,137 @@ public class BackrollBackupProviderTest {
} catch (Exception e) {
assertEquals(CloudRuntimeException.class, e.getClass());
String expected = String.format("You can't delete a backup while it still BackingUp");
assertEquals(expected , e.getMessage());
assertEquals(expected, e.getMessage());
}
}
@Test
public void listRestorePoints_Test() throws BackrollApiException, IOException {
List<RestorePoint> rps = Arrays.asList(new RestorePoint("rp1", new Date(), "incremental"),
new RestorePoint("rp2", new Date(), "incremental"),
new RestorePoint("rp3", new Date(), "incremental"),
new RestorePoint("rp4", new Date(), "incremental"));
VMInstanceVO vmInstanceVO3 = new VMInstanceVO();
vmInstanceVO3.setInstanceName("test3");
vmInstanceVO3.setDataCenterId(3l);
vmInstanceVO3.setBackupOfferingId(3l);
Mockito.doReturn(rps).when(clientMock).listRestorePoints(Mockito.anyString());
List<RestorePoint> rPoints = backupProvider.listRestorePoints(vmInstanceVO3);
assertEquals(rPoints.size(), rps.size());
}
@Test
public void createNewBackupEntryForRestorePoint_Test() throws BackrollApiException, IOException {
RestorePoint restorePoint = new RestorePoint("restore-123", new Date(), "INCREMENTAL");
VMInstanceVO vm = new VMInstanceVO();
vm.setUuid("vm-uuid-456");
vm.setDataCenterId(2L);
vm.setBackupOfferingId(3L);
Backup.Metric metric = null;
BackrollBackupMetrics backupMetrics = new BackrollBackupMetrics(100L, 200L);
Mockito.doReturn(backupMetrics).when(clientMock).getBackupMetrics(vm.getUuid(), restorePoint.getId());
BackupVO savedBackup = new BackupVO();
Mockito.doReturn(savedBackup).when(backupDao).persist(Mockito.any(BackupVO.class));
Backup result = backupProvider.createNewBackupEntryForRestorePoint(restorePoint, vm, metric);
assertNotNull(result);
assertEquals(vm.getId(), result.getVmId());
assertEquals(restorePoint.getId(), result.getExternalId());
assertEquals("INCREMENTAL", result.getType());
assertEquals(restorePoint.getCreated(), result.getDate());
assertEquals(Backup.Status.BackedUp, result.getStatus());
assertEquals(vm.getBackupOfferingId(), (Long)result.getBackupOfferingId());
assertEquals(vm.getAccountId(), result.getAccountId());
assertEquals(vm.getDomainId(), result.getDomainId());
assertEquals(vm.getDataCenterId(), result.getZoneId());
assertEquals((Long)backupMetrics.getDeduplicated(), result.getSize());
assertEquals((Long)backupMetrics.getSize(), result.getProtectedSize());
Mockito.verify(clientMock).getBackupMetrics(vm.getUuid(), restorePoint.getId());
Mockito.verify(backupDao).persist(Mockito.any(BackupVO.class));
}
@Test
public void createNewBackupEntryForRestorePoint_WithMetric_Test() throws BackrollApiException, IOException {
RestorePoint restorePoint = new RestorePoint("restore-789", new Date(), "INCREMENTAL");
VMInstanceVO vm = new VMInstanceVO();
vm.setUuid("vm-uuid-789");
vm.setDataCenterId(3L);
vm.setBackupOfferingId(4L);
Backup.Metric metric = new Backup.Metric(150L, 250L);
BackupVO savedBackup = new BackupVO();
Mockito.doReturn(savedBackup).when(backupDao).persist(Mockito.any(BackupVO.class));
Backup result = backupProvider.createNewBackupEntryForRestorePoint(restorePoint, vm, metric);
assertNotNull(result);
assertEquals(vm.getId(), result.getVmId());
assertEquals(restorePoint.getId(), result.getExternalId());
assertEquals("INCREMENTAL", result.getType());
assertEquals(restorePoint.getCreated(), result.getDate());
assertEquals(Backup.Status.BackedUp, result.getStatus());
assertEquals(vm.getBackupOfferingId(), (Long)result.getBackupOfferingId());
assertEquals(vm.getAccountId(), result.getAccountId());
assertEquals(vm.getDomainId(), result.getDomainId());
assertEquals(vm.getDataCenterId(), result.getZoneId());
assertEquals(metric.getBackupSize(), result.getSize());
assertEquals(metric.getDataSize(), result.getProtectedSize());
Mockito.verify(clientMock, Mockito.never()).getBackupMetrics(Mockito.anyString(), Mockito.anyString());
Mockito.verify(backupDao).persist(Mockito.any(BackupVO.class));
}
@Test(expected = CloudRuntimeException.class)
public void createNewBackupEntryForRestorePoint_BackrollApiException_Test()
throws BackrollApiException, IOException {
RestorePoint restorePoint =new RestorePoint("restore-404", new Date(), "INCREMENTAL");
VMInstanceVO vm = new VMInstanceVO();
vm.setUuid("vm-uuid-404");
vm.setDataCenterId(4L);
vm.setBackupOfferingId(5L);
Backup.Metric metric = null;
Mockito.doThrow(new BackrollApiException()).when(clientMock).getBackupMetrics(vm.getUuid(),
restorePoint.getId());
backupProvider.createNewBackupEntryForRestorePoint(restorePoint, vm, metric);
}
@Test(expected = CloudRuntimeException.class)
public void createNewBackupEntryForRestorePoint_IOException_Test() throws BackrollApiException, IOException {
RestorePoint restorePoint = new RestorePoint("restore-500", new Date(), "INCREMENTAL");
VMInstanceVO vm = new VMInstanceVO();
vm.setUuid("vm-uuid-500");
vm.setDataCenterId(5L);
vm.setBackupOfferingId(6L);
Backup.Metric metric = null;
Mockito.doThrow(new IOException("IO Error")).when(clientMock).getBackupMetrics(vm.getUuid(),
restorePoint.getId());
backupProvider.createNewBackupEntryForRestorePoint(restorePoint, vm, metric);
}
}

View File

@ -15,15 +15,20 @@
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.backup.backroll;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.cloudstack.backup.Backup;
import org.apache.cloudstack.backup.Backup.Metric;
import org.apache.cloudstack.backup.BackupOffering;
import org.apache.cloudstack.backup.backroll.model.BackrollBackupMetrics;
@ -46,6 +51,7 @@ import org.apache.cloudstack.backup.backroll.model.response.policy.BackupPolicie
import org.apache.cloudstack.backup.backroll.utils.BackrollApiException;
import org.apache.cloudstack.backup.backroll.utils.BackrollHttpClientProvider;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTime;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
@ -62,7 +68,6 @@ public class BackrollClientTest {
@Mock
private BackrollHttpClientProvider backrollHttpClientProviderMock;
@Rule
public WireMockRule wireMockRule = new WireMockRule(9399);
@ -79,20 +84,23 @@ public class BackrollClientTest {
String virtualMachineResponseString = "{ \"state\": \"SUCCESS\", \"info\": { \"archives\": [ { \"archive\": \"ROOT-00000\", \"barchive\": \"ROOT-00000\", \"id\": \"25d55ad283aa400af464c76d713c07ad7d163abdd3b8fbcdbdc46b827e5e0457\", \"name\": \"ROOT-00000\", \"start\": \"2024-11-08T18:24:48.000000\", \"time\": \"2024-11-08T18:24:48.000000\" } ], \"encryption\": { \"mode\": \"none\" }, \"repository\": { \"id\": \"36a11ebc0775a097c927735cc7015d19be7309be69fc15b896c5b1fd87fcbd79\", \"last_modified\": \"2024-11-29T09:53:09.000000\", \"location\": \"/mnt/backup/backup1\" } } }";
BackrollTaskRequestResponse backrollTaskReqResponseMock = new BackrollTaskRequestResponse();
backrollTaskReqResponseMock.location = "/api/v1/status/f32092e4-3e8a-461b-8733-ed93e23fa782";
VirtualMachineBackupsResponse virtualMachineBackupsResponseMock = new ObjectMapper().readValue(virtualMachineResponseString, VirtualMachineBackupsResponse.class);
VirtualMachineBackupsResponse virtualMachineBackupsResponseMock = new ObjectMapper()
.readValue(virtualMachineResponseString, VirtualMachineBackupsResponse.class);
// Mocking client responses
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).get(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(virtualMachineBackupsResponseMock).when(backrollHttpClientProviderMock).waitGet(Mockito.anyString(), Mockito.any());
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock)
.get(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(virtualMachineBackupsResponseMock).when(backrollHttpClientProviderMock).waitGet(Mockito.anyString(),
Mockito.any());
// Run the method under test
List<BackrollVmBackup> backupsTestList = client.getAllBackupsfromVirtualMachine(vmId);
// Check results
assertEquals(1, backupsTestList.size()); // Should be 1 based on provided mock data
assertEquals(1, backupsTestList.size()); // Should be 1 based on provided mock data
}
@Test
public void getBackupMetrics_Test() throws IOException, BackrollApiException {
public void getBackupMetrics_Test() throws IOException, BackrollApiException {
BackrollTaskRequestResponse backrollTaskReqResponseMock = new BackrollTaskRequestResponse();
backrollTaskReqResponseMock.location = "/api/v1/status/f32092e4-3e8a-461b-8733-ed93e23fa782";
BackrollBackupMetricsResponse mockResponse = new BackrollBackupMetricsResponse();
@ -100,7 +108,8 @@ public class BackrollClientTest {
mockResponse.info.originalSize = "1000";
mockResponse.info.deduplicatedSize = "800";
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).get(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock)
.get(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(mockResponse).when(backrollHttpClientProviderMock).waitGet(Mockito.anyString(), Mockito.any());
BackrollBackupMetrics metrics = client.getBackupMetrics("dummyVMId", "dummyBackupId");
@ -121,16 +130,18 @@ public class BackrollClientTest {
mockResponse.infos.cache.stats = new CacheStats();
mockResponse.infos.cache.stats.totalSize = "10000";
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).get(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock)
.get(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(mockResponse).when(backrollHttpClientProviderMock).waitGet(Mockito.anyString(), Mockito.any());
Metric metrics = client.getVirtualMachineMetrics("dummyVMId");
assertEquals(10000L, (long)metrics.getBackupSize());
assertEquals(10000L, (long)metrics.getDataSize());
assertEquals(10000L, (long) metrics.getBackupSize());
assertEquals(10000L, (long) metrics.getDataSize());
}
@Test
public void deleteBackup_Test() throws IOException, BackrollApiException{
public void deleteBackup_Test() throws IOException, BackrollApiException {
BackrollTaskRequestResponse backrollTaskReqResponseMock = new BackrollTaskRequestResponse();
backrollTaskReqResponseMock.location = "/api/v1/status/f32092e4-3e8a-461b-8733-ed93e23fa782";
@ -139,17 +150,20 @@ public class BackrollClientTest {
mockResponse.archives = new BackrollArchivesResponse();
mockResponse.archives.archives = Arrays.asList(new BackrollArchiveResponse());
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).delete(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock)
.delete(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(mockResponse).when(backrollHttpClientProviderMock).waitGet(Mockito.anyString(), Mockito.any());
Boolean isBackupDeleted = client.deleteBackup("dummyVMId", "dummyBackUpName");
assertTrue(isBackupDeleted);
}
@Test
public void checkBackupTaskStatusSuccess_Test() throws IOException, BackrollApiException {
String backupResponse = "{\"state\":\"SUCCESS\",\"info\":\"test\"}";
doReturn(backupResponse).when(backrollHttpClientProviderMock).getWithoutParseResponse(Mockito.matches(".*status/.*"));
doReturn(backupResponse).when(backrollHttpClientProviderMock)
.getWithoutParseResponse(Mockito.matches(".*status/.*"));
BackrollTaskStatus status = client.checkBackupTaskStatus("dummytaskid");
@ -160,18 +174,21 @@ public class BackrollClientTest {
@Test
public void checkBackupTaskStatus_Test() throws IOException, BackrollApiException {
String backupResponse = "{\"state\":\"PENDING\",\"current\":0,\"total\":1,\"status\":\"Pending...\"}";
doReturn(backupResponse).when(backrollHttpClientProviderMock).getWithoutParseResponse(Mockito.matches(".*/status/.*"));
doReturn(backupResponse).when(backrollHttpClientProviderMock)
.getWithoutParseResponse(Mockito.matches(".*/status/.*"));
BackrollTaskStatus status = client.checkBackupTaskStatus("dummytaskid");
assertEquals(TaskState.PENDING, status.getState());
}
@Test
public void restoreVMFromBackup_Test() throws IOException, BackrollApiException {
public void restoreVMFromBackup_Test() throws IOException, BackrollApiException {
BackrollTaskRequestResponse backrollTaskReqResponseMock = new BackrollTaskRequestResponse();
backrollTaskReqResponseMock.location = "/api/v1/status/f32092e4-3e8a-461b-8733-ed93e23fa782";
String resultMock = "SUCCESS WOW YOUHOU";
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).post(Mockito.matches(".*/tasks/restore/.*"), Mockito.any(JSONObject.class),Mockito.any());
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock)
.post(Mockito.matches(".*/tasks/restore/.*"), Mockito.any(JSONObject.class), Mockito.any());
doReturn(resultMock).when(backrollHttpClientProviderMock).waitGetWithoutParseResponse(Mockito.anyString());
Boolean isRestoreOk = client.restoreVMFromBackup("dummyVMId", "dummyBackUpName");
@ -184,7 +201,8 @@ public class BackrollClientTest {
BackrollTaskRequestResponse backrollTaskReqResponseMock = new BackrollTaskRequestResponse();
backrollTaskReqResponseMock.location = "/api/v1/status/f32092e4-3e8a-461b-8733-ed93e23fa782";
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).post(Mockito.matches(".*/tasks/singlebackup/.*"), Mockito.nullable(JSONObject.class),Mockito.any());
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock)
.post(Mockito.matches(".*/tasks/singlebackup/.*"), Mockito.nullable(JSONObject.class), Mockito.any());
String response = client.startBackupJob("dummyJobId");
@ -192,10 +210,11 @@ public class BackrollClientTest {
}
@Test
public void getBackupOfferingUrl_Test() throws IOException, BackrollApiException {
public void getBackupOfferingUrl_Test() throws IOException, BackrollApiException {
BackrollTaskRequestResponse backrollTaskReqResponseMock = new BackrollTaskRequestResponse();
backrollTaskReqResponseMock.location = "/api/v1/status/f32092e4-3e8a-461b-8733-ed93e23fa782";
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).get(Mockito.matches(".*/backup_policies.*"), Mockito.any());
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock)
.get(Mockito.matches(".*/backup_policies.*"), Mockito.any());
String response = client.getBackupOfferingUrl();
@ -233,11 +252,79 @@ public class BackrollClientTest {
BackupPoliciesResponse backupPoliciesResponseMock = new BackupPoliciesResponse();
backupPoliciesResponseMock.backupPolicies = Arrays.asList(policy1, policy2);
doReturn(backupPoliciesResponseMock).when(backrollHttpClientProviderMock).waitGet(Mockito.matches("/status/f32092e4-3e8a-461b-8733-ed93e23fa782"), Mockito.any());
doReturn(backupPoliciesResponseMock).when(backrollHttpClientProviderMock)
.waitGet(Mockito.matches("/status/f32092e4-3e8a-461b-8733-ed93e23fa782"), Mockito.any());
List<BackupOffering> response = client.getBackupOfferings("/status/f32092e4-3e8a-461b-8733-ed93e23fa782");
assertEquals(response.size(), 2);
}
@Test
public void listRestorePoints_Test() throws BackrollApiException, IOException {
String vmId = "vm-123";
String virtualMachineResponseString = "{ \"state\": \"SUCCESS\", \"info\": { \"archives\": [ { \"archive\": \"ROOT-00000\", \"barchive\": \"ROOT-00000\", \"id\": \"25d55ad283aa400af464c76d713c07ad7d163abdd3b8fbcdbdc46b827e5e0457\", \"name\": \"ROOT-00000\", \"start\": \"2024-11-08T18:24:48.000000\", \"time\": \"2024-11-08T18:24:48.000000\" } ], \"encryption\": { \"mode\": \"none\" }, \"repository\": { \"id\": \"36a11ebc0775a097c927735cc7015d19be7309be69fc15b896c5b1fd87fcbd79\", \"last_modified\": \"2024-11-29T09:53:09.000000\", \"location\": \"/mnt/backup/backup1\" } } }";
BackrollTaskRequestResponse backrollTaskReqResponseMock = new BackrollTaskRequestResponse();
backrollTaskReqResponseMock.location = "/api/v1/status/f32092e4-3e8a-461b-8733-ed93e23fa782";
VirtualMachineBackupsResponse virtualMachineBackupsResponseMock = new ObjectMapper()
.readValue(virtualMachineResponseString, VirtualMachineBackupsResponse.class);
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock)
.get(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
doReturn(virtualMachineBackupsResponseMock).when(backrollHttpClientProviderMock).waitGet(Mockito.anyString(),
Mockito.any());
List<Backup.RestorePoint> restorePoints = client.listRestorePoints(vmId);
assertNotNull(restorePoints);
assertEquals(1, restorePoints.size());
Backup.RestorePoint rp1 = restorePoints.get(0);
assertEquals("25d55ad283aa400af464c76d713c07ad7d163abdd3b8fbcdbdc46b827e5e0457", rp1.getId());
assertEquals(new DateTime("2024-11-08T18:24:48.000000").toDate(), rp1.getCreated());
assertEquals("INCREMENTAL", rp1.getType());
Mockito.verify(backrollHttpClientProviderMock).get(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
Mockito.verify(backrollHttpClientProviderMock).waitGet(anyString(), Mockito.any());
}
@Test
public void triggerTaskStatus_Test() throws IOException, BackrollApiException {
String urlToRequest = "/api/v1/task/status";
// Simuler le comportement normal sans exception
Mockito.doReturn("ok").when(backrollHttpClientProviderMock).waitGetWithoutParseResponse(urlToRequest);
// Appel de la méthode sous test
client.triggerTaskStatus(urlToRequest);
// Vérifier que la méthode a été appelée une fois avec le bon paramètre
Mockito.verify(backrollHttpClientProviderMock, times(1)).waitGetWithoutParseResponse(urlToRequest);
}
@Test(expected = IOException.class)
public void triggerTaskStatus_IOException_Test() throws IOException, BackrollApiException {
String urlToRequest = "/api/v1/task/status";
// Simuler une exception IOException
Mockito.doThrow(new IOException("IO Error")).when(backrollHttpClientProviderMock)
.waitGetWithoutParseResponse(urlToRequest);
// Appel de la méthode sous test (devrait lancer une IOException)
client.triggerTaskStatus(urlToRequest);
}
@Test(expected = BackrollApiException.class)
public void triggerTaskStatus_BackrollApiException_Test() throws IOException, BackrollApiException {
String urlToRequest = "/api/v1/task/status";
// Simuler une exception BackrollApiException
Mockito.doThrow(new BackrollApiException()).when(backrollHttpClientProviderMock)
.waitGetWithoutParseResponse(urlToRequest);
// Appel de la méthode sous test (devrait lancer une BackrollApiException)
client.triggerTaskStatus(urlToRequest);
}
}