junits in policy-management 65/8965/2
authorJorge Hernandez <jh1730@att.com>
Mon, 28 Aug 2017 18:51:08 +0000 (13:51 -0500)
committerJorge Hernandez <jh1730@att.com>
Mon, 28 Aug 2017 21:14:02 +0000 (16:14 -0500)
- 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 <jh1730@att.com>
.gitignore
policy-endpoints/src/test/java/org/onap/policy/drools/http/server/test/HttpServerTest.java
policy-endpoints/src/test/resources/logback-test.xml
policy-management/config/policy-engine.properties [deleted file]
policy-management/src/main/java/org/onap/policy/drools/persistence/SystemPersistence.java
policy-management/src/main/java/org/onap/policy/drools/system/Main.java
policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java
policy-management/src/test/java/org/onap/policy/drools/system/test/PolicyEngineTest.java
policy-management/src/test/resources/logback-test.xml

index 817b204..30e6052 100644 (file)
@@ -4,13 +4,12 @@
 .classpath
 .jupiter
 .pydevproject
-target
-.metadata/
-policy-endpoints/logs/
-feature-session-persistence/sql/
-feature-session-persistence/testingLogs/
-policy-utils/debug-logs/
-policy-utils/logs/
-/bin/
+*.swp
 *.log
-policy-management/config/unnamed-controller.properties
+*.out
+.metadata/
+target/
+*/logs/
+*/sql/
+*/testingLogs/
+*/config/
index db2fa0a..aa3fa58 100644 (file)
@@ -24,6 +24,8 @@ import static org.junit.Assert.assertTrue;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.net.ConnectException;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.UUID;
 
@@ -52,20 +54,17 @@ public class HttpServerTest {
                
                assertTrue(HttpServletServer.factory.get(5678).isAlive());
                
-               String echo = "hello";
-               URL url = new URL("http://localhost:5678/junit/echo/" + echo);
-               String response = response(url);
-               assertTrue(response.equals(echo));
+               String response =
+                               http(HttpServletServer.factory.get(5678), "http://localhost:5678/junit/echo/hello");
+               assertTrue("hello".equals(response));
        
-               String responseSwagger =  null;
+               response = null;
                try {
-                       URL urlSwagger = new URL("http://localhost:5678/swagger.json" + echo);
-                       responseSwagger = response(urlSwagger);
-               } catch(IOException ioe) {
+                       response = http(HttpServletServer.factory.get(5678), "http://localhost:5678/swagger.json");
+               } catch (IOException e) {
                        // Expected
                }
-               
-               assertTrue(responseSwagger == null);
+               assertTrue(response == null);
                
                assertTrue(HttpServletServer.factory.get(5678).isAlive());
                assertTrue(HttpServletServer.factory.inventory().size() == 1);
@@ -78,39 +77,36 @@ public class HttpServerTest {
        public void testMultipleServers() throws Exception {
                logger.info("-- testMultipleServers() --");
                
-               HttpServletServer server1 = HttpServletServer.factory.build("echo-1", "localhost", 5678, "/", true, true);
+               HttpServletServer server1 = HttpServletServer.factory.build("echo-1", "localhost", 5688, "/", true, true);
                server1.addServletPackage("/*", this.getClass().getPackage().getName());
                server1.waitedStart(5000);
                
-               HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", 5679, "/", false, true);
+               HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", 5689, "/", false, true);
                server2.addServletPackage("/*", this.getClass().getPackage().getName());
                server2.waitedStart(5000);
                
-               assertTrue(HttpServletServer.factory.get(5678).isAlive());
-               assertTrue(HttpServletServer.factory.get(5679).isAlive());
-               
-               String echo = "hello";
-               
-               URL url1 = new URL("http://localhost:5678/junit/echo/" + echo);
-               String response1 = response(url1);
-               assertTrue(response1.equals(echo));
-               
-               URL urlSwagger = new URL("http://localhost:5678/swagger.json");
-               String responseSwagger = response(urlSwagger);  
-               assertTrue(responseSwagger != null);
+               assertTrue(HttpServletServer.factory.get(5688).isAlive());
+               assertTrue(HttpServletServer.factory.get(5689).isAlive());
+
+               String response =
+                               http(HttpServletServer.factory.get(5688), "http://localhost:5688/junit/echo/hello");
+               assertTrue("hello".equals(response));
                
-               URL url2 = new URL("http://localhost:5679/junit/echo/" + echo);
-               String response2 = response(url2);
-               assertTrue(response2.equals(echo));
+               response =
+                               http(HttpServletServer.factory.get(5688), "http://localhost:5688/swagger.json");
+               assertTrue(response != null);
+
+               response =
+                               http(HttpServletServer.factory.get(5689), "http://localhost:5689/junit/echo/hello");
+               assertTrue("hello".equals(response));   
                
-               String responseSwagger2 =  null;
+               response = null;
                try {
-                       URL urlSwagger2 = new URL("http://localhost:5679/swagger.json");
-                       responseSwagger2 = response(urlSwagger2);
-               } catch(IOException ioe) {
+                       response = http(HttpServletServer.factory.get(5689), "http://localhost:5689/swagger.json");
+               } catch (IOException e) {
                        // Expected
                }
-               assertTrue(responseSwagger2 == null);
+               assertTrue(response == null);
                
                HttpServletServer.factory.destroy();            
                assertTrue(HttpServletServer.factory.inventory().size() == 0);
@@ -122,20 +118,19 @@ public class HttpServerTest {
                
                String randomName = UUID.randomUUID().toString();
                
-               HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", false, true);
+               HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5668, "/", false, true);
                server.addServletPackage("/*", this.getClass().getPackage().getName());
                server.waitedStart(5000);
                
-               assertTrue(HttpServletServer.factory.get(5678).isAlive());
+               assertTrue(HttpServletServer.factory.get(5668).isAlive());
                
-               String echo = "hello";
-               URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo);
-               String responseService1 = response(urlService1);
-               assertTrue(responseService1.equals(echo));
+               String response =
+                               http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/echo/hello");
+               assertTrue("hello".equals(response));
                
-               URL urlService2 = new URL("http://localhost:5678/junit/endpoints/http/servers");
-               String responseService2 = response(urlService2);
-               assertTrue(responseService2.contains(randomName));
+               response =
+                               http(HttpServletServer.factory.get(5668), "http://localhost:5668/junit/endpoints/http/servers");
+               assertTrue(response.contains(randomName));
                
                HttpServletServer.factory.destroy();            
                assertTrue(HttpServletServer.factory.inventory().size() == 0);
@@ -146,16 +141,15 @@ public class HttpServerTest {
                logger.info("-- testServiceClass() --");
                String randomName = UUID.randomUUID().toString();
                
-               HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", false, true);
+               HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5658, "/", false, true);
                server.addServletClass("/*", RestEchoService.class.getCanonicalName());
                server.waitedStart(5000);
                
-               assertTrue(HttpServletServer.factory.get(5678).isAlive());
+               assertTrue(HttpServletServer.factory.get(5658).isAlive());
                
-               String echo = "hello";
-               URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo);
-               String responseService1 = response(urlService1);
-               assertTrue(responseService1.equals(echo));
+               String response =
+                               http(HttpServletServer.factory.get(5658), "http://localhost:5658/junit/echo/hello");
+               assertTrue("hello".equals(response));
                
                HttpServletServer.factory.destroy();            
                assertTrue(HttpServletServer.factory.inventory().size() == 0);
@@ -167,38 +161,69 @@ public class HttpServerTest {
                
                String randomName = UUID.randomUUID().toString();
                
-               HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", false, true);
+               HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5648, "/", false, true);
                server.addServletClass("/*", RestEchoService.class.getCanonicalName());
                server.addServletClass("/*", RestEndpoints.class.getCanonicalName());
                server.waitedStart(5000);
                
-               assertTrue(HttpServletServer.factory.get(5678).isAlive());
+               assertTrue(HttpServletServer.factory.get(5648).isAlive());
                
-               String echo = "hello";
-               URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo);
-               String responseService1 = response(urlService1);
-               assertTrue(responseService1.equals(echo));
+               String response =
+                               http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/echo/hello");
+               assertTrue("hello".equals(response));
                
