|
43 | 43 | import org.apache.cloudstack.api.command.user.bootgroup.DeleteInstanceBootGroupReadinessRuleCmd; |
44 | 44 | import org.apache.cloudstack.api.command.user.bootgroup.ListInstanceBootGroupMembersCmd; |
45 | 45 | import org.apache.cloudstack.api.command.user.bootgroup.ListInstanceBootGroupReadinessRulesCmd; |
| 46 | +import org.apache.cloudstack.api.command.user.bootgroup.ListInstanceBootGroupsCmd; |
46 | 47 | import org.apache.cloudstack.api.command.user.bootgroup.RebootInstanceBootGroupCmd; |
47 | 48 | import org.apache.cloudstack.api.command.user.bootgroup.RemoveInstanceBootGroupMemberCmd; |
48 | 49 | import org.apache.cloudstack.api.command.user.bootgroup.StartInstanceBootGroupCmd; |
|
51 | 52 | import org.apache.cloudstack.api.command.user.bootgroup.UpdateInstanceBootGroupMemberCmd; |
52 | 53 | import org.apache.cloudstack.api.command.user.bootgroup.UpdateInstanceBootGroupReadinessRuleCmd; |
53 | 54 | import org.apache.cloudstack.api.query.dao.InstanceBootGroupJoinDao; |
| 55 | +import org.apache.cloudstack.api.query.vo.InstanceBootGroupJoinVO; |
54 | 56 | import org.apache.cloudstack.api.response.InstanceBootGroupMemberChildResponse; |
55 | 57 | import org.apache.cloudstack.api.response.InstanceBootGroupMemberResponse; |
56 | 58 | import org.apache.cloudstack.api.response.InstanceBootGroupReadinessRuleResponse; |
| 59 | +import org.apache.cloudstack.api.response.InstanceBootGroupResponse; |
57 | 60 | import org.apache.cloudstack.api.response.ListResponse; |
58 | 61 | import org.apache.cloudstack.context.CallContext; |
59 | 62 | import org.apache.cloudstack.vm.bootgroup.readiness.InstanceBootGroupReadinessRule; |
|
75 | 78 | import com.cloud.hypervisor.Hypervisor.HypervisorType; |
76 | 79 | import com.cloud.user.Account; |
77 | 80 | import com.cloud.user.AccountManager; |
| 81 | +import com.cloud.utils.db.SearchBuilder; |
| 82 | +import com.cloud.utils.db.SearchCriteria; |
78 | 83 | import com.cloud.utils.db.Transaction; |
79 | 84 | import com.cloud.utils.db.TransactionCallback; |
80 | 85 | import com.cloud.vm.InstanceGroupVMMapVO; |
@@ -374,6 +379,74 @@ public void testGetGroupAndCheckAccessSuccessDelegatesToAccountManager() { |
374 | 379 | verify(accountManager).checkAccess(callerMock, null, true, group); |
375 | 380 | } |
376 | 381 |
|
| 382 | + // ---------------------------------------------------------------- listInstanceBootGroups |
| 383 | + |
| 384 | + private ListInstanceBootGroupsCmd baseListGroupsCmd(Long virtualMachineId, Long instanceGroupId) { |
| 385 | + ListInstanceBootGroupsCmd cmd = mock(ListInstanceBootGroupsCmd.class); |
| 386 | + when(cmd.getVirtualMachineId()).thenReturn(virtualMachineId); |
| 387 | + when(cmd.getInstanceGroupId()).thenReturn(instanceGroupId); |
| 388 | + return cmd; |
| 389 | + } |
| 390 | + |
| 391 | + private SearchBuilder<InstanceBootGroupJoinVO> mockSearchBuilder() { |
| 392 | + @SuppressWarnings("unchecked") |
| 393 | + SearchBuilder<InstanceBootGroupJoinVO> sb = mock(SearchBuilder.class); |
| 394 | + @SuppressWarnings("unchecked") |
| 395 | + SearchCriteria<InstanceBootGroupJoinVO> sc = mock(SearchCriteria.class); |
| 396 | + when(sb.entity()).thenReturn(new InstanceBootGroupJoinVO()); |
| 397 | + when(sb.create()).thenReturn(sc); |
| 398 | + when(instanceBootGroupJoinDao.createSearchBuilder()).thenReturn(sb); |
| 399 | + return sb; |
| 400 | + } |
| 401 | + |
| 402 | + @Test(expected = InvalidParameterValueException.class) |
| 403 | + public void testListInstanceBootGroupsBothVmAndInstanceGroupIdThrows() { |
| 404 | + ListInstanceBootGroupsCmd cmd = baseListGroupsCmd(VM_ID, INSTANCE_GROUP_ID); |
| 405 | + service.listInstanceBootGroups(cmd); |
| 406 | + } |
| 407 | + |
| 408 | + @Test |
| 409 | + public void testListInstanceBootGroupsByVirtualMachineIdNotAMemberReturnsEmpty() { |
| 410 | + ListInstanceBootGroupsCmd cmd = baseListGroupsCmd(VM_ID, null); |
| 411 | + when(instanceBootGroupMemberDao.findByMember(InstanceBootGroupMember.MemberType.VirtualMachine, VM_ID)).thenReturn(null); |
| 412 | + |
| 413 | + ListResponse<InstanceBootGroupResponse> response = service.listInstanceBootGroups(cmd); |
| 414 | + |
| 415 | + assertEquals(0, response.getCount().intValue()); |
| 416 | + assertTrue(response.getResponses().isEmpty()); |
| 417 | + verify(instanceBootGroupJoinDao, never()).createSearchBuilder(); |
| 418 | + } |
| 419 | + |
| 420 | + @Test |
| 421 | + public void testListInstanceBootGroupsByVirtualMachineIdFiltersToMemberBootGroup() { |
| 422 | + ListInstanceBootGroupsCmd cmd = baseListGroupsCmd(VM_ID, null); |
| 423 | + InstanceBootGroupMemberVO member = newMember(MEMBER_ID, GROUP_ID, InstanceBootGroupMember.MemberType.VirtualMachine, VM_ID, 0); |
| 424 | + when(instanceBootGroupMemberDao.findByMember(InstanceBootGroupMember.MemberType.VirtualMachine, VM_ID)).thenReturn(member); |
| 425 | + |
| 426 | + SearchBuilder<InstanceBootGroupJoinVO> sb = mockSearchBuilder(); |
| 427 | + InstanceBootGroupJoinVO joinVO = new InstanceBootGroupJoinVO(); |
| 428 | + ReflectionTestUtils.setField(joinVO, "id", GROUP_ID); |
| 429 | + ReflectionTestUtils.setField(joinVO, "name", "group1"); |
| 430 | + when(instanceBootGroupJoinDao.searchAndCount(any(), any())) |
| 431 | + .thenReturn(new com.cloud.utils.Pair<>(Collections.singletonList(joinVO), 1)); |
| 432 | + |
| 433 | + ListResponse<InstanceBootGroupResponse> response = service.listInstanceBootGroups(cmd); |
| 434 | + |
| 435 | + assertEquals(1, response.getResponses().size()); |
| 436 | + verify(sb.create()).setParameters("memberBootGroupId", GROUP_ID); |
| 437 | + } |
| 438 | + |
| 439 | + @Test |
| 440 | + public void testListInstanceBootGroupsByInstanceGroupIdNotAMemberReturnsEmpty() { |
| 441 | + ListInstanceBootGroupsCmd cmd = baseListGroupsCmd(null, INSTANCE_GROUP_ID); |
| 442 | + when(instanceBootGroupMemberDao.findByMember(InstanceBootGroupMember.MemberType.InstanceGroup, INSTANCE_GROUP_ID)).thenReturn(null); |
| 443 | + |
| 444 | + ListResponse<InstanceBootGroupResponse> response = service.listInstanceBootGroups(cmd); |
| 445 | + |
| 446 | + assertEquals(0, response.getCount().intValue()); |
| 447 | + verify(instanceBootGroupJoinDao, never()).createSearchBuilder(); |
| 448 | + } |
| 449 | + |
377 | 450 | // ---------------------------------------------------------------- addMemberToInstanceBootGroup |
378 | 451 |
|
379 | 452 | @Test |
|
0 commit comments