b8404a61b05f8d0341ff90978509dd348a15b987
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3;
22
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;
49
50 import java.lang.invoke.MethodHandles;
51 import java.nio.file.Path;
52
53 import static org.mockito.Mockito.when;
54
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", //
63         "app.filepath=", //
64         "app.s3.bucket=" // If this is set, S3 will be used to store data.
65 })
66 public class PolicyControllerTest {
67     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
68
69     @Autowired
70     private ApplicationConfig applicationConfig;
71     @Autowired
72     private TestHelper testHelper;
73
74     @Autowired
75     private Rics rics;
76
77     @Autowired
78     private Policies policies;
79
80     @Autowired
81     private PolicyTypes policyTypes;
82
83     @Autowired
84     private Services services;
85
86     @Autowired
87     private MockA1ClientFactory a1ClientFactory;
88
89     @Autowired
90     private RappSimulatorController rAppSimulator;
91
92     @Autowired
93     private SecurityContext securityContext;
94
95     @Autowired
96     private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
97
98     @LocalServerPort
99     private int port;
100
101     @SpyBean
102     private Helper helper;
103
104     @BeforeEach
105     void init() {
106         testHelper.port = port;
107         this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
108     }
109
110     @AfterEach
111     void reset() {
112         rics.clear();
113         policies.clear();
114         policyTypes.clear();
115         services.clear();
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();
121     }
122
123     @AfterAll
124     static void clearTestDir() {
125         try {
126             FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3"));
127         } catch (Exception e) {
128             logger.warn("Could test directory : {}", e.getMessage());
129         }
130     }
131
132     @Test
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\"}");
142     }
143
144     @Test
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);
151
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");
156     }
157
158     @Test
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");
167     }
168
169     @Test
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");
179     }
180 }