2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
6 * ======================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3;
23 import com.google.gson.Gson;
24 import org.junit.jupiter.api.*;
25 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
26 import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig;
27 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
28 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController;
29 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController;
30 import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ServiceRegistrationInfo;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
33 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
34 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
35 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.web.server.LocalServerPort;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.TestPropertySource;
45 import reactor.core.publisher.Mono;
47 import java.lang.invoke.MethodHandles;
48 import java.time.Duration;
50 @TestMethodOrder(MethodOrderer.MethodName.class)
51 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
52 @ContextConfiguration(classes = TestConfig.class)
53 @TestPropertySource(properties = { //
54 "server.ssl.key-store=./config/keystore.jks", //
55 "app.webclient.trust-store=./config/truststore.jks", //
56 "app.webclient.trust-store-used=true", //
57 "app.vardata-directory=/tmp/pmstestv3", //a
59 "app.s3.bucket=" // If this is set, S3 will be used to store data.
61 class ServiceControllerV3Test {
63 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
66 private TestHelper testHelper;
69 private ApplicationConfig applicationConfig;
72 private Services services;
75 private MockA1ClientFactory a1ClientFactory;
78 private RappSimulatorController rAppSimulator;
81 private SecurityContext securityContext;
84 private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
87 private PolicyTypes policyTypes;
97 testHelper.port = port;
98 this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
104 a1ClientFactory.reset();
105 this.rAppSimulator.getTestResults().clear();
106 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
107 this.securityContext.setAuthTokenFilePath(null);
108 this.openPolicyAgentSimulatorController.getTestResults().reset();
112 void testPutService() {
113 ServiceRegistrationInfo serviceRegistrationInfo = new ServiceRegistrationInfo("serviceId");
114 serviceRegistrationInfo.callbackUrl("http://callback.com/").keepAliveIntervalSeconds(10L);
115 Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3()
116 .putForEntity("/services", gson.toJson(serviceRegistrationInfo));
117 testHelper.testSuccessResponse(responseEntityMono, HttpStatus.CREATED, responseBody -> services.size() == 1);
121 void testGetService() {
122 services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/"));
123 Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3().getForEntity("/services");
124 testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBoy -> responseBoy
125 .contains("http://callback.com/"));
129 void testDeleteService() {
130 services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/"));
131 Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3().deleteForEntity("/services/newServiceId");
132 testHelper.testSuccessResponse(responseEntityMono, HttpStatus.NO_CONTENT, responseBody -> services.size() == 0);
136 void testKeepAliveService() {
137 services.put(new Service("newServiceId", Duration.ofSeconds(10L), "http://callback.com/"));
138 Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3().putForEntity("/services/newServiceId/keepalive", "");
139 testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> services.size() == 1);