bugfix - rule deployment issue during init
[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.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;
34
35 import java.util.HashMap;
36 import java.util.Map;
37
38 import static org.hamcrest.CoreMatchers.containsString;
39 import static org.hamcrest.core.IsNot.not;
40 import static org.junit.Assert.assertThat;
41
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest({JerseyClient.class})
44 @SuppressStaticInitializationFor({"org.onap.holmes.common.utils.JerseyClient"})
45 public class ConfigFileScanningTaskTest {
46
47     @Rule
48     public final SystemOutRule systemOut = new SystemOutRule().enableLog();
49
50     @Test
51     public void run_failed_to_get_existing_rules() throws Exception {
52         System.setProperty("ENABLE_ENCRYPT", "true");
53
54         String indexPath = getFilePath("index-add.json");
55
56         ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
57         Whitebox.setInternalState(cfst, "configFile", indexPath);
58
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());
63
64         PowerMock.replayAll();
65         cfst.run();
66         PowerMock.verifyAll();
67
68         assertThat(systemOut.getLog(), containsString("Failed to get existing rules for comparison."));
69     }
70
71     @Test
72     public void run_add_rules() throws Exception {
73         System.setProperty("ENABLE_ENCRYPT", "true");
74
75         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
76         String indexPath = getFilePath("index-add.json");
77         String contents = FileUtils.readTextFile(indexPath);
78
79         ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
80         Whitebox.setInternalState(cfst, "configFile", indexPath);
81
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);
87
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("");
91
92         PowerMock.replayAll();
93         cfst.run();
94         PowerMock.verifyAll();
95
96         assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been deployed."));
97
98         System.clearProperty("ENABLE_ENCRYPT");
99     }
100
101     @Test
102     public void run_remove_rules_normal() throws Exception {
103         System.setProperty("ENABLE_ENCRYPT", "false");
104
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", getFilePath("index-empty.json"));
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(clName, contents));
120         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
121
122         // mock for deleteRule
123         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
124
125         PowerMock.replayAll();
126         cfst.run();
127         PowerMock.verifyAll();
128
129         assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed."));
130
131         System.clearProperty("ENABLE_ENCRYPT");
132     }
133
134     @Test
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);
139
140         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
141         Whitebox.setInternalState(cfst, "configFile", indexPath);
142
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);
149
150         // mock for deleteRule
151         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
152
153         PowerMock.replayAll();
154         cfst.run();
155         PowerMock.verifyAll();
156
157         assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"));
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
166         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
167         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
168
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);
175
176         // mock for deleteRule
177         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
178
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("");
182
183         PowerMock.replayAll();
184         cfst.run();
185         PowerMock.verifyAll();
186
187         assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated."));
188     }
189
190     @Test
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);
195
196         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
197         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
198
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);
205
206         PowerMock.replayAll();
207         cfst.run();
208         PowerMock.verifyAll();
209
210         assertThat(systemOut.getLog(), not(containsString("has been updated.")));
211     }
212
213     private String getFilePath(String fileName) {
214         return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
215     }
216
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;
226     }
227 }