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.service.v3;
23 import org.junit.jupiter.api.*;
24 import org.mockito.Mockito;
25 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
26 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
27 import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
28 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
30 import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.AuthorizationService;
31 import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.PolicyService;
32 import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
33 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.boot.test.mock.mockito.MockBean;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.test.context.TestPropertySource;
42 import org.springframework.util.FileSystemUtils;
43 import org.springframework.web.server.ServerWebExchange;
44 import org.springframework.web.server.adapter.DefaultServerWebExchange;
45 import reactor.core.publisher.Mono;
47 import java.io.IOException;
48 import java.lang.invoke.MethodHandles;
49 import java.nio.file.Path;
51 import static org.mockito.ArgumentMatchers.any;
52 import static org.mockito.Mockito.when;
54 @TestMethodOrder(MethodOrderer.MethodName.class)
56 @TestPropertySource(properties = { //
57 "app.vardata-directory=/tmp/pmstestv3", //
59 public class PolicyServiceTest {
61 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
63 private Policies policies;
66 private A1ClientFactory a1ClientFactory;
69 private ErrorHandlingService errorHandlingService;
72 private PolicyService policyService;
75 private TestHelper testHelper;
78 private Helper helper;
81 private AuthorizationService authorizationService;
89 static void clearTestDir() {
91 FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3"));
92 } catch (Exception e) {
93 logger.warn("Could test directory : {}", e.getMessage());
98 public void testPolicyAlreadyCreatedTrue() throws Exception{
100 String policyTypeName = "uri_type_123";
101 String nonRtRicId = "Ric_347";
102 testHelper.addPolicyType(policyTypeName, nonRtRicId);
103 ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class);
104 Policy policy = testHelper.buidTestPolicy(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), "122344-5674");
105 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
106 when(helper.buildPolicy(any(),any(), any(), any())).thenReturn(policy);
107 when(helper.isPolicyAlreadyCreated(any(), any())).thenReturn(Mono.error(new ServiceException
108 ("Same policy content already created with policy ID: 122344-5674", HttpStatus.BAD_REQUEST)));
109 Mono<ResponseEntity<PolicyObjectInformation>> responseMono = policyService.createPolicyService(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), serverWebExchange);
110 testHelper.verifyMockError(responseMono, "Same policy content already created with policy ID: 122344-5674");
114 public void testPolicyNotAuthorizedFail() throws IOException {
116 String policyTypeName = "uri_type_123";
117 String nonRtRicId = "Ric_347";
118 testHelper.addPolicyType(policyTypeName, nonRtRicId);
119 ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class);
120 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
121 when(helper.isPolicyAlreadyCreated(any(), any())).thenReturn(Mono.just(Policy.builder().build()));
122 when(authorizationService.authCheck(any(), any(), any())).thenReturn(Mono.error(new ServiceException("Not authorized", HttpStatus.UNAUTHORIZED)));
123 Mono<ResponseEntity<PolicyObjectInformation>> responseMono = policyService.createPolicyService(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), serverWebExchange);
124 testHelper.verifyMockError(responseMono, "Not authorized");