diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2021-02-24 11:51:58 +0000 |
---|---|---|
committer | FrancescoFioraEst <francesco.fiora@est.tech> | 2021-03-01 17:47:20 +0000 |
commit | 45e55ebb10dbcb48cad6a5e111df72439286de53 (patch) | |
tree | d23d5dcf92650242dc3ba2f47047b3240d045761 /tosca-controlloop/runtime/src/test/java | |
parent | 19f16ce2d6c58e5b603dc99c760820341563bebe (diff) |
Add Instantiation Provider
Issue-ID: POLICY-2980
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Change-Id: I957f947ece23fbd5f1745e18f909754c5945f465
Diffstat (limited to 'tosca-controlloop/runtime/src/test/java')
3 files changed, 479 insertions, 0 deletions
diff --git a/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java new file mode 100644 index 000000000..a0940ef97 --- /dev/null +++ b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java @@ -0,0 +1,294 @@ +/*- + * ============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.instantiation; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import java.util.ArrayList; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; +import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand; +import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; +import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +/** + * Class to perform unit test of {@link ControlLoopInstantiationProvider}}. + * + */ +public class ControlLoopInstantiationProviderTest { + + private static final String CL_INSTANTIATION_CREATE_JSON = "src/test/resources/rest/controlloops/ControlLoops.json"; + private static final String CL_INSTANTIATION_UPDATE_JSON = + "src/test/resources/rest/controlloops/ControlLoopsUpdate.json"; + private static final String CL_INSTANTIATION_CHANGE_STATE_JSON = + "src/test/resources/rest/controlloops/PassiveCommand.json"; + + private static final String CONTROL_LOOP_NOT_FOUND = "Control Loop not found"; + private static final String DELETE_BAD_REQUEST = "Control Loop State is still %s"; + private static final String ORDERED_STATE_INVALID = "ordered state invalid or not specified on command"; + + private static PolicyModelsProviderParameters databaseProviderParameters; + + @BeforeClass + public static void setupDbProviderParameters() throws ControlLoopException, PfModelException { + databaseProviderParameters = + CommonTestData.geParameterGroup(0, "instantproviderdb").getDatabaseProviderParameters(); + } + + @Test + public void testInstantiationCrud() throws Exception { + ControlLoops controlLoopsCreate = + InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Crud"); + + ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); + assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); + + try (ControlLoopInstantiationProvider instantiationProvider = + new ControlLoopInstantiationProvider(databaseProviderParameters)) { + InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate); + InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate); + + controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); + assertThat(controlLoopsDb.getControlLoopList()).isNotEmpty(); + Assert.assertEquals(controlLoopsCreate, controlLoopsDb); + + for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) { + ControlLoops controlLoopsGet = + instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); + assertThat(controlLoopsGet.getControlLoopList()).hasSize(1); + Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0)); + } + + ControlLoops controlLoopsUpdate = + InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_UPDATE_JSON, "Crud"); + Assert.assertNotEquals(controlLoopsUpdate, controlLoopsDb); + + instantiationResponse = instantiationProvider.updateControlLoops(controlLoopsUpdate); + InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsUpdate); + + controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); + assertThat(controlLoopsDb.getControlLoopList()).isNotEmpty(); + Assert.assertEquals(controlLoopsUpdate, controlLoopsDb); + + InstantiationCommand instantiationCommand = + InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Crud"); + instantiationResponse = instantiationProvider.issueControlLoopCommand(instantiationCommand); + InstantiationUtils.assertInstantiationResponse(instantiationResponse, instantiationCommand); + + for (ToscaConceptIdentifier toscaConceptIdentifier : instantiationCommand.getControlLoopIdentifierList()) { + ControlLoops controlLoopsGet = instantiationProvider.getControlLoops(toscaConceptIdentifier.getName(), + toscaConceptIdentifier.getVersion()); + assertThat(controlLoopsGet.getControlLoopList()).hasSize(1); + Assert.assertEquals(instantiationCommand.getOrderedState(), + controlLoopsGet.getControlLoopList().get(0).getOrderedState()); + } + + // in order to delete a controlLoop the state must be UNINITIALISED + controlLoopsCreate.getControlLoopList().forEach(cl -> cl.setState(ControlLoopState.UNINITIALISED)); + instantiationProvider.updateControlLoops(controlLoopsCreate); + + for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) { + instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion()); + } + + controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); + assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); + } + } + + private ControlLoops getControlLoopsFromDb(ControlLoops controlLoopsSource) throws Exception { + ControlLoops controlLoopsDb = new ControlLoops(); + controlLoopsDb.setControlLoopList(new ArrayList<>()); + + try (ControlLoopInstantiationProvider instantiationProvider = + new ControlLoopInstantiationProvider(databaseProviderParameters)) { + + for (ControlLoop controlLoop : controlLoopsSource.getControlLoopList()) { + ControlLoops controlLoopsFromDb = + instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); + controlLoopsDb.getControlLoopList().addAll(controlLoopsFromDb.getControlLoopList()); + } + return controlLoopsDb; + } + } + + @Test + public void testInstantiationDelete() throws Exception { + ControlLoops controlLoops = + InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Delete"); + assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty(); + + ControlLoop controlLoop0 = controlLoops.getControlLoopList().get(0); + + try (ControlLoopInstantiationProvider instantiationProvider = + new ControlLoopInstantiationProvider(databaseProviderParameters)) { + + assertThatThrownBy(() -> { + instantiationProvider.deleteControlLoop(controlLoop0.getName(), controlLoop0.getVersion()); + }).hasMessageMatching(CONTROL_LOOP_NOT_FOUND); + + InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoops), + controlLoops); + + for (ControlLoopState state : ControlLoopState.values()) { + if (!ControlLoopState.UNINITIALISED.equals(state)) { + assertThatDeleteThrownBy(controlLoops, state); + } + } + + controlLoop0.setState(ControlLoopState.UNINITIALISED); + instantiationProvider.updateControlLoops(controlLoops); + + for (ControlLoop controlLoop : controlLoops.getControlLoopList()) { + instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion()); + } + + for (ControlLoop controlLoop : controlLoops.getControlLoopList()) { + ControlLoops controlLoopsGet = + instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); + assertThat(controlLoopsGet.getControlLoopList()).isEmpty(); + } + } + } + + private void assertThatDeleteThrownBy(ControlLoops controlLoops, ControlLoopState state) throws Exception { + ControlLoop controlLoop = controlLoops.getControlLoopList().get(0); + + controlLoop.setState(state); + + try (ControlLoopInstantiationProvider instantiationProvider = + new ControlLoopInstantiationProvider(databaseProviderParameters)) { + + instantiationProvider.updateControlLoops(controlLoops); + assertThatThrownBy(() -> { + instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion()); + }).hasMessageMatching(String.format(DELETE_BAD_REQUEST, state)); + } + } + + @Test + public void testCreateControlLoops_NoDuplicates() throws Exception { + + ControlLoops controlLoopsCreate = + InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "NoDuplicates"); + + ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); + assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); + + try (ControlLoopInstantiationProvider instantiationProvider = + new ControlLoopInstantiationProvider(databaseProviderParameters)) { + InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate); + InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate); + + assertThatThrownBy(() -> { + instantiationProvider.createControlLoops(controlLoopsCreate); + }).hasMessageMatching( + controlLoopsCreate.getControlLoopList().get(0).getKey().asIdentifier() + " already defined"); + + for (ControlLoop controlLoop : controlLoopsCreate.getControlLoopList()) { + instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion()); + } + } + } + + @Test + public void testIssueControlLoopCommand_OrderedStateInvalid() throws ControlLoopRuntimeException, IOException { + try (ControlLoopInstantiationProvider instantiationProvider = + new ControlLoopInstantiationProvider(databaseProviderParameters)) { + assertThatThrownBy(() -> { + instantiationProvider.issueControlLoopCommand(new InstantiationCommand()); + }).hasMessageMatching(ORDERED_STATE_INVALID); + } + } + + @Test + public void testInstantiationVersions() throws Exception { + + // create controlLoops V1 + ControlLoops controlLoopsV1 = + InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V1"); + assertThat(getControlLoopsFromDb(controlLoopsV1).getControlLoopList()).isEmpty(); + + try (ControlLoopInstantiationProvider instantiationProvider = + new ControlLoopInstantiationProvider(databaseProviderParameters)) { + InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV1), + controlLoopsV1); + + // create controlLoops V2 + ControlLoops controlLoopsV2 = + InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V2"); + assertThat(getControlLoopsFromDb(controlLoopsV2).getControlLoopList()).isEmpty(); + InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV2), + controlLoopsV2); + + // GET controlLoops V2 + for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) { + ControlLoops controlLoopsGet = + instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); + assertThat(controlLoopsGet.getControlLoopList()).hasSize(1); + Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0)); + } + + // DELETE controlLoops V1 + for (ControlLoop controlLoop : controlLoopsV1.getControlLoopList()) { + instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion()); + } + + // GET controlLoops V1 is not available + for (ControlLoop controlLoop : controlLoopsV1.getControlLoopList()) { + ControlLoops controlLoopsGet = + instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); + assertThat(controlLoopsGet.getControlLoopList()).isEmpty(); + } + + // GET controlLoops V2 is still available + for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) { + ControlLoops controlLoopsGet = + instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); + assertThat(controlLoopsGet.getControlLoopList()).hasSize(1); + Assert.assertEquals(controlLoop, controlLoopsGet.getControlLoopList().get(0)); + } + + // DELETE controlLoops V2 + for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) { + instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion()); + } + + // GET controlLoops V2 is not available + for (ControlLoop controlLoop : controlLoopsV2.getControlLoopList()) { + ControlLoops controlLoopsGet = + instantiationProvider.getControlLoops(controlLoop.getName(), controlLoop.getVersion()); + assertThat(controlLoopsGet.getControlLoopList()).isEmpty(); + } + } + } +} diff --git a/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/InstantiationUtils.java b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/InstantiationUtils.java new file mode 100644 index 000000000..a10156807 --- /dev/null +++ b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/InstantiationUtils.java @@ -0,0 +1,122 @@ +/*- + * ============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.instantiation; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import org.junit.Assert; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; +import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand; +import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +/** + * Utility methods supporting tests for Instantiation. + */ +public class InstantiationUtils { + + private static final Coder CODER = new StandardCoder(); + + /** + * Gets the ControlLoops from Resource. + * + * @param path path of the resource + * @param suffix suffix to add to all names in ControlLoops + * @return the ControlLoops from Resource + * @throws CoderException if an error occurs + */ + public static ControlLoops getControlLoopsFromResource(final String path, final String suffix) + throws CoderException { + ControlLoops controlLoops = CODER.decode(new File(path), ControlLoops.class); + + // add suffix to all names + controlLoops.getControlLoopList().forEach(controlLoop -> controlLoop.setName(controlLoop.getName() + suffix)); + return controlLoops; + } + + /** + * Gets InstantiationCommand from Resource. + * + * @param path path of the resource + * @param suffix suffix to add to all names in ControlLoops + * @return the InstantiationCommand + * @throws CoderException if an error occurs + */ + public static InstantiationCommand getInstantiationCommandFromResource(final String path, final String suffix) + throws CoderException { + InstantiationCommand instantiationCommand = CODER.decode(new File(path), InstantiationCommand.class); + + // add suffix to all names + instantiationCommand.getControlLoopIdentifierList().forEach(cl -> cl.setName(cl.getName() + suffix)); + return instantiationCommand; + } + + /** + * Assert that Instantiation Response contains proper ControlLoops. + * + * @param response InstantiationResponse + * @param controlLoops ControlLoops + */ + public static void assertInstantiationResponse(InstantiationResponse response, ControlLoops controlLoops) { + assertNotNull(response); + Assert.assertNull(response.getErrorDetails()); + assertEquals(response.getAffectedControlLoops().size(), controlLoops.getControlLoopList().size()); + for (ControlLoop controlLoop : controlLoops.getControlLoopList()) { + assertTrue(response.getAffectedControlLoops().stream() + .filter(ac -> ac.equals(controlLoop.getKey().asIdentifier())).findAny().isPresent()); + } + } + + /** + * Assert that Instantiation Response contains proper ControlLoops. + * + * @param response InstantiationResponse + * @param command InstantiationCommand + */ + public static void assertInstantiationResponse(InstantiationResponse response, InstantiationCommand command) { + assertNotNull(response); + assertEquals(response.getAffectedControlLoops().size(), command.getControlLoopIdentifierList().size()); + for (ToscaConceptIdentifier toscaConceptIdentifier : command.getControlLoopIdentifierList()) { + assertTrue(response.getAffectedControlLoops().stream() + .filter(ac -> ac.compareTo(toscaConceptIdentifier) == 0).findAny().isPresent()); + } + } + + /** + * Assert that Instantiation Response contains ControlLoop equals to controlLoop. + * + * @param response InstantiationResponse + * @param controlLoop ControlLoop + */ + public static void assertInstantiationResponse(InstantiationResponse response, ControlLoop controlLoop) { + assertNotNull(response); + Assert.assertNull(response.getErrorDetails()); + assertEquals(1, response.getAffectedControlLoops().size()); + assertEquals(0, response.getAffectedControlLoops().get(0).compareTo(controlLoop.getKey().asIdentifier())); + } +} diff --git a/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java new file mode 100644 index 000000000..31d4be472 --- /dev/null +++ b/tosca-controlloop/runtime/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java @@ -0,0 +1,63 @@ +/*- + * ============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.util; + +import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; + +/** + * Class to hold/create all parameters for test cases. + * + */ +public class CommonTestData { + private static final Coder coder = new StandardCoder(); + + /** + * Gets the standard Control Loop parameters. + * + * @param port port to be inserted into the parameters + * @param dbName the database name + * @return the standard Control Loop parameters + */ + public static ClRuntimeParameterGroup geParameterGroup(final int port, final String dbName) { + try { + return coder.decode(getParameterGroupAsString(port, dbName), ClRuntimeParameterGroup.class); + + } catch (CoderException e) { + throw new RuntimeException("cannot read Control Loop parameters", e); + } + } + + /** + * Gets the standard Control Loop parameters, as a String. + * + * @param port port to be inserted into the parameters + * @param dbName the database name + * @return the standard Control Loop parameters as string + */ + public static String getParameterGroupAsString(final int port, final String dbName) { + return ResourceUtils.getResourceAsString("src/test/resources/parameters/ConfigParametersStd.json") + .replace("${port}", String.valueOf(port)).replace("${dbName}", "jdbc:h2:mem:" + dbName); + } +} |