From 5b4ed906ca55a2e24973c391c6006d282e0fe82a Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Mon, 17 Aug 2020 11:29:16 +0100 Subject: Changes to make APEX container crash with non zero exitCode Change-Id: I1d36c28cfb07881ebede1cf918140397b076512c Issue-ID: POLICY-2753 Signed-off-by: a.sreekumar --- .../apex/services/onappf/ApexStarterMain.java | 42 ++++++++++------------ .../apex/services/onappf/TestApexStarterMain.java | 36 +++++++++++-------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java index 609be10c2..7daa22846 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * 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 +25,10 @@ package org.onap.policy.apex.services.onappf; import java.util.Arrays; import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; +import org.onap.policy.common.utils.resources.MessageConstants; import org.onap.policy.common.utils.services.Registry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,8 +40,6 @@ import org.slf4j.LoggerFactory; */ public class ApexStarterMain { - private static final String APEX_STARTER_FAIL_MSG = "start of services-onappf failed"; - private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterMain.class); private ApexStarterActivator activator; @@ -65,36 +65,30 @@ public class ApexStarterMain { } // Validate that the arguments are sane arguments.validate(); - } catch (final ApexStarterException e) { - LOGGER.error(APEX_STARTER_FAIL_MSG, e); - return; - } - // Read the parameters - try { + // Read the parameters parameterGroup = new ApexStarterParameterHandler().getParameters(arguments); - } catch (final Exception e) { - LOGGER.error(APEX_STARTER_FAIL_MSG, e); - return; - } - // create the activator - activator = new ApexStarterActivator(parameterGroup); - Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); - Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, new ApexPolicyStatisticsManager()); - // Start the activator - try { + // create the activator + activator = new ApexStarterActivator(parameterGroup); + Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); + Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, + new ApexPolicyStatisticsManager()); + + // Start the activator activator.initialize(); } catch (final ApexStarterException e) { - LOGGER.error("start of ApexStarter failed, used parameters are {}", Arrays.toString(args), e); - Registry.unregister(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR); - return; + if (null != activator) { + Registry.unregister(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR); + } + throw new ApexStarterRunTimeException( + String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_APEX_PDP), e); } // Add a shutdown hook to shut everything down in an orderly manner Runtime.getRuntime().addShutdownHook(new ApexStarterShutdownHookClass()); - - LOGGER.info("Started ApexStarter service"); + String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_APEX_PDP); + LOGGER.info(successMsg); } /** diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java index adcec020b..f24329c29 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2020 Nordix Foundation + * 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. @@ -22,9 +23,9 @@ package org.onap.policy.apex.services.onappf; 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.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.After; @@ -32,7 +33,9 @@ import org.junit.Before; import org.junit.Test; import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException; import org.onap.policy.apex.services.onappf.parameters.CommonTestData; +import org.onap.policy.common.utils.resources.MessageConstants; import org.onap.policy.common.utils.services.Registry; /** @@ -59,8 +62,8 @@ public class TestApexStarterMain { @After public void tearDown() throws Exception { // shut down activator - final ApexStarterActivator activator = Registry.getOrDefault(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, - ApexStarterActivator.class, null); + final ApexStarterActivator activator = + Registry.getOrDefault(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class, null); if (activator != null && activator.isAlive()) { activator.terminate(); } @@ -68,43 +71,46 @@ public class TestApexStarterMain { @Test public void testApexStarter() throws ApexStarterException { - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; + final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; apexStarter = new ApexStarterMain(apexStarterConfigParameters); assertTrue(apexStarter.getParameters().isValid()); assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName()); // ensure items were added to the registry assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class)); - assertNotNull(Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, - ApexPolicyStatisticsManager.class)); + assertNotNull( + Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, ApexPolicyStatisticsManager.class)); apexStarter.shutdown(); } @Test public void testApexStarter_NoArguments() { final String[] apexStarterConfigParameters = {}; - apexStarter = new ApexStarterMain(apexStarterConfigParameters); - assertNull(apexStarter.getParameters()); + assertThatThrownBy(() -> new ApexStarterMain(apexStarterConfigParameters)) + .isInstanceOf(ApexStarterRunTimeException.class) + .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_APEX_PDP)); } @Test public void testApexStarter_InvalidArguments() { - final String[] apexStarterConfigParameters = { "src/test/resources/ApexStarterConfigParameters.json" }; - apexStarter = new ApexStarterMain(apexStarterConfigParameters); - assertNull(apexStarter.getParameters()); + final String[] apexStarterConfigParameters = {"src/test/resources/ApexStarterConfigParameters.json"}; + assertThatThrownBy(() -> new ApexStarterMain(apexStarterConfigParameters)) + .isInstanceOf(ApexStarterRunTimeException.class) + .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_APEX_PDP)); } @Test public void testApexStarter_Help() { - final String[] apexStarterConfigParameters = { "-h" }; + final String[] apexStarterConfigParameters = {"-h"}; assertThatCode(() -> ApexStarterMain.main(apexStarterConfigParameters)).doesNotThrowAnyException(); } @Test public void testApexStarter_InvalidParameters() { final String[] apexStarterConfigParameters = - { "-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json" }; - apexStarter = new ApexStarterMain(apexStarterConfigParameters); - assertNull(apexStarter.getParameters()); + {"-c", "src/test/resources/ApexStarterConfigParameters_InvalidName.json"}; + assertThatThrownBy(() -> new ApexStarterMain(apexStarterConfigParameters)) + .isInstanceOf(ApexStarterRunTimeException.class) + .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_APEX_PDP)); } } -- cgit 1.2.3-korg