1616// under the License.
1717package com .cloud .capacity .dao ;
1818
19+ import com .cloud .capacity .CapacityVO ;
20+ import com .cloud .host .Host ;
21+ import com .cloud .utils .Pair ;
22+ import com .cloud .utils .Ternary ;
23+ import com .cloud .utils .db .SearchBuilder ;
24+ import com .cloud .utils .db .SearchCriteria ;
25+ import com .cloud .utils .db .TransactionLegacy ;
26+ import org .junit .After ;
27+ import org .junit .Before ;
28+ import org .junit .Test ;
29+ import org .junit .runner .RunWith ;
30+ import org .mockito .InjectMocks ;
31+ import org .mockito .Mock ;
32+ import org .mockito .MockedStatic ;
33+ import org .mockito .Mockito ;
34+ import org .mockito .Spy ;
35+ import org .mockito .junit .MockitoJUnitRunner ;
36+
37+ import java .sql .PreparedStatement ;
38+ import java .sql .ResultSet ;
39+ import java .util .Arrays ;
40+ import java .util .Collections ;
41+ import java .util .List ;
42+ import java .util .Map ;
43+
1944import static org .junit .Assert .assertEquals ;
45+ import static org .junit .Assert .assertNotNull ;
2046import static org .junit .Assert .assertSame ;
2147import static org .junit .Assert .assertTrue ;
48+ import static org .mockito .ArgumentMatchers .anyString ;
2249import static org .mockito .Mockito .any ;
50+ import static org .mockito .Mockito .doNothing ;
2351import static org .mockito .Mockito .doReturn ;
2452import static org .mockito .Mockito .eq ;
2553import static org .mockito .Mockito .mock ;
54+ import static org .mockito .Mockito .times ;
2655import static org .mockito .Mockito .verify ;
2756import static org .mockito .Mockito .when ;
2857
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 .Mockito ;
38- import org .mockito .Spy ;
39- import org .mockito .junit .MockitoJUnitRunner ;
40-
41- import com .cloud .capacity .CapacityVO ;
42- import com .cloud .utils .db .SearchBuilder ;
43- import com .cloud .utils .db .SearchCriteria ;
44-
4558@ RunWith (MockitoJUnitRunner .class )
4659public class CapacityDaoImplTest {
4760 @ Spy
4861 @ InjectMocks
4962 CapacityDaoImpl capacityDao = new CapacityDaoImpl ();
5063
64+ @ Mock
65+ private TransactionLegacy txn ;
66+ @ Mock
67+ private PreparedStatement pstmt ;
68+ @ Mock
69+ private ResultSet resultSet ;
70+ private MockedStatic <TransactionLegacy > mockedTransactionLegacy ;
71+
5172 private SearchBuilder <CapacityVO > searchBuilder ;
5273 private SearchCriteria <CapacityVO > searchCriteria ;
5374
@@ -59,6 +80,16 @@ public void setUp() {
5980 searchCriteria = mock (SearchCriteria .class );
6081 doReturn (searchBuilder ).when (capacityDao ).createSearchBuilder ();
6182 when (searchBuilder .create ()).thenReturn (searchCriteria );
83+
84+ mockedTransactionLegacy = Mockito .mockStatic (TransactionLegacy .class );
85+ mockedTransactionLegacy .when (TransactionLegacy ::currentTxn ).thenReturn (txn );
86+ }
87+
88+ @ After
89+ public void tearDown () {
90+ if (mockedTransactionLegacy != null ) {
91+ mockedTransactionLegacy .close ();
92+ }
6293 }
6394
6495 @ Test
@@ -96,4 +127,207 @@ public void testListByHostIdTypesEmptyResult() {
96127 verify (capacityDao ).listBy (searchCriteria );
97128 assertTrue (result .isEmpty ());
98129 }
130+
131+ @ Test
132+ public void testListClustersCrossingThresholdEmptyResult () throws Exception {
133+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
134+ when (pstmt .executeQuery ()).thenReturn (resultSet );
135+ when (resultSet .next ()).thenReturn (false );
136+ List <Long > result = capacityDao .listClustersCrossingThreshold ((short )1 , 1L , "cpu.threshold" , 5000L );
137+ assertNotNull (result );
138+ assertTrue (result .isEmpty ());
139+ }
140+
141+ @ Test
142+ public void testFindCapacityByZoneAndHostTagNoResults () throws Exception {
143+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
144+ when (pstmt .executeQuery ()).thenReturn (resultSet );
145+ when (resultSet .next ()).thenReturn (false );
146+
147+ Ternary <Long , Long , Long > result = capacityDao .findCapacityByZoneAndHostTag (1L , "host-tag" );
148+ assertNotNull (result );
149+ assertEquals (Long .valueOf (0L ), result .first ());
150+ assertEquals (Long .valueOf (0L ), result .second ());
151+ assertEquals (Long .valueOf (0L ), result .third ());
152+ }
153+ @ Test
154+ public void testFindByHostIdType () {
155+ CapacityVO capacity = new CapacityVO ();
156+ capacity .setHostId (1L );
157+ capacity .setCapacityType ((short ) 1 );
158+
159+ doReturn (capacity ).when (capacityDao ).findOneBy (any ());
160+
161+ CapacityVO found = capacityDao .findByHostIdType (1L , (short ) 1 );
162+ assertNotNull (found );
163+ assertEquals (Long .valueOf (1L ), found .getHostOrPoolId ());
164+ }
165+
166+ @ Test
167+ public void testUpdateAllocatedAddition () throws Exception {
168+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
169+ doNothing ().when (txn ).start ();
170+ when (txn .commit ()).thenReturn (true );
171+
172+ capacityDao .updateAllocated (1L , 1000L , (short )1 , true );
173+
174+ verify (txn , times (1 )).start ();
175+ verify (txn , times (1 )).commit ();
176+ verify (pstmt , times (1 )).executeUpdate ();
177+ }
178+
179+ @ Test
180+ public void testUpdateAllocatedSubtraction () throws Exception {
181+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
182+ doNothing ().when (txn ).start ();
183+ when (txn .commit ()).thenReturn (true );
184+
185+ capacityDao .updateAllocated (1L , 500L , (short )1 , false );
186+
187+ verify (txn , times (1 )).start ();
188+ verify (txn , times (1 )).commit ();
189+ verify (pstmt , times (1 )).executeUpdate ();
190+ }
191+
192+ @ Test
193+ public void testFindFilteredCapacityByEmptyResult () throws Exception {
194+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
195+ when (pstmt .executeQuery ()).thenReturn (resultSet );
196+ when (resultSet .next ()).thenReturn (false );
197+ List <CapacityDaoImpl .SummedCapacity > result = capacityDao .findFilteredCapacityBy (null , null , null , null , Collections .emptyList (), Collections .emptyList ());
198+ assertNotNull (result );
199+ assertTrue (result .isEmpty ());
200+ }
201+
202+ @ Test
203+ public void testListClustersInZoneOrPodByHostCapacitiesEmpty () throws Exception {
204+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
205+ when (pstmt .executeQuery ()).thenReturn (resultSet );
206+ when (resultSet .next ()).thenReturn (false );
207+
208+ List <Long > resultZone = capacityDao .listClustersInZoneOrPodByHostCapacities (1L , 123L , 2 , 2048L , (short )0 , true );
209+ assertNotNull (resultZone );
210+ assertTrue (resultZone .isEmpty ());
211+
212+ List <Long > resultPod = capacityDao .listClustersInZoneOrPodByHostCapacities (1L , 123L , 2 , 2048L , (short )0 , false );
213+ assertNotNull (resultPod );
214+ assertTrue (resultPod .isEmpty ());
215+ }
216+
217+
218+ @ Test
219+ public void testListHostsWithEnoughCapacityEmptyResult () throws Exception {
220+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
221+ when (pstmt .executeQuery ()).thenReturn (resultSet );
222+ when (resultSet .next ()).thenReturn (false );
223+
224+ List <Long > result = capacityDao .listHostsWithEnoughCapacity (1 , 100L , 200L , Host .Type .Routing .toString ());
225+ assertNotNull (result );
226+ assertTrue (result .isEmpty ());
227+ }
228+
229+
230+ @ Test
231+ public void testOrderClustersByAggregateCapacityEmptyResult () throws Exception {
232+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
233+ when (pstmt .executeQuery ()).thenReturn (resultSet );
234+ when (resultSet .next ()).thenReturn (false );
235+
236+ Pair <List <Long >, Map <Long , Double >> result = capacityDao .orderClustersByAggregateCapacity (1L , 1L , (short ) 1 , true );
237+ assertNotNull (result );
238+ assertTrue (result .first ().isEmpty ());
239+ assertTrue (result .second ().isEmpty ());
240+ }
241+
242+
243+ @ Test
244+ public void testOrderPodsByAggregateCapacityEmptyResult () throws Exception {
245+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
246+ when (pstmt .executeQuery ()).thenReturn (resultSet );
247+ when (resultSet .next ()).thenReturn (false );
248+
249+ Pair <List <Long >, Map <Long , Double >> result = capacityDao .orderPodsByAggregateCapacity (1L , (short ) 1 );
250+ assertNotNull (result );
251+ assertTrue (result .first ().isEmpty ());
252+ assertTrue (result .second ().isEmpty ());
253+ }
254+
255+
256+ @ Test
257+ public void testUpdateCapacityState () throws Exception {
258+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
259+ when (pstmt .executeUpdate ()).thenReturn (1 );
260+
261+ capacityDao .updateCapacityState (1L , 1L , 1L , 1L , "Enabled" , new short []{1 });
262+
263+ verify (pstmt , times (1 )).executeUpdate ();
264+ }
265+
266+
267+ @ Test
268+ public void testFindClusterConsumption () throws Exception {
269+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
270+ when (pstmt .executeQuery ()).thenReturn (resultSet );
271+ when (resultSet .next ()).thenReturn (true );
272+ when (resultSet .getFloat (1 )).thenReturn (0.5f );
273+
274+ float result = capacityDao .findClusterConsumption (1L , (short ) 1 , 1000L );
275+ assertEquals (0.5f , result , 0.0f );
276+ }
277+
278+ @ Test
279+ public void testListPodsByHostCapacitiesEmptyResult () throws Exception {
280+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
281+ when (pstmt .executeQuery ()).thenReturn (resultSet );
282+ when (resultSet .next ()).thenReturn (false );
283+
284+ List <Long > result = capacityDao .listPodsByHostCapacities (1L , 2 , 1024L , (short )0 );
285+ assertNotNull (result );
286+ assertTrue (result .isEmpty ());
287+ }
288+
289+ @ Test
290+ public void testOrderHostsByFreeCapacityEmptyResult () throws Exception {
291+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
292+ when (pstmt .executeQuery ()).thenReturn (resultSet );
293+ when (resultSet .next ()).thenReturn (false );
294+
295+ Pair <List <Long >, Map <Long , Double >> result = capacityDao .orderHostsByFreeCapacity (1L , 1L , (short ) 0 );
296+ assertNotNull (result );
297+ assertTrue (result .first ().isEmpty ());
298+ }
299+
300+ @ Test
301+ public void testFindByClusterPodZoneEmptyResult () throws Exception {
302+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
303+ when (pstmt .executeQuery ()).thenReturn (resultSet );
304+ when (resultSet .next ()).thenReturn (false );
305+
306+ List <CapacityDaoImpl .SummedCapacity > result = capacityDao .findByClusterPodZone (1L , 1L , 1L );
307+ assertNotNull (result );
308+ assertTrue (result .isEmpty ());
309+ }
310+
311+ @ Test
312+ public void testListCapacitiesGroupedByLevelAndTypeEmptyResult () throws Exception {
313+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
314+ when (pstmt .executeQuery ()).thenReturn (resultSet );
315+ when (resultSet .next ()).thenReturn (false );
316+
317+ List <CapacityDaoImpl .SummedCapacity > result = capacityDao .listCapacitiesGroupedByLevelAndType (0 , 1L ,
318+ 1L , 1L , 0 , Collections .emptyList (), Collections .emptyList (), 1L );
319+ assertNotNull (result );
320+ assertTrue (result .isEmpty ());
321+ }
322+
323+ @ Test
324+ public void testFindCapacityByEmptyResult () throws Exception {
325+ when (txn .prepareAutoCloseStatement (anyString ())).thenReturn (pstmt );
326+ when (pstmt .executeQuery ()).thenReturn (resultSet );
327+ when (resultSet .next ()).thenReturn (false );
328+
329+ List <CapacityDaoImpl .SummedCapacity > result = capacityDao .findCapacityBy (1 , 1L , 1L , 1L );
330+ assertNotNull (result );
331+ assertTrue (result .isEmpty ());
332+ }
99333}
0 commit comments