2 * Copyright 2021-2022 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.Rule;
21 import org.junit.Test;
22 import org.junit.contrib.java.lang.system.SystemOutRule;
23 import org.junit.runner.RunWith;
24 import org.onap.holmes.common.ConfigFileScanner;
25 import org.onap.holmes.common.utils.FileUtils;
26 import org.onap.holmes.common.utils.JerseyClient;
27 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
28 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
29 import org.powermock.api.easymock.PowerMock;
30 import org.powermock.core.classloader.annotations.PrepareForTest;
31 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
32 import org.powermock.modules.junit4.PowerMockRunner;
33 import org.powermock.reflect.Whitebox;
35 import static org.hamcrest.CoreMatchers.containsString;
36 import static org.hamcrest.core.IsNot.not;
37 import static org.junit.Assert.assertThat;
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest({JerseyClient.class})
41 @SuppressStaticInitializationFor({"org.onap.holmes.common.utils.JerseyClient"})
42 public class ConfigFileScanningTaskTest {
45 public final SystemOutRule systemOut = new SystemOutRule().enableLog();
48 public void run_failed_to_get_existing_rules() throws Exception {
49 System.setProperty("ENABLE_ENCRYPT", "true");
51 String indexPath = getFilePath("index-add.json");
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 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andThrow(new RuntimeException());
61 PowerMock.replayAll();
63 PowerMock.verifyAll();
65 assertThat(systemOut.getLog(), containsString("Failed to get existing rules for comparison."));
69 public void run_add_rules() throws Exception {
70 System.setProperty("ENABLE_ENCRYPT", "true");
72 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
73 String indexPath = getFilePath("index-add.json");
74 String contents = FileUtils.readTextFile(indexPath);
76 ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
77 Whitebox.setInternalState(cfst, "configFile", indexPath);
79 // mock for getExistingRules
80 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
81 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
82 RuleQueryListResponse rqlr = new RuleQueryListResponse();
83 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
85 // mock for deployRule
86 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock).times(2);
87 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
89 PowerMock.replayAll();
91 PowerMock.verifyAll();
93 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been deployed."));
95 System.clearProperty("ENABLE_ENCRYPT");
99 public void run_remove_rules_normal() throws Exception {
100 System.setProperty("ENABLE_ENCRYPT", "false");
102 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
103 String indexPath = getFilePath("index-add.json");
104 String contents = FileUtils.readTextFile(indexPath);
106 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
107 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
109 // mock for getExistingRules
110 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
111 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
112 RuleQueryListResponse rqlr = new RuleQueryListResponse();
113 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
114 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
116 // mock for deleteRule
117 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
119 PowerMock.replayAll();
121 PowerMock.verifyAll();
123 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed."));
125 System.clearProperty("ENABLE_ENCRYPT");
129 public void run_remove_rules_api_calling_returning_null() throws Exception {
130 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
131 String indexPath = getFilePath("index-add.json");
132 String contents = FileUtils.readTextFile(indexPath);
134 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
135 Whitebox.setInternalState(cfst, "configFile", indexPath);
137 // mock for getExistingRules
138 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
139 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
140 RuleQueryListResponse rqlr = new RuleQueryListResponse();
141 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
142 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
144 // mock for deleteRule
145 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
147 PowerMock.replayAll();
149 PowerMock.verifyAll();
151 assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"));
155 public void run_change_rules_normal() throws Exception {
156 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
157 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
158 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
160 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
161 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
163 // mock for getExistingRules
164 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
165 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
166 RuleQueryListResponse rqlr = new RuleQueryListResponse();
167 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
168 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
170 // mock for deleteRule
171 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
173 // mock for deployRule
174 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock).times(2);
175 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
177 PowerMock.replayAll();
179 PowerMock.verifyAll();
181 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated."));
185 public void run_change_rules_no_change_except_for_spaces() throws Exception {
186 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
187 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
188 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
190 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
191 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
193 // mock for getExistingRules
194 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
195 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
196 RuleQueryListResponse rqlr = new RuleQueryListResponse();
197 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
198 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
200 PowerMock.replayAll();
202 PowerMock.verifyAll();
204 assertThat(systemOut.getLog(), not(containsString("has been updated.")));
207 private String getFilePath(String fileName) {
208 return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
211 private RuleResult4API getRuleResult4API(String clName, String contents) {
212 RuleResult4API ruleResult4API = new RuleResult4API();
213 ruleResult4API.setRuleId(clName);
214 ruleResult4API.setRuleName(clName);
215 ruleResult4API.setLoopControlName(clName);
216 ruleResult4API.setContent(contents);
217 ruleResult4API.setDescription("");
218 ruleResult4API.setEnabled(1);
219 ruleResult4API.setCreator("__SYSTEM__DEFAULT__");
220 return ruleResult4API;