2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.persistence;
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;
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;
44 * (File) System Persistence Tests.
46 @TestMethodOrder(MethodOrderer.MethodName.class)
47 public class SystemPersistenceTest {
49 * sample configuration dir.
51 private static final String OTHER_CONFIG_DIR = "tmp";
54 * Test JUnit Controller Name.
56 private static final String TEST_CONTROLLER_NAME = "foo";
60 * Test JUnit Topic Name.
62 private static final String TEST_TOPIC_NAME = TEST_CONTROLLER_NAME;
65 * Test JUnit HTTP Server Name.
67 private static final String TEST_HTTP_SERVER_NAME = TEST_CONTROLLER_NAME;
70 * Test JUnit HTTP Client Name.
72 private static final String TEST_HTTP_CLIENT_NAME = TEST_CONTROLLER_NAME;
75 * Test JUnit Controller File.
77 private static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
80 * Test JUnit Controller Backup File.
82 private static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_FILE + ".bak";
85 * Test JUnit Topic File.
87 private static final String TEST_TOPIC_FILE = TEST_CONTROLLER_NAME + "-topic.properties";
90 * Test JUnit Controller Name Backup.
92 private static final String TEST_TOPIC_FILE_BAK = TEST_TOPIC_FILE + ".bak";
95 * Test JUnit Http Server File.
97 private static final String TEST_HTTP_SERVER_FILE = TEST_CONTROLLER_NAME
98 + FileSystemPersistence.PROPERTIES_FILE_HTTP_SERVER_SUFFIX;
101 * Test JUnit Backup Http Server File.
103 private static final String TEST_HTTP_SERVER_FILE_BAK = TEST_HTTP_SERVER_FILE + ".bak";
106 * Test JUnit Http Client File.
108 private static final String TEST_HTTP_CLIENT_FILE = TEST_CONTROLLER_NAME
109 + FileSystemPersistence.PROPERTIES_FILE_HTTP_CLIENT_SUFFIX;
112 * Test JUnit Backup Http Server File.
114 private static final String TEST_HTTP_CLIENT_FILE_BAK = TEST_HTTP_CLIENT_FILE + ".bak";
117 * Test JUnit Environment/Engine properties.
119 private static final String ENV_PROPS = TEST_CONTROLLER_NAME;
120 private static final String ENV_PROPS_FILE = ENV_PROPS + ".environment";
123 * Test JUnit system properties.
125 private static final String SYSTEM_PROPS = TEST_CONTROLLER_NAME;
126 private static final String SYSTEM_PROPS_FILE = SYSTEM_PROPS + "-system.properties";
129 public static void setUp() throws IOException {
130 cleanUpWorkingDirs();
134 public static void tearDown() throws IOException {
135 cleanUpWorkingDirs();
139 void test1NonDefaultConfigDir() {
140 SystemPersistenceConstants.getManager().setConfigurationDir(OTHER_CONFIG_DIR);
141 assertEquals(OTHER_CONFIG_DIR, SystemPersistenceConstants.getManager().getConfigurationPath().toString());
143 SystemPersistenceConstants.getManager().setConfigurationDir(null);
144 assertEquals(SystemPersistenceConstants.DEFAULT_CONFIGURATION_DIR,
145 SystemPersistenceConstants.getManager().getConfigurationPath().toString());
147 SystemPersistenceConstants.getManager().setConfigurationDir();
148 assertEquals(SystemPersistenceConstants.DEFAULT_CONFIGURATION_DIR,
149 SystemPersistenceConstants.getManager().getConfigurationPath().toString());
153 void test2Engine_Environment_System() throws IOException {
154 SystemPersistenceConstants.getManager().setConfigurationDir(OTHER_CONFIG_DIR);
156 final Path policyEnginePropsPath =
157 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
158 FileSystemPersistence.PROPERTIES_FILE_ENGINE);
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, "");
169 assertEquals(engineProps, SystemPersistenceConstants.getManager().getEngineProperties());
171 final Path environmentPropertiesPath =
172 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), ENV_PROPS_FILE);
173 if (Files.notExists(environmentPropertiesPath)) {
174 Files.createFile(environmentPropertiesPath);
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));
182 Path systemPropertiesPath =
183 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), SYSTEM_PROPS_FILE);
184 if (Files.notExists(systemPropertiesPath)) {
185 Files.createFile(systemPropertiesPath);
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));
196 SystemPersistenceConstants.getManager().setConfigurationDir(null);
198 Path topicPath = Paths
199 .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_TOPIC_FILE);
201 Path topicBakPath = Paths
202 .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_TOPIC_FILE_BAK);
204 assertTrue(Files.notExists(topicPath));
205 assertTrue(Files.notExists(topicBakPath));
207 SystemPersistenceConstants.getManager().storeTopic(TEST_TOPIC_NAME, new Properties());
209 assertTrue(Files.exists(topicPath));
211 Properties properties = SystemPersistenceConstants.getManager().getTopicProperties(TEST_TOPIC_NAME);
212 assertNotNull(properties);
214 List<Properties> topicPropsList = SystemPersistenceConstants.getManager().getTopicProperties();
215 assertEquals(1, topicPropsList.size());
217 SystemPersistenceConstants.getManager().backupTopic(TEST_TOPIC_NAME);
218 assertTrue(Files.exists(topicBakPath));
220 SystemPersistenceConstants.getManager().deleteTopic(TEST_TOPIC_NAME);
221 assertTrue(Files.notExists(topicPath));
225 void test4HttpServer() {
226 SystemPersistenceConstants.getManager().setConfigurationDir(null);
228 Path httpServerPath = Paths
229 .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_SERVER_FILE);
231 Path httpServerBakPath = Paths
232 .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_SERVER_FILE_BAK);
234 assertTrue(Files.notExists(httpServerPath));
235 assertTrue(Files.notExists(httpServerBakPath));
237 SystemPersistenceConstants.getManager().storeHttpServer(TEST_HTTP_SERVER_NAME, new Properties());
239 assertTrue(Files.exists(httpServerPath));
241 Properties properties = SystemPersistenceConstants.getManager().getHttpServerProperties(TEST_HTTP_SERVER_NAME);
242 assertNotNull(properties);
244 List<Properties> httpServerPropsList = SystemPersistenceConstants.getManager().getHttpServerProperties();
245 assertEquals(1, httpServerPropsList.size());
247 SystemPersistenceConstants.getManager().backupHttpServer(TEST_HTTP_SERVER_NAME);
248 assertTrue(Files.exists(httpServerBakPath));
250 SystemPersistenceConstants.getManager().deleteHttpServer(TEST_HTTP_SERVER_NAME);
251 assertTrue(Files.notExists(httpServerPath));
255 void test5HttpClient() {
256 SystemPersistenceConstants.getManager().setConfigurationDir(null);
258 Path httpClientPath = Paths
259 .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_CLIENT_FILE);
261 Path httpClientBakPath = Paths
262 .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_HTTP_CLIENT_FILE_BAK);
264 assertTrue(Files.notExists(httpClientPath));
265 assertTrue(Files.notExists(httpClientBakPath));
267 SystemPersistenceConstants.getManager().storeHttpClient(TEST_HTTP_CLIENT_NAME, new Properties());
269 assertTrue(Files.exists(httpClientPath));
271 Properties properties = SystemPersistenceConstants.getManager().getHttpClientProperties(TEST_HTTP_CLIENT_NAME);
272 assertNotNull(properties);
274 List<Properties> httpClientPropsList = SystemPersistenceConstants.getManager().getHttpClientProperties();
275 assertEquals(1, httpClientPropsList.size());
277 SystemPersistenceConstants.getManager().backupHttpClient(TEST_HTTP_CLIENT_NAME);
278 assertTrue(Files.exists(httpClientBakPath));
280 SystemPersistenceConstants.getManager().deleteHttpClient(TEST_HTTP_CLIENT_NAME);
281 assertTrue(Files.notExists(httpClientPath));
285 void test6Controller() {
286 SystemPersistenceConstants.getManager().setConfigurationDir(null);
288 Path controllerPath = Paths
289 .get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
291 Path controllerBakPath = Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
292 TEST_CONTROLLER_FILE_BAK);
294 assertTrue(Files.notExists(controllerPath));
295 assertTrue(Files.notExists(controllerBakPath));
297 SystemPersistenceConstants.getManager().storeController(TEST_CONTROLLER_NAME, new Properties());
299 assertTrue(Files.exists(controllerPath));
301 Properties properties = SystemPersistenceConstants.getManager().getControllerProperties(TEST_CONTROLLER_NAME);
302 assertNotNull(properties);
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));
309 SystemPersistenceConstants.getManager().backupController(TEST_CONTROLLER_NAME);
310 assertTrue(Files.exists(controllerBakPath));
312 SystemPersistenceConstants.getManager().deleteController(TEST_CONTROLLER_NAME);
313 assertTrue(Files.notExists(controllerPath));
317 * Clean up the working directories.
319 * @throws IOException throws IO exception
321 private static void cleanUpWorkingDirs() throws IOException {
322 SystemPersistenceConstants.getManager().setConfigurationDir(null);
324 for (Properties properties : SystemPersistenceConstants.getManager().getControllerProperties()) {
325 SystemPersistenceConstants.getManager().deleteController(properties
326 .getProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME));
329 SystemPersistenceConstants.getManager().deleteTopic(TEST_TOPIC_NAME);
330 SystemPersistenceConstants.getManager().deleteHttpServer(TEST_HTTP_SERVER_NAME);
331 SystemPersistenceConstants.getManager().deleteHttpClient(TEST_HTTP_CLIENT_NAME);
333 final Path testControllerBakPath =
334 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
335 TEST_CONTROLLER_FILE_BAK);
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);
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);
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);