2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021,2023 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T Inc.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.distribution.forwarding.lifecycle.api;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import org.junit.jupiter.api.AfterAll;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
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.LifecycleApiSimulatorMain;
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 LifecycleApiPolicyForwarder}.
46 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
48 class LifecycleApiPolicyForwarderTest {
50 private static final String POLICY = "src/test/resources/parameters/sample_policy.json";
51 private static final String POLICY_ERROR = "src/test/resources/parameters/sample_policy_failure.json";
52 private static final String POLICY_TYPE = "src/test/resources/parameters/sample_policy_type.json";
53 private final StandardCoder standardCoder = new StandardCoder();
54 private static final LifecycleApiSimulatorMain simulator = new LifecycleApiSimulatorMain();
59 * @throws CoderException if any error occurs
60 * @throws PolicyForwardingException if any error occurs
61 * @throws InterruptedException if any error occurs
64 static void setUp() throws PolicyForwardingException, CoderException, InterruptedException {
65 final var parameterGroup = CommonTestData.getPolicyForwarderParameters(
66 "src/test/resources/parameters/LifecycleApiPolicyForwarderParameters.json",
67 LifecycleApiForwarderParameters.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 static void tearDown() {
80 ParameterService.deregister(LifecycleApiForwarderParameters.class.getSimpleName());
81 simulator.stopLifecycycleApiSimulator();
85 void testForwardPolicyUsingSimulator() throws Exception {
86 assertThatCode(() -> {
87 final var toscaServiceTemplate1 =
88 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE), ToscaServiceTemplate.class);
89 final var toscaServiceTemplate2 =
90 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY), ToscaServiceTemplate.class);
92 final var forwarder = new LifecycleApiPolicyForwarder();
93 forwarder.configure(LifecycleApiForwarderParameters.class.getSimpleName());
95 final Collection<ToscaEntity> policies = new ArrayList<>();
96 policies.add(toscaServiceTemplate1);
97 policies.add(toscaServiceTemplate2);
99 forwarder.forward(policies);
101 }).doesNotThrowAnyException();
105 void testForwardPolicyFailureUsingSimulator() throws Exception {
107 final var toscaServiceTemplate1 =
108 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE), ToscaServiceTemplate.class);
109 final var toscaServiceTemplate2 =
110 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY), ToscaServiceTemplate.class);
111 final var toscaServiceTemplate3 =
112 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_ERROR), ToscaServiceTemplate.class);
113 final var unsupportedPolicy = new UnsupportedPolicy();
115 final var forwarder = new LifecycleApiPolicyForwarder();
116 forwarder.configure(LifecycleApiForwarderParameters.class.getSimpleName());
118 final Collection<ToscaEntity> policies = new ArrayList<>();
119 policies.add(toscaServiceTemplate1);
120 policies.add(toscaServiceTemplate2);
121 policies.add(toscaServiceTemplate3);
122 policies.add(unsupportedPolicy);
124 assertThatThrownBy(() -> forwarder.forward(policies)).isInstanceOf(PolicyForwardingException.class)
125 .hasMessageContaining("Failed forwarding the following entities:");
128 static class UnsupportedPolicy extends ToscaEntity {
131 public String getName() {
132 return "unsupported";