|
17 | 17 |
|
18 | 18 | package com.cloud.api.query; |
19 | 19 |
|
| 20 | +import com.cloud.api.ApiDBUtils; |
20 | 21 | import com.cloud.api.query.dao.TemplateJoinDao; |
| 22 | +import com.cloud.api.query.dao.UserAccountJoinDao; |
21 | 23 | import com.cloud.api.query.dao.UserVmJoinDao; |
22 | 24 | import com.cloud.api.query.vo.EventJoinVO; |
23 | 25 | import com.cloud.api.query.vo.TemplateJoinVO; |
| 26 | +import com.cloud.api.query.vo.UserAccountJoinVO; |
24 | 27 | import com.cloud.api.query.vo.UserVmJoinVO; |
25 | 28 | import com.cloud.dc.ClusterVO; |
26 | 29 | import com.cloud.dc.dao.ClusterDao; |
| 30 | +import com.cloud.domain.DomainVO; |
| 31 | +import com.cloud.domain.dao.DomainDao; |
27 | 32 | import com.cloud.event.EventVO; |
28 | 33 | import com.cloud.event.dao.EventDao; |
29 | 34 | import com.cloud.event.dao.EventJoinDao; |
|
45 | 50 | import com.cloud.user.AccountVO; |
46 | 51 | import com.cloud.user.User; |
47 | 52 | import com.cloud.user.UserVO; |
| 53 | +import com.cloud.user.dao.AccountDao; |
48 | 54 | import com.cloud.utils.Pair; |
49 | 55 | import com.cloud.utils.db.EntityManager; |
50 | 56 | import com.cloud.utils.db.Filter; |
|
57 | 63 | import org.apache.cloudstack.acl.SecurityChecker; |
58 | 64 | import org.apache.cloudstack.api.ApiCommandResourceType; |
59 | 65 | import org.apache.cloudstack.api.command.admin.storage.ListObjectStoragePoolsCmd; |
| 66 | +import org.apache.cloudstack.api.command.admin.user.ListUsersCmd; |
60 | 67 | import org.apache.cloudstack.api.command.admin.vm.ListAffectedVmsForStorageScopeChangeCmd; |
| 68 | +import org.apache.cloudstack.api.command.user.account.ListAccountsCmd; |
61 | 69 | import org.apache.cloudstack.api.command.user.bucket.ListBucketsCmd; |
62 | 70 | import org.apache.cloudstack.api.command.user.event.ListEventsCmd; |
63 | 71 | import org.apache.cloudstack.api.command.user.resource.ListDetailOptionsCmd; |
64 | 72 | import org.apache.cloudstack.api.response.DetailOptionsResponse; |
65 | 73 | import org.apache.cloudstack.api.response.EventResponse; |
66 | 74 | import org.apache.cloudstack.api.response.ListResponse; |
67 | 75 | import org.apache.cloudstack.api.response.ObjectStoreResponse; |
| 76 | +import org.apache.cloudstack.api.response.UserResponse; |
68 | 77 | import org.apache.cloudstack.api.response.VirtualMachineResponse; |
69 | 78 | import org.apache.cloudstack.context.CallContext; |
70 | 79 | import org.apache.cloudstack.storage.datastore.db.ObjectStoreDao; |
@@ -150,6 +159,15 @@ public class QueryManagerImplTest { |
150 | 159 | @Mock |
151 | 160 | UserVmJoinDao userVmJoinDao; |
152 | 161 |
|
| 162 | + @Mock |
| 163 | + UserAccountJoinDao userAccountJoinDao; |
| 164 | + |
| 165 | + @Mock |
| 166 | + DomainDao domainDao; |
| 167 | + |
| 168 | + @Mock |
| 169 | + AccountDao accountDao; |
| 170 | + |
153 | 171 | private AccountVO account; |
154 | 172 | private UserVO user; |
155 | 173 |
|
@@ -477,4 +495,79 @@ public void testListAffectedVmsForScopeChange() { |
477 | 495 | Assert.assertEquals(response.getResponses().get(0).getId(), instanceUuid); |
478 | 496 | Assert.assertEquals(response.getResponses().get(0).getName(), vmName); |
479 | 497 | } |
| 498 | + |
| 499 | + @Test |
| 500 | + public void testSearchForUsers() { |
| 501 | + ListUsersCmd cmd = Mockito.mock(ListUsersCmd.class); |
| 502 | + String username = "Admin"; |
| 503 | + String accountName = "Admin"; |
| 504 | + Account.Type accountType = Account.Type.ADMIN; |
| 505 | + Long domainId = 1L; |
| 506 | + String apiKeyAccess = "Disabled"; |
| 507 | + Mockito.when(cmd.getUsername()).thenReturn(username); |
| 508 | + Mockito.when(cmd.getAccountName()).thenReturn(accountName); |
| 509 | + Mockito.when(cmd.getAccountType()).thenReturn(accountType); |
| 510 | + Mockito.when(cmd.getDomainId()).thenReturn(domainId); |
| 511 | + Mockito.when(cmd.getApiKeyAccess()).thenReturn(apiKeyAccess); |
| 512 | + |
| 513 | + UserAccountJoinVO user = new UserAccountJoinVO(); |
| 514 | + DomainVO domain = Mockito.mock(DomainVO.class); |
| 515 | + SearchBuilder<UserAccountJoinVO> sb = Mockito.mock(SearchBuilder.class); |
| 516 | + SearchCriteria<UserAccountJoinVO> sc = Mockito.mock(SearchCriteria.class); |
| 517 | + List<UserAccountJoinVO> users = new ArrayList<>(); |
| 518 | + Pair<List<UserAccountJoinVO>, Integer> result = new Pair<>(users, 0); |
| 519 | + UserResponse response = Mockito.mock(UserResponse.class); |
| 520 | + |
| 521 | + Mockito.when(userAccountJoinDao.createSearchBuilder()).thenReturn(sb); |
| 522 | + Mockito.when(sb.entity()).thenReturn(user); |
| 523 | + Mockito.when(sb.create()).thenReturn(sc); |
| 524 | + Mockito.when(userAccountJoinDao.searchAndCount(any(SearchCriteria.class), any(Filter.class))).thenReturn(result); |
| 525 | + |
| 526 | + queryManager.searchForUsers(cmd); |
| 527 | + |
| 528 | + Mockito.verify(sc).setParameters("username", username); |
| 529 | + Mockito.verify(sc).setParameters("accountName", accountName); |
| 530 | + Mockito.verify(sc).setParameters("type", accountType); |
| 531 | + Mockito.verify(sc).setParameters("domainId", domainId); |
| 532 | + Mockito.verify(sc).setParameters("apiKeyAccess", false); |
| 533 | + Mockito.verify(userAccountJoinDao, Mockito.times(1)).searchAndCount( |
| 534 | + any(SearchCriteria.class), any(Filter.class)); |
| 535 | + } |
| 536 | + |
| 537 | + @Test |
| 538 | + public void testSearchForAccounts() { |
| 539 | + ListAccountsCmd cmd = Mockito.mock(ListAccountsCmd.class); |
| 540 | + Long domainId = 1L; |
| 541 | + String accountName = "Admin"; |
| 542 | + Account.Type accountType = Account.Type.ADMIN; |
| 543 | + String apiKeyAccess = "Enabled"; |
| 544 | + Mockito.when(cmd.getId()).thenReturn(null); |
| 545 | + Mockito.when(cmd.getDomainId()).thenReturn(domainId); |
| 546 | + Mockito.when(cmd.getSearchName()).thenReturn(accountName); |
| 547 | + Mockito.when(cmd.getAccountType()).thenReturn(accountType); |
| 548 | + Mockito.when(cmd.getApiKeyAccess()).thenReturn(apiKeyAccess); |
| 549 | + |
| 550 | + DomainVO domain = Mockito.mock(DomainVO.class); |
| 551 | + SearchBuilder<AccountVO> sb = Mockito.mock(SearchBuilder.class); |
| 552 | + SearchCriteria<AccountVO> sc = Mockito.mock(SearchCriteria.class); |
| 553 | + Pair<List<AccountVO>, Integer> uniqueAccountPair = new Pair<>(new ArrayList<>(), 0); |
| 554 | + Mockito.when(domainDao.findById(domainId)).thenReturn(domain); |
| 555 | + Mockito.doNothing().when(accountManager).checkAccess(account, domain); |
| 556 | + |
| 557 | + Mockito.when(accountDao.createSearchBuilder()).thenReturn(sb); |
| 558 | + Mockito.when(sb.entity()).thenReturn(account); |
| 559 | + Mockito.when(sb.create()).thenReturn(sc); |
| 560 | + Mockito.when(accountDao.searchAndCount(any(SearchCriteria.class), any(Filter.class))).thenReturn(uniqueAccountPair); |
| 561 | + |
| 562 | + try (MockedStatic<ApiDBUtils> apiDBUtilsMocked = Mockito.mockStatic(ApiDBUtils.class)) { |
| 563 | + queryManager.searchForAccounts(cmd); |
| 564 | + } |
| 565 | + |
| 566 | + Mockito.verify(sc).setParameters("domainId", domainId); |
| 567 | + Mockito.verify(sc).setParameters("accountName", accountName); |
| 568 | + Mockito.verify(sc).setParameters("type", accountType); |
| 569 | + Mockito.verify(sc).setParameters("apiKeyAccess", true); |
| 570 | + Mockito.verify(accountDao, Mockito.times(1)).searchAndCount( |
| 571 | + any(SearchCriteria.class), any(Filter.class)); |
| 572 | + } |
480 | 573 | } |
0 commit comments