From b001c1ac5a0b4d938a69adb47f4613f64dc71c1a Mon Sep 17 00:00:00 2001 From: Jorge Hernandez Date: Thu, 28 Feb 2019 10:10:49 -0600 Subject: move all hard install config to environment vars + support multiple system properties files with variable interpolation loaded at initialization + support of configurable JVM options (-X, etc ..). + rearrange aaf configuration to avoid {{}} installation variables and use dynamic enviroment variables. + miscellaneous clean up in areas touched and checkstyle. Change-Id: I71ad839778e17eb57c098a2c5cc2bf96e468669a Issue-ID: POLICY-1524 Signed-off-by: Jorge Hernandez --- .../drools/persistence/SystemPersistenceTest.java | 204 +++++++++++++++++++++ .../persistence/test/SystemPersistenceTest.java | 193 ------------------- .../drools/system/PolicyEngineManagerTest.java | 10 + .../policy/drools/system/PolicyEngineTest.java | 2 +- 4 files changed, 215 insertions(+), 194 deletions(-) create mode 100644 policy-management/src/test/java/org/onap/policy/drools/persistence/SystemPersistenceTest.java delete mode 100644 policy-management/src/test/java/org/onap/policy/drools/persistence/test/SystemPersistenceTest.java (limited to 'policy-management/src/test/java/org') diff --git a/policy-management/src/test/java/org/onap/policy/drools/persistence/SystemPersistenceTest.java b/policy-management/src/test/java/org/onap/policy/drools/persistence/SystemPersistenceTest.java new file mode 100644 index 00000000..de0a7518 --- /dev/null +++ b/policy-management/src/test/java/org/onap/policy/drools/persistence/SystemPersistenceTest.java @@ -0,0 +1,204 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.persistence; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Properties; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +import org.onap.policy.drools.properties.DroolsProperties; + +/** + * (File) System Persistence Tests. + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class SystemPersistenceTest { + /** + * sample configuration dir. + */ + private static final String OTHER_CONFIG_DIR = "tmp"; + + /** + * Test JUnit Controller Name. + */ + private static final String TEST_CONTROLLER_NAME = "foo"; + + /** + * Test JUnit Controller Name. + */ + private static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties"; + + /** + * Test JUnit Controller Name Backup. + */ + private static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_FILE + ".bak"; + + /** + * Test JUnit Environment/Engine properties. + */ + private static final String ENV_PROPS = TEST_CONTROLLER_NAME; + private static final String ENV_PROPS_FILE = ENV_PROPS + ".environment"; + + /** + * Test JUnit system properties. + */ + private static final String SYSTEM_PROPS = TEST_CONTROLLER_NAME; + private static final String SYSTEM_PROPS_FILE = SYSTEM_PROPS + "-system.properties"; + + @BeforeClass + public static void setUp() throws IOException { + cleanUpWorkingDirs(); + } + + @AfterClass + public static void tearDown() throws IOException { + cleanUpWorkingDirs(); + } + + @Test + public void test1NonDefaultConfigDir() { + SystemPersistence.manager.setConfigurationDir(OTHER_CONFIG_DIR); + assertEquals(OTHER_CONFIG_DIR, SystemPersistence.manager.getConfigurationPath().toString()); + + SystemPersistence.manager.setConfigurationDir(null); + assertEquals(SystemPersistence.DEFAULT_CONFIGURATION_DIR, + SystemPersistence.manager.getConfigurationPath().toString()); + + SystemPersistence.manager.setConfigurationDir(); + assertEquals(SystemPersistence.DEFAULT_CONFIGURATION_DIR, + SystemPersistence.manager.getConfigurationPath().toString()); + } + + @Test + public void test2Engine_Environment_System() throws IOException { + SystemPersistence.manager.setConfigurationDir(OTHER_CONFIG_DIR); + + final Path policyEnginePropsPath = + Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), + FileSystemPersistence.PROPERTIES_FILE_ENGINE); + + final Properties engineProps = new Properties(); + engineProps.setProperty("foo", "bar"); + engineProps.setProperty("fiz", "buz"); + if (Files.notExists(policyEnginePropsPath)) { + try (final OutputStream fout = new FileOutputStream(policyEnginePropsPath.toFile())) { + engineProps.store(fout, ""); + } + } + + assertEquals(engineProps, SystemPersistence.manager.getEngineProperties()); + + final Path environmentPropertiesPath = + Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), ENV_PROPS_FILE); + if (Files.notExists(environmentPropertiesPath)) { + Files.createFile(environmentPropertiesPath); + } + assertNotNull(SystemPersistence.manager.getEnvironmentProperties(ENV_PROPS)); + assertTrue(SystemPersistence.manager.getEnvironmentProperties(ENV_PROPS).isEmpty()); + assertEquals(1, SystemPersistence.manager.getEnvironmentProperties().size()); + assertEquals(SystemPersistence.manager.getEnvironmentProperties(ENV_PROPS), + SystemPersistence.manager.getEnvironmentProperties().get(0)); + + Path systemPropertiesPath = + Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), SYSTEM_PROPS_FILE); + if (Files.notExists(systemPropertiesPath)) { + Files.createFile(systemPropertiesPath); + } + assertNotNull(SystemPersistence.manager.getSystemProperties(SYSTEM_PROPS)); + assertTrue(SystemPersistence.manager.getSystemProperties(SYSTEM_PROPS).isEmpty()); + assertEquals(1, SystemPersistence.manager.getSystemProperties().size()); + assertEquals(SystemPersistence.manager.getSystemProperties(SYSTEM_PROPS), + SystemPersistence.manager.getSystemProperties().get(0)); + } + + @Test + public void test3Controller() { + SystemPersistence.manager.setConfigurationDir(null); + + final Path controllerPath = Paths + .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE); + + final Path controllerBakPath = Paths + .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK); + + assertTrue(Files.notExists(controllerPath)); + assertTrue(Files.notExists(controllerBakPath)); + + Properties properties = new Properties(); + SystemPersistence.manager.storeController(TEST_CONTROLLER_NAME, properties); + + assertTrue(Files.exists(controllerPath)); + + properties = SystemPersistence.manager.getControllerProperties(TEST_CONTROLLER_NAME); + assertNotNull(properties); + + List controllerPropsList = SystemPersistence.manager.getControllerProperties(); + assertEquals(1, controllerPropsList.size()); + assertEquals(TEST_CONTROLLER_NAME, controllerPropsList + .get(0).getProperty(DroolsProperties.PROPERTY_CONTROLLER_NAME)); + + SystemPersistence.manager.backupController(TEST_CONTROLLER_NAME); + assertTrue(Files.exists(controllerBakPath)); + + SystemPersistence.manager.deleteController(TEST_CONTROLLER_NAME); + assertTrue(Files.notExists(controllerPath)); + } + + /** + * Clean up the working directories. + * + * @throws IOException throws IO exception + */ + private static void cleanUpWorkingDirs() throws IOException { + SystemPersistence.manager.setConfigurationDir(null); + + for (Properties properties : SystemPersistence.manager.getControllerProperties()) { + SystemPersistence.manager.deleteController(properties + .getProperty(DroolsProperties.PROPERTY_CONTROLLER_NAME)); + } + + final Path testControllerBakPath = Paths + .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK); + + final Path policyEnginePath = Paths.get(OTHER_CONFIG_DIR, FileSystemPersistence.PROPERTIES_FILE_ENGINE); + final Path environmentPath = Paths.get(OTHER_CONFIG_DIR, ENV_PROPS_FILE); + final Path systemPath = Paths.get(OTHER_CONFIG_DIR, SYSTEM_PROPS_FILE); + + Files.deleteIfExists(testControllerBakPath); + Files.deleteIfExists(policyEnginePath); + Files.deleteIfExists(environmentPath); + Files.deleteIfExists(systemPath); + } + +} diff --git a/policy-management/src/test/java/org/onap/policy/drools/persistence/test/SystemPersistenceTest.java b/policy-management/src/test/java/org/onap/policy/drools/persistence/test/SystemPersistenceTest.java deleted file mode 100644 index 1e51cfec..00000000 --- a/policy-management/src/test/java/org/onap/policy/drools/persistence/test/SystemPersistenceTest.java +++ /dev/null @@ -1,193 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.drools.persistence.test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Properties; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runners.MethodSorters; -import org.onap.policy.drools.persistence.FileSystemPersistence; -import org.onap.policy.drools.persistence.SystemPersistence; -import org.onap.policy.drools.properties.DroolsProperties; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * (File) System Persistence Tests. - */ -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class SystemPersistenceTest { - - /** - * logger. - */ - private static final Logger logger = LoggerFactory.getLogger(SystemPersistenceTest.class); - - /** - * sample configuration dir. - */ - public static final String OTHER_CONFIG_DIR = "tmp"; - - /** - * Test JUnit Controller Name. - */ - public static final String TEST_CONTROLLER_NAME = "foo"; - - /** - * Test JUnit Controller Name. - */ - public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties"; - - /** - * Test JUnit Controller Name Backup. - */ - public static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_FILE + ".bak"; - - /** - * Test JUnit Environment/Engine properties. - */ - private static final String ENV_PROPS = "envProps"; - private static final String ENV_PROPS_FILE = ENV_PROPS + ".environment"; - private static final String POLICY_ENGINE_PROPERTIES_FILE = "policy-engine.properties"; - - @BeforeClass - public static void setUp() throws IOException { - cleanUpWorkingDirs(); - } - - @AfterClass - public static void tearDown() throws IOException { - cleanUpWorkingDirs(); - } - - @Test - public void test1NonDefaultConfigDir() throws IOException { - logger.info("enter"); - - SystemPersistence.manager.setConfigurationDir(OTHER_CONFIG_DIR); - assertTrue( - SystemPersistence.manager.getConfigurationPath().toString().equals(OTHER_CONFIG_DIR)); - - SystemPersistence.manager.setConfigurationDir(null); - assertTrue(SystemPersistence.manager.getConfigurationPath().toString() - .equals(SystemPersistence.DEFAULT_CONFIGURATION_DIR)); - } - - @Test - public void test2Engine() throws IOException { - logger.info("enter"); - - SystemPersistence.manager.setConfigurationDir(OTHER_CONFIG_DIR); - - final Path policyEnginePropsPath = - Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), - FileSystemPersistence.PROPERTIES_FILE_ENGINE); - - final Properties engineProps = new Properties(); - engineProps.setProperty("foo", "bar"); - engineProps.setProperty("fiz", "buz"); - if (Files.notExists(policyEnginePropsPath)) { - try (final OutputStream fout = new FileOutputStream(policyEnginePropsPath.toFile())) { - engineProps.store(fout, ""); - } - } - - assertEquals(SystemPersistence.manager.getEngineProperties(), engineProps); - - final Path environmentPropertiesPath = - Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), ENV_PROPS_FILE); - if (Files.notExists(environmentPropertiesPath)) { - Files.createFile(environmentPropertiesPath); - } - assertTrue(SystemPersistence.manager.getEnvironmentProperties(ENV_PROPS).isEmpty()); - assertTrue(SystemPersistence.manager.getEnvironmentProperties().size() == 1); - } - - @Test - public void test3PersistConfiguration() { - logger.info("enter"); - - SystemPersistence.manager.setConfigurationDir(null); - - final Path controllerPath = Paths - .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE); - - final Path controllerBakPath = Paths - .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK); - - assertTrue(Files.notExists(controllerPath)); - assertTrue(Files.notExists(controllerBakPath)); - - Properties properties = new Properties(); - properties.put(DroolsProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME); - SystemPersistence.manager.storeController(TEST_CONTROLLER_NAME, properties); - - assertTrue(Files.exists(controllerPath)); - - properties = SystemPersistence.manager.getControllerProperties(TEST_CONTROLLER_NAME); - assertTrue(properties != null); - - SystemPersistence.manager.backupController(TEST_CONTROLLER_NAME); - assertTrue(Files.exists(controllerBakPath)); - - assertFalse(SystemPersistence.manager.getControllerProperties().isEmpty()); - - SystemPersistence.manager.deleteController(TEST_CONTROLLER_NAME); - assertTrue(Files.notExists(controllerPath)); - } - - /** - * Clean up the working directories. - * - * @throws IOException throws IO exception - */ - public static void cleanUpWorkingDirs() throws IOException { - - SystemPersistence.manager.setConfigurationDir(null); - - final Path testControllerPath = Paths - .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE); - final Path testControllerBakPath = Paths - .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK); - - final Path policyEnginePath = Paths.get(OTHER_CONFIG_DIR, POLICY_ENGINE_PROPERTIES_FILE); - final Path environmentPath = Paths.get(OTHER_CONFIG_DIR, ENV_PROPS_FILE); - - Files.deleteIfExists(testControllerPath); - Files.deleteIfExists(testControllerBakPath); - Files.deleteIfExists(policyEnginePath); - Files.deleteIfExists(environmentPath); - } - -} diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java index 4b8357a2..8050b26a 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java @@ -368,6 +368,16 @@ public class PolicyEngineManagerTest { assertNotNull(mgr.getEnvironmentProperty("PATH")); assertNull(mgr.getEnvironmentProperty("unknown-env-property")); + + System.setProperty("propS-a", "valueS-a"); + assertEquals("valueS-a", mgr.getEnvironmentProperty("propS-a")); + + Properties props3 = new Properties(); + props3.put("prop3-a", "${env:HOME}"); + mgr.setEnvironment(props3); + assertEquals(System.getenv("HOME"), mgr.getEnvironmentProperty("prop3-a")); + assertEquals("valueS-a", mgr.getEnvironmentProperty("propS-a")); + assertEquals(newValue, mgr.getEnvironmentProperty(propKey)); } @Test diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java index a00d6db2..64af6d4f 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java @@ -163,7 +163,7 @@ public class PolicyEngineTest { PolicyEngine.manager.configure(engineProps); assertFalse(PolicyEngine.manager.isAlive()); - logger.info("policy-engine {} has configuration {}", PolicyEngine.manager, engineProps); + logger.info("engine {} has configuration {}", PolicyEngine.manager, engineProps); gson.compareGson(PolicyEngine.manager, new File(PolicyEngineTest.class.getSimpleName() + "Config.json")); } -- cgit 1.2.3-korg