2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2022 Nordix Foundation.
 
   4  *  Modifications Copyright (C) 2022 Nordix Foundation.
 
   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.AfterClass;
 
  30 import org.junit.BeforeClass;
 
  31 import org.junit.Test;
 
  32 import org.onap.policy.common.parameters.ParameterGroup;
 
  33 import org.onap.policy.common.parameters.ParameterService;
 
  34 import org.onap.policy.common.utils.coder.CoderException;
 
  35 import org.onap.policy.common.utils.coder.StandardCoder;
 
  36 import org.onap.policy.common.utils.network.NetworkUtil;
 
  37 import org.onap.policy.common.utils.resources.ResourceUtils;
 
  38 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
 
  39 import org.onap.policy.distribution.forwarding.testclasses.CommonTestData;
 
  40 import org.onap.policy.distribution.forwarding.testclasses.LifecycleApiControlLoopSimulatorMain;
 
  41 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  45  * Class to perform unit test of {@link LifecycleApiControlLoopForwarder}.
 
  47  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
 
  49 public class LifecycleApiControlLoopForwarderTest {
 
  51     private static final String CONTROL_LOOP = "src/test/resources/parameters/sample_control_loop.json";
 
  52     private final StandardCoder standardCoder = new StandardCoder();
 
  53     private static final LifecycleApiControlLoopSimulatorMain simulator = new LifecycleApiControlLoopSimulatorMain();
 
  58      * @throws CoderException if any error occurs
 
  59      * @throws PolicyForwardingException if any error occurs
 
  60      * @throws InterruptedException if any error occurs
 
  63     public static void setUp() throws PolicyForwardingException, CoderException, InterruptedException {
 
  64         final ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters(
 
  65                 "src/test/resources/parameters/LifecycleApiControlLoopForwarderParameters.json",
 
  66                 LifecycleApiControlLoopForwarderParameters.class);
 
  67         ParameterService.register(parameterGroup);
 
  68         simulator.startLifecycycleApiSimulator();
 
  69         if (!NetworkUtil.isTcpPortOpen("0.0.0.0", 6969, 50, 200L)) {
 
  70             throw new IllegalStateException("cannot connect to port 6969");
 
  78     public static void tearDown() {
 
  79         ParameterService.deregister(LifecycleApiControlLoopForwarderParameters.class.getSimpleName());
 
  80         simulator.stopLifecycycleApiSimulator();
 
  84     public void testForwardControlLoopUsingSimulator() throws Exception {
 
  85         assertThatCode(() -> {
 
  86             final ToscaServiceTemplate toscaServiceTemplate =
 
  87                     standardCoder.decode(ResourceUtils.getResourceAsString(CONTROL_LOOP), ToscaServiceTemplate.class);
 
  89             final LifecycleApiControlLoopForwarder forwarder = new LifecycleApiControlLoopForwarder();
 
  90             forwarder.configure(LifecycleApiControlLoopForwarderParameters.class.getSimpleName());
 
  92             final Collection<ToscaEntity> controlLoopList = new ArrayList<>();
 
  93             controlLoopList.add(toscaServiceTemplate);
 
  95             forwarder.forward(controlLoopList);
 
  97         }).doesNotThrowAnyException();
 
 101     public void testForwardControlLoopFailureUsingSimulator() throws Exception {
 
 103         final ToscaEntity toscaEntity = new ToscaEntity();
 
 104         toscaEntity.setName("FailureCase");
 
 106         final LifecycleApiControlLoopForwarder forwarder = new LifecycleApiControlLoopForwarder();
 
 107         forwarder.configure(LifecycleApiControlLoopForwarderParameters.class.getSimpleName());
 
 109         final Collection<ToscaEntity> controlLoopList = new ArrayList<>();
 
 110         controlLoopList.add(toscaEntity);
 
 112         assertThatThrownBy(() -> forwarder.forward(controlLoopList)).isInstanceOf(PolicyForwardingException.class)
 
 113                 .hasMessageContaining("Failed forwarding the following entities:");