4d837cdf5f8bda9900284122303c4548d0d5f23d
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.assertThatThrownBy;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27
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.xacml.pdp.testclasses.CommonTestData;
39 import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.LifecycleApiSimulatorMain;
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 LifecycleApiPolicyForwarder}.
45  *
46  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
47  */
48 public class LifecycleApiPolicyForwarderTest {
49
50     private static final String POLICY = "src/test/resources/parameters/sample_policy.json";
51     private static final String POLICY_ERROR = "src/test/resources/parameters/sample_policy_failure.json";
52     private static final String POLICY_TYPE = "src/test/resources/parameters/sample_policy_type.json";
53     private StandardCoder standardCoder = new StandardCoder();
54     private static LifecycleApiSimulatorMain simulator = new LifecycleApiSimulatorMain();
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/LifecycleApiPolicyForwarderParameters.json",
67                 LifecycleApiForwarderParameters.class);
68         ParameterService.register(parameterGroup);
69         simulator.startLifecycycleApiSimulator();
70         if (!NetworkUtil.isTcpPortOpen("0.0.0.0", 6969, 6, 10000L)) {
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(LifecycleApiForwarderParameters.class.getSimpleName());
81         simulator.stopLifecycycleApiSimulator();
82     }
83
84     @Test
85     public void testForwardPolicyUsingSimulator() throws Exception {
86
87         final ToscaServiceTemplate toscaServiceTemplate1 =
88                 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE), ToscaServiceTemplate.class);
89         final ToscaServiceTemplate toscaServiceTemplate2 =
90                 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY), ToscaServiceTemplate.class);
91
92         final LifecycleApiPolicyForwarder forwarder = new LifecycleApiPolicyForwarder();
93         forwarder.configure(LifecycleApiForwarderParameters.class.getSimpleName());
94
95         final Collection<ToscaEntity> policies = new ArrayList<>();
96         policies.add(toscaServiceTemplate1);
97         policies.add(toscaServiceTemplate2);
98
99         forwarder.forward(policies);
100     }
101
102     @Test
103     public void testForwardPolicyFailureUsingSimulator() throws Exception {
104
105         final ToscaServiceTemplate toscaServiceTemplate1 =
106                 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE), ToscaServiceTemplate.class);
107         final ToscaServiceTemplate toscaServiceTemplate2 =
108                 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY), ToscaServiceTemplate.class);
109         final ToscaServiceTemplate toscaServiceTemplate3 =
110                 standardCoder.decode(ResourceUtils.getResourceAsString(POLICY_ERROR), ToscaServiceTemplate.class);
111         final ToscaEntity unsupportedPolicy = new UnsupportedPolicy();
112
113         final LifecycleApiPolicyForwarder forwarder = new LifecycleApiPolicyForwarder();
114         forwarder.configure(LifecycleApiForwarderParameters.class.getSimpleName());
115
116         final Collection<ToscaEntity> policies = new ArrayList<>();
117         policies.add(toscaServiceTemplate1);
118         policies.add(toscaServiceTemplate2);
119         policies.add(toscaServiceTemplate3);
120         policies.add(unsupportedPolicy);
121
122         assertThatThrownBy(() -> forwarder.forward(policies)).isInstanceOf(PolicyForwardingException.class)
123                 .hasMessageContaining("Failed forwarding the following entities:");
124     }
125
126     class UnsupportedPolicy extends ToscaEntity {
127
128         @Override
129         public String getName() {
130             return "unsupported";
131         }
132     }
133 }