Change rule retrieval from CBS to ConfigMap
[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         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
48         String indexPath = getFilePath("index-add.json");
49         String contents = FileUtils.readTextFile(indexPath);
50
51         ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
52         Whitebox.setInternalState(cfst, "configFile", indexPath);
53
54         // mock for getExistingRules
55         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
56         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
57         RuleQueryListResponse rqlr = new RuleQueryListResponse();
58         rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
59         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
60
61         // mock for deployRule
62         EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
63         EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
64
65         PowerMock.replayAll();
66         cfst.run();
67         PowerMock.verifyAll();
68
69         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
70         assertThat(config.size(), is(1));
71     }
72
73     @Test
74     public void run_remove_rules_normal() throws Exception {
75         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
76         String indexPath = getFilePath("index-add.json");
77         String contents = FileUtils.readTextFile(indexPath);
78         Map<String, String> configInEffect = new HashMap<>();
79         configInEffect.put(clName, contents);
80
81         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
82         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
83         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
84
85         // mock for getExistingRules
86         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
87         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
88         RuleQueryListResponse rqlr = new RuleQueryListResponse();
89         rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
90         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
91
92         // mock for deleteRule
93         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
94
95         PowerMock.replayAll();
96         cfst.run();
97         PowerMock.verifyAll();
98
99         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
100         assertThat(config.size(), is(0));
101     }
102
103     @Test
104     public void run_remove_rules_delete_null_pointer() throws Exception {
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);
110
111         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
112         Whitebox.setInternalState(cfst, "configFile", indexPath);
113         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
114
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("a-non-existing-rule", contents));
120         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
121
122         PowerMock.replayAll();
123         cfst.run();
124         PowerMock.verifyAll();
125
126         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
127         assertThat(config.size(), is(1));
128     }
129
130     @Test
131     public void run_remove_rules_api_calling_returning_null() throws Exception {
132         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
133         String indexPath = getFilePath("index-add.json");
134         String contents = FileUtils.readTextFile(indexPath);
135         Map<String, String> configInEffect = new HashMap<>();
136         configInEffect.put(clName, contents);
137
138         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
139         Whitebox.setInternalState(cfst, "configFile", indexPath);
140         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
141
142         // mock for getExistingRules
143         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
144         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
145         RuleQueryListResponse rqlr = new RuleQueryListResponse();
146         rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
147         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
148
149         // mock for deleteRule
150         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
151
152         PowerMock.replayAll();
153         cfst.run();
154         PowerMock.verifyAll();
155
156         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
157         assertThat(config.size(), is(1));
158     }
159
160     @Test
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);
165         Map<String, String> configInEffect = new HashMap<>();
166         configInEffect.put(clName, oldDrlContents);
167
168         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
169         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
170         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
171
172         // mock for getExistingRules
173         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
174         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
175         RuleQueryListResponse rqlr = new RuleQueryListResponse();
176         rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
177         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
178
179         // mock for deleteRule
180         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
181
182         // mock for deployRule
183         EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
184         EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
185
186         PowerMock.replayAll();
187         cfst.run();
188         PowerMock.verifyAll();
189
190         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
191         assertThat(config.size(), is(1));
192         assertThat(config.get(clName),
193                 equalTo(FileUtils.readTextFile(
194                         getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b-changed.drl"))));
195     }
196
197     @Test
198     public void run_change_rules_no_change_except_for_spaces() throws Exception {
199         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
200         String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
201         String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
202         Map<String, String> configInEffect = new HashMap<>();
203         configInEffect.put(clName, oldDrlContents);
204
205         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
206         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
207         Whitebox.setInternalState(cfst, "configInEffect", configInEffect);
208
209         // mock for getExistingRules
210         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
211         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
212         RuleQueryListResponse rqlr = new RuleQueryListResponse();
213         rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
214         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
215
216         PowerMock.replayAll();
217         cfst.run();
218         PowerMock.verifyAll();
219
220         Map<String, String> config = Whitebox.getInternalState(cfst, "configInEffect");
221         assertThat(config.size(), is(1));
222         assertThat(config.get(clName),
223                 equalTo(FileUtils.readTextFile(
224                         getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl"))));
225     }
226
227     private String getFilePath(String fileName) {
228         return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
229     }
230
231     private RuleResult4API getRuleResult4API(String clName, String contents) {
232         RuleResult4API ruleResult4API = new RuleResult4API();
233         ruleResult4API.setRuleId(clName);
234         ruleResult4API.setRuleName(clName);
235         ruleResult4API.setLoopControlName(clName);
236         ruleResult4API.setContent(contents);
237         ruleResult4API.setDescription("");
238         ruleResult4API.setEnabled(1);
239         return ruleResult4API;
240     }
241 }