summaryrefslogtreecommitdiffstats
path: root/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTaskTest.java
blob: a4ab15b42003efaeb7233b411791142c3a3b25aa (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
/**
 * Copyright 2021-2022 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.rulemgt.dcae;

import org.easymock.EasyMock;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.runner.RunWith;
import org.onap.holmes.common.ConfigFileScanner;
import org.onap.holmes.common.utils.FileUtils;
import org.onap.holmes.common.utils.JerseyClient;
import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;

@RunWith(PowerMockRunner.class)
@PrepareForTest({JerseyClient.class})
public class ConfigFileScanningTaskTest {

    @Rule
    public final SystemOutRule systemOut = new SystemOutRule().enableLog();

    @Test
    public void run_failed_to_get_existing_rules() throws Exception {
        System.setProperty("ENABLE_ENCRYPT", "true");

        String indexPath = getFilePath("index-add.json");

        ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
        Whitebox.setInternalState(cfst, "configFile", indexPath);

        // mock for getExistingRules
        JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
        PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
        EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andThrow(new RuntimeException());

        PowerMock.replayAll();
        cfst.run();
        PowerMock.verifyAll();

        assertThat(systemOut.getLog(), containsString("Failed to get existing rules for comparison."));
    }

    @Test
    public void run_add_rules() throws Exception {
        System.setProperty("ENABLE_ENCRYPT", "true");

        String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
        String indexPath = getFilePath("index-add.json");
        String contents = FileUtils.readTextFile(indexPath);

        ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
        Whitebox.setInternalState(cfst, "configFile", indexPath);

        // mock for getExistingRules
        JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
        PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
        RuleQueryListResponse rqlr = new RuleQueryListResponse();
        EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);

        // mock for deployRule
        EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
        EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");

        PowerMock.replayAll();
        cfst.run();
        PowerMock.verifyAll();

        assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been deployed."));

        System.clearProperty("ENABLE_ENCRYPT");
    }

    @Test
    public void run_remove_rules_normal() throws Exception {
        System.setProperty("ENABLE_ENCRYPT", "false");

        String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
        String indexPath = getFilePath("index-add.json");
        String contents = FileUtils.readTextFile(indexPath);

        ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
        Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));

        // mock for getExistingRules
        JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
        PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
        RuleQueryListResponse rqlr = new RuleQueryListResponse();
        rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
        EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);

        // mock for deleteRule
        EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");

        PowerMock.replayAll();
        cfst.run();
        PowerMock.verifyAll();

        assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed."));

        System.clearProperty("ENABLE_ENCRYPT");
    }

    @Test
    public void run_remove_rules_api_calling_returning_null() throws Exception {
        String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
        String indexPath = getFilePath("index-add.json");
        String contents = FileUtils.readTextFile(indexPath);

        ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
        Whitebox.setInternalState(cfst, "configFile", indexPath);

        // mock for getExistingRules
        JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
        PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
        RuleQueryListResponse rqlr = new RuleQueryListResponse();
        rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
        EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);

        // mock for deleteRule
        EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);

        PowerMock.replayAll();
        cfst.run();
        PowerMock.verifyAll();

        assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"));
    }

    @Test
    public void run_change_rules_normal() throws Exception {
        String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
        String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
        String oldDrlContents = FileUtils.readTextFile(oldDrlPath);

        ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
        Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));

        // mock for getExistingRules
        JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
        PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
        RuleQueryListResponse rqlr = new RuleQueryListResponse();
        rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
        EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);

        // mock for deleteRule
        EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");

        // mock for deployRule
        EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
        EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");

        PowerMock.replayAll();
        cfst.run();
        PowerMock.verifyAll();

        assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated."));
    }

    @Test
    public void run_change_rules_no_change_except_for_spaces() throws Exception {
        String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
        String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
        String oldDrlContents = FileUtils.readTextFile(oldDrlPath);

        ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
        Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));

        // mock for getExistingRules
        JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
        PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
        RuleQueryListResponse rqlr = new RuleQueryListResponse();
        rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
        EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);

        PowerMock.replayAll();
        cfst.run();
        PowerMock.verifyAll();

        assertThat(systemOut.getLog(), not(containsString("has been updated.")));
    }

    private String getFilePath(String fileName) {
        return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
    }

    private RuleResult4API getRuleResult4API(String clName, String contents) {
        RuleResult4API ruleResult4API = new RuleResult4API();
        ruleResult4API.setRuleId(clName);
        ruleResult4API.setRuleName(clName);
        ruleResult4API.setLoopControlName(clName);
        ruleResult4API.setContent(contents);
        ruleResult4API.setDescription("");
        ruleResult4API.setEnabled(1);
        ruleResult4API.setCreator("__SYSTEM__DEFAULT__");
        return ruleResult4API;
    }
}