From 843df4cc5bacf7170c9f93719ec57298391b2400 Mon Sep 17 00:00:00 2001 From: Jorge Hernandez Date: Mon, 28 Aug 2017 13:51:08 -0500 Subject: junits in policy-management - add additional junits for policy-management module - allow for no configuration pdp-d start up. - minor changes junits for policy-endpoints to avoid race conditions in jenkins environment, starting up, and shutting down servers. Issue-ID: POLICY-109 Change-Id: Ibccefeb5d7cf762da27fe3282887df18d79db5df Signed-off-by: Jorge Hernandez --- policy-management/config/policy-engine.properties | 30 --- .../drools/persistence/SystemPersistence.java | 7 +- .../java/org/onap/policy/drools/system/Main.java | 45 +++-- .../onap/policy/drools/system/PolicyEngine.java | 62 ++++++- .../drools/system/test/PolicyEngineTest.java | 201 +++++++++++++++++---- .../src/test/resources/logback-test.xml | 2 +- 6 files changed, 257 insertions(+), 90 deletions(-) delete mode 100644 policy-management/config/policy-engine.properties (limited to 'policy-management') diff --git a/policy-management/config/policy-engine.properties b/policy-management/config/policy-engine.properties deleted file mode 100644 index bf28bc80..00000000 --- a/policy-management/config/policy-engine.properties +++ /dev/null @@ -1,30 +0,0 @@ -# Policy Engine Configuration - -# Configuration Channel Settings: PDPD_CONFIGURATION - -ueb.source.topics=PDPD_CONFIGURATION -ueb.source.topics.PDPD_CONFIGURATION.servers= -ueb.source.topics.PDPD_CONFIGURATION.apiKey= -ueb.source.topics.PDPD_CONFIGURATION.apiSecret= -ueb.source.topics.PDPD_CONFIGURATION.consumerGroup= -ueb.source.topics.PDPD_CONFIGURATION.consumerInstance= -ueb.source.topics.PDPD_CONFIGURATION.managed=false - -ueb.sink.topics=PDPD_CONFIGURATION -ueb.sink.topics.PDPD_CONFIGURATION.servers= -ueb.sink.topics.PDPD_CONFIGURATION.apiKey= -ueb.sink.topics.PDPD_CONFIGURATION.apiSecret= -ueb.sink.topics.PDPD_CONFIGURATION.partitionKey= -ueb.sink.topics.PDPD_CONFIGURATION.managed=false - -http.server.services=CONFIG -http.server.services.CONFIG.host=0.0.0.0 -http.server.services.CONFIG.port=9696 -http.server.services.CONFIG.userName=x -http.server.services.CONFIG.password=y -http.server.services.CONFIG.restPackages=org.onap.policy.drools.server.restful -#http.server.services.CONFIG.restPackages=org.onap.policy.drools.server.restful,org.onap.policy.drools.healthcheck -#http.server.services.CONFIG.restClasses=org.onap.policy.drools.server.restful.RestManager,org.onap.policy.drools.healthcheck.RestHealthCheck -#http.server.services.CONFIG.restClasses=org.onap.policy.drools.server.restful.RestManager -http.server.services.CONFIG.managed=false -http.server.services.CONFIG.swagger=true diff --git a/policy-management/src/main/java/org/onap/policy/drools/persistence/SystemPersistence.java b/policy-management/src/main/java/org/onap/policy/drools/persistence/SystemPersistence.java index bdda8e84..2390fc89 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/persistence/SystemPersistence.java +++ b/policy-management/src/main/java/org/onap/policy/drools/persistence/SystemPersistence.java @@ -182,10 +182,9 @@ class SystemPropertiesPersistence implements SystemPersistence { } } - try { - File controllerPropertiesFile = controllerPropertiesPath.toFile(); - FileWriter writer = new FileWriter(controllerPropertiesFile); - properties.store(writer, "Machine created Policy Controller Configuration"); + File controllerPropertiesFile = controllerPropertiesPath.toFile(); + try (FileWriter writer = new FileWriter(controllerPropertiesFile)) { + properties.store(writer, "Machine created Policy Controller Configuration"); } catch (Exception e) { logger.warn("{}: cannot be STORED", controllerName, e); return false; diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/Main.java b/policy-management/src/main/java/org/onap/policy/drools/system/Main.java index fe0cc01f..49dfafca 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/Main.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/Main.java @@ -21,6 +21,8 @@ package org.onap.policy.drools.system; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; @@ -54,9 +56,25 @@ public class Main { * main * * @param args program arguments + * @throws IOException */ public static void main(String args[]) { + /* make sure the configuration directory exists */ + + Path configDir = Paths.get(SystemPersistence.CONFIG_DIR_NAME); + if (Files.notExists(configDir)) { + try { + Files.createDirectories(configDir); + } catch (IOException e) { + throw new IllegalStateException("cannot create " + SystemPersistence.CONFIG_DIR_NAME, e); + } + } + + if (!Files.isDirectory(configDir)) + throw new IllegalStateException + ("config directory: " + configDir + " is not a directory"); + /* logging defaults */ if (System.getProperty(LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY) == null) @@ -66,24 +84,23 @@ public class Main { PolicyEngine.manager.boot(args); - Logger logger = LoggerFactory.getLogger(Main.class); + /* start logger */ - File configDir = new File(SystemPersistence.CONFIG_DIR_NAME); - - if (!configDir.isDirectory()) { - throw new IllegalArgumentException - ("config directory: " + configDir.getAbsolutePath() + - " not found"); - } + Logger logger = LoggerFactory.getLogger(Main.class); /* 1. Configure the Engine */ - + + Path policyEnginePath = Paths.get(configDir.toString(), SystemPersistence.PROPERTIES_FILE_ENGINE); try { - Path policyEnginePath = Paths.get(configDir.toPath().toString(), SystemPersistence.PROPERTIES_FILE_ENGINE); - Properties properties = PropertyUtil.getProperties(policyEnginePath.toFile()); - PolicyEngine.manager.configure(properties); + if (Files.exists(policyEnginePath)) + PolicyEngine.manager.configure(PropertyUtil.getProperties(policyEnginePath.toFile())); + else + PolicyEngine.manager.configure(PolicyEngine.manager.defaultTelemetryConfig()); } catch (Exception e) { - logger.warn("Main: cannot initialize {} because of {}", PolicyEngine.manager, e.getMessage(), e); + logger.warn("Main: {} could not find custom configuration in {}.", + PolicyEngine.manager, SystemPersistence.PROPERTIES_FILE_ENGINE, e); + + /* continue without telemetry or other custom components - this is OK */ } /* 2. Start the Engine with the basic services only (no Policy Controllers) */ @@ -102,7 +119,7 @@ public class Main { /* 3. Create and start the controllers */ - File[] controllerFiles = configDir.listFiles(); + File[] controllerFiles = configDir.toFile().listFiles(); for (File config : controllerFiles) { if (config.getName().endsWith(SystemPersistence.PROPERTIES_FILE_CONTROLLER_SUFFIX)) { diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java index 36821659..d7e0ecdd 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java @@ -45,6 +45,7 @@ import org.onap.policy.drools.properties.Startable; import org.onap.policy.drools.protocol.coders.EventProtocolCoder; import org.onap.policy.drools.protocol.configuration.ControllerConfiguration; import org.onap.policy.drools.protocol.configuration.PdpdConfiguration; +import org.onap.policy.drools.server.restful.RestManager; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; @@ -71,17 +72,21 @@ import com.google.gson.GsonBuilder; *
* PolicyEngine 1 --- 1 ManagementServer */ -public interface PolicyEngine extends Startable, Lockable, TopicListener { +public interface PolicyEngine extends Startable, Lockable, TopicListener { + /** + * Default Telemetry Server Port + */ + public static final int TELEMETRY_SERVER_DEFAULT_PORT = 9696; /** - * Default Config Server Port + * Default Telemetry Server Hostname */ - public static final int CONFIG_SERVER_DEFAULT_PORT = 9696; + public static final String TELEMETRY_SERVER_DEFAULT_HOST = "localhost"; /** - * Default Config Server Hostname + * Default Telemetry Server Name */ - public static final String CONFIG_SERVER_DEFAULT_HOST = "localhost"; + public static final String TELEMETRY_SERVER_DEFAULT_NAME = "TELEMETRY"; /** * Boot the engine @@ -89,7 +94,7 @@ public interface PolicyEngine extends Startable, Lockable, TopicListener { * @param cliArgs command line arguments */ public void boot(String cliArgs[]); - + /** * configure the policy engine according to the given properties * @@ -308,6 +313,13 @@ public interface PolicyEngine extends Startable, Lockable, TopicListener { * Invoked when the host goes into the standby state. */ public void deactivate(); + + /** + * produces a default telemetry configuration + * + * @return policy engine configuration + */ + public Properties defaultTelemetryConfig(); /** * Policy Engine Manager @@ -319,6 +331,7 @@ public interface PolicyEngine extends Startable, Lockable, TopicListener { * Policy Engine Manager Implementation */ class PolicyEngineManager implements PolicyEngine { + /** * logger */ @@ -390,6 +403,41 @@ class PolicyEngineManager implements PolicyEngine { } } + + /** + * produces a minimum configuration with the telemetry service enabled + * + * @return policy engine configuration + */ + @Override + public final Properties defaultTelemetryConfig() { + Properties defaultConfig = new Properties(); + + defaultConfig.put(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES, "TELEMETRY"); + defaultConfig.put(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + TELEMETRY_SERVER_DEFAULT_NAME + + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, + TELEMETRY_SERVER_DEFAULT_HOST); + defaultConfig.put(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + TELEMETRY_SERVER_DEFAULT_NAME + + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, + "" + TELEMETRY_SERVER_DEFAULT_PORT); + defaultConfig.put(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + TELEMETRY_SERVER_DEFAULT_NAME + + PolicyProperties.PROPERTY_HTTP_REST_PACKAGES_SUFFIX, + RestManager.class.getPackage().getName()); + defaultConfig.put(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + TELEMETRY_SERVER_DEFAULT_NAME + + PolicyProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, + true); + defaultConfig.put(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + TELEMETRY_SERVER_DEFAULT_NAME + + PolicyProperties.PROPERTY_MANAGED_SUFFIX, + false); + + return defaultConfig; + } + @Override public synchronized void configure(Properties properties) { @@ -813,7 +861,7 @@ class PolicyEngineManager implements PolicyEngine { logger.error("{}: cannot start http-server {} because of {}", this, httpServer, e.getMessage(), e); } - } + } /* policy-engine dispatch pre stop hook */ for (PolicyEngineFeatureAPI feature : PolicyEngineFeatureAPI.providers.getList()) { diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/test/PolicyEngineTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/test/PolicyEngineTest.java index 1a771032..4928ca0a 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/test/PolicyEngineTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/test/PolicyEngineTest.java @@ -22,15 +22,19 @@ package org.onap.policy.drools.system.test; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Properties; -import javax.ws.rs.core.Response; - import org.junit.AfterClass; import org.junit.BeforeClass; +import org.junit.FixMethodOrder; import org.junit.Test; -import org.onap.policy.drools.http.client.HttpClient; -import org.onap.policy.drools.http.server.HttpServletServer; +import org.junit.runners.MethodSorters; +import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.drools.properties.PolicyProperties; import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyEngine; import org.slf4j.Logger; @@ -39,63 +43,192 @@ import org.slf4j.LoggerFactory; /** * PolicyEngine unit tests */ + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class PolicyEngineTest { + /** + * Default Telemetry port for JUnits + */ + public static final int DEFAULT_TELEMETRY_PORT = 9698; + + /** + * Test JUnit Controller Name + */ + public static final String TEST_CONTROLLER_NAME = "unnamed"; + + /** + * Controller Configuration File + */ + public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties"; + + /** + * Controller Configuration Backup File + */ + public static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_NAME + "-controller.properties.bak"; + + /** + * logger + */ private static Logger logger = LoggerFactory.getLogger(PolicyEngineTest.class); + /** + * clean up working directory + */ + protected static void cleanUpWorkingDir() { + Path testControllerPath = Paths.get(SystemPersistence.CONFIG_DIR_NAME, TEST_CONTROLLER_FILE); + try { + Files.deleteIfExists(testControllerPath); + } catch (Exception e) { + logger.info("Problem cleaning {}", testControllerPath, e); + } + + Path testControllerBakPath = Paths.get(SystemPersistence.CONFIG_DIR_NAME, TEST_CONTROLLER_FILE_BAK); + try { + Files.deleteIfExists(testControllerBakPath); + } catch (Exception e) { + logger.info("Problem cleaning {}", testControllerBakPath, e); + } + } + @BeforeClass - public static void startUp() { - logger.info("----- TEST: startUp() ---------"); + public static void startUp() throws IOException { + logger.info("enter"); - Properties engineProperties = new Properties(); - engineProperties.put("http.server.services", "CONFIG"); - engineProperties.put("http.server.services.CONFIG.host", "0.0.0.0"); - engineProperties.put("http.server.services.CONFIG.port", "9696"); - engineProperties.put("http.server.services.CONFIG.restPackages", "org.onap.policy.drools.server.restful"); + cleanUpWorkingDir(); - assertFalse(PolicyEngine.manager.isAlive()); + /* ensure presence of config directory */ + Path configDir = Paths.get(SystemPersistence.CONFIG_DIR_NAME); + if (Files.notExists(configDir)) + Files.createDirectories(configDir); + } + + @AfterClass + public static void tearDown() throws IOException { + logger.info("enter"); + cleanUpWorkingDir(); + } + + @Test + public void test100Configure() { + logger.info("enter"); - PolicyEngine.manager.configure(engineProperties); + Properties engineProps = PolicyEngine.manager.defaultTelemetryConfig(); + + /* override default port */ + engineProps.put(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + + PolicyEngine.TELEMETRY_SERVER_DEFAULT_NAME + + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, + ""+DEFAULT_TELEMETRY_PORT); + + assertFalse(PolicyEngine.manager.isAlive()); + PolicyEngine.manager.configure(engineProps); assertFalse(PolicyEngine.manager.isAlive()); + logger.info("policy-engine {} has configuration {}", PolicyEngine.manager, engineProps); + } + + @Test + public void test200Start() { + logger.info("enter"); + PolicyEngine.manager.start(); + assertTrue(PolicyEngine.manager.isAlive()); assertFalse(PolicyEngine.manager.isLocked()); - assertTrue(HttpServletServer.factory.get(9696).isAlive()); + assertFalse(PolicyEngine.manager.getHttpServers().isEmpty()); + assertTrue(PolicyEngine.manager.getHttpServers().get(0).isAlive()); } - @AfterClass - public static void tearDown() { - logger.info("----- TEST: tearDown() ---------"); + @Test + public void test300Lock() { + logger.info("enter"); - PolicyEngine.manager.stop(); - assertFalse(PolicyEngine.manager.isAlive()); + PolicyEngine.manager.lock(); + + assertTrue(PolicyEngine.manager.isAlive()); + assertTrue(PolicyEngine.manager.isLocked()); + assertFalse(PolicyEngine.manager.getHttpServers().isEmpty()); + assertTrue(PolicyEngine.manager.getHttpServers().get(0).isAlive()); + } + + @Test + public void test301Unlock() { + logger.info("enter"); + + PolicyEngine.manager.unlock(); + + assertTrue(PolicyEngine.manager.isAlive()); + assertFalse(PolicyEngine.manager.isLocked()); + assertFalse(PolicyEngine.manager.getHttpServers().isEmpty()); + assertTrue(PolicyEngine.manager.getHttpServers().get(0).isAlive()); } @Test - public void addController() throws Exception { - logger.info("----- TEST: addController() ---------"); + public void test400ControllerAdd() throws Exception { + logger.info("enter"); Properties controllerProperties = new Properties(); - controllerProperties.put("controller.name", "unnamed"); + controllerProperties.put(PolicyProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME); + PolicyEngine.manager.createPolicyController(TEST_CONTROLLER_NAME, controllerProperties); - PolicyEngine.manager.createPolicyController("unnamed", controllerProperties); assertTrue(PolicyController.factory.inventory().size() == 1); + } + + @Test + public void test401ControllerVerify() { + logger.info("enter"); - HttpClient client = HttpClient.factory.build("telemetry", false, false, - "localhost", 9696, "policy/pdp", - null, null, false); - Response response = client.get("engine"); - Object body = HttpClient.getBody(response, Object.class); - logger.info("policy-engine: {}", body); + PolicyController testController = PolicyController.factory.get(TEST_CONTROLLER_NAME); - assertTrue(response.getStatus() == 200); + assertFalse(testController.isAlive()); + assertFalse(testController.isLocked()); + + testController.start(); + + assertTrue(testController.isAlive()); + assertFalse(testController.isLocked()); + } + + @Test + public void test500Deactivate() throws Exception { + logger.info("enter"); - PolicyController testController = PolicyController.factory.get("unnamed"); - assertFalse(testController.getDrools().isAlive()); - assertFalse(testController.getDrools().isLocked()); + PolicyEngine.manager.deactivate(); - PolicyEngine.manager.removePolicyController("unnamed"); + PolicyController testController = PolicyController.factory.get(TEST_CONTROLLER_NAME); + assertFalse(testController.isAlive()); + assertTrue(testController.isLocked()); + assertTrue(PolicyEngine.manager.isLocked()); + assertTrue(PolicyEngine.manager.isAlive()); + } + + @Test + public void test501Activate() throws Exception { + logger.info("enter"); + + PolicyEngine.manager.activate(); + + PolicyController testController = PolicyController.factory.get(TEST_CONTROLLER_NAME); + assertTrue(testController.isAlive()); + assertFalse(testController.isLocked()); + assertFalse(PolicyEngine.manager.isLocked()); + assertTrue(PolicyEngine.manager.isAlive()); + } + + @Test + public void test900ControllerRemove() throws Exception { + logger.info("enter"); + + PolicyEngine.manager.removePolicyController(TEST_CONTROLLER_NAME); assertTrue(PolicyController.factory.inventory().isEmpty()); } + + @Test + public void test901Stop() { + logger.info("enter"); + + PolicyEngine.manager.stop(); + assertFalse(PolicyEngine.manager.isAlive()); + } } diff --git a/policy-management/src/test/resources/logback-test.xml b/policy-management/src/test/resources/logback-test.xml index 58e3248e..4a3b561f 100644 --- a/policy-management/src/test/resources/logback-test.xml +++ b/policy-management/src/test/resources/logback-test.xml @@ -10,7 +10,7 @@ - + -- cgit 1.2.3-korg