0c4eabc219f6b039e13c6522b79164f372b0b7e5
[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.a1pms.webclient;
22
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;
28
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;
40
41 @ExtendWith(SpringExtension.class)
42 class AcA1PmsClientTest {
43
44     private static int mockServerPort;
45
46     private static MockServerRest mockServer;
47
48     private static CommonTestData commonTestData;
49
50     @Mock
51     private A1PmsParameters a1PmsParameters;
52
53
54     private final String healthyUrl = "/healthy";
55
56     private final String servicesUrl = "/services/success";
57
58     private final String serviceUrl = "/service/success/{serviceId}";
59
60     /**
61      * Set up Mock server.
62      */
63     @BeforeAll
64     static void setUpMockServer() throws IOException, InterruptedException {
65         mockServerPort = NetworkUtil.allocPort();
66         mockServer = new MockServerRest(mockServerPort);
67         mockServer.validate();
68         commonTestData = new CommonTestData();
69     }
70
71     @AfterAll
72     static void stopServer() throws Exception {
73         mockServer.close();
74         mockServer = null;
75     }
76
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);
83     }
84
85     void initializePmsServicesParameters(String servicesUrl) {
86         initializePmsHealthParameters(healthyUrl);
87         var endpoints = a1PmsParameters.getEndpoints();
88         endpoints.setServices(servicesUrl);
89         when(a1PmsParameters.getEndpoints()).thenReturn(endpoints);
90     }
91
92     void initializePmsServiceParameters(String serviceUrl) {
93         initializePmsServicesParameters(servicesUrl);
94         var endpoints = a1PmsParameters.getEndpoints();
95         endpoints.setService(serviceUrl);
96         when(a1PmsParameters.getEndpoints()).thenReturn(endpoints);
97     }
98
99
100     @Test
101     void test_healthyPms() {
102         initializePmsHealthParameters(healthyUrl);
103         var client = new AcA1PmsClient(a1PmsParameters);
104         assertTrue(client.isPmsHealthy());
105     }
106
107     @Test
108     void test_unhealthyPms() {
109         String unhealthyUrl = "/unhealthy";
110         initializePmsHealthParameters(unhealthyUrl);
111         var client = new AcA1PmsClient(a1PmsParameters);
112         assertFalse(client.isPmsHealthy());
113     }
114
115     @Test
116     void test_createServicesSuccess() {
117         initializePmsServicesParameters(servicesUrl);
118         var client = new AcA1PmsClient(a1PmsParameters);
119         assertDoesNotThrow(() -> client.createService(commonTestData.getValidPolicyEntities()));
120     }
121
122     @Test
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);
130     }
131
132     @Test
133     void test_deleteServicesSuccess() {
134         initializePmsServiceParameters(serviceUrl);
135         var client = new AcA1PmsClient(a1PmsParameters);
136         assertDoesNotThrow(() -> client.deleteService(commonTestData.getValidPolicyEntities()));
137     }
138
139     @Test
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);
147     }
148 }