0c9a8f6e5147f4c1d98a89c6cbd661edd2da82c7
[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.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;
42
43 /**
44  * Class to perform unit test of {@link LifecycleApiAutomationCompositionForwarder}.
45  *
46  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
47  */
48 public class LifecycleApiAutomationCompositionForwarderTest {
49
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();
55
56     /**
57      * Set up.
58      *
59      * @throws CoderException if any error occurs
60      * @throws PolicyForwardingException if any error occurs
61      * @throws InterruptedException if any error occurs
62      */
63     @BeforeClass
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");
72         }
73     }
74
75     /**
76      * Tear down.
77      */
78     @AfterClass
79     public static void tearDown() {
80         ParameterService.deregister(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
81         simulator.stopLifecycycleApiSimulator();
82     }
83
84     @Test
85     public void testForwardAutomationCompositionUsingSimulator() throws Exception {
86         assertThatCode(() -> {
87             final ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(
88                 ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION), ToscaServiceTemplate.class);
89
90             final LifecycleApiAutomationCompositionForwarder forwarder =
91                     new LifecycleApiAutomationCompositionForwarder();
92             forwarder.configure(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
93
94             final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
95             automationCompositionList.add(toscaServiceTemplate);
96
97             forwarder.forward(automationCompositionList);
98
99         }).doesNotThrowAnyException();
100     }
101
102     @Test
103     public void testForwardAutomationCompositionFailureUsingSimulator() throws Exception {
104
105         final ToscaEntity toscaEntity = new ToscaEntity();
106         toscaEntity.setName("FailureCase");
107
108         final LifecycleApiAutomationCompositionForwarder forwarder = new LifecycleApiAutomationCompositionForwarder();
109         forwarder.configure(LifecycleApiAutomationCompositionForwarderParameters.class.getSimpleName());
110
111         final Collection<ToscaEntity> automationCompositionList = new ArrayList<>();
112         automationCompositionList.add(toscaEntity);
113
114         assertThatThrownBy(() -> forwarder.forward(automationCompositionList))
115                 .isInstanceOf(PolicyForwardingException.class)
116                 .hasMessageContaining("Failed forwarding the following entities:");
117     }
118 }