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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.policy.client;
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;
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;
42 * Tests for api and pap http clients.
44 public class HttpClientTest {
46 private static int mockServerPort;
48 private static PolicyApiHttpClient apiHttpClient;
50 private static PolicyPapHttpClient papHttpClient;
52 private static MockServer mockServer;
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);
68 apiHttpClient = new PolicyApiHttpClient(params);
69 papHttpClient = new PolicyPapHttpClient(params);
74 void testCreatePolicy() {
75 assertDoesNotThrow(() -> apiHttpClient.createPolicy(getTestToscaServiceTemplate()));
79 void testCreatePolicyTypes() {
80 assertDoesNotThrow(() -> apiHttpClient.createPolicyType(getTestToscaServiceTemplate()));
84 void testDeletePolicy() {
85 assertDoesNotThrow(() -> apiHttpClient.deletePolicy("dummyPolicy", "1.0.0"));
89 void testDeletePolicyType() {
90 assertDoesNotThrow(() -> apiHttpClient.deletePolicyType("dummyPolicy", "1.0.0"));
94 void testDeployPolicy() {
95 assertDoesNotThrow(() -> papHttpClient.handlePolicyDeployOrUndeploy("dummyPolicy", "1.0.0",
96 DeploymentSubGroup.Action.POST));
100 void testUnDeployPolicy() {
101 assertDoesNotThrow(() -> papHttpClient.handlePolicyDeployOrUndeploy("dummyPolicy", "1.0.0",
102 DeploymentSubGroup.Action.DELETE));
106 void testInvalidEndpoint() {
107 Response response = apiHttpClient.executePost("/invalid", Entity.entity(getTestToscaServiceTemplate(),
108 MediaType.APPLICATION_JSON));
109 assertThat(response.getStatus()).isEqualTo(404);
113 void testInvalidClientParameter() {
114 assertThrows(AutomationCompositionRuntimeException.class,
115 () -> new PolicyApiHttpClient(new ParticipantPolicyParameters()));
119 private ToscaServiceTemplate getTestToscaServiceTemplate() {
120 return new ToscaServiceTemplate();
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);