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.a1pms.webclient;
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.Mockito.when;
29 import java.io.IOException;
30 import org.junit.jupiter.api.AfterAll;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.onap.policy.clamp.acm.participant.a1pms.parameters.A1PmsParameters;
36 import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData;
37 import org.onap.policy.clamp.acm.participant.a1pms.utils.MockServerRest;
38 import org.onap.policy.common.utils.network.NetworkUtil;
39 import org.springframework.test.context.junit.jupiter.SpringExtension;
41 @ExtendWith(SpringExtension.class)
42 class AcA1PmsClientTest {
44 private static int mockServerPort;
46 private static MockServerRest mockServer;
48 private static CommonTestData commonTestData;
51 private A1PmsParameters a1PmsParameters;
54 private final String healthyUrl = "/healthy";
56 private final String servicesUrl = "/services/success";
58 private final String serviceUrl = "/service/success/{serviceId}";
64 static void setUpMockServer() throws IOException, InterruptedException {
65 mockServerPort = NetworkUtil.allocPort();
66 mockServer = new MockServerRest(mockServerPort);
67 mockServer.validate();
68 commonTestData = new CommonTestData();
72 static void stopServer() throws Exception {
77 void initializePmsHealthParameters(String healthUrl) {
78 String testMockUrl = "http://localhost";
79 when(a1PmsParameters.getBaseUrl()).thenReturn(testMockUrl + ":" + mockServerPort);
80 var endpoints = new A1PmsParameters.Endpoints();
81 endpoints.setHealth(healthUrl);
82 when(a1PmsParameters.getEndpoints()).thenReturn(endpoints);
85 void initializePmsServicesParameters(String servicesUrl) {
86 initializePmsHealthParameters(healthyUrl);
87 var endpoints = a1PmsParameters.getEndpoints();
88 endpoints.setServices(servicesUrl);
89 when(a1PmsParameters.getEndpoints()).thenReturn(endpoints);
92 void initializePmsServiceParameters(String serviceUrl) {
93 initializePmsServicesParameters(servicesUrl);
94 var endpoints = a1PmsParameters.getEndpoints();
95 endpoints.setService(serviceUrl);
96 when(a1PmsParameters.getEndpoints()).thenReturn(endpoints);
101 void test_healthyPms() {
102 initializePmsHealthParameters(healthyUrl);
103 var client = new AcA1PmsClient(a1PmsParameters);
104 assertTrue(client.isPmsHealthy());
108 void test_unhealthyPms() {
109 String unhealthyUrl = "/unhealthy";
110 initializePmsHealthParameters(unhealthyUrl);
111 var client = new AcA1PmsClient(a1PmsParameters);
112 assertFalse(client.isPmsHealthy());
116 void test_createServicesSuccess() {
117 initializePmsServicesParameters(servicesUrl);
118 var client = new AcA1PmsClient(a1PmsParameters);
119 assertDoesNotThrow(() -> client.createService(commonTestData.getValidPolicyEntities()));
123 void test_createServicesFailure() {
124 String createServiceFailureUrl = "services/failure";
125 initializePmsServicesParameters(createServiceFailureUrl);
126 var client = new AcA1PmsClient(a1PmsParameters);
127 String expectedMessage = "Error in creating policy service";
128 assertThrows(Exception.class,
129 () -> client.createService(commonTestData.getValidPolicyEntities()), expectedMessage);
133 void test_deleteServicesSuccess() {
134 initializePmsServiceParameters(serviceUrl);
135 var client = new AcA1PmsClient(a1PmsParameters);
136 assertDoesNotThrow(() -> client.deleteService(commonTestData.getValidPolicyEntities()));
140 void test_deleteServicesFailure() {
141 String deleteServiceFailureUrl = "services/failure/{serviceId}";
142 initializePmsServiceParameters(deleteServiceFailureUrl);
143 var client = new AcA1PmsClient(a1PmsParameters);
144 String expectedMessage = "Error in deleting policy service";
145 assertThrows(Exception.class,
146 () -> client.deleteService(commonTestData.getValidPolicyEntities()), expectedMessage);