2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.drools.persistence.test;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.Properties;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.FixMethodOrder;
37 import org.junit.Test;
38 import org.junit.runners.MethodSorters;
39 import org.onap.policy.drools.persistence.FileSystemPersistence;
40 import org.onap.policy.drools.persistence.SystemPersistence;
41 import org.onap.policy.drools.properties.PolicyProperties;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * (File) System Persistence Tests
48 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
49 public class SystemPersistenceTest {
54 private static final Logger logger = LoggerFactory.getLogger(SystemPersistenceTest.class);
57 * sample configuration dir
59 public static final String OTHER_CONFIG_DIR = "tmp";
62 * Test JUnit Controller Name
64 public static final String TEST_CONTROLLER_NAME = "foo";
67 * Test JUnit Controller Name
69 public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
72 * Test JUnit Controller Name Backup
74 public static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_FILE + ".bak";
77 * Test JUnit Environment/Engine properties
79 private static final String ENV_PROPS = "envProps";
80 private static final String ENV_PROPS_FILE = ENV_PROPS + ".environment";
81 private static final String POLICY_ENGINE_PROPERTIES_FILE = "policy-engine.properties";
84 public static void setUp() throws IOException {
89 public static void tearDown() throws IOException {
94 public void test1NonDefaultConfigDir() throws IOException {
97 SystemPersistence.manager.setConfigurationDir(OTHER_CONFIG_DIR);
99 SystemPersistence.manager.getConfigurationPath().toString().equals(OTHER_CONFIG_DIR));
101 SystemPersistence.manager.setConfigurationDir(null);
102 assertTrue(SystemPersistence.manager.getConfigurationPath().toString()
103 .equals(SystemPersistence.DEFAULT_CONFIGURATION_DIR));
107 public void test2Engine() throws IOException {
108 logger.info("enter");
110 SystemPersistence.manager.setConfigurationDir(OTHER_CONFIG_DIR);
112 final Path policyEnginePropsPath =
113 Paths.get(SystemPersistence.manager.getConfigurationPath().toString(),
114 FileSystemPersistence.PROPERTIES_FILE_ENGINE);
116 final Properties engineProps = new Properties();
117 engineProps.setProperty("foo", "bar");
118 engineProps.setProperty("fiz", "buz");
119 if (Files.notExists(policyEnginePropsPath)) {
120 try (final OutputStream fout = new FileOutputStream(policyEnginePropsPath.toFile())) {
121 engineProps.store(fout, "");
125 assertEquals(SystemPersistence.manager.getEngineProperties(), engineProps);
127 final Path environmentPropertiesPath =
128 Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), ENV_PROPS_FILE);
129 if (Files.notExists(environmentPropertiesPath)) {
130 Files.createFile(environmentPropertiesPath);
132 assertTrue(SystemPersistence.manager.getEnvironmentProperties(ENV_PROPS).isEmpty());
133 assertTrue(SystemPersistence.manager.getEnvironmentProperties().size() == 1);
137 public void test3PersistConfiguration() {
138 logger.info("enter");
140 SystemPersistence.manager.setConfigurationDir(null);
142 final Path controllerPath = Paths
143 .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
145 final Path controllerBakPath = Paths
146 .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK);
148 assertTrue(Files.notExists(controllerPath));
149 assertTrue(Files.notExists(controllerBakPath));
151 Properties properties = new Properties();
152 properties.put(PolicyProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
153 SystemPersistence.manager.storeController(TEST_CONTROLLER_NAME, properties);
155 assertTrue(Files.exists(controllerPath));
157 properties = SystemPersistence.manager.getControllerProperties(TEST_CONTROLLER_NAME);
158 assertTrue(properties != null);
160 SystemPersistence.manager.backupController(TEST_CONTROLLER_NAME);
161 assertTrue(Files.exists(controllerBakPath));
163 assertFalse(SystemPersistence.manager.getControllerProperties().isEmpty());
165 SystemPersistence.manager.deleteController(TEST_CONTROLLER_NAME);
166 assertTrue(Files.notExists(controllerPath));
169 public static void cleanUpWorkingDirs() throws IOException {
171 SystemPersistence.manager.setConfigurationDir(null);
173 final Path testControllerPath = Paths
174 .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
175 final Path testControllerBakPath = Paths
176 .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK);
178 final Path policyEnginePath = Paths.get(OTHER_CONFIG_DIR, POLICY_ENGINE_PROPERTIES_FILE);
179 final Path environmentPath = Paths.get(OTHER_CONFIG_DIR, ENV_PROPS_FILE);
181 Files.deleteIfExists(testControllerPath);
182 Files.deleteIfExists(testControllerBakPath);
183 Files.deleteIfExists(policyEnginePath);
184 Files.deleteIfExists(environmentPath);