9f1343e67f6cb9c52cb5110734b4e4a3a1930835
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.clamp.acm.participant.policy.client;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26
27 import java.io.IOException;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.clamp.acm.participant.policy.main.parameters.ParticipantPolicyParameters;
34 import org.onap.policy.clamp.acm.participant.policy.main.utils.MockServer;
35 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
36 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
37 import org.onap.policy.common.utils.network.NetworkUtil;
38 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40
41 /**
42  * Tests for api and pap http clients.
43  */
44 public class HttpClientTest {
45
46     private static int mockServerPort;
47
48     private static PolicyApiHttpClient apiHttpClient;
49
50     private static PolicyPapHttpClient papHttpClient;
51
52     private static MockServer mockServer;
53
54     /**
55      * Set up Mock server.
56      */
57     @BeforeAll
58     static void setUpMockServer() throws IOException, InterruptedException {
59         mockServerPort = NetworkUtil.allocPort();
60         mockServer = new MockServer(mockServerPort);
61         mockServer.validate();
62         // Setup mock api and pap client
63         ParticipantPolicyParameters params = new ParticipantPolicyParameters();
64         RestClientParameters restClientParameters = getMockClientParameters(mockServerPort);
65         params.setPolicyApiParameters(restClientParameters);
66         params.setPolicyPapParameters(restClientParameters);
67
68         apiHttpClient = new PolicyApiHttpClient(params);
69         papHttpClient = new PolicyPapHttpClient(params);
70     }
71
72
73     @Test
74     void testCreatePolicy() {
75         assertDoesNotThrow(() -> apiHttpClient.createPolicy(getTestToscaServiceTemplate()));
76     }
77
78     @Test
79     void testCreatePolicyTypes() {
80         assertDoesNotThrow(() -> apiHttpClient.createPolicyType(getTestToscaServiceTemplate()));
81     }
82
83     @Test
84     void testDeletePolicy() {
85         assertDoesNotThrow(() -> apiHttpClient.deletePolicy("dummyPolicy", "1.0.0"));
86     }
87
88     @Test
89     void testDeletePolicyType() {
90         assertDoesNotThrow(() -> apiHttpClient.deletePolicyType("dummyPolicy", "1.0.0"));
91     }
92
93     @Test
94     void testDeployPolicy() {
95         assertDoesNotThrow(() -> papHttpClient.handlePolicyDeployOrUndeploy("dummyPolicy", "1.0.0",
96                 DeploymentSubGroup.Action.POST));
97     }
98
99     @Test
100     void testUnDeployPolicy() {
101         assertDoesNotThrow(() -> papHttpClient.handlePolicyDeployOrUndeploy("dummyPolicy", "1.0.0",
102                 DeploymentSubGroup.Action.DELETE));
103     }
104
105     @Test
106     void testInvalidEndpoint() {
107         Response response = apiHttpClient.executePost("/invalid", Entity.entity(getTestToscaServiceTemplate(),
108                 MediaType.APPLICATION_JSON));
109         assertThat(response.getStatus()).isEqualTo(404);
110     }
111
112     @Test
113     void testInvalidClientParameter() {
114         assertThrows(AutomationCompositionRuntimeException.class,
115                () -> new PolicyApiHttpClient(new ParticipantPolicyParameters()));
116     }
117
118
119     private ToscaServiceTemplate getTestToscaServiceTemplate() {
120         return new ToscaServiceTemplate();
121     }
122
123     private static RestClientParameters getMockClientParameters(int port) {
124         RestClientParameters params = new RestClientParameters();
125         params.setName("policyClient");
126         params.setHostname("localhost");
127         params.setPort(port);
128         params.setUseHttps(false);
129         return params;
130     }
131
132
133
134 }