b57fa308e4a6ba30368bf12ba3a908b9204e043e
[holmes/engine-management.git] / engine-d / src / test / java / org / onap / holmes / engine / manager / DroolsEngineTest.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\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
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package org.onap.holmes.engine.manager;\r
18 \r
19 import static org.easymock.EasyMock.anyInt;\r
20 import static org.easymock.EasyMock.expect;\r
21 \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
44 \r
45 public class DroolsEngineTest {\r
46 \r
47     @Rule\r
48     public ExpectedException thrown = ExpectedException.none();\r
49 \r
50     @Rule\r
51     public PowerMockRule powerMockRule = new PowerMockRule();\r
52 \r
53     private RuleMgtWrapper ruleMgtWrapper;\r
54 \r
55     private KnowledgeBase kbase;\r
56 \r
57     private KnowledgeBaseConfiguration kconf;\r
58 \r
59     private StatefulKnowledgeSession ksession;\r
60 \r
61 \r
62     private DroolsEngine droolsEngine;\r
63 \r
64     @Before\r
65     public void setUp() {\r
66         droolsEngine = new DroolsEngine();\r
67 \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
73 \r
74         ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class);\r
75 \r
76         Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper);\r
77 \r
78         Whitebox.setInternalState(droolsEngine, "kconf", kconf);\r
79         Whitebox.setInternalState(droolsEngine, "kbase", kbase);\r
80         Whitebox.setInternalState(droolsEngine, "ksession", ksession);\r
81 \r
82         PowerMock.resetAll();\r
83     }\r
84 \r
85     @Test\r
86     public void init() throws Exception {\r
87 \r
88         List<CorrelationRule> rules = new ArrayList<CorrelationRule>();\r
89         CorrelationRule rule = new CorrelationRule();\r
90         rule.setContent("content");\r
91         rules.add(rule);\r
92 \r
93         expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);\r
94         PowerMock.replayAll();\r
95 \r
96         Method method = DroolsEngine.class.getDeclaredMethod("init");\r
97         method.setAccessible(true);\r
98         method.invoke(droolsEngine);\r
99 \r
100         PowerMock.verifyAll();\r
101     }\r
102 \r
103     @Test\r
104     public void deployRule_rule_is_null() throws CorrelationException {\r
105         Locale locale = PowerMock.createMock(Locale.class);\r
106 \r
107         thrown.expect(NullPointerException.class);\r
108 \r
109         droolsEngine.deployRule(null, locale);\r
110     }\r
111 \r
112     @Test\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
117 \r
118         thrown.expect(CorrelationException.class);\r
119 \r
120         droolsEngine.deployRule(rule, locale);\r
121     }\r
122 \r
123     @Test\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
128 \r
129         thrown.expect(CorrelationException.class);\r
130 \r
131         droolsEngine.deployRule(rule, locale);\r
132         droolsEngine.deployRule(rule, locale);\r
133     }\r
134 \r
135     @Test\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
139 \r
140         thrown.expect(CorrelationException.class);\r
141 \r
142         droolsEngine.undeployRule(packageName, locale);\r
143     }\r
144 \r
145     @Test\r
146     public void undeployRule_normal() throws CorrelationException {\r
147         Locale locale = new Locale(AlarmConst.I18N_EN);\r
148 \r
149         DeployRuleRequest rule = new DeployRuleRequest();\r
150         rule.setContent("package rule123");\r
151         droolsEngine.deployRule(rule, locale);\r
152 \r
153         String packageName = "rule123";\r
154 \r
155         droolsEngine.undeployRule(packageName, locale);\r
156     }\r
157 \r
158     @Test\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
162 \r
163         thrown.expect(CorrelationException.class);\r
164 \r
165         droolsEngine.compileRule(content, locale);\r
166     }\r
167 \r
168     @Test\r
169     public void putRaisedIntoStream_facthandle_is_not_null() {\r
170         VesAlarm raiseAlarm = new VesAlarm();\r
171         raiseAlarm.setSourceId("11111");\r
172         raiseAlarm.setEventName("alarm");\r
173         droolsEngine.putRaisedIntoStream(raiseAlarm);\r
174         droolsEngine.putRaisedIntoStream(raiseAlarm);\r
175     }\r
176 \r
177     @Test\r
178     public void putRaisedIntoStream_factHandle_is_null() {\r
179         VesAlarm raiseAlarm = new VesAlarm();\r
180         raiseAlarm.setSourceId("11111");\r
181         raiseAlarm.setEventName("alarm");\r
182         droolsEngine.putRaisedIntoStream(raiseAlarm);\r
183     }\r
184 \r
185     @Test\r
186     public void stop() throws Exception {\r
187         droolsEngine.stop();\r
188     }\r
189 }\r