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.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 java.util.HashMap;
38 import static org.hamcrest.CoreMatchers.containsString;
39 import static org.hamcrest.core.IsNot.not;
40 import static org.junit.Assert.assertThat;
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest({JerseyClient.class})
44 @SuppressStaticInitializationFor({"org.onap.holmes.common.utils.JerseyClient"})
45 public class ConfigFileScanningTaskTest {
48 public final SystemOutRule systemOut = new SystemOutRule().enableLog();
51 public void run_failed_to_get_existing_rules() throws Exception {
52 System.setProperty("ENABLE_ENCRYPT", "true");
54 String indexPath = getFilePath("index-add.json");
56 ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
57 Whitebox.setInternalState(cfst, "configFile", indexPath);
59 // mock for getExistingRules
60 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
61 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
62 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andThrow(new RuntimeException());
64 PowerMock.replayAll();
66 PowerMock.verifyAll();
68 assertThat(systemOut.getLog(), containsString("Failed to get existing rules for comparison."));
72 public void run_add_rules() throws Exception {
73 System.setProperty("ENABLE_ENCRYPT", "true");
75 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
76 String indexPath = getFilePath("index-add.json");
77 String contents = FileUtils.readTextFile(indexPath);
79 ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
80 Whitebox.setInternalState(cfst, "configFile", indexPath);
82 // mock for getExistingRules
83 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
84 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
85 RuleQueryListResponse rqlr = new RuleQueryListResponse();
86 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
88 // mock for deployRule
89 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
90 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
92 PowerMock.replayAll();
94 PowerMock.verifyAll();
96 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been deployed."));
98 System.clearProperty("ENABLE_ENCRYPT");
102 public void run_remove_rules_normal() throws Exception {
103 System.setProperty("ENABLE_ENCRYPT", "false");
105 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
106 String indexPath = getFilePath("index-add.json");
107 String contents = FileUtils.readTextFile(indexPath);
109 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
110 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
112 // mock for getExistingRules
113 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
114 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
115 RuleQueryListResponse rqlr = new RuleQueryListResponse();
116 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
117 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
119 // mock for deleteRule
120 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
122 PowerMock.replayAll();
124 PowerMock.verifyAll();
126 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed."));
128 System.clearProperty("ENABLE_ENCRYPT");
132 public void run_remove_rules_api_calling_returning_null() throws Exception {
133 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
134 String indexPath = getFilePath("index-add.json");
135 String contents = FileUtils.readTextFile(indexPath);
137 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
138 Whitebox.setInternalState(cfst, "configFile", indexPath);
140 // mock for getExistingRules
141 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
142 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
143 RuleQueryListResponse rqlr = new RuleQueryListResponse();
144 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
145 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
147 // mock for deleteRule
148 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
150 PowerMock.replayAll();
152 PowerMock.verifyAll();
154 assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"));
158 public void run_change_rules_normal() throws Exception {
159 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
160 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
161 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
163 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
164 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
166 // mock for getExistingRules
167 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
168 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
169 RuleQueryListResponse rqlr = new RuleQueryListResponse();
170 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
171 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
173 // mock for deleteRule
174 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
176 // mock for deployRule
177 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
178 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
180 PowerMock.replayAll();
182 PowerMock.verifyAll();
184 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated."));
188 public void run_change_rules_no_change_except_for_spaces() throws Exception {
189 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
190 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
191 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
193 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
194 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
196 // mock for getExistingRules
197 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
198 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
199 RuleQueryListResponse rqlr = new RuleQueryListResponse();
200 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
201 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
203 PowerMock.replayAll();
205 PowerMock.verifyAll();
207 assertThat(systemOut.getLog(), not(containsString("has been updated.")));
210 private String getFilePath(String fileName) {
211 return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
214 private RuleResult4API getRuleResult4API(String clName, String contents) {
215 RuleResult4API ruleResult4API = new RuleResult4API();
216 ruleResult4API.setRuleId(clName);
217 ruleResult4API.setRuleName(clName);
218 ruleResult4API.setLoopControlName(clName);
219 ruleResult4API.setContent(contents);
220 ruleResult4API.setDescription("");
221 ruleResult4API.setEnabled(1);
222 return ruleResult4API;