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);
108 Map<String, String> configInEffect = new HashMap<>();
109 configInEffect.put(clName, contents);
111 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
112 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
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(clName, contents));
120 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
122 // mock for deleteRule
123 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
125 PowerMock.replayAll();
127 PowerMock.verifyAll();
129 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed."));
131 System.clearProperty("ENABLE_ENCRYPT");
135 public void run_remove_rules_api_calling_returning_null() throws Exception {
136 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
137 String indexPath = getFilePath("index-add.json");
138 String contents = FileUtils.readTextFile(indexPath);
140 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
141 Whitebox.setInternalState(cfst, "configFile", indexPath);
143 // mock for getExistingRules
144 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
145 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
146 RuleQueryListResponse rqlr = new RuleQueryListResponse();
147 rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
148 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
150 // mock for deleteRule
151 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
153 PowerMock.replayAll();
155 PowerMock.verifyAll();
157 assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"));
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);
166 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
167 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
169 // mock for getExistingRules
170 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
171 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
172 RuleQueryListResponse rqlr = new RuleQueryListResponse();
173 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
174 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
176 // mock for deleteRule
177 EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
179 // mock for deployRule
180 EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
181 EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
183 PowerMock.replayAll();
185 PowerMock.verifyAll();
187 assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated."));
191 public void run_change_rules_no_change_except_for_spaces() throws Exception {
192 String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
193 String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
194 String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
196 ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
197 Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
199 // mock for getExistingRules
200 JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
201 PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
202 RuleQueryListResponse rqlr = new RuleQueryListResponse();
203 rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
204 EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
206 PowerMock.replayAll();
208 PowerMock.verifyAll();
210 assertThat(systemOut.getLog(), not(containsString("has been updated.")));
213 private String getFilePath(String fileName) {
214 return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
217 private RuleResult4API getRuleResult4API(String clName, String contents) {
218 RuleResult4API ruleResult4API = new RuleResult4API();
219 ruleResult4API.setRuleId(clName);
220 ruleResult4API.setRuleName(clName);
221 ruleResult4API.setLoopControlName(clName);
222 ruleResult4API.setContent(contents);
223 ruleResult4API.setDescription("");
224 ruleResult4API.setEnabled(1);
225 return ruleResult4API;