-               URL urlService2 = new URL("http://localhost:5678/junit/endpoints/http/servers");
-               String responseService2 = response(urlService2);
-               assertTrue(responseService2.contains(randomName));
+               response =
+                               http(HttpServletServer.factory.get(5648), "http://localhost:5648/junit/endpoints/http/servers");
+               assertTrue(response.contains(randomName));
                
                HttpServletServer.factory.destroy();            
                assertTrue(HttpServletServer.factory.inventory().size() == 0);
        }
 
        /**
-        * @param url
+        * performs an http request 
+        * 
+        * @throws MalformedURLException
+        * @throws IOException
+        * @throws InterruptedException 
+        */
+       protected String http(HttpServletServer server, String aUrl) 
+                       throws MalformedURLException, IOException, InterruptedException {
+               URL url = new URL(aUrl);
+               String response = null;
+               int numRetries = 1, maxNumberRetries = 5;
+               while (numRetries <= maxNumberRetries) {
+                       try {
+                               response = response(url);
+                               break;
+                       } catch (ConnectException e) {
+                               logger.warn("http server {} @ {} ({}) - cannot connect yet ..", 
+                                                       server, aUrl, numRetries, e);
+                               numRetries++;
+                               Thread.sleep(10000L);
+                       } catch (Exception e) {
+                               throw e;
+                       } 
+               }
+               
+               return response;
+       }
+
+       /**
+        * gets http response 
+        * 
+        * @param url url
+        * 
         * @throws IOException
         */
        protected String response(URL url) throws IOException {
-               BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream()));
                String response = "";
-               String line;
-               while ((line = ioReader.readLine()) != null) {
-                       response += line; 
-               }
-               ioReader.close();
+               try (BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
+                       String line;
+                       while ((line = ioReader.readLine()) != null) {
+                               response += line; 
+                       }
+               } 
                return response;
        }
        
index d93bc4a..6f74515 100644 (file)
@@ -10,7 +10,7 @@
     
     <logger name="org.onap.policy.drools.http.server.test" level="INFO"/>
 
-    <root level="warn">
+    <root level="WARN">
         <appender-ref ref="STDOUT"/>
     </root>
 
diff --git a/policy-management/config/policy-engine.properties b/policy-management/config/policy-engine.properties
deleted file mode 100644 (file)
index bf28bc8..0000000
+++ /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
index bdda8e8..2390fc8 100644 (file)
@@ -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;
index fe0cc01..49dfafc 100644 (file)
@@ -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)) {
index 3682165..d7e0ecd 100644 (file)
@@ -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;
  * <br>
  * 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()) {
index 1a77103..4928ca0 100644 (file)
@@ -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());
+       }
 
 }
index 58e3248..4a3b561 100644 (file)
@@ -10,7 +10,7 @@
     
     <logger name="org.onap.policy.drools.system.test" level="INFO"/>
 
-    <root level="warn">
+    <root level="WARN">
         <appender-ref ref="STDOUT"/>
     </root>