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