bfc0b683c4d49681c9fbec1ec2a7aea1410b9119
[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 com.google.gson.Gson;
24 import org.junit.jupiter.api.*;
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.models.v3.ServiceRegistrationInfo;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
33 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
34 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
35 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelper;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.web.server.LocalServerPort;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.TestPropertySource;
45 import reactor.core.publisher.Mono;
46
47 import java.lang.invoke.MethodHandles;
48 import java.time.Duration;
49
50 @TestMethodOrder(MethodOrderer.MethodName.class)
51 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
52 @ContextConfiguration(classes = TestConfig.class)
53 @TestPropertySource(properties = { //
54         "server.ssl.key-store=./config/keystore.jks", //
55         "app.webclient.trust-store=./config/truststore.jks", //
56         "app.webclient.trust-store-used=true", //
57         "app.vardata-directory=/tmp/pmstestv3", //a
58         "app.filepath=", //
59         "app.s3.bucket=" // If this is set, S3 will be used to store data.
60 })
61 class ServiceControllerV3Test {
62
63     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
64
65     @Autowired
66     private TestHelper testHelper;
67
68     @Autowired
69     private ApplicationConfig applicationConfig;
70
71     @Autowired
72     private Services services;
73
74     @Autowired
75     private MockA1ClientFactory a1ClientFactory;
76
77     @Autowired
78     private RappSimulatorController rAppSimulator;
79
80     @Autowired
81     private SecurityContext securityContext;
82
83     @Autowired
84     private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
85
86     @Autowired
87     private PolicyTypes policyTypes;
88
89     @Autowired
90     private Gson gson;
91
92     @LocalServerPort
93     private int port;
94
95     @BeforeEach
96     void init() {
97         testHelper.port = port;
98         this.applicationConfig.setAuthProviderUrl(testHelper.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
99     }
100
101     @AfterEach
102     void reset() {
103         services.clear();
104         a1ClientFactory.reset();
105         this.rAppSimulator.getTestResults().clear();
106         this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
107         this.securityContext.setAuthTokenFilePath(null);
108         this.openPolicyAgentSimulatorController.getTestResults().reset();
109     }
110
111     @Test
112     public void testPutService() {
113         ServiceRegistrationInfo serviceRegistrationInfo = new ServiceRegistrationInfo("serviceId");
114         serviceRegistrationInfo.callbackUrl("http://callback.com/").keepAliveIntervalSeconds(10L);
115         Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3()
116                 .putForEntity("/services", gson.toJson(serviceRegistrationInfo));
117         testHelper.testSuccessResponse(responseEntityMono, HttpStatus.CREATED, responseBody -> services.size() == 1);
118     }
119
120     @Test
121     public void testGetService() {
122         services.put(new Service("newServiceId", Duration.ofSeconds(10L),  "http://callback.com/"));
123         Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3().getForEntity("/services");
124         testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBoy -> responseBoy
125                 .contains("http://callback.com/"));
126     }
127
128     @Test
129     public void testDeleteService() {
130         services.put(new Service("newServiceId", Duration.ofSeconds(10L),  "http://callback.com/"));
131         Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3().deleteForEntity("/services/newServiceId");
132         testHelper.testSuccessResponse(responseEntityMono, HttpStatus.NO_CONTENT, responseBody -> services.size() == 0);
133     }
134
135     @Test
136     public void testKeepAliveService() {
137         services.put(new Service("newServiceId", Duration.ofSeconds(10L),  "http://callback.com/"));
138         Mono<ResponseEntity<String>> responseEntityMono = testHelper.restClientV3().putForEntity("/services/newServiceId/keepalive", "");
139         testHelper.testSuccessResponse(responseEntityMono, HttpStatus.OK, responseBody -> services.size() == 1);
140     }
141 }