Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-management / src / test / java / org / onap / policy / drools / persistence / SystemPersistenceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.persistence;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.List;
35 import java.util.Properties;
36 import org.junit.jupiter.api.AfterAll;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.MethodOrderer;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.TestMethodOrder;
41 import org.onap.policy.drools.properties.DroolsPropertyConstants;
42
43 /**
44  * (File) System Persistence Tests.
45  */
46 @TestMethodOrder(MethodOrderer.MethodName.class)
47 public class SystemPersistenceTest {
48     /**
49      * sample configuration dir.
50      */
51     private static final String OTHER_CONFIG_DIR = "tmp";
52
53     /**
54      * Test JUnit Controller Name.
55      */
56     private static final String TEST_CONTROLLER_NAME = "foo";
57
58
59     /**
60      * Test JUnit Topic Name.
61      */
62     private static final String TEST_TOPIC_NAME = TEST_CONTROLLER_NAME;
63
64     /**
65      * Test JUnit HTTP Server Name.
66      */
67     private static final String TEST_HTTP_SERVER_NAME = TEST_CONTROLLER_NAME;
68
69     /**
70      * Test JUnit HTTP Client Name.
71      */
72     private static final String TEST_HTTP_CLIENT_NAME = TEST_CONTROLLER_NAME;
73
74     /**
75      * Test JUnit Controller File.
76      */
77     private static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
78
79     /**
80      * Test JUnit Controller Backup File.
81      */
82     private static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_FILE + ".bak";
83
84     /**
85      * Test JUnit Topic File.
86      */
87     private static final String TEST_TOPIC_FILE = TEST_CONTROLLER_NAME + "-topic.properties";
88
89     /**
90      * Test JUnit Controller Name Backup.
91      */
92     private static final String TEST_TOPIC_FILE_BAK = TEST_TOPIC_FILE + ".bak";
93
94     /**
95      * Test JUnit Http Server File.
96      */
97     private static final String TEST_HTTP_SERVER_FILE = TEST_CONTROLLER_NAME
98                             + FileSystemPersistence.PROPERTIES_FILE_HTTP_SERVER_SUFFIX;
99
100     /**
101      * Test JUnit Backup Http Server File.
102      */
103     private static final String TEST_HTTP_SERVER_FILE_BAK = TEST_HTTP_SERVER_FILE + ".bak";
104
105     /**
106      * Test JUnit Http Client File.
107      */
108     private static final String TEST_HTTP_CLIENT_FILE = TEST_CONTROLLER_NAME
109                             + FileSystemPersistence.PROPERTIES_FILE_HTTP_CLIENT_SUFFIX;
110
111     /**
112      * Test JUnit Backup Http Server File.
113      */
114     private static final String TEST_HTTP_CLIENT_FILE_BAK = TEST_HTTP_CLIENT_FILE + ".bak";
115
116     /**
117      * Test JUnit Environment/Engine properties.
118      */
119     private static final String ENV_PROPS = TEST_CONTROLLER_NAME;
120     private static final String ENV_PROPS_FILE = ENV_PROPS + ".environment";
121
122     /**
123      * Test JUnit system properties.
124      */
125     private static final String SYSTEM_PROPS = TEST_CONTROLLER_NAME;
126     private static final String SYSTEM_PROPS_FILE =  SYSTEM_PROPS + "-system.properties";
127
128     @BeforeAll
129     public static void setUp() throws IOException {
130         cleanUpWorkingDirs();
131     }
132
133     @AfterAll
134     public static void tearDown() throws IOException {
135         cleanUpWorkingDirs();
136     }
137
138     @Test
139     void test1NonDefaultConfigDir() {
140         SystemPersistenceConstants.getManager().setConfigurationDir(OTHER_CONFIG_DIR);
141         assertEquals(OTHER_CONFIG_DIR, SystemPersistenceConstants.getManager().getConfigurationPath().toString());
142
143         SystemPersistenceConstants.getManager().setConfigurationDir(null);
144         assertEquals(SystemPersistenceConstants.DEFAULT_CONFIGURATION_DIR,
145                 SystemPersistenceConstants.getManager().getConfigurationPath().toString());
146
147         SystemPersistenceConstants.getManager().setConfigurationDir();
148         assertEquals(SystemPersistenceConstants.DEFAULT_CONFIGURATION_DIR,
149                 SystemPersistenceConstants.getManager().getConfigurationPath().toString());
150     }
151
152     @Test
153     void test2Engine_Environment_System() throws IOException {
154         SystemPersistenceConstants.getManager().setConfigurationDir(OTHER_CONFIG_DIR);
155
156         final Path policyEnginePropsPath =
157                 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
158                         FileSystemPersistence.PROPERTIES_FILE_ENGINE);
159
160         final Properties engineProps = new Properties();
161         engineProps.setProperty("foo", "bar");
162         engineProps.setProperty("fiz", "buz");
163         if (Files.notExists(policyEnginePropsPath)) {
164             try (final OutputStream fout = new FileOutputStream(policyEnginePropsPath.toFile())) {
165                 engineProps.store(fout, "");
166             }
167         }
168
169         assertEquals(engineProps, SystemPersistenceConstants.getManager().getEngineProperties());
170
171         final Path environmentPropertiesPath =
172                 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), ENV_PROPS_FILE);
173         if (Files.notExists(environmentPropertiesPath)) {
174             Files.createFile(environmentPropertiesPath);
175         }
176         assertNotNull(SystemPersistenceConstants.getManager().getEnvironmentProperties(ENV_PROPS));
177         assertTrue(SystemPersistenceConstants.getManager().getEnvironmentProperties(ENV_PROPS).isEmpty());
178         assertEquals(1, SystemPersistenceConstants.getManager().getEnvironmentProperties().size());
179         assertEquals(SystemPersistenceConstants.getManager().getEnvironmentProperties(ENV_PROPS),
180                      SystemPersistenceConstants.getManager().getEnvironmentProperties().get(0));
181
182         Path systemPropertiesPath =
183             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), SYSTEM_PROPS_FILE);
184         if (Files.notExists(systemPropertiesPath)) {
185             Files.createFile(systemPropertiesPath);
186         }
187         assertNotNull(SystemPersistenceConstants.getManager().getSystemProperties(SYSTEM_PROPS));
188         assertTrue(SystemPersistenceConstants.getManager().getSystemProperties(SYSTEM_PROPS).isEmpty());
189         assertEquals(1, SystemPersistenceConstants.getManager().getSystemProperties().size());
190         assertEquals(SystemPersistenceConstants.getManager().getSystemProperties(SYSTEM_PROPS),
191                      SystemPersistenceConstants.getManager().getSystemProperties().get(0));
192     }
193
194     @Test
195     void test3Topic() {
196         SystemPersistenceConstants.getManager().setConfigurationDir(null);
197
198         Path topicPath = Paths
199             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_TOPIC_FILE);
200
201         Path topicBakPath = Paths
202             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_TOPIC_FILE_BAK);
203
204         assertTrue(Files.notExists(topicPath));
205         assertTrue(Files.notExists(topicBakPath));
206
207         SystemPersistenceConstants.getManager().storeTopic(TEST_TOPIC_NAME, new Properties());
208
209         assertTrue(Files.exists(topicPath));
210
211         Properties properties = SystemPersistenceConstants.getManager().getTopicProperties(TEST_TOPIC_NAME);
212         assertNotNull(properties);
213
214         List<Properties> topicPropsList = SystemPersistenceConstants.getManager().getTopicProperties();
215         assertEquals(1,  topicPropsList.size());
216
217         SystemPersistenceConstants.getManager().backupTopic(TEST_TOPIC_NAME);
218         assertTrue(Files.exists(topicBakPath));
219
220         SystemPersistenceConstants.getManager().deleteTopic(TEST_TOPIC_NAME);
221         assertTrue(Files.notExists(topicPath));
222     }
223
224     @Test
225     void test4HttpServer() {
226         SystemPersistenceConstants.getManager().setConfigurationDir(null);
227
228         Path httpServerPath = Paths
229             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_SERVER_FILE);
230
231         Path httpServerBakPath = Paths
232             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_SERVER_FILE_BAK);
233
234         assertTrue(Files.notExists(httpServerPath));
235         assertTrue(Files.notExists(httpServerBakPath));
236
237         SystemPersistenceConstants.getManager().storeHttpServer(TEST_HTTP_SERVER_NAME, new Properties());
238
239         assertTrue(Files.exists(httpServerPath));
240
241         Properties properties = SystemPersistenceConstants.getManager().getHttpServerProperties(TEST_HTTP_SERVER_NAME);
242         assertNotNull(properties);
243
244         List<Properties> httpServerPropsList = SystemPersistenceConstants.getManager().getHttpServerProperties();
245         assertEquals(1,  httpServerPropsList.size());
246
247         SystemPersistenceConstants.getManager().backupHttpServer(TEST_HTTP_SERVER_NAME);
248         assertTrue(Files.exists(httpServerBakPath));
249
250         SystemPersistenceConstants.getManager().deleteHttpServer(TEST_HTTP_SERVER_NAME);
251         assertTrue(Files.notExists(httpServerPath));
252     }
253
254     @Test
255     void test5HttpClient() {
256         SystemPersistenceConstants.getManager().setConfigurationDir(null);
257
258         Path httpClientPath = Paths
259             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_CLIENT_FILE);
260
261         Path httpClientBakPath = Paths
262             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_CLIENT_FILE_BAK);
263
264         assertTrue(Files.notExists(httpClientPath));
265         assertTrue(Files.notExists(httpClientBakPath));
266
267         SystemPersistenceConstants.getManager().storeHttpClient(TEST_HTTP_CLIENT_NAME, new Properties());
268
269         assertTrue(Files.exists(httpClientPath));
270
271         Properties properties = SystemPersistenceConstants.getManager().getHttpClientProperties(TEST_HTTP_CLIENT_NAME);
272         assertNotNull(properties);
273
274         List<Properties> httpClientPropsList = SystemPersistenceConstants.getManager().getHttpClientProperties();
275         assertEquals(1,  httpClientPropsList.size());
276
277         SystemPersistenceConstants.getManager().backupHttpClient(TEST_HTTP_CLIENT_NAME);
278         assertTrue(Files.exists(httpClientBakPath));
279
280         SystemPersistenceConstants.getManager().deleteHttpClient(TEST_HTTP_CLIENT_NAME);
281         assertTrue(Files.notExists(httpClientPath));
282     }
283
284     @Test
285     void test6Controller() {
286         SystemPersistenceConstants.getManager().setConfigurationDir(null);
287
288         Path controllerPath = Paths
289                 .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
290
291         Path controllerBakPath = Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
292                         TEST_CONTROLLER_FILE_BAK);
293
294         assertTrue(Files.notExists(controllerPath));
295         assertTrue(Files.notExists(controllerBakPath));
296
297         SystemPersistenceConstants.getManager().storeController(TEST_CONTROLLER_NAME, new Properties());
298
299         assertTrue(Files.exists(controllerPath));
300
301         Properties properties = SystemPersistenceConstants.getManager().getControllerProperties(TEST_CONTROLLER_NAME);
302         assertNotNull(properties);
303
304         List<Properties> controllerPropsList = SystemPersistenceConstants.getManager().getControllerProperties();
305         assertEquals(1,  controllerPropsList.size());
306         assertEquals(TEST_CONTROLLER_NAME, controllerPropsList
307                      .get(0).getProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME));
308
309         SystemPersistenceConstants.getManager().backupController(TEST_CONTROLLER_NAME);
310         assertTrue(Files.exists(controllerBakPath));
311
312         SystemPersistenceConstants.getManager().deleteController(TEST_CONTROLLER_NAME);
313         assertTrue(Files.notExists(controllerPath));
314     }
315
316     /**
317      * Clean up the working directories.
318      *
319      * @throws IOException throws IO exception
320      */
321     private static void cleanUpWorkingDirs() throws IOException {
322         SystemPersistenceConstants.getManager().setConfigurationDir(null);
323
324         for (Properties properties : SystemPersistenceConstants.getManager().getControllerProperties()) {
325             SystemPersistenceConstants.getManager().deleteController(properties
326                         .getProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME));
327         }
328
329         SystemPersistenceConstants.getManager().deleteTopic(TEST_TOPIC_NAME);
330         SystemPersistenceConstants.getManager().deleteHttpServer(TEST_HTTP_SERVER_NAME);
331         SystemPersistenceConstants.getManager().deleteHttpClient(TEST_HTTP_CLIENT_NAME);
332
333         final Path testControllerBakPath =
334                         Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
335                                         TEST_CONTROLLER_FILE_BAK);
336
337         final Path testTopicBakPath = Paths
338             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_TOPIC_FILE_BAK);
339         final Path testHttpServerBakPath = Paths
340             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_SERVER_FILE_BAK);
341         final Path testHttpClientBakPath = Paths
342             .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_CLIENT_FILE_BAK);
343
344         final Path policyEnginePath = Paths.get(OTHER_CONFIG_DIR, FileSystemPersistence.PROPERTIES_FILE_ENGINE);
345         final Path environmentPath = Paths.get(OTHER_CONFIG_DIR, ENV_PROPS_FILE);
346         final Path systemPath = Paths.get(OTHER_CONFIG_DIR, SYSTEM_PROPS_FILE);
347
348         Files.deleteIfExists(testControllerBakPath);
349         Files.deleteIfExists(testTopicBakPath);
350         Files.deleteIfExists(testHttpServerBakPath);
351         Files.deleteIfExists(testHttpClientBakPath);
352         Files.deleteIfExists(policyEnginePath);
353         Files.deleteIfExists(environmentPath);
354         Files.deleteIfExists(systemPath);
355     }
356
357 }