update link to upper-constraints.txt
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / dcae / ConfigFileScanningTaskTest.java
1 /**
2  * Copyright 2021-2022 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 static org.hamcrest.CoreMatchers.containsString;
36 import static org.hamcrest.core.IsNot.not;
37 import static org.junit.Assert.assertThat;
38
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest({JerseyClient.class})
41 @SuppressStaticInitializationFor({"org.onap.holmes.common.utils.JerseyClient"})
42 public class ConfigFileScanningTaskTest {
43
44     @Rule
45     public final SystemOutRule systemOut = new SystemOutRule().enableLog();
46
47     @Test
48     public void run_failed_to_get_existing_rules() throws Exception {
49         System.setProperty("ENABLE_ENCRYPT", "true");
50
51         String indexPath = getFilePath("index-add.json");
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         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andThrow(new RuntimeException());
60
61         PowerMock.replayAll();
62         cfst.run();
63         PowerMock.verifyAll();
64
65         assertThat(systemOut.getLog(), containsString("Failed to get existing rules for comparison."));
66     }
67
68     @Test
69     public void run_add_rules() throws Exception {
70         System.setProperty("ENABLE_ENCRYPT", "true");
71
72         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
73         String indexPath = getFilePath("index-add.json");
74         String contents = FileUtils.readTextFile(indexPath);
75
76         ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
77         Whitebox.setInternalState(cfst, "configFile", indexPath);
78
79         // mock for getExistingRules
80         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
81         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
82         RuleQueryListResponse rqlr = new RuleQueryListResponse();
83         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
84
85         // mock for deployRule
86         EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock).times(2);
87         EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
88
89         PowerMock.replayAll();
90         cfst.run();
91         PowerMock.verifyAll();
92
93         assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been deployed."));
94
95         System.clearProperty("ENABLE_ENCRYPT");
96     }
97
98     @Test
99     public void run_remove_rules_normal() throws Exception {
100         System.setProperty("ENABLE_ENCRYPT", "false");
101
102         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
103         String indexPath = getFilePath("index-add.json");
104         String contents = FileUtils.readTextFile(indexPath);
105
106         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
107         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
108
109         // mock for getExistingRules
110         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
111         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
112         RuleQueryListResponse rqlr = new RuleQueryListResponse();
113         rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
114         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
115
116         // mock for deleteRule
117         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
118
119         PowerMock.replayAll();
120         cfst.run();
121         PowerMock.verifyAll();
122
123         assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed."));
124
125         System.clearProperty("ENABLE_ENCRYPT");
126     }
127
128     @Test
129     public void run_remove_rules_api_calling_returning_null() throws Exception {
130         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
131         String indexPath = getFilePath("index-add.json");
132         String contents = FileUtils.readTextFile(indexPath);
133
134         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
135         Whitebox.setInternalState(cfst, "configFile", indexPath);
136
137         // mock for getExistingRules
138         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
139         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
140         RuleQueryListResponse rqlr = new RuleQueryListResponse();
141         rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
142         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
143
144         // mock for deleteRule
145         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
146
147         PowerMock.replayAll();
148         cfst.run();
149         PowerMock.verifyAll();
150
151         assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"));
152     }
153
154     @Test
155     public void run_change_rules_normal() throws Exception {
156         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
157         String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
158         String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
159
160         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
161         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
162
163         // mock for getExistingRules
164         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
165         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
166         RuleQueryListResponse rqlr = new RuleQueryListResponse();
167         rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
168         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
169
170         // mock for deleteRule
171         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
172
173         // mock for deployRule
174         EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock).times(2);
175         EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
176
177         PowerMock.replayAll();
178         cfst.run();
179         PowerMock.verifyAll();
180
181         assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated."));
182     }
183
184     @Test
185     public void run_change_rules_no_change_except_for_spaces() throws Exception {
186         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
187         String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
188         String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
189
190         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
191         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
192
193         // mock for getExistingRules
194         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
195         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
196         RuleQueryListResponse rqlr = new RuleQueryListResponse();
197         rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
198         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
199
200         PowerMock.replayAll();
201         cfst.run();
202         PowerMock.verifyAll();
203
204         assertThat(systemOut.getLog(), not(containsString("has been updated.")));
205     }
206
207     private String getFilePath(String fileName) {
208         return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
209     }
210
211     private RuleResult4API getRuleResult4API(String clName, String contents) {
212         RuleResult4API ruleResult4API = new RuleResult4API();
213         ruleResult4API.setRuleId(clName);
214         ruleResult4API.setRuleName(clName);
215         ruleResult4API.setLoopControlName(clName);
216         ruleResult4API.setContent(contents);
217         ruleResult4API.setDescription("");
218         ruleResult4API.setEnabled(1);
219         ruleResult4API.setCreator("__SYSTEM__DEFAULT__");
220         return ruleResult4API;
221     }
222 }