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.modules.junit4.PowerMockRunner;
32 import org.powermock.reflect.Whitebox;
34 import static org.hamcrest.CoreMatchers.containsString;
35 import static org.hamcrest.core.IsNot.not;
36 import static org.junit.Assert.assertThat;
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest({JerseyClient.class})
40 public class ConfigFileScanningTaskTest {
43 public final SystemOutRule systemOut = new SystemOutRule().enableLog();
46 public void run_failed_to_get_existing_rules() throws Exception {
47 System.setProperty("ENABLE_ENCRYPT", "true");
49 String indexPath = getFilePath("index-add.json");
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 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andThrow(new RuntimeException());
59 PowerMock.replayAll();
61 PowerMock.verifyAll();
63 assertThat(systemOut.getLog(), containsString("Failed to get existing rules for comparison."));
67 public void run_add_rules() throws Exception {
68 System.setProperty("ENABLE_ENCRYPT", "true");
70 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
71 String indexPath = getFilePath("index-add.json");
72 String contents = FileUtils.readTextFile(indexPath);
74 ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
75 Whitebox.setInternalState(cfst, "configFile", indexPath);
77 // mock for getExistingRules
78 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
79 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
80 RuleQueryListResponse rqlr = new RuleQueryListResponse();
81 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
83 // mock for deployRule
84 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
85 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
87 PowerMock.replayAll();
89 PowerMock.verifyAll();
91 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been deployed."));
93 System.clearProperty("ENABLE_ENCRYPT");
97 public void run_remove_rules_normal() throws Exception {
98 System.setProperty("ENABLE_ENCRYPT", "false");
100 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
101 String indexPath = getFilePath("index-add.json");
102 String contents = FileUtils.readTextFile(indexPath);
104 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
105 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
107 // mock for getExistingRules
108 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
109 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
110 RuleQueryListResponse rqlr = new RuleQueryListResponse();
111 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
112 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
114 // mock for deleteRule
115 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
117 PowerMock.replayAll();
119 PowerMock.verifyAll();
121 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed."));
123 System.clearProperty("ENABLE_ENCRYPT");
127 public void run_remove_rules_api_calling_returning_null() throws Exception {
128 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
129 String indexPath = getFilePath("index-add.json");
130 String contents = FileUtils.readTextFile(indexPath);
132 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
133 Whitebox.setInternalState(cfst, "configFile", indexPath);
135 // mock for getExistingRules
136 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
137 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
138 RuleQueryListResponse rqlr = new RuleQueryListResponse();
139 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
140 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
142 // mock for deleteRule
143 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
145 PowerMock.replayAll();
147 PowerMock.verifyAll();
149 assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"));
153 public void run_change_rules_normal() throws Exception {
154 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
155 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
156 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
158 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
159 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
161 // mock for getExistingRules
162 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
163 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
164 RuleQueryListResponse rqlr = new RuleQueryListResponse();
165 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
166 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
168 // mock for deleteRule
169 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
171 // mock for deployRule
172 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
173 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
175 PowerMock.replayAll();
177 PowerMock.verifyAll();
179 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated."));
183 public void run_change_rules_no_change_except_for_spaces() throws Exception {
184 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
185 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
186 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
188 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
189 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
191 // mock for getExistingRules
192 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
193 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
194 RuleQueryListResponse rqlr = new RuleQueryListResponse();
195 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
196 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
198 PowerMock.replayAll();
200 PowerMock.verifyAll();
202 assertThat(systemOut.getLog(), not(containsString("has been updated.")));
205 private String getFilePath(String fileName) {
206 return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
209 private RuleResult4API getRuleResult4API(String clName, String contents) {
210 RuleResult4API ruleResult4API = new RuleResult4API();
211 ruleResult4API.setRuleId(clName);
212 ruleResult4API.setRuleName(clName);
213 ruleResult4API.setLoopControlName(clName);
214 ruleResult4API.setContent(contents);
215 ruleResult4API.setDescription("");
216 ruleResult4API.setEnabled(1);
217 ruleResult4API.setCreator("__SYSTEM__DEFAULT__");
218 return ruleResult4API;