bugfix - rule deployment failure due to existed rule name
[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
109         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
110         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json"));
111
112         // mock for getExistingRules
113         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
114         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
115         RuleQueryListResponse rqlr = new RuleQueryListResponse();
116         rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
117         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
118
119         // mock for deleteRule
120         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
121
122         PowerMock.replayAll();
123         cfst.run();
124         PowerMock.verifyAll();
125
126         assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed."));
127
128         System.clearProperty("ENABLE_ENCRYPT");
129     }
130
131     @Test
132     public void run_remove_rules_api_calling_returning_null() throws Exception {
133         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
134         String indexPath = getFilePath("index-add.json");
135         String contents = FileUtils.readTextFile(indexPath);
136
137         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
138         Whitebox.setInternalState(cfst, "configFile", indexPath);
139
140         // mock for getExistingRules
141         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
142         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
143         RuleQueryListResponse rqlr = new RuleQueryListResponse();
144         rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents));
145         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
146
147         // mock for deleteRule
148         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(null);
149
150         PowerMock.replayAll();
151         cfst.run();
152         PowerMock.verifyAll();
153
154         assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"));
155     }
156
157     @Test
158     public void run_change_rules_normal() throws Exception {
159         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
160         String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
161         String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
162
163         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
164         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json"));
165
166         // mock for getExistingRules
167         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
168         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
169         RuleQueryListResponse rqlr = new RuleQueryListResponse();
170         rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
171         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
172
173         // mock for deleteRule
174         EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn("");
175
176         // mock for deployRule
177         EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock);
178         EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn("");
179
180         PowerMock.replayAll();
181         cfst.run();
182         PowerMock.verifyAll();
183
184         assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated."));
185     }
186
187     @Test
188     public void run_change_rules_no_change_except_for_spaces() throws Exception {
189         String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b";
190         String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl");
191         String oldDrlContents = FileUtils.readTextFile(oldDrlPath);
192
193         ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner());
194         Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json"));
195
196         // mock for getExistingRules
197         JerseyClient jcMock = PowerMock.createMock(JerseyClient.class);
198         PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes();
199         RuleQueryListResponse rqlr = new RuleQueryListResponse();
200         rqlr.getCorrelationRules().add(getRuleResult4API(clName, oldDrlContents));
201         EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr);
202
203         PowerMock.replayAll();
204         cfst.run();
205         PowerMock.verifyAll();
206
207         assertThat(systemOut.getLog(), not(containsString("has been updated.")));
208     }
209
210     private String getFilePath(String fileName) {
211         return ConfigFileScanningTaskTest.class.getResource("/" + fileName).getFile();
212     }
213
214     private RuleResult4API getRuleResult4API(String clName, String contents) {
215         RuleResult4API ruleResult4API = new RuleResult4API();
216         ruleResult4API.setRuleId(clName);
217         ruleResult4API.setRuleName(clName);
218         ruleResult4API.setLoopControlName(clName);
219         ruleResult4API.setContent(contents);
220         ruleResult4API.setDescription("");
221         ruleResult4API.setEnabled(1);
222         return ruleResult4API;
223     }
224 }