2 * Copyright 2021 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.holmes.rulemgt.dcae;
19 import org.easymock.EasyMock;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.onap.holmes.common.ConfigFileScanner;
23 import org.onap.holmes.common.utils.FileUtils;
24 import org.onap.holmes.common.utils.JerseyClient;
25 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
26 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
27 import org.powermock.api.easymock.PowerMock;
28 import org.powermock.core.classloader.annotations.PrepareForTest;
29 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
30 import org.powermock.modules.junit4.PowerMockRunner;
31 import org.powermock.reflect.Whitebox;
33 import java.util.HashMap;
36 import static org.hamcrest.core.Is.is;
37 import static org.hamcrest.core.IsEqual.equalTo;
38 import static org.junit.Assert.assertThat;
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest({JerseyClient.class})
42 @SuppressStaticInitializationFor({"org.onap.holmes.common.utils.JerseyClient"})
43 public class ConfigFileScanningTaskTest {
46 public void run_add_rules() throws Exception {
47 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
48 String indexPath = getFilePath("index-add.json");
49 String contents = FileUtils.readTextFile(indexPath);
51 ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
52 Whitebox.setInternalState(cfst, "configFile", indexPath);
54 // mock for getExistingRules
55 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
56 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
57 RuleQueryListResponse rqlr = new RuleQueryListResponse();
58 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
59 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
61 // mock for deployRule
62 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
63 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
65 PowerMock.replayAll();
67 PowerMock.verifyAll();
69 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
70 assertThat(config.size(), is(1));
74 public void run_remove_rules_normal() throws Exception {
75 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
76 String indexPath = getFilePath("index-add.json");
77 String contents = FileUtils.readTextFile(indexPath);
78 Map<String, String> configInEffect = new HashMap<>();
79 configInEffect.put(clName, contents);
81 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
82 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
83 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
85 // mock for getExistingRules
86 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
87 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
88 RuleQueryListResponse rqlr = new RuleQueryListResponse();
89 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
90 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
92 // mock for deleteRule
93 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
95 PowerMock.replayAll();
97 PowerMock.verifyAll();
99 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
100 assertThat(config.size(), is(0));
104 public void run_remove_rules_delete_null_pointer() throws Exception {
105 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
106 String indexPath = getFilePath("index-add.json");
107 String contents = FileUtils.readTextFile(indexPath);
108 Map<String, String> configInEffect = new HashMap<>();
109 configInEffect.put(clName, contents);
111 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
112 Whitebox.setInternalState(cfst, "configFile", indexPath);
113 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
115 // mock for getExistingRules
116 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
117 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
118 RuleQueryListResponse rqlr = new RuleQueryListResponse();
119 rqlr.getCorrelationRules().add(getRuleResult4API("a-non-existing-rule", contents));
120 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
122 PowerMock.replayAll();
124 PowerMock.verifyAll();
126 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
127 assertThat(config.size(), is(1));
131 public void run_remove_rules_api_calling_returning_null() throws Exception {
132 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
133 String indexPath = getFilePath("index-add.json");
134 String contents = FileUtils.readTextFile(indexPath);
135 Map<String, String> configInEffect = new HashMap<>();
136 configInEffect.put(clName, contents);
138 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
139 Whitebox.setInternalState(cfst, "configFile", indexPath);
140 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
142 // mock for getExistingRules
143 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
144 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
145 RuleQueryListResponse rqlr = new RuleQueryListResponse();
146 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
147 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
149 // mock for deleteRule
150 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
152 PowerMock.replayAll();
154 PowerMock.verifyAll();
156 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
157 assertThat(config.size(), is(1));
161 public void run_change_rules_normal() throws Exception {
162 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
163 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
164 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
165 Map<String, String> configInEffect = new HashMap<>();
166 configInEffect.put(clName, oldDrlContents);
168 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
169 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
170 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
172 // mock for getExistingRules
173 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
174 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
175 RuleQueryListResponse rqlr = new RuleQueryListResponse();
176 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
177 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
179 // mock for deleteRule
180 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
182 // mock for deployRule
183 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
184 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
186 PowerMock.replayAll();
188 PowerMock.verifyAll();
190 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
191 assertThat(config.size(), is(1));
192 assertThat(config.get(clName),
193 equalTo(FileUtils.readTextFile(
194 getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b-changed.drl"))));
198 public void run_change_rules_no_change_except_for_spaces() throws Exception {
199 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
200 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
201 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
202 Map<String, String> configInEffect = new HashMap<>();
203 configInEffect.put(clName, oldDrlContents);
205 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
206 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
207 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
209 // mock for getExistingRules
210 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
211 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
212 RuleQueryListResponse rqlr = new RuleQueryListResponse();
213 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
214 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
216 PowerMock.replayAll();
218 PowerMock.verifyAll();
220 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
221 assertThat(config.size(), is(1));
222 assertThat(config.get(clName),
223 equalTo(FileUtils.readTextFile(
224 getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl"))));
227 private String getFilePath(String fileName) {
228 return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
231 private RuleResult4API getRuleResult4API(String clName, String contents) {
232 RuleResult4API ruleResult4API = new RuleResult4API();
233 ruleResult4API.setRuleId(clName);
234 ruleResult4API.setRuleName(clName);
235 ruleResult4API.setLoopControlName(clName);
236 ruleResult4API.setContent(contents);
237 ruleResult4API.setDescription("");
238 ruleResult4API.setEnabled(1);
239 return ruleResult4API;