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 System.setProperty("ENABLE_ENCRYPT", "true");
49 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
50 String indexPath = getFilePath("index-add.json");
51 String contents = FileUtils.readTextFile(indexPath);
53 ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
54 Whitebox.setInternalState(cfst, "configFile", indexPath);
56 // mock for getExistingRules
57 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
58 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
59 RuleQueryListResponse rqlr = new RuleQueryListResponse();
60 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
61 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
63 // mock for deployRule
64 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
65 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
67 PowerMock.replayAll();
69 PowerMock.verifyAll();
71 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
72 assertThat(config.size(), is(1));
74 System.clearProperty("ENABLE_ENCRYPT");
78 public void run_remove_rules_normal() throws Exception {
79 System.setProperty("ENABLE_ENCRYPT", "false");
81 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
82 String indexPath = getFilePath("index-add.json");
83 String contents = FileUtils.readTextFile(indexPath);
84 Map<String, String> configInEffect = new HashMap<>();
85 configInEffect.put(clName, contents);
87 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
88 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
89 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
91 // mock for getExistingRules
92 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
93 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
94 RuleQueryListResponse rqlr = new RuleQueryListResponse();
95 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
96 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
98 // mock for deleteRule
99 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
101 PowerMock.replayAll();
103 PowerMock.verifyAll();
105 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
106 assertThat(config.size(), is(0));
108 System.clearProperty("ENABLE_ENCRYPT");
112 public void run_remove_rules_delete_null_pointer() throws Exception {
113 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
114 String indexPath = getFilePath("index-add.json");
115 String contents = FileUtils.readTextFile(indexPath);
116 Map<String, String> configInEffect = new HashMap<>();
117 configInEffect.put(clName, contents);
119 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
120 Whitebox.setInternalState(cfst, "configFile", indexPath);
121 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
123 // mock for getExistingRules
124 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
125 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
126 RuleQueryListResponse rqlr = new RuleQueryListResponse();
127 rqlr.getCorrelationRules().add(getRuleResult4API("a-non-existing-rule", contents));
128 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
130 PowerMock.replayAll();
132 PowerMock.verifyAll();
134 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
135 assertThat(config.size(), is(1));
139 public void run_remove_rules_api_calling_returning_null() throws Exception {
140 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
141 String indexPath = getFilePath("index-add.json");
142 String contents = FileUtils.readTextFile(indexPath);
143 Map<String, String> configInEffect = new HashMap<>();
144 configInEffect.put(clName, contents);
146 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
147 Whitebox.setInternalState(cfst, "configFile", indexPath);
148 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
150 // mock for getExistingRules
151 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
152 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
153 RuleQueryListResponse rqlr = new RuleQueryListResponse();
154 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
155 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
157 // mock for deleteRule
158 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
160 PowerMock.replayAll();
162 PowerMock.verifyAll();
164 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
165 assertThat(config.size(), is(1));
169 public void run_change_rules_normal() throws Exception {
170 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
171 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
172 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
173 Map<String, String> configInEffect = new HashMap<>();
174 configInEffect.put(clName, oldDrlContents);
176 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
177 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
178 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
180 // mock for getExistingRules
181 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
182 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
183 RuleQueryListResponse rqlr = new RuleQueryListResponse();
184 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
185 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
187 // mock for deleteRule
188 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
190 // mock for deployRule
191 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
192 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
194 PowerMock.replayAll();
196 PowerMock.verifyAll();
198 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
199 assertThat(config.size(), is(1));
200 assertThat(config.get(clName),
201 equalTo(FileUtils.readTextFile(
202 getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b-changed.drl"))));
206 public void run_change_rules_no_change_except_for_spaces() throws Exception {
207 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
208 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
209 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
210 Map<String, String> configInEffect = new HashMap<>();
211 configInEffect.put(clName, oldDrlContents);
213 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
214 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
215 Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
217 // mock for getExistingRules
218 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
219 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
220 RuleQueryListResponse rqlr = new RuleQueryListResponse();
221 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
222 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
224 PowerMock.replayAll();
226 PowerMock.verifyAll();
228 Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
229 assertThat(config.size(), is(1));
230 assertThat(config.get(clName),
231 equalTo(FileUtils.readTextFile(
232 getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl"))));
235 private String getFilePath(String fileName) {
236 return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
239 private RuleResult4API getRuleResult4API(String clName, String contents) {
240 RuleResult4API ruleResult4API = new RuleResult4API();
241 ruleResult4API.setRuleId(clName);
242 ruleResult4API.setRuleName(clName);
243 ruleResult4API.setLoopControlName(clName);
244 ruleResult4API.setContent(contents);
245 ruleResult4API.setDescription("");
246 ruleResult4API.setEnabled(1);
247 return ruleResult4API;