From 740179c10b6906e6c1e6016c3dd6afce46c8c74b Mon Sep 17 00:00:00 2001 From: subhash yedugundla Date: Mon, 28 Sep 2015 23:05:43 +0530 Subject: [PATCH] CLOUDSTACK-8908 After copying the template charging for that template is stopped --- .../cloud/usage/dao/UsageStorageDaoImpl.java | 3 +- .../usage/dao/UsageStorageDaoImplTest.java | 88 +++++++++++++++++++ engine/schema/test/resources/db.properties | 70 +++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 engine/schema/test/com/cloud/usage/dao/UsageStorageDaoImplTest.java create mode 100644 engine/schema/test/resources/db.properties diff --git a/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java index 8d419e5a62e..45337b22e13 100644 --- a/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java +++ b/engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java @@ -41,7 +41,7 @@ public class UsageStorageDaoImpl extends GenericDaoBase im public static final Logger s_logger = Logger.getLogger(UsageStorageDaoImpl.class.getName()); protected static final String REMOVE_BY_USERID_STORAGEID = "DELETE FROM usage_storage WHERE account_id = ? AND id = ? AND storage_type = ?"; - protected static final String UPDATE_DELETED = "UPDATE usage_storage SET deleted = ? WHERE account_id = ? AND id = ? AND storage_type = ? and deleted IS NULL"; + protected static final String UPDATE_DELETED = "UPDATE usage_storage SET deleted = ? WHERE account_id = ? AND id = ? AND storage_type = ? AND zone_id = ? and deleted IS NULL"; protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT id, zone_id, account_id, domain_id, storage_type, source_id, size, created, deleted, virtual_size " + "FROM usage_storage " + "WHERE account_id = ? AND ((deleted IS NULL) OR (created BETWEEN ? AND ?) OR " + " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?)))"; @@ -124,6 +124,7 @@ public class UsageStorageDaoImpl extends GenericDaoBase im pstmt.setLong(2, usage.getAccountId()); pstmt.setLong(3, usage.getId()); pstmt.setInt(4, usage.getStorageType()); + pstmt.setLong(5, usage.getZoneId()); pstmt.executeUpdate(); } }catch (SQLException e) diff --git a/engine/schema/test/com/cloud/usage/dao/UsageStorageDaoImplTest.java b/engine/schema/test/com/cloud/usage/dao/UsageStorageDaoImplTest.java new file mode 100644 index 00000000000..4c3311ff0fb --- /dev/null +++ b/engine/schema/test/com/cloud/usage/dao/UsageStorageDaoImplTest.java @@ -0,0 +1,88 @@ +// 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.usage.dao; + +import static org.mockito.Matchers.contains; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.sql.PreparedStatement; +import com.cloud.utils.DateUtil; +import com.cloud.utils.db.TransactionLegacy; +import java.util.Date; +import java.util.TimeZone; + +import com.cloud.usage.UsageStorageVO; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(TransactionLegacy.class) +@PowerMockIgnore("javax.management.*") +public class UsageStorageDaoImplTest { + + @Mock + private PreparedStatement preparedStatementMock; + + @Mock + private TransactionLegacy transactionMock; + + @Mock + private UsageStorageVO userStorageVOMock; + + private final UsageStorageDaoImpl usageDao = new UsageStorageDaoImpl(); + + @Test + public void testUpdate() throws Exception { + + + long id = 21, zoneId = 31, accountId = 41; + int storageType = 1; + String UPDATE_DELETED = "UPDATE usage_storage SET deleted = ? WHERE account_id = ? AND id = ? AND storage_type = ? AND zone_id = ? and deleted IS NULL"; + Date deleted = new Date(); + + PowerMockito.mockStatic(TransactionLegacy.class); + Mockito.when(TransactionLegacy.open(TransactionLegacy.USAGE_DB)).thenReturn(transactionMock); + + when(transactionMock.prepareStatement(contains(UPDATE_DELETED))).thenReturn(preparedStatementMock); + when(userStorageVOMock.getAccountId()).thenReturn(accountId); + when(userStorageVOMock.getId()).thenReturn(id); + when(userStorageVOMock.getStorageType()).thenReturn(storageType); + when(userStorageVOMock.getZoneId()).thenReturn(zoneId); + when(userStorageVOMock.getDeleted()).thenReturn(deleted); + + + + usageDao.update(userStorageVOMock); + + verify(transactionMock, times(1)).prepareStatement(UPDATE_DELETED); + verify(preparedStatementMock, times(1)).setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), deleted)); + verify(preparedStatementMock, times(1)).setLong(2, accountId); + verify(preparedStatementMock, times(1)).setLong(3, id); + verify(preparedStatementMock, times(1)).setInt(4, storageType); + verify(preparedStatementMock, times(1)).setLong(5, zoneId); + verify(preparedStatementMock, times(1)).executeUpdate(); + } +} diff --git a/engine/schema/test/resources/db.properties b/engine/schema/test/resources/db.properties new file mode 100644 index 00000000000..49fd68aa933 --- /dev/null +++ b/engine/schema/test/resources/db.properties @@ -0,0 +1,70 @@ +# +# 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. +# + + +# management server clustering parameters, change cluster.node.IP to the machine IP address +# in which the management server(Tomcat) is running +cluster.node.IP=127.0.0.1 +cluster.servlet.port=9090 +region.id=1 + +# CloudStack database settings +db.cloud.username=cloud +db.cloud.password=cloud +db.root.password= +db.cloud.host=localhost +db.cloud.port=3306 +db.cloud.name=cloud + +# CloudStack database tuning parameters +db.cloud.maxActive=250 +db.cloud.maxIdle=30 +db.cloud.maxWait=10000 +db.cloud.autoReconnect=true +db.cloud.validationQuery=SELECT 1 +db.cloud.testOnBorrow=true +db.cloud.testWhileIdle=true +db.cloud.timeBetweenEvictionRunsMillis=40000 +db.cloud.minEvictableIdleTimeMillis=240000 +db.cloud.poolPreparedStatements=false +db.cloud.url.params=prepStmtCacheSize=517&cachePrepStmts=true&prepStmtCacheSqlLimit=4096 + +# usage database settings +db.usage.username=cloud +db.usage.password=cloud +db.usage.host=localhost +db.usage.port=3306 +db.usage.name=cloud_usage + +# usage database tuning parameters +db.usage.maxActive=100 +db.usage.maxIdle=30 +db.usage.maxWait=10000 +db.usage.autoReconnect=true + +# Simulator database settings +db.simulator.username=cloud +db.simulator.password=cloud +db.simulator.host=localhost +db.simulator.port=3306 +db.simulator.name=simulator +db.simulator.maxActive=250 +db.simulator.maxIdle=30 +db.simulator.maxWait=10000 +db.simulator.autoReconnect=true