Skip to content

Commit 7d61ee6

Browse files
author
Kishan Kavala
committed
CLOUDSTACK-1295 : Added usage unit tests
Fixed Component annontation for usage parsers Fixed mvn target to run usage removed UsageServerComponentConfig which is not required Added region_id to account table in cloud_usage db Conflicts: setup/db/db/schema-40to410.sql
1 parent 3c764c0 commit 7d61ee6

18 files changed

Lines changed: 335 additions & 193 deletions
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 org.apache.cloudstack.api.command.test;
18+
19+
import junit.framework.TestCase;
20+
import org.apache.cloudstack.api.command.admin.usage.GetUsageRecordsCmd;
21+
import org.apache.cloudstack.usage.Usage;
22+
import org.apache.cloudstack.usage.UsageService;
23+
import org.junit.Before;
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
import org.junit.rules.ExpectedException;
27+
import org.mockito.Mockito;
28+
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
public class UsageCmdTest extends TestCase {
33+
34+
private GetUsageRecordsCmd getUsageRecordsCmd;
35+
36+
@Rule
37+
public ExpectedException expectedException = ExpectedException.none();
38+
39+
@Before
40+
public void setUp() {
41+
42+
getUsageRecordsCmd = new GetUsageRecordsCmd() {
43+
44+
};
45+
}
46+
47+
@Test
48+
public void testExecuteSuccess() {
49+
UsageService usageService = Mockito.mock(UsageService.class);
50+
getUsageRecordsCmd._usageService = usageService;
51+
getUsageRecordsCmd.execute();
52+
}
53+
54+
@Test
55+
public void testExecuteEmptyResult() {
56+
57+
UsageService usageService = Mockito.mock(UsageService.class);
58+
59+
List usageRecords = new ArrayList<Usage>();
60+
61+
Mockito.when(usageService.getUsageRecords(getUsageRecordsCmd)).thenReturn(
62+
usageRecords);
63+
64+
getUsageRecordsCmd._usageService = usageService;
65+
getUsageRecordsCmd.execute();
66+
67+
}
68+
69+
}

setup/db/create-schema-premium.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ CREATE TABLE `cloud_usage`.`account` (
137137
`cleanup_needed` tinyint(1) NOT NULL default '0',
138138
`network_domain` varchar(100) COMMENT 'Network domain name of the Vms of the account',
139139
`default_zone_id` bigint unsigned,
140+
`region_id` int unsigned NOT NULL,
140141
CONSTRAINT `uc_account__uuid` UNIQUE (`uuid`),
141142
PRIMARY KEY (`id`)
142143
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

setup/db/db/schema-40to410.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,3 +1323,5 @@ INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Account Defaults', 'DEFAULT'
13231323
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Project Defaults', 'DEFAULT', 'management-server', 'max.project.cpus', '40', 'The default maximum number of cpu cores that can be used for a project');
13241324

13251325
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Project Defaults', 'DEFAULT', 'management-server', 'max.project.memory', '40960', 'The default maximum memory (in MB) that can be used for a project');
1326+
1327+
ALTER TABLE `cloud_usage`.`account` ADD COLUMN `region_id` int unsigned NOT NULL DEFAULT '1';

usage/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<defaultGoal>install</defaultGoal>
4343
<sourceDirectory>src</sourceDirectory>
4444
<testSourceDirectory>test</testSourceDirectory>
45+
<testResources>
46+
<testResource>
47+
<directory>test/resources</directory>
48+
</testResource>
49+
</testResources>
4550
<resources>
4651
<resource>
4752
<directory>resources</directory>

usage/resources/usageApplicationContext.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
3232

3333
<context:annotation-config />
34-
<context:component-scan base-package="com.cloud.usage" />
34+
<context:component-scan base-package="com.cloud.usage, com.cloud.event.dao, com.cloud.user.dao, com.cloud.configuration.dao, com.cloud.alert.dao, com.cloud.domain.dao">
35+
<context:exclude-filter type="assignable" expression="com.cloud.usage.UsageServiceImpl"/>
36+
</context:component-scan>
3537

3638
<!--
3739
@DB support
@@ -48,6 +50,5 @@
4850

4951
<bean id="transactionContextBuilder" class="com.cloud.utils.db.TransactionContextBuilder" />
5052
<bean id="ComponentContext" class="com.cloud.utils.component.ComponentContext" />
51-
<bean id="UsageServerConfig" class="com.cloud.usage.UsageServerComponentConfig" />
5253

5354
</beans>

usage/src/com/cloud/usage/UsageManagerImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import org.apache.log4j.Logger;
3737
import org.apache.cloudstack.usage.UsageTypes;
38+
import org.springframework.stereotype.Component;
3839

3940
import com.cloud.alert.AlertManager;
4041
import com.cloud.configuration.dao.ConfigurationDao;
@@ -79,6 +80,7 @@
7980
import com.cloud.utils.db.Transaction;
8081
import com.cloud.utils.exception.CloudRuntimeException;
8182

83+
@Component
8284
@Local(value={UsageManager.class})
8385
public class UsageManagerImpl extends ManagerBase implements UsageManager, Runnable {
8486
public static final Logger s_logger = Logger.getLogger(UsageManagerImpl.class.getName());

usage/src/com/cloud/usage/UsageServerComponentConfig.java

Lines changed: 0 additions & 180 deletions
This file was deleted.

usage/src/com/cloud/usage/parser/NetworkOfferingUsageParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
import com.cloud.usage.dao.UsageNetworkOfferingDao;
3636
import com.cloud.user.AccountVO;
3737
import com.cloud.utils.Pair;
38+
import org.springframework.stereotype.Component;
3839

39-
40+
@Component
4041
public class NetworkOfferingUsageParser {
4142
public static final Logger s_logger = Logger.getLogger(NetworkOfferingUsageParser.class.getName());
4243

usage/src/com/cloud/usage/parser/NetworkUsageParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import com.cloud.user.AccountVO;
3535

3636
import com.cloud.utils.db.SearchCriteria;
37+
import org.springframework.stereotype.Component;
3738

39+
@Component
3840
public class NetworkUsageParser {
3941
public static final Logger s_logger = Logger.getLogger(NetworkUsageParser.class.getName());
4042

usage/src/com/cloud/usage/parser/PortForwardingUsageParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
import com.cloud.usage.dao.UsagePortForwardingRuleDao;
3636
import com.cloud.user.AccountVO;
3737
import com.cloud.utils.Pair;
38+
import org.springframework.stereotype.Component;
3839

39-
40+
@Component
4041
public class PortForwardingUsageParser {
4142
public static final Logger s_logger = Logger.getLogger(PortForwardingUsageParser.class.getName());
4243

@@ -49,7 +50,7 @@ public class PortForwardingUsageParser {
4950
@PostConstruct
5051
void init() {
5152
m_usageDao = _usageDao;
52-
_usagePFRuleDao = _usagePFRuleDao;
53+
m_usagePFRuleDao = _usagePFRuleDao;
5354
}
5455

5556
public static boolean parse(AccountVO account, Date startDate, Date endDate) {

0 commit comments

Comments
 (0)