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