d6d93a9deab8b314029eae7ce15c4da6c8e8e199
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / dcae / ConfigFileScanningTaskTest.java
1 /**
2  * Copyright 2021 ZTE Corporation.
3  * <p>
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
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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.
15  */
16
17 package org.onap.holmes.rulemgt.dcae;
18
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;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import static org.hamcrest.core.Is.is;
37 import static org.hamcrest.core.IsEqual.equalTo;
38 import static org.junit.Assert.assertThat;
39
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest({JerseyClient.class})
42 @SuppressStaticInitializationFor({"org.onap.holmes.common.utils.JerseyClient"})
43 public class ConfigFileScanningTaskTest {
44
45     @Test
46     public void run_add_rules() throws Exception {
47         System.setProperty("ENABLE_ENCRYPT", "true");
48
49         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
50         String indexPath = getFilePath("index-add.json");
51         String contents = FileUtils.readTextFile(indexPath);
52
53         ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
54         Whitebox.setInternalState(cfst, "configFile", indexPath);
55
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);
62
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("");
66
67         PowerMock.replayAll();
68         cfst.run();
69         PowerMock.verifyAll();
70
71         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
72         assertThat(config.size(), is(1));
73
74         System.clearProperty("ENABLE_ENCRYPT");
75     }
76
77     @Test
78     public void run_remove_rules_normal() throws Exception {
79         System.setProperty("ENABLE_ENCRYPT", "false");
80
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);
86
87         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
88         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
89         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
90
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);
97
98         // mock for deleteRule
99         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
100
101         PowerMock.replayAll();
102         cfst.run();
103         PowerMock.verifyAll();
104
105         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
106         assertThat(config.size(), is(0));
107
108         System.clearProperty("ENABLE_ENCRYPT");
109     }
110
111     @Test
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);
118
119         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
120         Whitebox.setInternalState(cfst, "configFile", indexPath);
121         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
122
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);
129
130         PowerMock.replayAll();
131         cfst.run();
132         PowerMock.verifyAll();
133
134         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
135         assertThat(config.size(), is(1));
136     }
137
138     @Test
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);
145
146         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
147         Whitebox.setInternalState(cfst, "configFile", indexPath);
148         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
149
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);
156
157         // mock for deleteRule
158         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
159
160         PowerMock.replayAll();
161         cfst.run();
162         PowerMock.verifyAll();
163
164         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
165         assertThat(config.size(), is(1));
166     }
167
168     @Test
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);
175
176         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
177         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
178         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
179
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);
186
187         // mock for deleteRule
188         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
189
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("");
193
194         PowerMock.replayAll();
195         cfst.run();
196         PowerMock.verifyAll();
197
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"))));
203     }
204
205     @Test
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);
212
213         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
214         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
215         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
216
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);
223
224         PowerMock.replayAll();
225         cfst.run();
226         PowerMock.verifyAll();
227
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"))));
233     }
234
235     private String getFilePath(String fileName) {
236         return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
237     }
238
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;
248     }
249
250
251 }