forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 3
/
StoragePoolTagsDaoImplTest.java
executable file
·128 lines (106 loc) · 5.75 KB
/
StoragePoolTagsDaoImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.storage.dao;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import com.cloud.storage.StoragePoolTagVO;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.doNothing;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
@RunWith(MockitoJUnitRunner.class)
public class StoragePoolTagsDaoImplTest extends TestCase {
@Mock
ConfigurationDao _configDao;
@Mock
SearchBuilder<StoragePoolTagVO> StoragePoolIdsSearch;
@Spy
@InjectMocks
private StoragePoolTagsDaoImpl _storagePoolTagsDaoImpl = new StoragePoolTagsDaoImpl();
@Mock
StoragePoolTagVO storagePoolTag1;
@Mock
StoragePoolTagVO storagePoolTag2;
private final String batchSizeConfigurationKey = "detail.batch.query.size";
private final String batchSizeValue = "2800";
private final String batchSizeDefaultValue = "2000";
private final String batchSizeLow = "2";
private final Long[] storageTagsIds = {1l,2l,3l,4l,5l};
private final List<StoragePoolTagVO> storagePoolTagList = Arrays.asList(storagePoolTag1, storagePoolTag2);
@Before
public void setup() {
when(_configDao.getValue(batchSizeConfigurationKey)).thenReturn(batchSizeValue);
doReturn(storagePoolTagList).when(_storagePoolTagsDaoImpl).searchIncludingRemoved(
Matchers.any(SearchCriteria.class), Matchers.isNull(Filter.class), Matchers.isNull(Boolean.class), Matchers.eq(false));
}
@Test
public void testGetDetailsBatchSizeNotNull() {
assertEquals(Integer.parseInt(batchSizeValue), _storagePoolTagsDaoImpl.getDetailsBatchSize());
}
@Test
public void testGetDetailsBatchSizeNull() {
when(_configDao.getValue(batchSizeConfigurationKey)).thenReturn(null);
assertEquals(Integer.parseInt(batchSizeDefaultValue), _storagePoolTagsDaoImpl.getDetailsBatchSize());
}
@Test
public void testSearchForStoragePoolIdsInternalStorageTagsNotNullSearch() {
List<StoragePoolTagVO> storagePoolTags = new ArrayList<StoragePoolTagVO>();
_storagePoolTagsDaoImpl.searchForStoragePoolIdsInternal(0, storageTagsIds.length, storageTagsIds, storagePoolTags);
verify(_storagePoolTagsDaoImpl).searchIncludingRemoved(Matchers.any(SearchCriteria.class), Matchers.isNull(Filter.class), Matchers.isNull(Boolean.class), Matchers.eq(false));
assertEquals(2, storagePoolTags.size());
}
@Test
public void testSearchForStoragePoolIdsInternalStorageTagsNullSearch() {
List<StoragePoolTagVO> storagePoolTags = new ArrayList<StoragePoolTagVO>();
doReturn(null).when(_storagePoolTagsDaoImpl).searchIncludingRemoved(
Matchers.any(SearchCriteria.class), Matchers.isNull(Filter.class), Matchers.isNull(Boolean.class), Matchers.eq(false));
_storagePoolTagsDaoImpl.searchForStoragePoolIdsInternal(0, storageTagsIds.length, storageTagsIds, storagePoolTags);
verify(_storagePoolTagsDaoImpl).searchIncludingRemoved(Matchers.any(SearchCriteria.class), Matchers.isNull(Filter.class), Matchers.isNull(Boolean.class), Matchers.eq(false));
assertEquals(0, storagePoolTags.size());
}
@Test
public void testSearchByIdsStorageTagsIdsGreaterOrEqualThanBatchSize() {
when(_configDao.getValue(batchSizeConfigurationKey)).thenReturn(batchSizeLow);
doNothing().when(_storagePoolTagsDaoImpl).searchForStoragePoolIdsInternal(Matchers.anyInt(), Matchers.anyInt(), Matchers.any(Long[].class), Matchers.anyList());
_storagePoolTagsDaoImpl.searchByIds(storageTagsIds);
int batchSize = Integer.parseInt(batchSizeLow);
int difference = storageTagsIds.length - 2 * batchSize;
verify(_storagePoolTagsDaoImpl, Mockito.times(2)).searchForStoragePoolIdsInternal(Matchers.anyInt(), Matchers.eq(batchSize), Matchers.any(Long[].class), Matchers.anyList());
verify(_storagePoolTagsDaoImpl).searchForStoragePoolIdsInternal(Matchers.eq(2 * batchSize), Matchers.eq(difference), Matchers.any(Long[].class), Matchers.anyList());
}
@Test
public void testSearchByIdsStorageTagsIdsLowerThanBatchSize() {
doNothing().when(_storagePoolTagsDaoImpl).searchForStoragePoolIdsInternal(Matchers.anyInt(), Matchers.anyInt(), Matchers.any(Long[].class), Matchers.anyList());
_storagePoolTagsDaoImpl.searchByIds(storageTagsIds);
verify(_storagePoolTagsDaoImpl).searchForStoragePoolIdsInternal(Matchers.eq(0), Matchers.eq(storageTagsIds.length), Matchers.any(Long[].class), Matchers.anyList());
}
}