Skip to content

Commit a063e94

Browse files
committed
add unit tests
1 parent 39dacc3 commit a063e94

File tree

1 file changed

+285
-0
lines changed

1 file changed

+285
-0
lines changed

server/src/test/java/org/apache/cloudstack/backup/BackupManagerTest.java

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.apache.cloudstack.api.command.user.backup.CreateBackupCmd;
8181
import org.apache.cloudstack.api.command.user.backup.CreateBackupScheduleCmd;
8282
import org.apache.cloudstack.api.command.user.backup.DeleteBackupScheduleCmd;
83+
import org.apache.cloudstack.api.command.user.backup.ListBackupOfferingsCmd;
8384
import org.apache.cloudstack.api.command.user.backup.ListBackupScheduleCmd;
8485
import org.apache.cloudstack.api.response.BackupResponse;
8586
import org.apache.cloudstack.backup.dao.BackupDao;
@@ -2232,4 +2233,288 @@ public void testUpdateBackupOfferingPersistsDomainDetailsWhenProvided() {
22322233
verify(backupOfferingDetailsDao, times(1)).persist(Mockito.any(BackupOfferingDetailsVO.class));
22332234
}
22342235

2236+
@Test
2237+
public void testListBackupOfferingsWithDomainFilteringIncludesGlobalOfferings() {
2238+
Long requestedDomainId = 3L;
2239+
2240+
ListBackupOfferingsCmd cmd =
2241+
Mockito.mock(ListBackupOfferingsCmd.class);
2242+
when(cmd.getOfferingId()).thenReturn(null);
2243+
when(cmd.getDomainId()).thenReturn(requestedDomainId);
2244+
when(cmd.getStartIndex()).thenReturn(0L);
2245+
when(cmd.getPageSizeVal()).thenReturn(20L);
2246+
2247+
BackupOfferingVO globalOffering = createMockOffering(1L, "Global Offering");
2248+
BackupOfferingVO domainOffering = createMockOffering(2L, "Domain Offering");
2249+
2250+
List<BackupOfferingVO> allOfferings = List.of(globalOffering, domainOffering);
2251+
2252+
SearchBuilder<BackupOfferingVO> sb = Mockito.mock(SearchBuilder.class);
2253+
SearchCriteria<BackupOfferingVO> sc = Mockito.mock(SearchCriteria.class);
2254+
BackupOfferingVO entityMock = Mockito.mock(BackupOfferingVO.class);
2255+
when(backupOfferingDao.createSearchBuilder()).thenReturn(sb);
2256+
when(sb.entity()).thenReturn(entityMock);
2257+
when(sb.and(Mockito.anyString(), Mockito.any(), Mockito.any(SearchCriteria.Op.class))).thenReturn(sb);
2258+
when(sb.create()).thenReturn(sc);
2259+
when(backupOfferingDao.searchAndCount(Mockito.any(), Mockito.any()))
2260+
.thenReturn(new Pair<>(allOfferings, allOfferings.size()));
2261+
2262+
when(backupOfferingDetailsDao.findDomainIds(1L)).thenReturn(Collections.emptyList());
2263+
when(backupOfferingDetailsDao.findDomainIds(2L)).thenReturn(List.of(2L));
2264+
2265+
Account account = Mockito.mock(Account.class);
2266+
when(account.getType()).thenReturn(Account.Type.NORMAL);
2267+
2268+
try (MockedStatic<CallContext> mockedCallContext = Mockito.mockStatic(CallContext.class)) {
2269+
CallContext contextMock = Mockito.mock(CallContext.class);
2270+
mockedCallContext.when(CallContext::current).thenReturn(contextMock);
2271+
when(contextMock.getCallingAccount()).thenReturn(account);
2272+
2273+
Pair<List<BackupOffering>, Integer> result = backupManager.listBackupOfferings(cmd);
2274+
2275+
assertEquals(1, result.first().size());
2276+
assertEquals("Global Offering", result.first().get(0).getName());
2277+
}
2278+
}
2279+
2280+
@Test
2281+
public void testListBackupOfferingsWithDomainFilteringIncludesDirectDomainMapping() {
2282+
Long requestedDomainId = 3L;
2283+
2284+
ListBackupOfferingsCmd cmd =
2285+
Mockito.mock(ListBackupOfferingsCmd.class);
2286+
when(cmd.getOfferingId()).thenReturn(null);
2287+
when(cmd.getDomainId()).thenReturn(requestedDomainId);
2288+
when(cmd.getStartIndex()).thenReturn(0L);
2289+
when(cmd.getPageSizeVal()).thenReturn(20L);
2290+
2291+
BackupOfferingVO directDomainOffering = createMockOffering(1L, "Direct Domain Offering");
2292+
BackupOfferingVO otherDomainOffering = createMockOffering(2L, "Other Domain Offering");
2293+
2294+
List<BackupOfferingVO> allOfferings = List.of(directDomainOffering, otherDomainOffering);
2295+
2296+
SearchBuilder<BackupOfferingVO> sb = Mockito.mock(SearchBuilder.class);
2297+
SearchCriteria<BackupOfferingVO> sc = Mockito.mock(SearchCriteria.class);
2298+
BackupOfferingVO entityMock = Mockito.mock(BackupOfferingVO.class);
2299+
when(backupOfferingDao.createSearchBuilder()).thenReturn(sb);
2300+
when(sb.entity()).thenReturn(entityMock);
2301+
when(sb.and(Mockito.anyString(), Mockito.any(), Mockito.any(SearchCriteria.Op.class))).thenReturn(sb);
2302+
when(sb.create()).thenReturn(sc);
2303+
when(backupOfferingDao.searchAndCount(Mockito.any(), Mockito.any()))
2304+
.thenReturn(new Pair<>(allOfferings, allOfferings.size()));
2305+
2306+
when(backupOfferingDetailsDao.findDomainIds(1L)).thenReturn(List.of(requestedDomainId));
2307+
when(backupOfferingDetailsDao.findDomainIds(2L)).thenReturn(List.of(5L));
2308+
2309+
Account account = Mockito.mock(Account.class);
2310+
when(account.getType()).thenReturn(Account.Type.NORMAL);
2311+
2312+
try (MockedStatic<CallContext> mockedCallContext = Mockito.mockStatic(CallContext.class)) {
2313+
CallContext contextMock = Mockito.mock(CallContext.class);
2314+
mockedCallContext.when(CallContext::current).thenReturn(contextMock);
2315+
when(contextMock.getCallingAccount()).thenReturn(account);
2316+
2317+
Pair<List<BackupOffering>, Integer> result = backupManager.listBackupOfferings(cmd);
2318+
2319+
assertEquals(1, result.first().size());
2320+
assertEquals("Direct Domain Offering", result.first().get(0).getName());
2321+
}
2322+
}
2323+
2324+
@Test
2325+
public void testListBackupOfferingsWithDomainFilteringIncludesParentDomainOfferings() {
2326+
Long parentDomainId = 1L;
2327+
Long childDomainId = 3L;
2328+
2329+
ListBackupOfferingsCmd cmd =
2330+
Mockito.mock(ListBackupOfferingsCmd.class);
2331+
when(cmd.getOfferingId()).thenReturn(null);
2332+
when(cmd.getDomainId()).thenReturn(childDomainId);
2333+
when(cmd.getStartIndex()).thenReturn(0L);
2334+
when(cmd.getPageSizeVal()).thenReturn(20L);
2335+
2336+
BackupOfferingVO parentDomainOffering = createMockOffering(1L, "Parent Domain Offering");
2337+
BackupOfferingVO siblingDomainOffering = createMockOffering(2L, "Sibling Domain Offering");
2338+
2339+
List<BackupOfferingVO> allOfferings = List.of(parentDomainOffering, siblingDomainOffering);
2340+
2341+
SearchBuilder<BackupOfferingVO> sb = Mockito.mock(SearchBuilder.class);
2342+
SearchCriteria<BackupOfferingVO> sc = Mockito.mock(SearchCriteria.class);
2343+
BackupOfferingVO entityMock = Mockito.mock(BackupOfferingVO.class);
2344+
when(backupOfferingDao.createSearchBuilder()).thenReturn(sb);
2345+
when(sb.entity()).thenReturn(entityMock);
2346+
when(sb.and(Mockito.anyString(), Mockito.any(), Mockito.any(SearchCriteria.Op.class))).thenReturn(sb);
2347+
when(sb.create()).thenReturn(sc);
2348+
when(backupOfferingDao.searchAndCount(Mockito.any(), Mockito.any()))
2349+
.thenReturn(new Pair<>(allOfferings, allOfferings.size()));
2350+
2351+
when(backupOfferingDetailsDao.findDomainIds(1L)).thenReturn(List.of(parentDomainId));
2352+
when(backupOfferingDetailsDao.findDomainIds(2L)).thenReturn(List.of(4L));
2353+
2354+
when(domainDao.isChildDomain(parentDomainId, childDomainId)).thenReturn(true);
2355+
when(domainDao.isChildDomain(4L, childDomainId)).thenReturn(false);
2356+
2357+
Account account = Mockito.mock(Account.class);
2358+
when(account.getType()).thenReturn(Account.Type.NORMAL);
2359+
2360+
try (MockedStatic<CallContext> mockedCallContext = Mockito.mockStatic(CallContext.class)) {
2361+
CallContext contextMock = Mockito.mock(CallContext.class);
2362+
mockedCallContext.when(CallContext::current).thenReturn(contextMock);
2363+
when(contextMock.getCallingAccount()).thenReturn(account);
2364+
2365+
Pair<List<BackupOffering>, Integer> result = backupManager.listBackupOfferings(cmd);
2366+
2367+
assertEquals(1, result.first().size());
2368+
assertEquals("Parent Domain Offering", result.first().get(0).getName());
2369+
}
2370+
}
2371+
2372+
@Test
2373+
public void testListBackupOfferingsWithDomainFilteringExcludesSiblingDomainOfferings() {
2374+
Long requestedDomainId = 3L;
2375+
Long siblingDomainId = 4L;
2376+
2377+
ListBackupOfferingsCmd cmd =
2378+
Mockito.mock(ListBackupOfferingsCmd.class);
2379+
when(cmd.getOfferingId()).thenReturn(null);
2380+
when(cmd.getDomainId()).thenReturn(requestedDomainId);
2381+
when(cmd.getStartIndex()).thenReturn(0L);
2382+
when(cmd.getPageSizeVal()).thenReturn(20L);
2383+
2384+
BackupOfferingVO siblingOffering = createMockOffering(1L, "Sibling Domain Offering");
2385+
List<BackupOfferingVO> allOfferings = List.of(siblingOffering);
2386+
2387+
SearchBuilder<BackupOfferingVO> sb = Mockito.mock(SearchBuilder.class);
2388+
SearchCriteria<BackupOfferingVO> sc = Mockito.mock(SearchCriteria.class);
2389+
BackupOfferingVO entityMock = Mockito.mock(BackupOfferingVO.class);
2390+
when(backupOfferingDao.createSearchBuilder()).thenReturn(sb);
2391+
when(sb.entity()).thenReturn(entityMock);
2392+
when(sb.and(Mockito.anyString(), Mockito.any(), Mockito.any(SearchCriteria.Op.class))).thenReturn(sb);
2393+
when(sb.create()).thenReturn(sc);
2394+
when(backupOfferingDao.searchAndCount(Mockito.any(), Mockito.any()))
2395+
.thenReturn(new Pair<>(allOfferings, allOfferings.size()));
2396+
2397+
when(backupOfferingDetailsDao.findDomainIds(1L)).thenReturn(List.of(siblingDomainId));
2398+
when(domainDao.isChildDomain(siblingDomainId, requestedDomainId)).thenReturn(false);
2399+
2400+
Account account = Mockito.mock(Account.class);
2401+
when(account.getType()).thenReturn(Account.Type.NORMAL);
2402+
2403+
try (MockedStatic<CallContext> mockedCallContext = Mockito.mockStatic(CallContext.class)) {
2404+
CallContext contextMock = Mockito.mock(CallContext.class);
2405+
mockedCallContext.when(CallContext::current).thenReturn(contextMock);
2406+
when(contextMock.getCallingAccount()).thenReturn(account);
2407+
2408+
Pair<List<BackupOffering>, Integer> result = backupManager.listBackupOfferings(cmd);
2409+
2410+
assertEquals(0, result.first().size());
2411+
}
2412+
}
2413+
2414+
@Test
2415+
public void testListBackupOfferingsWithDomainFilteringMultipleDomainMappings() {
2416+
Long requestedDomainId = 5L;
2417+
Long parentDomainId1 = 1L;
2418+
Long parentDomainId2 = 2L;
2419+
Long unrelatedDomainId = 8L;
2420+
2421+
ListBackupOfferingsCmd cmd =
2422+
Mockito.mock(ListBackupOfferingsCmd.class);
2423+
when(cmd.getOfferingId()).thenReturn(null);
2424+
when(cmd.getDomainId()).thenReturn(requestedDomainId);
2425+
when(cmd.getStartIndex()).thenReturn(0L);
2426+
when(cmd.getPageSizeVal()).thenReturn(20L);
2427+
2428+
BackupOfferingVO multiDomainOffering = createMockOffering(1L, "Multi-Domain Offering");
2429+
List<BackupOfferingVO> allOfferings = List.of(multiDomainOffering);
2430+
2431+
SearchBuilder<BackupOfferingVO> sb = Mockito.mock(SearchBuilder.class);
2432+
SearchCriteria<BackupOfferingVO> sc = Mockito.mock(SearchCriteria.class);
2433+
BackupOfferingVO entityMock = Mockito.mock(BackupOfferingVO.class);
2434+
when(backupOfferingDao.createSearchBuilder()).thenReturn(sb);
2435+
when(sb.entity()).thenReturn(entityMock);
2436+
when(sb.and(Mockito.anyString(), Mockito.any(), Mockito.any(SearchCriteria.Op.class))).thenReturn(sb);
2437+
when(sb.create()).thenReturn(sc);
2438+
when(backupOfferingDao.searchAndCount(Mockito.any(), Mockito.any()))
2439+
.thenReturn(new Pair<>(allOfferings, allOfferings.size()));
2440+
2441+
when(backupOfferingDetailsDao.findDomainIds(1L))
2442+
.thenReturn(List.of(parentDomainId1, unrelatedDomainId, parentDomainId2));
2443+
2444+
when(domainDao.isChildDomain(parentDomainId1, requestedDomainId)).thenReturn(false);
2445+
when(domainDao.isChildDomain(unrelatedDomainId, requestedDomainId)).thenReturn(false);
2446+
when(domainDao.isChildDomain(parentDomainId2, requestedDomainId)).thenReturn(true);
2447+
2448+
Account account = Mockito.mock(Account.class);
2449+
when(account.getType()).thenReturn(Account.Type.NORMAL);
2450+
2451+
try (MockedStatic<CallContext> mockedCallContext = Mockito.mockStatic(CallContext.class)) {
2452+
CallContext contextMock = Mockito.mock(CallContext.class);
2453+
mockedCallContext.when(CallContext::current).thenReturn(contextMock);
2454+
when(contextMock.getCallingAccount()).thenReturn(account);
2455+
2456+
Pair<List<BackupOffering>, Integer> result = backupManager.listBackupOfferings(cmd);
2457+
2458+
assertEquals(1, result.first().size());
2459+
assertEquals("Multi-Domain Offering", result.first().get(0).getName());
2460+
}
2461+
}
2462+
2463+
@Test
2464+
public void testListBackupOfferingsNormalUserDefaultsToDomainFiltering() {
2465+
Long userDomainId = 7L;
2466+
2467+
ListBackupOfferingsCmd cmd =
2468+
Mockito.mock(ListBackupOfferingsCmd.class);
2469+
when(cmd.getOfferingId()).thenReturn(null);
2470+
when(cmd.getDomainId()).thenReturn(null); // User didn't pass domain filter
2471+
when(cmd.getStartIndex()).thenReturn(0L);
2472+
when(cmd.getPageSizeVal()).thenReturn(20L);
2473+
2474+
BackupOfferingVO globalOffering = createMockOffering(1L, "Global Offering");
2475+
BackupOfferingVO userDomainOffering = createMockOffering(2L, "User Domain Offering");
2476+
BackupOfferingVO otherDomainOffering = createMockOffering(3L, "Other Domain Offering");
2477+
2478+
List<BackupOfferingVO> allOfferings = List.of(globalOffering, userDomainOffering, otherDomainOffering);
2479+
2480+
SearchBuilder<BackupOfferingVO> sb = Mockito.mock(SearchBuilder.class);
2481+
SearchCriteria<BackupOfferingVO> sc = Mockito.mock(SearchCriteria.class);
2482+
BackupOfferingVO entityMock = Mockito.mock(BackupOfferingVO.class);
2483+
when(backupOfferingDao.createSearchBuilder()).thenReturn(sb);
2484+
when(sb.entity()).thenReturn(entityMock);
2485+
when(sb.and(Mockito.anyString(), Mockito.any(), Mockito.any(SearchCriteria.Op.class))).thenReturn(sb);
2486+
when(sb.create()).thenReturn(sc);
2487+
when(backupOfferingDao.searchAndCount(Mockito.any(), Mockito.any()))
2488+
.thenReturn(new Pair<>(allOfferings, allOfferings.size()));
2489+
2490+
when(backupOfferingDetailsDao.findDomainIds(1L)).thenReturn(Collections.emptyList()); // Global
2491+
when(backupOfferingDetailsDao.findDomainIds(2L)).thenReturn(List.of(userDomainId)); // User's domain
2492+
when(backupOfferingDetailsDao.findDomainIds(3L)).thenReturn(List.of(99L)); // Other domain
2493+
2494+
when(domainDao.isChildDomain(99L, userDomainId)).thenReturn(false);
2495+
2496+
Account account = Mockito.mock(Account.class);
2497+
when(account.getType()).thenReturn(Account.Type.NORMAL);
2498+
when(account.getDomainId()).thenReturn(userDomainId);
2499+
2500+
try (MockedStatic<CallContext> mockedCallContext = Mockito.mockStatic(CallContext.class)) {
2501+
CallContext contextMock = Mockito.mock(CallContext.class);
2502+
mockedCallContext.when(CallContext::current).thenReturn(contextMock);
2503+
when(contextMock.getCallingAccount()).thenReturn(account);
2504+
2505+
Pair<List<BackupOffering>, Integer> result = backupManager.listBackupOfferings(cmd);
2506+
2507+
assertEquals(2, result.first().size());
2508+
assertTrue(result.first().stream().anyMatch(o -> o.getName().equals("Global Offering")));
2509+
assertTrue(result.first().stream().anyMatch(o -> o.getName().equals("User Domain Offering")));
2510+
}
2511+
}
2512+
2513+
private BackupOfferingVO createMockOffering(Long id, String name) {
2514+
BackupOfferingVO offering = Mockito.mock(BackupOfferingVO.class);
2515+
when(offering.getId()).thenReturn(id);
2516+
when(offering.getName()).thenReturn(name);
2517+
return offering;
2518+
}
2519+
22352520
}

0 commit comments

Comments
 (0)