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.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.onap.policy.common.parameters.ParameterGroup;
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.network.NetworkUtil;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
38 import org.onap.policy.distribution.forwarding.testclasses.CommonTestData;
39 import org.onap.policy.distribution.forwarding.testclasses.LifecycleApiAutomationCompositionSimulatorMain;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 * Class to perform unit test of {@link LifecycleApiAutomationCompositionForwarder}.
46 * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
48 public class LifecycleApiAutomationCompositionForwarderTest {
50 private static final String AUTOMATION_COMPOSITION =
51 "src/test/resources/parameters/sample_automation_composition.json";
52 private final StandardCoder standardCoder = new StandardCoder();
53 private static final LifecycleApiAutomationCompositionSimulatorMain simulator =
54 new LifecycleApiAutomationCompositionSimulatorMain();
59 * @throws CoderException if any error occurs
60 * @throws PolicyForwardingException if any error occurs
61 * @throws InterruptedException if any error occurs
64 public static void setUp() throws PolicyForwardingException, CoderException, InterruptedException {
65 final ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters(
66 "src/test/resources/parameters/LifecycleApiAutomationCompositionForwarderParameters.json",
67 LifecycleApiAutomationCompositionForwarderParameters.class);
68 ParameterService.register(parameterGroup);
69 simulator.startLifecycycleApiSimulator();
70 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 50, 200L)) {
71 throw new IllegalStateException("cannot connect to port 6969");
79 public static void tearDown() {
80 ParameterService.deregister(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
81 simulator.stopLifecycycleApiSimulator();
85 public void testForwardAutomationCompositionUsingSimulator() throws Exception {
86 assertThatCode(() -> {
87 final ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
88 ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION), ToscaServiceTemplate.class);
90 final LifecycleApiAutomationCompositionForwarder forwarder =
91 new LifecycleApiAutomationCompositionForwarder();
92 forwarder.configure(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
94 final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
95 automationCompositionList.add(toscaServiceTemplate);
97 forwarder.forward(automationCompositionList);
99 }).doesNotThrowAnyException();
103 public void testForwardAutomationCompositionFailureUsingSimulator() throws Exception {
105 final ToscaEntity toscaEntity = new ToscaEntity();
106 toscaEntity.setName("FailureCase");
108 final LifecycleApiAutomationCompositionForwarder forwarder = new LifecycleApiAutomationCompositionForwarder();
109 forwarder.configure(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
111 final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
112 automationCompositionList.add(toscaEntity);
114 assertThatThrownBy(() -> forwarder.forward(automationCompositionList))
115 .isInstanceOf(PolicyForwardingException.class)
116 .hasMessageContaining("Failed forwarding the following entities:");