summaryrefslogtreecommitdiffstats
path: root/engine-d/src/test/java/org/onap/holmes/engine/manager/DroolsEngineTest.java
blob: 885b5e44515b0e7200c6eb446674e941fd3cb295 (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
/**
 * Copyright 2017 ZTE Corporation.
 *
 * 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
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 static org.easymock.EasyMock.anyInt;
import static org.easymock.EasyMock.expect;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.conf.EventProcessingOption;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.engine.request.DeployRuleRequest;
import org.onap.holmes.common.api.entity.CorrelationRule;
import org.onap.holmes.common.constant.AlarmConst;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
import org.powermock.api.easymock.PowerMock;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.reflect.Whitebox;

public class DroolsEngineTest {

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

    @Rule
    public PowerMockRule powerMockRule = new PowerMockRule();

    private RuleMgtWrapper ruleMgtWrapper;

    private KnowledgeBase kbase;

    private KnowledgeBaseConfiguration kconf;

    private StatefulKnowledgeSession ksession;


    private DroolsEngine droolsEngine;

    @Before
    public void setUp() {
        droolsEngine = new DroolsEngine();

        this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
        this.kconf.setOption(EventProcessingOption.STREAM);
        this.kconf.setProperty("drools.assertBehaviour", "equality");
        this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf);
        this.ksession = kbase.newStatefulKnowledgeSession();

        ruleMgtWrapper = PowerMock.createMock(RuleMgtWrapper.class);

        Whitebox.setInternalState(droolsEngine, "ruleMgtWrapper", ruleMgtWrapper);

        Whitebox.setInternalState(droolsEngine, "kconf", kconf);
        Whitebox.setInternalState(droolsEngine, "kbase", kbase);
        Whitebox.setInternalState(droolsEngine, "ksession", ksession);

        PowerMock.resetAll();
    }

    @Test
    public void init() throws Exception {

        List<CorrelationRule> rules = new ArrayList<CorrelationRule>();
        CorrelationRule rule = new CorrelationRule();
        rule.setContent("content");
        rules.add(rule);

        expect(ruleMgtWrapper.queryRuleByEnable(anyInt())).andReturn(rules);
        PowerMock.replayAll();

        Method method = DroolsEngine.class.getDeclaredMethod("init");
        method.setAccessible(true);
        method.invoke(droolsEngine);

        PowerMock.verifyAll();
    }

    @Test
    public void deployRule_rule_is_null() throws CorrelationException {
        Locale locale = PowerMock.createMock(Locale.class);

        thrown.expect(NullPointerException.class);

        droolsEngine.deployRule(null, locale);
    }

    @Test
    public void deployRule_kbuilder_has_errors() throws CorrelationException {
        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent("rule123");
        Locale locale = new Locale(AlarmConst.I18N_EN);

        thrown.expect(CorrelationException.class);

        droolsEngine.deployRule(rule, locale);
    }

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

        thrown.expect(CorrelationException.class);

        droolsEngine.deployRule(rule, locale);
        droolsEngine.deployRule(rule, locale);
    }

    @Test
    public void undeployRule_package_name_is_null() throws CorrelationException {
        String packageName = null;
        Locale locale = new Locale(AlarmConst.I18N_EN);

        thrown.expect(CorrelationException.class);

        droolsEngine.undeployRule(packageName, locale);
    }

    @Test
    public void undeployRule_normal() throws CorrelationException {
        Locale locale = new Locale(AlarmConst.I18N_EN);

        DeployRuleRequest rule = new DeployRuleRequest();
        rule.setContent("package rule123");
        droolsEngine.deployRule(rule, locale);

        String packageName = "rule123";

        droolsEngine.undeployRule(packageName, locale);
    }

    @Test
    public void compileRule_kbuilder_has_errors() throws CorrelationException {
        String content = "have error content";
        Locale locale = new Locale(AlarmConst.I18N_EN);

        thrown.expect(CorrelationException.class);

        droolsEngine.compileRule(content, locale);
    }

    @Test
    public void putRaisedIntoStream_facthandle_is_null() {
        VesAlarm raiseAlarm = new VesAlarm();
        raiseAlarm.setVersion((long) 245235);
        droolsEngine.putRaisedIntoStream(raiseAlarm);
        droolsEngine.putRaisedIntoStream(raiseAlarm);
    }

    @Test
    public void putRaisedIntoStream_factHandle_is_not_null() {
        VesAlarm raiseAlarm = new VesAlarm();
        raiseAlarm.setVersion((long) 245235);
        droolsEngine.putRaisedIntoStream(raiseAlarm);
    }

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