|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.api.query.dao; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertSame; |
| 20 | +import static org.mockito.AdditionalMatchers.aryEq; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.Mockito.doReturn; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.never; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | + |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.mockito.InjectMocks; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.Spy; |
| 39 | +import org.mockito.junit.MockitoJUnitRunner; |
| 40 | + |
| 41 | +import com.cloud.api.query.vo.StoragePoolJoinVO; |
| 42 | +import com.cloud.hypervisor.Hypervisor; |
| 43 | +import com.cloud.storage.Storage; |
| 44 | +import com.cloud.utils.db.Filter; |
| 45 | +import com.cloud.utils.db.SearchBuilder; |
| 46 | +import com.cloud.utils.db.SearchCriteria; |
| 47 | + |
| 48 | +@RunWith(MockitoJUnitRunner.class) |
| 49 | +public class StoragePoolJoinDaoImplTest { |
| 50 | + |
| 51 | + @Spy |
| 52 | + @InjectMocks |
| 53 | + private StoragePoolJoinDaoImpl storagePoolJoinDao = new StoragePoolJoinDaoImpl(); |
| 54 | + |
| 55 | + @Mock |
| 56 | + private SearchCriteria<StoragePoolJoinVO> searchCriteria; |
| 57 | + |
| 58 | + @Before |
| 59 | + @SuppressWarnings("unchecked") |
| 60 | + public void setUp() { |
| 61 | + StoragePoolJoinVO storagePoolJoinVO = mock(StoragePoolJoinVO.class); |
| 62 | + SearchBuilder<StoragePoolJoinVO> searchBuilder = mock(SearchBuilder.class); |
| 63 | + when(searchBuilder.entity()).thenReturn(storagePoolJoinVO); |
| 64 | + when(searchBuilder.create()).thenReturn(searchCriteria); |
| 65 | + doReturn(searchBuilder).when(storagePoolJoinDao).createSearchBuilder(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void listByZoneHypervisorAndTypeReturnsMatchingPoolsWhenTypesAreProvided() { |
| 70 | + long zoneId = 42L; |
| 71 | + Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.KVM; |
| 72 | + List<Storage.StoragePoolType> types = Arrays.asList(Storage.StoragePoolType.NetworkFilesystem, Storage.StoragePoolType.Filesystem); |
| 73 | + Filter filter = mock(Filter.class); |
| 74 | + List<StoragePoolJoinVO> expectedPools = Collections.singletonList(mock(StoragePoolJoinVO.class)); |
| 75 | + |
| 76 | + doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, filter); |
| 77 | + |
| 78 | + List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, types, filter); |
| 79 | + |
| 80 | + assertSame(expectedPools, result); |
| 81 | + verify(searchCriteria).setParameters("zoneId", zoneId); |
| 82 | + verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType})); |
| 83 | + verify(searchCriteria).setParameters(eq("types"), aryEq(types.toArray())); |
| 84 | + verify(storagePoolJoinDao).listBy(searchCriteria, filter); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void listByZoneHypervisorAndTypeSkipsTypeFilterWhenTypesAreNull() { |
| 89 | + long zoneId = 7L; |
| 90 | + Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.VMware; |
| 91 | + Filter filter = mock(Filter.class); |
| 92 | + List<StoragePoolJoinVO> expectedPools = Collections.emptyList(); |
| 93 | + |
| 94 | + doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, filter); |
| 95 | + |
| 96 | + List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, null, filter); |
| 97 | + |
| 98 | + assertSame(expectedPools, result); |
| 99 | + verify(searchCriteria).setParameters("zoneId", zoneId); |
| 100 | + verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType})); |
| 101 | + verify(searchCriteria, never()).setParameters(eq("types"), any(Object[].class)); |
| 102 | + verify(storagePoolJoinDao).listBy(searchCriteria, filter); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void listByZoneHypervisorAndTypeSkipsTypeFilterForEmptyTypesAndPassesNullFilter() { |
| 107 | + long zoneId = 9L; |
| 108 | + Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.XenServer; |
| 109 | + List<StoragePoolJoinVO> expectedPools = Collections.singletonList(mock(StoragePoolJoinVO.class)); |
| 110 | + |
| 111 | + doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, null); |
| 112 | + |
| 113 | + List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, Collections.emptyList(), null); |
| 114 | + |
| 115 | + assertSame(expectedPools, result); |
| 116 | + verify(searchCriteria).setParameters("zoneId", zoneId); |
| 117 | + verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType})); |
| 118 | + verify(searchCriteria, never()).setParameters(eq("types"), any(Object[].class)); |
| 119 | + verify(storagePoolJoinDao).listBy(searchCriteria, null); |
| 120 | + } |
| 121 | +} |
0 commit comments