f28b52e5172081069e89511cae43569de3394bfd
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.forwarding.lifecycle.api;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25
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;
41
42 /**
43  * Class to perform unit test of {@link LifecycleApiAutomationCompositionForwarder}.
44  *
45  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
46  */
47 class LifecycleApiAutomationCompositionForwarderTest {
48
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();
54
55     /**
56      * Set up.
57      *
58      * @throws CoderException if any error occurs
59      * @throws PolicyForwardingException if any error occurs
60      * @throws InterruptedException if any error occurs
61      */
62     @BeforeAll
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");
71         }
72     }
73
74     /**
75      * Tear down.
76      */
77     @AfterAll
78     static void tearDown() {
79         ParameterService.deregister(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
80         simulator.stopLifecycycleApiSimulator();
81     }
82
83     @Test
84     void testForwardAutomationCompositionUsingSimulator() throws Exception {
85         assertThatCode(() -> {
86             final var toscaServiceTemplate = standardCoder.decode(
87                 ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION), ToscaServiceTemplate.class);
88
89             final var forwarder =
90                     new LifecycleApiAutomationCompositionForwarder();
91             forwarder.configure(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
92
93             final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
94             automationCompositionList.add(toscaServiceTemplate);
95
96             forwarder.forward(automationCompositionList);
97
98         }).doesNotThrowAnyException();
99     }
100
101     @Test
102     void testForwardAutomationCompositionFailureUsingSimulator() throws Exception {
103
104         final var toscaEntity = new ToscaEntity();
105         toscaEntity.setName("FailureCase");
106
107         final var forwarder = new LifecycleApiAutomationCompositionForwarder();
108         forwarder.configure(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
109
110         final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
111         automationCompositionList.add(toscaEntity);
112
113         assertThatThrownBy(() -> forwarder.forward(automationCompositionList))
114                 .isInstanceOf(PolicyForwardingException.class)
115                 .hasMessageContaining("Failed forwarding the following entities:");
116     }
117 }