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 org.junit.jupiter.api.*;
24 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
25 import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig;
26 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
27 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController;
28 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
33 import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
34 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper;
35 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
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.mock.mockito.SpyBean;
41 import org.springframework.boot.test.web.server.LocalServerPort;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.TestPropertySource;
46 import org.springframework.util.FileSystemUtils;
47 import reactor.core.publisher.Mono;
49 import java.lang.invoke.MethodHandles;
50 import java.nio.file.Path;
52 import static org.mockito.ArgumentMatchers.any;
53 import static org.mockito.Mockito.when;
55 @TestMethodOrder(MethodOrderer.MethodName.class)
56 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
57 @ContextConfiguration(classes = TestConfig.class)
58 @TestPropertySource(properties = { //
59 "server.ssl.key-store=./config/keystore.jks", //
60 "app.webclient.trust-store=./config/truststore.jks", //
61 "app.webclient.trust-store-used=true", //
62 "app.vardata-directory=/tmp/pmstestv3", //
64 "app.s3.bucket=" // If this is set, S3 will be used to store data.
66 public class PolicyControllerTest {
67 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
70 private ApplicationConfig applicationConfig;
73 private TestHelper testHelper;
79 private Policies policies;
82 private PolicyTypes policyTypes;
85 private Services services;
88 private MockA1ClientFactory a1ClientFactory;
91 private RappSimulatorController rAppSimulator;
94 private SecurityContext securityContext;
97 private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
103 private Helper helper;
107 testHelper.port = port;
108 this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
117 a1ClientFactory.reset();
118 this.rAppSimulator.getTestResults().clear();
119 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
120 this.securityContext.setAuthTokenFilePath(null);
121 this.openPolicyAgentSimulatorController.getTestResults().reset();
125 static void clearTestDir() {
127 FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3"));
128 } catch (Exception e) {
129 logger.warn("Could test directory : {}", e.getMessage());
134 @DisplayName("test Create Policy")
135 void testPostPolicy() throws Exception {
136 String nonRtRicId = "ric.1";
137 String policyTypeName = "type1_1.2.3";
138 String url = "/policies";
139 testHelper.addPolicyType(policyTypeName, nonRtRicId);
140 String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName);
141 Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody);
142 testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, "{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}");
146 @DisplayName("test Create Policy schema validation fail case")
147 void testPolicySchemaValidationFail() throws Exception {
148 String nonRtRicId = "ric.1";
149 String policyTypeName = "type1_1.2.3";
150 String url = "/policies";
151 testHelper.addPolicyType(policyTypeName, nonRtRicId);
152 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE);
153 String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName);
154 Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody);
155 testHelper.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed");
159 @DisplayName("test Create Policy No Ric fail case")
160 void testCreatePolicyNoRic() throws Exception {
161 String policyTypeName = "type1_1.2.3";
162 String url = "/policies";
163 testHelper.addPolicyType(policyTypeName, " ");
164 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
165 String policyBody = testHelper.postPolicyBody("noRic", policyTypeName);
166 Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody);
167 testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic");
171 @DisplayName("test Create Policy with No Policy Type fail case")
172 void testCreatePolicyNoPolicyType() throws Exception {
173 String policyTypeName = "type1_1.2.3";
174 String nonRtRicId = "ricOne";
175 String url = "/policies";
176 testHelper.addPolicyType(policyTypeName, nonRtRicId);
177 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
178 String policyBody = testHelper.postPolicyBody(nonRtRicId, "noPolicyType");
179 Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody);
180 testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType");