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.MethodOrderer;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.TestMethodOrder;
26 import org.mockito.Mockito;
27 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
28 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
29 import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
33 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
34 import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
35 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper;
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.web.server.ServerWebExchange;
42 import org.springframework.web.server.adapter.DefaultServerWebExchange;
43 import reactor.core.publisher.Mono;
45 import static org.mockito.ArgumentMatchers.any;
46 import static org.mockito.Mockito.when;
48 @TestMethodOrder(MethodOrderer.MethodName.class)
50 public class PolicyServiceTest {
56 private PolicyTypes policyTypes;
59 private Policies policies;
62 private A1ClientFactory a1ClientFactory;
65 private ErrorHandlingService errorHandlingService;
68 private PolicyService policyService;
71 private TestHelper testHelper;
74 private Helper helper;
77 private AuthorizationService authorizationService;
80 public void testPolicyAlreadyCreatedFail() {
82 String policyTypeName = "uri_type_123";
83 String nonRtRicId = "Ric_347";
84 testHelper.addPolicyType(policyTypeName, nonRtRicId);
85 ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class);
86 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
87 when(helper.isPolicyAlreadyCreated(any(), any())).thenReturn(Mono.error(new ServiceException
88 ("Policy already created", HttpStatus.CONFLICT)));
89 Mono<ResponseEntity<PolicyObjectInformation>> responseMono = policyService.createPolicyService(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), serverWebExchange);
90 testHelper.verifyMockError(responseMono, "Policy already created");
94 public void testPolicyNotAuthorizedFail() {
96 String policyTypeName = "uri_type_123";
97 String nonRtRicId = "Ric_347";
98 testHelper.addPolicyType(policyTypeName, nonRtRicId);
99 ServerWebExchange serverWebExchange = Mockito.mock(DefaultServerWebExchange.class);
100 when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
101 when(helper.isPolicyAlreadyCreated(any(), any())).thenReturn(Mono.just(Policy.builder().build()));
102 when(authorizationService.authCheck(any(), any(), any())).thenReturn(Mono.error(new ServiceException("Not authorized", HttpStatus.UNAUTHORIZED)));
103 Mono<ResponseEntity<PolicyObjectInformation>> responseMono = policyService.createPolicyService(testHelper.policyObjectInfo(nonRtRicId, policyTypeName), serverWebExchange);
104 testHelper.verifyMockError(responseMono, "Not authorized");