|
76 | 76 | import com.google.gson.Gson; |
77 | 77 | import org.apache.cloudstack.api.ApiConstants; |
78 | 78 | import org.apache.cloudstack.api.ServerApiException; |
| 79 | +import org.apache.cloudstack.api.command.admin.backup.CloneBackupOfferingCmd; |
79 | 80 | import org.apache.cloudstack.api.command.admin.backup.ImportBackupOfferingCmd; |
80 | 81 | import org.apache.cloudstack.api.command.admin.backup.UpdateBackupOfferingCmd; |
81 | 82 | import org.apache.cloudstack.api.command.user.backup.CreateBackupCmd; |
|
132 | 133 | import static org.mockito.Mockito.verify; |
133 | 134 | import static org.mockito.Mockito.when; |
134 | 135 | import static org.mockito.Mockito.atLeastOnce; |
| 136 | +import org.mockito.ArgumentCaptor; |
135 | 137 |
|
136 | 138 | @RunWith(MockitoJUnitRunner.class) |
137 | 139 | public class BackupManagerTest { |
@@ -2518,4 +2520,106 @@ private BackupOfferingVO createMockOffering(Long id, String name) { |
2518 | 2520 | return offering; |
2519 | 2521 | } |
2520 | 2522 |
|
| 2523 | + @Test |
| 2524 | + public void testCloneBackupOfferingUsesProvidedDomainIds() { |
| 2525 | + Long sourceOfferingId = 1L; |
| 2526 | + Long zoneId = 10L; |
| 2527 | + Long savedOfferingId = 2L; |
| 2528 | + List<Long> providedDomainIds = List.of(11L); |
| 2529 | + |
| 2530 | + // command |
| 2531 | + CloneBackupOfferingCmd cmd = Mockito.mock(CloneBackupOfferingCmd.class); |
| 2532 | + when(cmd.getSourceOfferingId()).thenReturn(sourceOfferingId); |
| 2533 | + when(cmd.getName()).thenReturn("Cloned Offering"); |
| 2534 | + when(cmd.getDescription()).thenReturn(null); |
| 2535 | + when(cmd.getExternalId()).thenReturn(null); |
| 2536 | + when(cmd.getUserDrivenBackups()).thenReturn(null); |
| 2537 | + when(cmd.getDomainIds()).thenReturn(providedDomainIds); |
| 2538 | + |
| 2539 | + // source offering |
| 2540 | + BackupOfferingVO sourceOffering = Mockito.mock(BackupOfferingVO.class); |
| 2541 | + when(sourceOffering.getZoneId()).thenReturn(zoneId); |
| 2542 | + when(sourceOffering.getExternalId()).thenReturn("ext-src"); |
| 2543 | + when(sourceOffering.getProvider()).thenReturn("testbackupprovider"); |
| 2544 | + when(sourceOffering.getDescription()).thenReturn("src desc"); |
| 2545 | + when(sourceOffering.isUserDrivenBackupAllowed()).thenReturn(true); |
| 2546 | + when(sourceOffering.getName()).thenReturn("Source Offering"); |
| 2547 | + |
| 2548 | + when(backupOfferingDao.findById(sourceOfferingId)).thenReturn(sourceOffering); |
| 2549 | + when(backupOfferingDao.findByName(cmd.getName(), zoneId)).thenReturn(null); |
| 2550 | + |
| 2551 | + BackupOfferingVO savedOffering = Mockito.mock(BackupOfferingVO.class); |
| 2552 | + when(savedOffering.getId()).thenReturn(savedOfferingId); |
| 2553 | + when(backupOfferingDao.persist(any(BackupOfferingVO.class))).thenReturn(savedOffering); |
| 2554 | + |
| 2555 | + DomainVO domain = Mockito.mock(DomainVO.class); |
| 2556 | + when(domainDao.findById(11L)).thenReturn(domain); |
| 2557 | + |
| 2558 | + overrideBackupFrameworkConfigValue(); |
| 2559 | + |
| 2560 | + BackupOffering result = backupManager.cloneBackupOffering(cmd); |
| 2561 | + |
| 2562 | + assertEquals(savedOffering, result); |
| 2563 | + |
| 2564 | + ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 2565 | + verify(backupOfferingDetailsDao, times(1)).saveDetails(captor.capture()); |
| 2566 | + List<BackupOfferingDetailsVO> savedDetails = captor.getValue(); |
| 2567 | + assertEquals(1, savedDetails.size()); |
| 2568 | + assertEquals(String.valueOf(11L), savedDetails.get(0).getValue()); |
| 2569 | + } |
| 2570 | + |
| 2571 | + @Test |
| 2572 | + public void testCloneBackupOfferingInheritsDomainIdsFromSource() { |
| 2573 | + Long sourceOfferingId = 3L; |
| 2574 | + Long zoneId = 20L; |
| 2575 | + Long savedOfferingId = 4L; |
| 2576 | + List<Long> sourceDomainIds = List.of(21L, 22L); |
| 2577 | + |
| 2578 | + CloneBackupOfferingCmd cmd = Mockito.mock(CloneBackupOfferingCmd.class); |
| 2579 | + when(cmd.getSourceOfferingId()).thenReturn(sourceOfferingId); |
| 2580 | + when(cmd.getName()).thenReturn("Cloned Inherit Offering"); |
| 2581 | + when(cmd.getDescription()).thenReturn(null); |
| 2582 | + when(cmd.getExternalId()).thenReturn(null); |
| 2583 | + when(cmd.getUserDrivenBackups()).thenReturn(null); |
| 2584 | + // Simulate resolver having provided the source offering domains (the real cmd#getDomainIds() would do this) |
| 2585 | + when(cmd.getDomainIds()).thenReturn(sourceDomainIds); |
| 2586 | + |
| 2587 | + BackupOfferingVO sourceOffering = Mockito.mock(BackupOfferingVO.class); |
| 2588 | + when(sourceOffering.getZoneId()).thenReturn(zoneId); |
| 2589 | + when(sourceOffering.getExternalId()).thenReturn("ext-src-2"); |
| 2590 | + when(sourceOffering.getProvider()).thenReturn("testbackupprovider"); |
| 2591 | + when(sourceOffering.getDescription()).thenReturn("src desc 2"); |
| 2592 | + when(sourceOffering.isUserDrivenBackupAllowed()).thenReturn(false); |
| 2593 | + when(sourceOffering.getName()).thenReturn("Source Offering 2"); |
| 2594 | + |
| 2595 | + when(backupOfferingDao.findById(sourceOfferingId)).thenReturn(sourceOffering); |
| 2596 | + when(backupOfferingDao.findByName(cmd.getName(), zoneId)).thenReturn(null); |
| 2597 | + |
| 2598 | + BackupOfferingVO savedOffering = Mockito.mock(BackupOfferingVO.class); |
| 2599 | + when(savedOffering.getId()).thenReturn(savedOfferingId); |
| 2600 | + when(backupOfferingDao.persist(any(BackupOfferingVO.class))).thenReturn(savedOffering); |
| 2601 | + |
| 2602 | + // domain handling |
| 2603 | + DomainVO domain21 = Mockito.mock(DomainVO.class); |
| 2604 | + DomainVO domain22 = Mockito.mock(DomainVO.class); |
| 2605 | + when(domainDao.findById(21L)).thenReturn(domain21); |
| 2606 | + when(domainDao.findById(22L)).thenReturn(domain22); |
| 2607 | + when(domainHelper.filterChildSubDomains(sourceDomainIds)).thenReturn(new ArrayList<>(sourceDomainIds)); |
| 2608 | + |
| 2609 | + overrideBackupFrameworkConfigValue(); |
| 2610 | + |
| 2611 | + BackupOffering result = backupManager.cloneBackupOffering(cmd); |
| 2612 | + assertEquals(savedOffering, result); |
| 2613 | + |
| 2614 | + ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 2615 | + verify(backupOfferingDetailsDao, times(1)).saveDetails(captor.capture()); |
| 2616 | + List<BackupOfferingDetailsVO> savedDetails = captor.getValue(); |
| 2617 | + assertEquals(2, savedDetails.size()); |
| 2618 | + List<String> values = new ArrayList<>(); |
| 2619 | + for (BackupOfferingDetailsVO d : savedDetails) { |
| 2620 | + values.add(d.getValue()); |
| 2621 | + } |
| 2622 | + assertTrue(values.contains(String.valueOf(21L))); |
| 2623 | + assertTrue(values.contains(String.valueOf(22L))); |
| 2624 | + } |
2521 | 2625 | } |
0 commit comments