+++ /dev/null
-/**\r
- * Copyright 2017-2020 ZTE Corporation.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.onap.holmes.rulemgt.db;\r
-\r
-import org.easymock.EasyMock;\r
-import org.junit.Before;\r
-import org.junit.Rule;\r
-import org.junit.Test;\r
-import org.junit.rules.ExpectedException;\r
-import org.junit.runner.RunWith;\r
-import org.onap.holmes.common.api.entity.CorrelationRule;\r
-import org.onap.holmes.common.exception.CorrelationException;\r
-import org.onap.holmes.common.utils.DbDaoUtil;\r
-import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;\r
-import org.powermock.api.easymock.PowerMock;\r
-import org.powermock.core.classloader.annotations.PrepareForTest;\r
-import org.powermock.modules.junit4.PowerMockRunner;\r
-import org.powermock.reflect.Whitebox;\r
-import org.skife.jdbi.v2.Handle;\r
-import org.skife.jdbi.v2.Query;\r
-\r
-import java.util.*;\r
-\r
-import static org.hamcrest.MatcherAssert.assertThat;\r
-import static org.hamcrest.Matchers.is;\r
-\r
-@RunWith(PowerMockRunner.class)\r
-@PrepareForTest({DbDaoUtil.class, Handle.class, Query.class})\r
-public class CorrelationRuleQueryDaoTest {\r
-\r
- @Rule\r
- public ExpectedException thrown = ExpectedException.none();\r
-\r
- private DbDaoUtil dbDaoUtil;\r
-\r
- private Handle handle;\r
-\r
- private Query query;\r
-\r
- private CorrelationRuleQueryDao correlationRuleQueryDao;\r
- private RuleQueryCondition ruleQueryCondition;\r
-\r
- @Before\r
- public void setUp() throws Exception {\r
- correlationRuleQueryDao = new CorrelationRuleQueryDao();\r
-\r
- dbDaoUtil = PowerMock.createMock(DbDaoUtil.class);\r
- handle = PowerMock.createMock(Handle.class);\r
- query = PowerMock.createMock(Query.class);\r
-\r
- Whitebox.setInternalState(correlationRuleQueryDao, "dbDaoUtil", dbDaoUtil);\r
-\r
- ruleQueryCondition = createRuleQueryCondition();\r
- }\r
-\r
-\r
- @Test\r
- public void getCorrelationRulesByCondition_db_exception() throws Exception {\r
-\r
- thrown.expect(CorrelationException.class);\r
- thrown.expectMessage("Failed to query the rule.");\r
-\r
- EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);\r
- EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);\r
- EasyMock.expect(query.list()).andThrow(new RuntimeException()).anyTimes();\r
- dbDaoUtil.close(handle);\r
- EasyMock.expectLastCall();\r
-\r
- PowerMock.replayAll();\r
-\r
- correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);\r
-\r
- PowerMock.verifyAll();\r
- }\r
-\r
- @Test\r
- public void getCorrelationRulesByCondition_normal() throws Exception {\r
- EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);\r
- EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);\r
- EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();\r
- dbDaoUtil.close(handle);\r
- EasyMock.expectLastCall();\r
-\r
- PowerMock.replayAll();\r
-\r
- List<CorrelationRule> result = correlationRuleQueryDao.getCorrelationRulesByCondition(ruleQueryCondition);\r
- assertThat(result.size(), is(1));\r
-\r
- PowerMock.verifyAll();\r
- }\r
-\r
- @Test\r
- public void getCorrelationRulesByCondition_get_where_sql_exception() throws Exception {\r
- thrown.expect(CorrelationException.class);\r
- thrown.expectMessage("An error occurred while building the query SQL.");\r
-\r
- EasyMock.expect(dbDaoUtil.getHandle()).andReturn(handle);\r
- EasyMock.expect(handle.createQuery(EasyMock.anyObject(String.class))).andReturn(query);\r
- EasyMock.expect(query.list()).andReturn(createQueryResult()).anyTimes();\r
- dbDaoUtil.close(handle);\r
- EasyMock.expectLastCall();\r
-\r
- PowerMock.replayAll();\r
-\r
- correlationRuleQueryDao.getCorrelationRulesByCondition(null);\r
-\r
- PowerMock.verifyAll();\r
- }\r
-\r
- private List<Map<String, Object>> createQueryResult() {\r
- List<Map<String, Object>> list = new ArrayList<>();\r
- Map<String, Object> value = new HashMap<>();\r
- value.put("name", "Rule-001");\r
- value.put("rid", "rule_" + System.currentTimeMillis());\r
- value.put("description", "desc");\r
- value.put("enable", 0);\r
- value.put("templateID", 1L);\r
- value.put("engineId", "engine-001");\r
- value.put("engineType", "engineType-001");\r
- value.put("creator", "admin");\r
- value.put("createTime", new Date());\r
- value.put("updator", "admin");\r
- value.put("updateTime", new Date());\r
- value.put("params", new Properties());\r
- value.put("domain", "Domain");\r
- value.put("isManual", 0);\r
- value.put("vendor", "Vendor");\r
- value.put("content", "Contents");\r
- value.put("package", "package");\r
- list.add(value);\r
- return list;\r
- }\r
-\r
- private RuleQueryCondition createRuleQueryCondition() {\r
- RuleQueryCondition ruleQueryCondition = new RuleQueryCondition();\r
- ruleQueryCondition.setRid("rule_" + System.currentTimeMillis());\r
- ruleQueryCondition.setName("Rule-001");\r
- ruleQueryCondition.setEnabled(0);\r
- ruleQueryCondition.setCreator("admin");\r
- ruleQueryCondition.setModifier("admin");\r
- return ruleQueryCondition;\r
- }\r
-\r
-}\r