ee0073a4603abb81fdfd329b55fc38059532c8a3
[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.*;
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;
46
47 import java.io.IOException;
48 import java.lang.invoke.MethodHandles;
49 import java.nio.file.Path;
50
51 import static org.mockito.ArgumentMatchers.any;
52 import static org.mockito.Mockito.when;
53
54 @TestMethodOrder(MethodOrderer.MethodName.class)
55 @SpringBootTest()
56 @TestPropertySource(properties = { //
57         "app.vardata-directory=/tmp/pmstestv3", //
58 })
59 public class PolicyServiceTest {
60
61     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62     @Autowired
63     private Policies policies;
64
65     @Autowired
66     private A1ClientFactory a1ClientFactory;
67
68     @Autowired
69     private ErrorHandlingService errorHandlingService;
70
71     @Autowired
72     private PolicyService policyService;
73
74     @Autowired
75     private TestHelper testHelper;
76
77     @MockBean
78     private Helper helper;
79
80     @MockBean
81     private AuthorizationService authorizationService;
82
83     @AfterEach
84     public void clear() {
85         policies.clear();
86     }
87
88     @AfterAll
89     static void clearTestDir() {
90         try {
91             FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3"));
92         } catch (Exception e) {
93             logger.warn("Could test directory : {}", e.getMessage());
94         }
95     }
96
97     @Test
98     public void testPolicyAlreadyCreatedTrue() throws Exception{
99
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");
111     }
112
113     @Test
114     public void testPolicyNotAuthorizedFail() throws IOException {
115
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");
125     }
126 }