d9979e6c50b7f20e56f89cb1335c3c0808fe3c30
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.forwarding.lifecycle.api;
23
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26
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.LifecycleApiSimulatorMain;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43
44 /**
45  * Class to perform unit test of {@link LifecycleApiPolicyForwarder}.
46  *
47  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
48  */
49 public class LifecycleApiPolicyForwarderTest {
50
51     private static final String POLICY = "src/test/resources/parameters/sample_policy.json";
52     private static final String POLICY_ERROR = "src/test/resources/parameters/sample_policy_failure.json";
53     private static final String POLICY_TYPE = "src/test/resources/parameters/sample_policy_type.json";
54     private StandardCoder standardCoder = new StandardCoder();
55     private static LifecycleApiSimulatorMain simulator = new LifecycleApiSimulatorMain();
56
57     /**
58      * Set up.
59      *
60      * @throws CoderException if any error occurs
61      * @throws PolicyForwardingException if any error occurs
62      * @throws InterruptedException if any error occurs
63      */
64     @BeforeClass
65     public static void setUp() throws PolicyForwardingException, CoderException, InterruptedException {
66         final ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters(
67                 "src/test/resources/parameters/LifecycleApiPolicyForwarderParameters.json",
68                 LifecycleApiForwarderParameters.class);
69         ParameterService.register(parameterGroup);
70         simulator.startLifecycycleApiSimulator();
71         if (!NetworkUtil.isTcpPortOpen("0.0.0.0", 6969, 6, 10000L)) {
72             throw new IllegalStateException("cannot connect to port 6969");
73         }
74     }
75
76     /**
77      * Tear down.
78      */
79     @AfterClass
80     public static void tearDown() {
81         ParameterService.deregister(LifecycleApiForwarderParameters.class.getSimpleName());
82         simulator.stopLifecycycleApiSimulator();
83     }
84
85     @Test
86     public void testForwardPolicyUsingSimulator() throws Exception {
87         assertThatCode(() -> {
88             final ToscaServiceTemplate toscaServiceTemplate1 =
89                     standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE), ToscaServiceTemplate.class);
90             final ToscaServiceTemplate toscaServiceTemplate2 =
91                     standardCoder.decode(ResourceUtils.getResourceAsString(POLICY), ToscaServiceTemplate.class);
92
93             final LifecycleApiPolicyForwarder forwarder = new LifecycleApiPolicyForwarder();
94             forwarder.configure(LifecycleApiForwarderParameters.class.getSimpleName());
95
96             final Collection<ToscaEntity> policies = new ArrayList<>();
97             policies.add(toscaServiceTemplate1);
98             policies.add(toscaServiceTemplate2);
99
100             forwarder.forward(policies);
101
102         }).doesNotThrowAnyException();
103     }
104
105     @Test
106     public void testForwardPolicyFailureUsingSimulator() throws Exception {
107
108         final ToscaServiceTemplate toscaServiceTemplate1 =
109                 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE), ToscaServiceTemplate.class);
110         final ToscaServiceTemplate toscaServiceTemplate2 =
111                 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY), ToscaServiceTemplate.class);
112         final ToscaServiceTemplate toscaServiceTemplate3 =
113                 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_ERROR), ToscaServiceTemplate.class);
114         final ToscaEntity unsupportedPolicy = new UnsupportedPolicy();
115
116         final LifecycleApiPolicyForwarder forwarder = new LifecycleApiPolicyForwarder();
117         forwarder.configure(LifecycleApiForwarderParameters.class.getSimpleName());
118
119         final Collection<ToscaEntity> policies = new ArrayList<>();
120         policies.add(toscaServiceTemplate1);
121         policies.add(toscaServiceTemplate2);
122         policies.add(toscaServiceTemplate3);
123         policies.add(unsupportedPolicy);
124
125         assertThatThrownBy(() -> forwarder.forward(policies)).isInstanceOf(PolicyForwardingException.class)
126                 .hasMessageContaining("Failed forwarding the following entities:");
127     }
128
129     class UnsupportedPolicy extends ToscaEntity {
130
131         @Override
132         public String getName() {
133             return "unsupported";
134         }
135     }
136 }