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.mockito.Mockito;
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.repository.Policies;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
33 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
34 import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
35 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
36 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.boot.test.mock.mockito.SpyBean;
42 import org.springframework.boot.test.web.server.LocalServerPort;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.TestPropertySource;
47 import org.springframework.util.FileSystemUtils;
48 import reactor.core.publisher.Mono;
50 import java.lang.invoke.MethodHandles;
51 import java.nio.file.Path;
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;
72 private TestHelper testHelper;
78 private Policies policies;
81 private PolicyTypes policyTypes;
84 private Services services;
87 private MockA1ClientFactory a1ClientFactory;
90 private RappSimulatorController rAppSimulator;
93 private SecurityContext securityContext;
96 private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
102 private Helper helper;
106 testHelper.port = port;
107 this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
116 a1ClientFactory.reset();
117 this.rAppSimulator.getTestResults().clear();
118 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
119 this.securityContext.setAuthTokenFilePath(null);
120 this.openPolicyAgentSimulatorController.getTestResults().reset();
124 static void clearTestDir() {
126 FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3"));
127 } catch (Exception e) {
128 logger.warn("Could test directory : {}", e.getMessage());
133 @DisplayName("test Create Policy")
134 void testPostPolicy() throws Exception {
135 String nonRtRicId = "ric.1";
136 String policyTypeName = "type1_1.2.3";
137 String url = "/policies";
138 testHelper.addPolicyType(policyTypeName, nonRtRicId);
139 String policyBody = testHelper.postPolicyBody(nonRtRicId, policyTypeName);
140 Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody);
141 testHelper.testSuccessResponse(responseMono, HttpStatus.CREATED, "{\"servingCellNrcgi\":\"1\"}");
145 @DisplayName("test Create Policy schema validation fail case")
146 void testPolicySchemaValidationFail() throws Exception {
147 String nonRtRicId = "ric.1";
148 String policyTypeName = "type1_1.2.3";
149 String url = "/policies";
150 testHelper.addPolicyType(policyTypeName, nonRtRicId);
152 when(helper.jsonSchemaValidation(Mockito.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 String policyBody = testHelper.postPolicyBody("noRic", policyTypeName);
165 Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody);
166 testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic");
170 @DisplayName("test Create Policy with No Policy Type fail case")
171 void testCreatePolicyNoPolicyType() throws Exception {
172 String policyTypeName = "type1_1.2.3";
173 String nonRtRicId = "ricOne";
174 String url = "/policies";
175 testHelper.addPolicyType(policyTypeName, nonRtRicId);
176 String policyBody = testHelper.postPolicyBody(nonRtRicId, "noPolicyType");
177 Mono<ResponseEntity<String>> responseMono = testHelper.restClientV3().postForEntity(url, policyBody);
178 testHelper.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType");