aboutsummaryrefslogtreecommitdiffstats
path: root/tosca-controlloop/runtime/src/test
diff options
context:
space:
mode:
authorFrancescoFioraEst <francesco.fiora@est.tech>2021-02-17 10:11:02 +0000
committerFrancesco Fiora <francesco.fiora@est.tech>2021-02-23 14:07:33 +0000
commit2bb80fbb9b8817158db912e45ae43a1a2d2e48a4 (patch)
tree13f70e6ac5b3c810227e48c300e6d063386d44f0 /tosca-controlloop/runtime/src/test
parent0440c94ef2a7e9c66eefbae4bf09aa5207dc019d (diff)
Added Main initial code
Issue-ID: POLICY-2995 Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech> Change-Id: I6f7da312f00341be8928fb66875c309c6933f194
Diffstat (limited to 'tosca-controlloop/runtime/src/test')
-rw-r--r--tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/ClRuntimeActivatorTest.java81
-rw-r--r--tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/MainTest.java157
-rw-r--r--tosca-controlloop/runtime/src/test/resources/parameters/EmptyParameters.json0
-rw-r--r--tosca-controlloop/runtime/src/test/resources/parameters/InvalidParameters.json3
-rw-r--r--tosca-controlloop/runtime/src/test/resources/parameters/NoParameters.json2
-rw-r--r--tosca-controlloop/runtime/src/test/resources/parameters/TestParameters.json79
-rw-r--r--tosca-controlloop/runtime/src/test/resources/parameters/Unreadable.json79
7 files changed, 401 insertions, 0 deletions
diff --git a/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/ClRuntimeActivatorTest.java b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/ClRuntimeActivatorTest.java
new file mode 100644
index 000000000..7928124c8
--- /dev/null
+++ b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/ClRuntimeActivatorTest.java
@@ -0,0 +1,81 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.controlloop.runtime.main.startstop;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
+import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
+import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterHandler;
+import org.onap.policy.common.utils.services.Registry;
+
+/**
+ * Class to perform unit test of {@link ClRuntimeActivator}}.
+ *
+ */
+public class ClRuntimeActivatorTest {
+
+ @Test
+ public void testStartAndStop() throws Exception {
+ Registry.newRegistry();
+ final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
+ final ClRuntimeCommandLineArguments arguments = new ClRuntimeCommandLineArguments();
+ arguments.parse(configParameters);
+ ClRuntimeParameterGroup parameterGroup = new ClRuntimeParameterHandler().getParameters(arguments);
+ ClRuntimeActivator activator = new ClRuntimeActivator(parameterGroup);
+ activator.isAlive();
+
+ assertFalse(activator.isAlive());
+ activator.start();
+ assertTrue(activator.isAlive());
+ assertTrue(activator.getParameterGroup().isValid());
+ assertEquals(activator.getParameterGroup().getName(),
+ activator.getParameterGroup().getRestServerParameters().getName());
+
+ // repeat start - should throw an exception
+ assertThatIllegalStateException().isThrownBy(() -> activator.start());
+ assertTrue(activator.isAlive());
+ assertTrue(activator.getParameterGroup().isValid());
+
+ activator.stop();
+ assertFalse(activator.isAlive());
+
+ // repeat stop - should throw an exception
+ assertThatIllegalStateException().isThrownBy(() -> activator.stop());
+ assertFalse(activator.isAlive());
+ }
+
+ @Test
+ public void testNull() {
+ assertThatExceptionOfType(ControlLoopRuntimeException.class).isThrownBy(() -> new ClRuntimeActivator(null));
+ }
+
+ @Test
+ public void testNotValid() {
+ assertThatExceptionOfType(ControlLoopRuntimeException.class)
+ .isThrownBy(() -> new ClRuntimeActivator(new ClRuntimeParameterGroup("name")));
+ }
+}
diff --git a/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/MainTest.java b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/MainTest.java
new file mode 100644
index 000000000..6dd4031ed
--- /dev/null
+++ b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/main/startstop/MainTest.java
@@ -0,0 +1,157 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.controlloop.runtime.main.startstop;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.policy.clamp.controlloop.common.ControlLoopConstants;
+import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
+import org.onap.policy.common.utils.resources.MessageConstants;
+import org.onap.policy.common.utils.services.Registry;
+
+/**
+ * Class to perform unit test of {@link Main}}.
+ *
+ */
+public class MainTest {
+
+ public static final String POLICY_CLAMP_FAILURE_MSG =
+ String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP);
+
+ /**
+ * Set up.
+ */
+ @BeforeClass
+ public static void setUp() {
+ Registry.newRegistry();
+ }
+
+ /**
+ * Shuts "main" down.
+ *
+ * @throws Exception if an error occurs
+ */
+ @AfterClass
+ public static void tearDown() throws Exception {
+ // shut down activator
+ final ClRuntimeActivator activator =
+ Registry.getOrDefault(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ClRuntimeActivator.class, null);
+ if (activator != null && activator.isAlive()) {
+ activator.shutdown();
+ }
+ }
+
+ @Test
+ public void testMain_Help() {
+ final String[] configParameters = {"-h"};
+ Main main = new Main(configParameters);
+ assertFalse(main.isRunning());
+ }
+
+ @Test
+ public void testMain_Version() {
+ final String[] configParameters = {"-v"};
+ Main main = new Main(configParameters);
+ assertFalse(main.isRunning());
+ }
+
+ @Test
+ public void testMain_Valid() {
+ final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
+ Main main = new Main(configParameters);
+ assertTrue(main.isRunning());
+
+ // ensure items were added to the registry
+ assertNotNull(Registry.get(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, ClRuntimeActivator.class));
+
+ assertThatCode(() -> main.shutdown()).doesNotThrowAnyException();
+
+ assertFalse(main.isRunning());
+ }
+
+ @Test
+ public void testMain_NoParameter() {
+ assertThatConfigParameterThrownException(new String[] {});
+ }
+
+ @Test
+ public void testMain_FilePathNotDefined() {
+ assertThatConfigParameterThrownException(new String[] {"-c"});
+ }
+
+ @Test
+ public void testMain_TooManyCommand() {
+ assertThatConfigParameterThrownException(new String[] {"-h", "d"});
+ }
+
+ @Test
+ public void testMain_WrongParameter() {
+ assertThatConfigParameterThrownException(new String[] {"-d"});
+ }
+
+ private void assertThatConfigParameterThrownException(final String[] configParameters) {
+ assertThatThrownBy(() -> Main.main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
+ .hasMessage(POLICY_CLAMP_FAILURE_MSG);
+ }
+
+ @Test
+ public void testParticipant_NoFileWithThisName() {
+ assertThatConfigFileThrownException("src/test/resources/parameters/NoFileWithThisName.json");
+ }
+
+ @Test
+ public void testParticipant_NotValidFile() {
+ assertThatConfigFileThrownException("src/test/resources/parameters");
+ }
+
+ @Test
+ public void testParticipant_FileEmpty() {
+ assertThatConfigFileThrownException("src/test/resources/parameters/EmptyParameters.json");
+ }
+
+ @Test
+ public void testParticipant_NoParameters() {
+ assertThatConfigFileThrownException("src/test/resources/parameters/NoParameters.json");
+ }
+
+ @Test
+ public void testParticipant_InvalidParameters() {
+ assertThatConfigFileThrownException("src/test/resources/parameters/InvalidParameters.json");
+ }
+
+ @Test
+ public void testParticipant_WrongJsonFormat() {
+ assertThatConfigFileThrownException("src/test/resources/parameters/Unreadable.json");
+ }
+
+ private void assertThatConfigFileThrownException(final String configFilePath) {
+ final String[] configParameters = new String[] {"-c", configFilePath};
+ assertThatThrownBy(() -> new Main(configParameters)).isInstanceOf(ControlLoopRuntimeException.class)
+ .hasMessage(POLICY_CLAMP_FAILURE_MSG);
+ }
+}
diff --git a/tosca-controlloop/runtime/src/test/resources/parameters/EmptyParameters.json b/tosca-controlloop/runtime/src/test/resources/parameters/EmptyParameters.json
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tosca-controlloop/runtime/src/test/resources/parameters/EmptyParameters.json
diff --git a/tosca-controlloop/runtime/src/test/resources/parameters/InvalidParameters.json b/tosca-controlloop/runtime/src/test/resources/parameters/InvalidParameters.json
new file mode 100644
index 000000000..976ec2937
--- /dev/null
+++ b/tosca-controlloop/runtime/src/test/resources/parameters/InvalidParameters.json
@@ -0,0 +1,3 @@
+{
+ "name": ""
+}
diff --git a/tosca-controlloop/runtime/src/test/resources/parameters/NoParameters.json b/tosca-controlloop/runtime/src/test/resources/parameters/NoParameters.json
new file mode 100644
index 000000000..2c63c0851
--- /dev/null
+++ b/tosca-controlloop/runtime/src/test/resources/parameters/NoParameters.json
@@ -0,0 +1,2 @@
+{
+}
diff --git a/tosca-controlloop/runtime/src/test/resources/parameters/TestParameters.json b/tosca-controlloop/runtime/src/test/resources/parameters/TestParameters.json
new file mode 100644
index 000000000..c3be762d6
--- /dev/null
+++ b/tosca-controlloop/runtime/src/test/resources/parameters/TestParameters.json
@@ -0,0 +1,79 @@
+{
+ "name": "ControlLoopRuntimeGroup",
+ "restServerParameters": {
+ "host": "0.0.0.0",
+ "port": 6969,
+ "userName": "healthcheck",
+ "password": "zb!XztG34",
+ "https": false,
+ "aaf": false
+ },
+ "participantParameters": {
+ "heartBeatMs": 120000,
+ "updateParameters": {
+ "maxRetryCount": 1,
+ "maxWaitMs": 30000
+ },
+ "stateChangeParameters": {
+ "maxRetryCount": 1,
+ "maxWaitMs": 30000
+ }
+ },
+ "databaseProviderParameters": {
+ "name": "PolicyProviderParameterGroup",
+ "implementation": "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl",
+ "databaseDriver": "org.h2.Driver",
+ "databaseUrl": "jdbc:h2:mem:testdb",
+ "databaseUser": "policy",
+ "databasePassword": "P01icY",
+ "persistenceUnit": "ToscaConceptTest"
+ },
+ "topicParameterGroup": {
+ "topicSources": [
+ {
+ "topic": "POLICY-CLRUNTIME-PARTICIPANT",
+ "servers": [
+ "localhost"
+ ],
+ "topicCommInfrastructure": "dmaap",
+ "fetchTimeout": 15000
+ }
+ ],
+ "topicSinks": [
+ {
+ "topic": "POLICY-CLRUNTIME-PARTICIPANT",
+ "servers": [
+ "localhost"
+ ],
+ "topicCommInfrastructure": "dmaap"
+ },
+ {
+ "topic": "POLICY-NOTIFICATION",
+ "servers": [
+ "localhost"
+ ],
+ "topicCommInfrastructure": "dmaap"
+ }
+ ]
+ },
+ "healthCheckRestClientParameters": [
+ {
+ "clientName": "api",
+ "hostname": "policy-api",
+ "port": 6969,
+ "userName": "healthcheck",
+ "password": "zb!XztG34",
+ "useHttps": true,
+ "basePath": "policy/api/v1/healthcheck"
+ },
+ {
+ "clientName": "distribution",
+ "hostname": "policy-distribution",
+ "port": 6969,
+ "userName": "healthcheck",
+ "password": "zb!XztG34",
+ "useHttps": true,
+ "basePath": "healthcheck"
+ }
+ ]
+}
diff --git a/tosca-controlloop/runtime/src/test/resources/parameters/Unreadable.json b/tosca-controlloop/runtime/src/test/resources/parameters/Unreadable.json
new file mode 100644
index 000000000..e69de158b
--- /dev/null
+++ b/tosca-controlloop/runtime/src/test/resources/parameters/Unreadable.json
@@ -0,0 +1,79 @@
+{
+ "name": "ControlLoopRuntimeGroup",
+ "restServerParameters": {
+ "host": "0.0.0.0",
+ "port": 6969,
+ "userName": "healthcheck",
+ "password": "zb!XztG34",
+ "https": false,
+ "aaf": false
+ },
+ "participantParameters": {
+ "heartBeatMs": 120000,
+ "updateParameters": {
+ "maxRetryCount": 1,
+ "maxWaitMs": 30000
+ },
+ "stateChangeParameters": {
+ "maxRetryCount": 1,
+ "maxWaitMs": 30000
+ }
+ },
+ "databaseProviderParameters": {
+ "name": "PolicyProviderParameterGroup",
+ "implementation": "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl",
+ "databaseDriver": "org.h2.Driver",
+ "databaseUrl": "jdbc:h2:mem:testdb",
+ "databaseUser": "policy",
+ "databasePassword": "P01icY",
+ "persistenceUnit": "ToscaConceptTest"
+ },
+ "topicParameterGroup": {
+ "topicSources": [
+ {
+ "topic": "POLICY-CLRUNTIME-PARTICIPANT",
+ "servers": [
+ "localhost"
+ ],
+ "topicCommInfrastructure": "dmaap",
+ "fetchTimeout": 15000
+ }
+ ],
+ "topicSinks": [
+ {
+ "topic": "POLICY-CLRUNTIME-PARTICIPANT",
+ "servers": [
+ "localhost"
+ ],
+ "topicCommInfrastructure": "dmaap"
+ },
+ {
+ "topic": "POLICY-NOTIFICATION",
+ "servers": [
+ "localhost"
+ ],
+ "topicCommInfrastructure": "dmaap"
+ }
+ ]
+ },
+ "healthCheckRestClientParameters": [
+ {
+ "clientName": "api",
+ "hostname": "policy-api",
+ "port": 6969,
+ "userName": "healthcheck",
+ "password": "zb!XztG34",
+ "useHttps": true,
+ "basePath": "policy/api/v1/healthcheck"
+ },
+ {
+ "clientName": "distribution",
+ "hostname": "policy-distribution",
+ "port": 6969,
+ "userName": "healthcheck",
+ "password": "zb!XztG34",
+ "useHttps": true,
+ "basePath": "healthcheck"
+ }
+ ]
+ \ No newline at end of file