|
| 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.storage.dao; |
| 18 | + |
| 19 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 20 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 21 | +import static org.mockito.ArgumentMatchers.startsWith; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import java.sql.PreparedStatement; |
| 27 | +import java.sql.ResultSet; |
| 28 | +import java.sql.SQLException; |
| 29 | + |
| 30 | +import org.junit.AfterClass; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.MockedStatic; |
| 36 | +import org.mockito.Mockito; |
| 37 | +import org.mockito.junit.MockitoJUnitRunner; |
| 38 | + |
| 39 | +import com.cloud.utils.db.TransactionLegacy; |
| 40 | + |
| 41 | +@RunWith(MockitoJUnitRunner.class) |
| 42 | +public class VolumeDaoImplTest { |
| 43 | + @Mock |
| 44 | + private PreparedStatement preparedStatementMock; |
| 45 | + |
| 46 | + @Mock |
| 47 | + private TransactionLegacy transactionMock; |
| 48 | + |
| 49 | + private static MockedStatic<TransactionLegacy> mockedTransactionLegacy; |
| 50 | + |
| 51 | + private final VolumeDaoImpl volumeDao = new VolumeDaoImpl(); |
| 52 | + |
| 53 | + @BeforeClass |
| 54 | + public static void init() { |
| 55 | + mockedTransactionLegacy = Mockito.mockStatic(TransactionLegacy.class); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterClass |
| 59 | + public static void close() { |
| 60 | + mockedTransactionLegacy.close(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testListPoolIdsByVolumeCount_with_cluster_details() throws SQLException { |
| 65 | + final String ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT_QUERY_WITH_CLUSTER = |
| 66 | + "SELECT pool.id, SUM(IF(vol.state='Ready' AND vol.account_id = ?, 1, 0)) FROM `cloud`.`storage_pool` pool LEFT JOIN `cloud`.`volumes` vol ON pool.id = vol.pool_id WHERE pool.data_center_id = ? AND pool.pod_id = ? AND pool.cluster_id = ? GROUP BY pool.id ORDER BY 2 ASC "; |
| 67 | + final long dcId = 1, accountId = 1; |
| 68 | + final Long podId = 1L, clusterId = 1L; |
| 69 | + |
| 70 | + when(TransactionLegacy.currentTxn()).thenReturn(transactionMock); |
| 71 | + when(transactionMock.prepareAutoCloseStatement(startsWith(ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT_QUERY_WITH_CLUSTER))).thenReturn(preparedStatementMock); |
| 72 | + ResultSet rs = Mockito.mock(ResultSet.class); |
| 73 | + when(preparedStatementMock.executeQuery()).thenReturn(rs, rs); |
| 74 | + |
| 75 | + volumeDao.listPoolIdsByVolumeCount(dcId, podId, clusterId, accountId); |
| 76 | + |
| 77 | + verify(transactionMock, times(1)).prepareAutoCloseStatement(ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT_QUERY_WITH_CLUSTER); |
| 78 | + verify(preparedStatementMock, times(1)).setLong(1, accountId); |
| 79 | + verify(preparedStatementMock, times(1)).setLong(2, dcId); |
| 80 | + verify(preparedStatementMock, times(1)).setLong(3, podId); |
| 81 | + verify(preparedStatementMock, times(1)).setLong(4, clusterId); |
| 82 | + verify(preparedStatementMock, times(4)).setLong(anyInt(), anyLong()); |
| 83 | + verify(preparedStatementMock, times(1)).executeQuery(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testListPoolIdsByVolumeCount_without_cluster_details() throws SQLException { |
| 88 | + final String ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT_QUERY_WITHOUT_CLUSTER = |
| 89 | + "SELECT pool.id, SUM(IF(vol.state='Ready' AND vol.account_id = ?, 1, 0)) FROM `cloud`.`storage_pool` pool LEFT JOIN `cloud`.`volumes` vol ON pool.id = vol.pool_id WHERE pool.data_center_id = ? GROUP BY pool.id ORDER BY 2 ASC "; |
| 90 | + final long dcId = 1, accountId = 1; |
| 91 | + |
| 92 | + when(TransactionLegacy.currentTxn()).thenReturn(transactionMock); |
| 93 | + when(transactionMock.prepareAutoCloseStatement(startsWith(ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT_QUERY_WITHOUT_CLUSTER))).thenReturn(preparedStatementMock); |
| 94 | + ResultSet rs = Mockito.mock(ResultSet.class); |
| 95 | + when(preparedStatementMock.executeQuery()).thenReturn(rs, rs); |
| 96 | + |
| 97 | + volumeDao.listPoolIdsByVolumeCount(dcId, null, null, accountId); |
| 98 | + |
| 99 | + verify(transactionMock, times(1)).prepareAutoCloseStatement(ORDER_POOLS_NUMBER_OF_VOLUMES_FOR_ACCOUNT_QUERY_WITHOUT_CLUSTER); |
| 100 | + verify(preparedStatementMock, times(1)).setLong(1, accountId); |
| 101 | + verify(preparedStatementMock, times(1)).setLong(2, dcId); |
| 102 | + verify(preparedStatementMock, times(2)).setLong(anyInt(), anyLong()); |
| 103 | + verify(preparedStatementMock, times(1)).executeQuery(); |
| 104 | + } |
| 105 | +} |
0 commit comments