2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.forwarding.lifecycle.api;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import org.junit.jupiter.api.AfterAll;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31 import org.onap.policy.common.parameters.ParameterService;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.network.NetworkUtil;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
37 import org.onap.policy.distribution.forwarding.testclasses.CommonTestData;
38 import org.onap.policy.distribution.forwarding.testclasses.LifecycleApiAutomationCompositionSimulatorMain;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43 * Class to perform unit test of {@link LifecycleApiAutomationCompositionForwarder}.
45 * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
47 class LifecycleApiAutomationCompositionForwarderTest {
49 private static final String AUTOMATION_COMPOSITION =
50 "src/test/resources/parameters/sample_automation_composition.json";
51 private final StandardCoder standardCoder = new StandardCoder();
52 private static final LifecycleApiAutomationCompositionSimulatorMain simulator =
53 new LifecycleApiAutomationCompositionSimulatorMain();
58 * @throws CoderException if any error occurs
59 * @throws PolicyForwardingException if any error occurs
60 * @throws InterruptedException if any error occurs
63 static void setUp() throws PolicyForwardingException, CoderException, InterruptedException {
64 final var parameterGroup = CommonTestData.getPolicyForwarderParameters(
65 "src/test/resources/parameters/LifecycleApiAutomationCompositionForwarderParameters.json",
66 LifecycleApiAutomationCompositionForwarderParameters.class);
67 ParameterService.register(parameterGroup);
68 simulator.startLifecycycleApiSimulator();
69 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 50, 200L)) {
70 throw new IllegalStateException("cannot connect to port 6969");
78 static void tearDown() {
79 ParameterService.deregister(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
80 simulator.stopLifecycycleApiSimulator();
84 void testForwardAutomationCompositionUsingSimulator() throws Exception {
85 assertThatCode(() -> {
86 final var toscaServiceTemplate = standardCoder.decode(
87 ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION), ToscaServiceTemplate.class);
90 new LifecycleApiAutomationCompositionForwarder();
91 forwarder.configure(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
93 final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
94 automationCompositionList.add(toscaServiceTemplate);
96 forwarder.forward(automationCompositionList);
98 }).doesNotThrowAnyException();
102 void testForwardAutomationCompositionFailureUsingSimulator() throws Exception {
104 final var toscaEntity = new ToscaEntity();
105 toscaEntity.setName("FailureCase");
107 final var forwarder = new LifecycleApiAutomationCompositionForwarder();
108 forwarder.configure(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
110 final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
111 automationCompositionList.add(toscaEntity);
113 assertThatThrownBy(() -> forwarder.forward(automationCompositionList))
114 .isInstanceOf(PolicyForwardingException.class)
115 .hasMessageContaining("Failed forwarding the following entities:");