summaryrefslogtreecommitdiffstats
path: root/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java
blob: 4ad910780c4c4f694c7996478f7e821b58c16e28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
 * Copyright 2017 ZTE Corporation.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.holmes.engine.manager;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onap.holmes.common.api.entity.AlarmInfo;
import org.onap.holmes.common.api.entity.CorrelationRule;
import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.utils.DbDaoUtil;
import org.onap.holmes.engine.db.AlarmInfoDao;
import org.onap.holmes.engine.request.DeployRuleRequest;
import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
import org.powermock.api.easymock.PowerMock;
import org.powermock.reflect.Whitebox;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

import static org.easymock.EasyMock.anyObject;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

public class DroolsEngineTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    private RuleMgtWrapper ruleMgtWrapper;

    private AlarmInfoDao alarmInfoDaoMock;

    private DroolsEngine droolsEngine;

    private DbDaoUtil dbDaoUtilStub;

    public DroolsEngineTest() throws Exception {
        droolsEngine = new DroolsEngine();
        ruleMgtWrapper = new RuleMgtWrapperStub();
        dbDaoUtilStub = new DbDaoUtilStub();
        Whitebox.setInternalState(droolsEngine, "daoUtil", dbDaoUtilStub);
        Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper);
        Whitebox.invokeMethod(droolsEngine, "init");
    }

    @Before
    public void setUp() throws Exception {
        PowerMock.resetAll();
    }

    @Test
    public void deployRule_rule_is_null() throws CorrelationException {
        thrown.expect(NullPointerException.class);
        droolsEngine.deployRule(null);
    }

    @Test
    public void deployRule_invalid_rule_no_pkg_name() throws CorrelationException {
        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent("rule123;");
        thrown.expect(CorrelationException.class);
        thrown.expectMessage("The package name can not be empty.");

        droolsEngine.deployRule(rule);
    }

    @Test
    public void deployRule_invalid_rule_illegal_contents() throws CorrelationException {
        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent("package rule123; a random string");
        thrown.expect(CorrelationException.class);

        droolsEngine.deployRule(rule);
    }

    @Test
    public void deployRule_package_name_repeat() throws CorrelationException {
        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent("package rule123");

        thrown.expect(CorrelationException.class);
        thrown.expectMessage("A rule with the same package name already exists in the system.");
        droolsEngine.deployRule(rule);
        droolsEngine.deployRule(rule);
    }

    @Test
    public void undeployRule_package_name_is_null() throws CorrelationException {
        String packageName = null;
        thrown.expect(CorrelationException.class);
        thrown.expectMessage("The package name should not be null.");

        droolsEngine.undeployRule(packageName);
    }

    @Test
    public void undeployRule_normal() throws CorrelationException {
        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent("package rule123");
        droolsEngine.deployRule(rule);
        droolsEngine.undeployRule("rule123");
    }

    @Test
    public void compileRule_compilation_failure() throws CorrelationException {
        String content = "invalid contents";

        thrown.expect(CorrelationException.class);

        droolsEngine.compileRule(content);
    }

    @Test
    public void compileRule_compilation_deployed_rule() throws CorrelationException {
        String content = "package deployed;";
        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent(content);
        rule.setLoopControlName(UUID.randomUUID().toString());
        thrown.expect(CorrelationException.class);

        droolsEngine.deployRule(rule);
        droolsEngine.compileRule(content);
    }

    @Test
    public void compileRule_compilation_normal() throws CorrelationException {
        String content = "package deployed;";
        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent(content);
        rule.setLoopControlName(UUID.randomUUID().toString());

        droolsEngine.compileRule(content);
    }

    @Test
    public void putRaisedIntoStream_facthandle_is_not_null() {
        VesAlarm raiseAlarm = new VesAlarm();
        raiseAlarm.setSourceId("11111");
        raiseAlarm.setEventName("alarm");
        droolsEngine.putRaisedIntoStream(raiseAlarm);
        droolsEngine.putRaisedIntoStream(raiseAlarm);
    }

    @Test
    public void putRaisedIntoStream_factHandle_is_null() {
        VesAlarm raiseAlarm = new VesAlarm();
        raiseAlarm.setSourceId("11111");
        raiseAlarm.setEventName("alarm");
        droolsEngine.putRaisedIntoStream(raiseAlarm);
    }

    @Test
    public void stop() throws Exception {
        droolsEngine.stop();
    }

    @Test
    public void testConvertAlarmInfo2VesAlarm() throws Exception {
        AlarmInfo alarmInfo = new AlarmInfo();
        alarmInfo.setEventId("eventId");
        alarmInfo.setEventName("eventName");
        alarmInfo.setStartEpochMicroSec(1L);
        alarmInfo.setLastEpochMicroSec(1L);
        alarmInfo.setSourceId("sourceId");
        alarmInfo.setSourceName("sourceName");
        alarmInfo.setRootFlag(0);
        alarmInfo.setAlarmIsCleared(1);

        VesAlarm vesAlarm = Whitebox.invokeMethod(droolsEngine, "convertAlarmInfo2VesAlarm", alarmInfo);

        assertThat(vesAlarm.getAlarmIsCleared(), is(1));
        assertThat(vesAlarm.getSourceName(), equalTo("sourceName"));
        assertThat(vesAlarm.getSourceId(), equalTo("sourceId"));
        assertThat(vesAlarm.getStartEpochMicrosec(), is(1L));
        assertThat(vesAlarm.getLastEpochMicrosec(), is(1L));
        assertThat(vesAlarm.getEventName(), equalTo("eventName"));
        assertThat(vesAlarm.getEventId(), equalTo("eventId"));
        assertThat(vesAlarm.getRootFlag(), is(0));
    }

    @Test
    public void testConvertVesAlarm2AlarmInfo() throws Exception {
        VesAlarm vesAlarm = new VesAlarm();
        vesAlarm.setEventId("eventId");
        vesAlarm.setEventName("eventName");
        vesAlarm.setStartEpochMicrosec(1L);
        vesAlarm.setLastEpochMicrosec(1L);
        vesAlarm.setSourceId("sourceId");
        vesAlarm.setSourceName("sourceName");
        vesAlarm.setRootFlag(0);
        vesAlarm.setAlarmIsCleared(1);

        AlarmInfo alarmInfo = Whitebox.invokeMethod(droolsEngine, "convertVesAlarm2AlarmInfo", vesAlarm);

        assertThat(alarmInfo.getAlarmIsCleared(), is(1));
        assertThat(alarmInfo.getSourceName(), equalTo("sourceName"));
        assertThat(alarmInfo.getSourceId(), equalTo("sourceId"));
        assertThat(alarmInfo.getStartEpochMicroSec(), is(1L));
        assertThat(alarmInfo.getLastEpochMicroSec(), is(1L));
        assertThat(alarmInfo.getEventName(), equalTo("eventName"));
        assertThat(alarmInfo.getEventId(), equalTo("eventId"));
        assertThat(alarmInfo.getRootFlag(), is(0));
    }

    @Test
    public void testQueryPackagesFromEngine() throws CorrelationException {

        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent("package packageCheck; rule \"test\" when eval(1==1) then System.out.println(1); end");
        rule.setLoopControlName(UUID.randomUUID().toString());

        droolsEngine.deployRule(rule);

        List<String> packages = droolsEngine.queryPackagesFromEngine();

        assertThat(packages.contains("packageCheck"), is(true));
    }
}

