Changes to make Policy-API container crash with non zero exitCode 78/111178/4
authora.sreekumar <ajith.sreekumar@bell.ca>
Wed, 12 Aug 2020 12:07:37 +0000 (13:07 +0100)
committera.sreekumar <ajith.sreekumar@bell.ca>
Fri, 14 Aug 2020 12:43:09 +0000 (13:43 +0100)
Change-Id: I9e46aa87bfefbd56ef0077017293df34b654e36f
Issue-ID: POLICY-2755
Signed-off-by: a.sreekumar <ajith.sreekumar@bell.ca>
main/src/main/java/org/onap/policy/api/main/startstop/Main.java
main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java

index 23dcd26..c8bce6f 100644 (file)
@@ -4,6 +4,7 @@
  * ================================================================================
  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Bell Canada. 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.
@@ -25,8 +26,10 @@ package org.onap.policy.api.main.startstop;
 
 import java.util.Arrays;
 import org.onap.policy.api.main.exception.PolicyApiException;
+import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
 import org.onap.policy.api.main.parameters.ApiParameterGroup;
 import org.onap.policy.api.main.parameters.ApiParameterHandler;
+import org.onap.policy.common.utils.resources.MessageConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -65,41 +68,26 @@ public class Main {
 
             // Validate that the arguments are sane
             arguments.validate();
-        } catch (final PolicyApiException e) {
-            LOGGER.error("start of policy api service failed", e);
-            return;
-        }
 
-        // Read the parameters
-        try {
+            // Read the parameters
             parameterGroup = new ApiParameterHandler().getParameters(arguments);
-        } catch (final Exception e) {
-            LOGGER.error("start of policy api service failed", e);
-            return;
-        }
-
-        // Initialize database
-        try {
+            // Initialize database
             new ApiDatabaseInitializer().initializeApiDatabase(parameterGroup);
-        } catch (final PolicyApiException e) {
-            LOGGER.error("Preloading policy types into DB failed", e);
-            return;
-        }
 
-        // Now, create the activator for the policy api service
-        activator = new ApiActivator(parameterGroup);
+            // Now, create the activator for the policy api service
+            activator = new ApiActivator(parameterGroup);
 
-        // Start the activator
-        try {
+            // Start the activator
             activator.initialize();
         } catch (final PolicyApiException e) {
-            LOGGER.error("start of policy api service failed, used parameters are {} ", argumentString, e);
-            return;
+            throw new PolicyApiRuntimeException(
+                String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API), e);
         }
 
         // Add a shutdown hook to shut everything down in an orderly manner
         Runtime.getRuntime().addShutdownHook(new PolicyApiShutdownHookClass());
-        LOGGER.info("Started policy api service");
+        String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_API);
+        LOGGER.info(successMsg);
     }
 
     /**
index 9039775..0c4eb4e 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2020 Bell Canada. 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.
 package org.onap.policy.api.main.startstop;
 
 import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
+import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
 import org.onap.policy.api.main.parameters.CommonTestData;
 import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.common.utils.resources.MessageConstants;
 
 /**
  * Class to perform unit test of Main.
@@ -51,15 +54,15 @@ public class TestMain {
     @Test
     public void testMain_NoArguments() {
         final String[] apiConfigParameters = {};
-        final Main main = new Main(apiConfigParameters);
-        assertNull(main.getParameters());
+        assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
+            .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
     }
 
     @Test
     public void testMain_InvalidArguments() {
         final String[] apiConfigParameters = {"parameters/ApiConfigParameters.json"};
-        final Main main = new Main(apiConfigParameters);
-        assertNull(main.getParameters());
+        assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
+            .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
     }
 
     @Test
@@ -71,7 +74,7 @@ public class TestMain {
     @Test
     public void testMain_InvalidParameters() {
         final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_InvalidName.json"};
-        final Main main = new Main(apiConfigParameters);
-        assertNull(main.getParameters());
+        assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
+            .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
     }
 }