a67a0bb332ebe41a19411060e4763b70d1584745
[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.service.v3;
22
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;
44
45 import static org.mockito.ArgumentMatchers.any;
46 import static org.mockito.Mockito.when;
47
48 @TestMethodOrder(MethodOrderer.MethodName.class)
49 @SpringBootTest()
50 public class PolicyServiceTest {
51
52     @Autowired
53     private Rics rics;
54
55     @Autowired
56     private PolicyTypes policyTypes;
57
58     @Autowired
59     private Policies policies;
60
61     @Autowired
62     private A1ClientFactory a1ClientFactory;
63
64     @Autowired
65     private ErrorHandlingService errorHandlingService;
66
67     @Autowired
68     private PolicyService policyService;
69
70     @Autowired
71     private TestHelper testHelper;
72
73     @MockBean
74     private Helper helper;
75
76     @MockBean
77     private AuthorizationService authorizationService;
78
79     @Test
80     public void testPolicyAlreadyCreatedFail() {
81
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");
91     }
92
93     @Test
94     public void testPolicyNotAuthorizedFail() {
95
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");
105     }
106 }