class RuleMgtWrapperStub extends RuleMgtWrapper {
    private List<CorrelationRule> rules;

    public RuleMgtWrapperStub() {
        rules = new ArrayList<>();
        CorrelationRule rule = new CorrelationRule();
        rule.setEnabled(1);
        rule.setContent("package org.onap.holmes;");
        rule.setPackageName("UT");
        rule.setClosedControlLoopName(UUID.randomUUID().toString());
        rules.add(rule);
    }

    public List<CorrelationRule> getRules() {
        return rules;
    }

    public void setRules(List<CorrelationRule> rules) {
        this.rules = rules;
    }

    @Override
    public List<CorrelationRule> queryRuleByEnable(int enabled) throws CorrelationException {
        return rules.stream().filter(rule -> rule.getEnabled() == enabled).collect(Collectors.toList());
    }
}

class AlarmInfoDaoStub extends AlarmInfoDao {

    private List<AlarmInfo> alarms;

    public AlarmInfoDaoStub() {
        alarms = new ArrayList<>();
        AlarmInfo info = new AlarmInfo();
        info.setEventId("eventId");
        info.setEventName("eventName");
        info.setStartEpochMicroSec(1L);
        info.setLastEpochMicroSec(1L);
        info.setSourceId("sourceId");
        info.setSourceName("sourceName");
        info.setRootFlag(0);
        info.setAlarmIsCleared(1);
        alarms.add(info);
    }

    @Override
    protected String addAlarm(AlarmInfo alarmInfo) {
        alarms.add(alarmInfo);
        return null;
    }

    @Override
    protected List<AlarmInfo> queryAlarm() {
        return alarms;
    }

    @Override
    protected int deleteAlarmByAlarmIsCleared(int alarmIsCleared) {
        return 1;
    }
}

class DbDaoUtilStub extends DbDaoUtil {
    private AlarmInfoDao dao = new AlarmInfoDaoStub();
    @Override
    public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {

        return (T)dao;

    }
}