2 * Copyright 2017 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.holmes.engine.manager;
\r
19 import static org.easymock.EasyMock.anyInt;
\r
20 import static org.easymock.EasyMock.expect;
\r
22 import java.lang.reflect.Method;
\r
23 import java.util.ArrayList;
\r
24 import java.util.List;
\r
25 import java.util.Locale;
\r
26 import org.drools.KnowledgeBase;
\r
27 import org.drools.KnowledgeBaseConfiguration;
\r
28 import org.drools.KnowledgeBaseFactory;
\r
29 import org.drools.conf.EventProcessingOption;
\r
30 import org.drools.runtime.StatefulKnowledgeSession;
\r
31 import org.junit.Before;
\r
32 import org.junit.Rule;
\r
33 import org.junit.Test;
\r
34 import org.junit.rules.ExpectedException;
\r
35 import org.onap.holmes.common.api.stat.VesAlarm;
\r
36 import org.onap.holmes.engine.request.DeployRuleRequest;
\r
37 import org.onap.holmes.common.api.entity.CorrelationRule;
\r
38 import org.onap.holmes.common.constant.AlarmConst;
\r
39 import org.onap.holmes.common.exception.CorrelationException;
\r
40 import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
\r
41 import org.powermock.api.easymock.PowerMock;
\r
42 import org.powermock.modules.junit4.rule.PowerMockRule;
\r
43 import org.powermock.reflect.Whitebox;
\r
45 public class DroolsEngineTest {
\r
48 public ExpectedException thrown = ExpectedException.none();
\r
51 public PowerMockRule powerMockRule = new PowerMockRule();
\r
53 private RuleMgtWrapper ruleMgtWrapper;
\r
55 private KnowledgeBase kbase;
\r
57 private KnowledgeBaseConfiguration kconf;
\r
59 private StatefulKnowledgeSession ksession;
\r
62 private DroolsEngine droolsEngine;
\r
65 public void setUp() {
\r
66 droolsEngine = new DroolsEngine();
\r
68 this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
\r
69 this.kconf.setOption(EventProcessingOption.STREAM);
\r
70 this.kconf.setProperty("drools.assertBehaviour", "equality");
\r
71 this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf);
\r
72 this.ksession = kbase.newStatefulKnowledgeSession();
\r
74 ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class);
\r
76 Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper);
\r
78 Whitebox.setInternalState(droolsEngine, "kconf", kconf);
\r
79 Whitebox.setInternalState(droolsEngine, "kbase", kbase);
\r
80 Whitebox.setInternalState(droolsEngine, "ksession", ksession);
\r
82 PowerMock.resetAll();
\r
86 public void init() throws Exception {
\r
88 List<CorrelationRule> rules = new ArrayList<CorrelationRule>();
\r
89 CorrelationRule rule = new CorrelationRule();
\r
90 rule.setContent("content");
\r
93 expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);
\r
94 PowerMock.replayAll();
\r
96 Method method = DroolsEngine.class.getDeclaredMethod("init");
\r
97 method.setAccessible(true);
\r
98 method.invoke(droolsEngine);
\r
100 PowerMock.verifyAll();
\r
104 public void deployRule_rule_is_null() throws CorrelationException {
\r
105 Locale locale = PowerMock.createMock(Locale.class);
\r
107 thrown.expect(NullPointerException.class);
\r
109 droolsEngine.deployRule(null, locale);
\r
113 public void deployRule_kbuilder_has_errors() throws CorrelationException {
\r
114 DeployRuleRequest rule = new DeployRuleRequest();
\r
115 rule.setContent("rule123");
\r
116 Locale locale = new Locale(AlarmConst.I18N_EN);
\r
118 thrown.expect(CorrelationException.class);
\r
120 droolsEngine.deployRule(rule, locale);
\r
124 public void deployRule_package_name_repeat() throws CorrelationException {
\r
125 DeployRuleRequest rule = new DeployRuleRequest();
\r
126 rule.setContent("package rule123");
\r
127 Locale locale = new Locale(AlarmConst.I18N_EN);
\r
129 thrown.expect(CorrelationException.class);
\r
131 droolsEngine.deployRule(rule, locale);
\r
132 droolsEngine.deployRule(rule, locale);
\r
136 public void undeployRule_package_name_is_null() throws CorrelationException {
\r
137 String packageName = null;
\r
138 Locale locale = new Locale(AlarmConst.I18N_EN);
\r
140 thrown.expect(CorrelationException.class);
\r
142 droolsEngine.undeployRule(packageName, locale);
\r
146 public void undeployRule_normal() throws CorrelationException {
\r
147 Locale locale = new Locale(AlarmConst.I18N_EN);
\r
149 DeployRuleRequest rule = new DeployRuleRequest();
\r
150 rule.setContent("package rule123");
\r
151 droolsEngine.deployRule(rule, locale);
\r
153 String packageName = "rule123";
\r
155 droolsEngine.undeployRule(packageName, locale);
\r
159 public void compileRule_kbuilder_has_errors() throws CorrelationException {
\r
160 String content = "have error content";
\r
161 Locale locale = new Locale(AlarmConst.I18N_EN);
\r
163 thrown.expect(CorrelationException.class);
\r
165 droolsEngine.compileRule(content, locale);
\r
169 public void putRaisedIntoStream_facthandle_is_null() {
\r
170 VesAlarm raiseAlarm = new VesAlarm();
\r
171 raiseAlarm.setVersion((long) 245235);
\r
172 droolsEngine.putRaisedIntoStream(raiseAlarm);
\r
173 droolsEngine.putRaisedIntoStream(raiseAlarm);
\r
177 public void putRaisedIntoStream_factHandle_is_not_null() {
\r
178 VesAlarm raiseAlarm = new VesAlarm();
\r
179 raiseAlarm.setVersion((long) 245235);
\r
180 droolsEngine.putRaisedIntoStream(raiseAlarm);
\r
184 public void stop() throws Exception {
\r
185 droolsEngine.stop();
\r