|
| 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 | + |
| 18 | +package com.cloud.usage.dao; |
| 19 | + |
| 20 | +import static org.mockito.Matchers.contains; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import java.sql.PreparedStatement; |
| 26 | +import com.cloud.utils.DateUtil; |
| 27 | +import com.cloud.utils.db.TransactionLegacy; |
| 28 | +import java.util.Date; |
| 29 | +import java.util.TimeZone; |
| 30 | + |
| 31 | +import com.cloud.usage.UsageStorageVO; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.Mockito; |
| 36 | +import org.powermock.api.mockito.PowerMockito; |
| 37 | +import org.powermock.core.classloader.annotations.PowerMockIgnore; |
| 38 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 39 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 40 | + |
| 41 | +@RunWith(PowerMockRunner.class) |
| 42 | +@PrepareForTest(TransactionLegacy.class) |
| 43 | +@PowerMockIgnore("javax.management.*") |
| 44 | +public class UsageStorageDaoImplTest { |
| 45 | + |
| 46 | + @Mock |
| 47 | + private PreparedStatement preparedStatementMock; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private TransactionLegacy transactionMock; |
| 51 | + |
| 52 | + @Mock |
| 53 | + private UsageStorageVO userStorageVOMock; |
| 54 | + |
| 55 | + private final UsageStorageDaoImpl usageDao = new UsageStorageDaoImpl(); |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testUpdate() throws Exception { |
| 59 | + |
| 60 | + |
| 61 | + long id = 21, zoneId = 31, accountId = 41; |
| 62 | + int storageType = 1; |
| 63 | + String UPDATE_DELETED = "UPDATE usage_storage SET deleted = ? WHERE account_id = ? AND id = ? AND storage_type = ? AND zone_id = ? and deleted IS NULL"; |
| 64 | + Date deleted = new Date(); |
| 65 | + |
| 66 | + PowerMockito.mockStatic(TransactionLegacy.class); |
| 67 | + Mockito.when(TransactionLegacy.open(TransactionLegacy.USAGE_DB)).thenReturn(transactionMock); |
| 68 | + |
| 69 | + when(transactionMock.prepareStatement(contains(UPDATE_DELETED))).thenReturn(preparedStatementMock); |
| 70 | + when(userStorageVOMock.getAccountId()).thenReturn(accountId); |
| 71 | + when(userStorageVOMock.getId()).thenReturn(id); |
| 72 | + when(userStorageVOMock.getStorageType()).thenReturn(storageType); |
| 73 | + when(userStorageVOMock.getZoneId()).thenReturn(zoneId); |
| 74 | + when(userStorageVOMock.getDeleted()).thenReturn(deleted); |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + usageDao.update(userStorageVOMock); |
| 79 | + |
| 80 | + verify(transactionMock, times(1)).prepareStatement(UPDATE_DELETED); |
| 81 | + verify(preparedStatementMock, times(1)).setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), deleted)); |
| 82 | + verify(preparedStatementMock, times(1)).setLong(2, accountId); |
| 83 | + verify(preparedStatementMock, times(1)).setLong(3, id); |
| 84 | + verify(preparedStatementMock, times(1)).setInt(4, storageType); |
| 85 | + verify(preparedStatementMock, times(1)).setLong(5, zoneId); |
| 86 | + verify(preparedStatementMock, times(1)).executeUpdate(); |
| 87 | + } |
| 88 | +} |
0 commit comments