Skip to content

Commit a13ee85

Browse files
committed
Merge pull request #896 from yvsubhash/CLOUDSTACK-8908
CLOUDSTACK-8908 After copying the template charging for that template is getting stoppedThis is happening as the zone id is not part of the query. Zone id is added to the query and unit tests are also added * pr/896: CLOUDSTACK-8908 After copying the template charging for that template is stopped Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
2 parents f8c9f45 + 740179c commit a13ee85

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed

engine/schema/src/com/cloud/usage/dao/UsageStorageDaoImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class UsageStorageDaoImpl extends GenericDaoBase<UsageStorageVO, Long> im
4141
public static final Logger s_logger = Logger.getLogger(UsageStorageDaoImpl.class.getName());
4242

4343
protected static final String REMOVE_BY_USERID_STORAGEID = "DELETE FROM usage_storage WHERE account_id = ? AND id = ? AND storage_type = ?";
44-
protected static final String UPDATE_DELETED = "UPDATE usage_storage SET deleted = ? WHERE account_id = ? AND id = ? AND storage_type = ? and deleted IS NULL";
44+
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";
4545
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT =
4646
"SELECT id, zone_id, account_id, domain_id, storage_type, source_id, size, created, deleted, virtual_size " + "FROM usage_storage "
4747
+ "WHERE account_id = ? AND ((deleted IS NULL) OR (created BETWEEN ? AND ?) OR " + " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?)))";
@@ -124,6 +124,7 @@ public void update(UsageStorageVO usage) {
124124
pstmt.setLong(2, usage.getAccountId());
125125
pstmt.setLong(3, usage.getId());
126126
pstmt.setInt(4, usage.getStorageType());
127+
pstmt.setLong(5, usage.getZoneId());
127128
pstmt.executeUpdate();
128129
}
129130
}catch (SQLException e)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
21+
# management server clustering parameters, change cluster.node.IP to the machine IP address
22+
# in which the management server(Tomcat) is running
23+
cluster.node.IP=127.0.0.1
24+
cluster.servlet.port=9090
25+
region.id=1
26+
27+
# CloudStack database settings
28+
db.cloud.username=cloud
29+
db.cloud.password=cloud
30+
db.root.password=
31+
db.cloud.host=localhost
32+
db.cloud.port=3306
33+
db.cloud.name=cloud
34+
35+
# CloudStack database tuning parameters
36+
db.cloud.maxActive=250
37+
db.cloud.maxIdle=30
38+
db.cloud.maxWait=10000
39+
db.cloud.autoReconnect=true
40+
db.cloud.validationQuery=SELECT 1
41+
db.cloud.testOnBorrow=true
42+
db.cloud.testWhileIdle=true
43+
db.cloud.timeBetweenEvictionRunsMillis=40000
44+
db.cloud.minEvictableIdleTimeMillis=240000
45+
db.cloud.poolPreparedStatements=false
46+
db.cloud.url.params=prepStmtCacheSize=517&cachePrepStmts=true&prepStmtCacheSqlLimit=4096
47+
48+
# usage database settings
49+
db.usage.username=cloud
50+
db.usage.password=cloud
51+
db.usage.host=localhost
52+
db.usage.port=3306
53+
db.usage.name=cloud_usage
54+
55+
# usage database tuning parameters
56+
db.usage.maxActive=100
57+
db.usage.maxIdle=30
58+
db.usage.maxWait=10000
59+
db.usage.autoReconnect=true
60+
61+
# Simulator database settings
62+
db.simulator.username=cloud
63+
db.simulator.password=cloud
64+
db.simulator.host=localhost
65+
db.simulator.port=3306
66+
db.simulator.name=simulator
67+
db.simulator.maxActive=250
68+
db.simulator.maxIdle=30
69+
db.simulator.maxWait=10000
70+
db.simulator.autoReconnect=true

0 commit comments

Comments
 (0)