|
18 | 18 | package com.cloud.api.query; |
19 | 19 |
|
20 | 20 | import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertTrue; |
21 | 22 | import static org.mockito.ArgumentMatchers.any; |
22 | 23 | import static org.mockito.Mockito.mock; |
23 | 24 | import static org.mockito.Mockito.verify; |
|
33 | 34 | import java.util.UUID; |
34 | 35 | import java.util.stream.Collectors; |
35 | 36 |
|
| 37 | +import com.cloud.host.dao.HostTagsDao; |
36 | 38 | import org.apache.cloudstack.acl.SecurityChecker; |
37 | 39 | import org.apache.cloudstack.api.ApiCommandResourceType; |
38 | 40 | import org.apache.cloudstack.api.ResponseObject; |
@@ -156,6 +158,9 @@ public class QueryManagerImplTest { |
156 | 158 | @Mock |
157 | 159 | HostDao hostDao; |
158 | 160 |
|
| 161 | + @Mock |
| 162 | + HostTagsDao hostTagsDao; |
| 163 | + |
159 | 164 | @Mock |
160 | 165 | ClusterDao clusterDao; |
161 | 166 |
|
@@ -622,4 +627,39 @@ public void updateHostsExtensions_withHostResponses_setsExtension() { |
622 | 627 | verify(host1).setExtensionId("a"); |
623 | 628 | verify(host2).setExtensionId("b"); |
624 | 629 | } |
| 630 | + |
| 631 | + @Test |
| 632 | + public void testAddVmCurrentClusterHostTags() { |
| 633 | + String tag1 = "tag1"; |
| 634 | + String tag2 = "tag2"; |
| 635 | + VMInstanceVO vmInstance = mock(VMInstanceVO.class); |
| 636 | + HostVO host = mock(HostVO.class); |
| 637 | + when(vmInstance.getHostId()).thenReturn(null); |
| 638 | + when(vmInstance.getLastHostId()).thenReturn(1L); |
| 639 | + when(hostDao.findById(1L)).thenReturn(host); |
| 640 | + when(host.getClusterId()).thenReturn(1L); |
| 641 | + when(hostTagsDao.listByClusterId(1L)).thenReturn(Arrays.asList(tag1, tag2)); |
| 642 | + |
| 643 | + List<String> hostTags = new ArrayList<>(Collections.singleton(tag1)); |
| 644 | + queryManagerImplSpy.addVmCurrentClusterHostTags(vmInstance, hostTags); |
| 645 | + assertEquals(2, hostTags.size()); |
| 646 | + assertTrue(hostTags.contains(tag2)); |
| 647 | + } |
| 648 | + |
| 649 | + @Test |
| 650 | + public void testAddVmCurrentClusterHostTagsEmptyHostTagsInCluster() { |
| 651 | + String tag1 = "tag1"; |
| 652 | + VMInstanceVO vmInstance = mock(VMInstanceVO.class); |
| 653 | + HostVO host = mock(HostVO.class); |
| 654 | + when(vmInstance.getHostId()).thenReturn(null); |
| 655 | + when(vmInstance.getLastHostId()).thenReturn(1L); |
| 656 | + when(hostDao.findById(1L)).thenReturn(host); |
| 657 | + when(host.getClusterId()).thenReturn(1L); |
| 658 | + when(hostTagsDao.listByClusterId(1L)).thenReturn(null); |
| 659 | + |
| 660 | + List<String> hostTags = new ArrayList<>(Collections.singleton(tag1)); |
| 661 | + queryManagerImplSpy.addVmCurrentClusterHostTags(vmInstance, hostTags); |
| 662 | + assertEquals(1, hostTags.size()); |
| 663 | + assertTrue(hostTags.contains(tag1)); |
| 664 | + } |
625 | 665 | } |
0 commit comments