174add3b16b90acb00dab7ea4b3b4602e81423de
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2022 Nordix Foundation. 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.v2;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doReturn;
29
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.io.PrintStream;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.time.Duration;
41 import java.time.Instant;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
45
46 import org.json.JSONObject;
47 import org.junit.jupiter.api.AfterEach;
48 import org.junit.jupiter.api.Test;
49 import org.junit.jupiter.api.extension.ExtendWith;
50 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
51 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
52 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
53 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
54 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
55 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
56 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
57 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
58 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
59 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
60 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
61 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
71 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
72 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
73 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
74 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
75 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
76 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
77 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80 import org.springframework.beans.factory.annotation.Autowired;
81 import org.springframework.boot.test.context.SpringBootTest;
82 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
83 import org.springframework.boot.test.context.TestConfiguration;
84 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
85 import org.springframework.boot.web.server.LocalServerPort;
86 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
87 import org.springframework.context.ApplicationContext;
88 import org.springframework.context.annotation.Bean;
89 import org.springframework.http.HttpStatus;
90 import org.springframework.http.MediaType;
91 import org.springframework.http.ResponseEntity;
92 import org.springframework.test.context.TestPropertySource;
93 import org.springframework.test.context.junit.jupiter.SpringExtension;
94 import org.springframework.web.reactive.function.client.WebClientResponseException;
95
96 import reactor.core.publisher.Mono;
97 import reactor.test.StepVerifier;
98 import reactor.util.annotation.Nullable;
99
100 @ExtendWith(SpringExtension.class)
101 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
102 @TestPropertySource(properties = { //
103         "server.ssl.key-store=./src/test/resources/keystore.jks", //
104         "app.webclient.trust-store=./src/test/resources/truststore.jks", //
105         "app.webclient.trust-store-used=true", //
106         "app.vardata-directory=./target/testdata", //
107         "app.filepath=" //
108 })
109 class ApplicationTest {
110     private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
111
112     @Autowired
113     ApplicationContext context;
114
115     @Autowired
116     private Rics rics;
117
118     @Autowired
119     private Policies policies;
120
121     @Autowired
122     private PolicyTypes policyTypes;
123
124     @Autowired
125     MockA1ClientFactory a1ClientFactory;
126
127     @Autowired
128     RicSupervision supervision;
129
130     @Autowired
131     ApplicationConfig applicationConfig;
132
133     @Autowired
134     Services services;
135
136     @Autowired
137     RappSimulatorController rAppSimulator;
138
139     @Autowired
140     RefreshConfigTask refreshConfigTask;
141
142     @Autowired
143     SecurityContext securityContext;
144
145     private static Gson gson = new GsonBuilder().create();
146
147     /**
148      * Overrides the BeanFactory.
149      */
150     @TestConfiguration
151     static class TestBeanFactory {
152
153         @Bean
154         A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
155             return new MockA1ClientFactory(appConfig, types);
156         }
157
158         @Bean
159         public ServiceSupervision getServiceSupervision(@Autowired Services services,
160                 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
161             Duration checkInterval = Duration.ofMillis(1);
162             return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
163         }
164
165         @Bean
166         public ServletWebServerFactory servletContainer() {
167             return new TomcatServletWebServerFactory();
168         }
169     }
170
171     @LocalServerPort
172     private int port;
173
174     @AfterEach
175     void reset() {
176         rics.clear();
177         policies.clear();
178         policyTypes.clear();
179         services.clear();
180         a1ClientFactory.reset();
181         this.rAppSimulator.getTestResults().clear();
182         this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
183         this.securityContext.setAuthTokenFilePath(null);
184     }
185
186     @AfterEach
187     void verifyNoRicLocks() {
188         for (Ric ric : this.rics.getRics()) {
189             Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "");
190             grant.unlockBlocking();
191             assertThat(ric.getLock().getLockCounter()).isZero();
192             assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
193         }
194     }
195
196     @Test
197     void generateApiDoc() throws IOException {
198         String url = "https://localhost:" + this.port + "/v3/api-docs";
199         ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
200         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
201         JSONObject jsonObj = new JSONObject(resp.getBody());
202         assertThat(jsonObj.remove("servers")).isNotNull();
203
204         String indented = (jsonObj).toString(4);
205         String docDir = "api/";
206         Files.createDirectories(Paths.get(docDir));
207         try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
208             out.print(indented);
209         }
210     }
211
212     @Test
213     void testPersistencyPolicies() throws ServiceException {
214         Ric ric = this.addRic("ric1");
215         PolicyType type = this.addPolicyType("type1", ric.id());
216
217         final int noOfPolicies = 100;
218         for (int i = 0; i < noOfPolicies; ++i) {
219             addPolicy("id" + i, type.getId(), "service", ric.id());
220         }
221
222         {
223             Policies policies = new Policies(this.applicationConfig);
224             policies.restoreFromDatabase(ric, this.policyTypes);
225             assertThat(policies.size()).isEqualTo(noOfPolicies);
226         }
227
228         {
229             restClient().delete("/policies/id2").block();
230             Policies policies = new Policies(this.applicationConfig);
231             policies.restoreFromDatabase(ric, this.policyTypes);
232             assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
233         }
234     }
235
236     @Test
237     void testPersistencyPolicyTypes() throws ServiceException {
238         Ric ric = this.addRic("ric1");
239         this.addPolicyType("type1", ric.id());
240         PolicyTypes types = new PolicyTypes(this.applicationConfig);
241         assertThat(types.size()).isEqualTo(1);
242     }
243
244     @Test
245     void testPersistencyService() throws ServiceException {
246         final String SERVICE = "serviceName";
247         putService(SERVICE, 1234, HttpStatus.CREATED);
248         assertThat(this.services.size()).isEqualTo(1);
249         Service service = this.services.getService(SERVICE);
250
251         Services servicesRestored = new Services(this.applicationConfig);
252         Service serviceRestored = servicesRestored.getService(SERVICE);
253         assertThat(servicesRestored.size()).isEqualTo(1);
254         assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
255         assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
256
257         // check that the service can be deleted
258         this.services.remove(SERVICE);
259         servicesRestored = new Services(this.applicationConfig);
260         assertThat(servicesRestored.size()).isZero();
261     }
262
263     @Test
264     void testAddingRicFromConfiguration() throws Exception {
265         // Test adding the RIC from configuration
266
267         final String RIC = "ric1";
268         final String TYPE = "type123";
269         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
270         nearRtRicPolicyTypes.put(createPolicyType(TYPE));
271         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
272
273         putService("service");
274
275         refreshConfigTask.handleUpdatedRicConfig( //
276                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
277                 .block();
278         waitForRicState(RIC, RicState.AVAILABLE);
279
280         // Test that the type has been synched
281         Ric addedRic = this.rics.getRic(RIC);
282         assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
283         assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
284
285         // Check that a service callback for the AVAILABLE RIC is invoked
286         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
287         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
288         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
289         assertThat(callbackInfo.ricId).isEqualTo(RIC);
290         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
291     }
292
293     @Test
294     void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
295         putService("service");
296
297         final String RIC = "NonRespondingRic";
298         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
299         doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
300
301         refreshConfigTask.handleUpdatedRicConfig( //
302                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
303                 .block();
304
305         waitForRicState(RIC, RicState.UNAVAILABLE);
306
307         // Check that no service callback for the UNAVAILABLE RIC is invoked
308         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
309         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
310
311         // Run a synch and check that the AVAILABLE notification is received
312         a1ClientFactory.reset();
313         supervision.checkAllRics();
314         waitForRicState(RIC, RicState.AVAILABLE);
315
316         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
317     }
318
319     @Test
320     void testTrustValidation() {
321         addRic("ric1");
322         String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
323         assertThat(rsp).contains("ric1");
324     }
325
326     @Test
327     void testGetRics() throws Exception {
328         addRic("ric1");
329         this.addPolicyType("type1", "ric1");
330         String url = "/rics?policytype_id=type1";
331         String rsp = restClient().get(url).block();
332         assertThat(rsp).contains("ric1");
333
334         // nameless type for ORAN A1 1.1
335         addRic("ric2");
336         this.addPolicyType("", "ric2");
337         url = "/rics?policytype_id=";
338         rsp = restClient().get(url).block();
339         assertThat(rsp).contains("ric2") //
340                 .doesNotContain("ric1") //
341                 .contains("AVAILABLE");
342
343         // All RICs
344         rsp = restClient().get("/rics").block();
345         assertThat(rsp).contains("ric2") //
346                 .contains("ric1");
347
348         // Non existing policy type
349         url = "/rics?policytype_id=XXXX";
350         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
351     }
352
353     @Test
354     void testSynchronization() throws Exception {
355         // Two polictypes will be put in the NearRT RICs
356         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
357         nearRtRicPolicyTypes.put(createPolicyType("typeName"));
358         nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
359         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
360
361         // One type and one instance added to the Policy Management Service's storage
362         final String ric1Name = "ric1";
363         Ric ric1 = addRic(ric1Name);
364         Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
365         Ric ric2 = addRic("ric2");
366
367         getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
368         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
369
370         String policyId = "policyId";
371         Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
372         supervision.checkAllRics(); // The created policy should be put in the RIC
373
374         // Wait until synch is completed
375         waitForRicState(ric1Name, RicState.SYNCHRONIZING);
376         waitForRicState(ric1Name, RicState.AVAILABLE);
377         waitForRicState("ric2", RicState.AVAILABLE);
378
379         Policies ricPolicies = getA1Client(ric1Name).getPolicies();
380         assertThat(ricPolicies.size()).isEqualTo(1);
381         Policy ricPolicy = ricPolicies.get(policyId);
382         assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
383
384         // Both types should be in the Policy Management Service's storage after the
385         // synch
386         assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
387         assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
388     }
389
390     @Test
391     void testGetRic() throws Exception {
392         String ricId = "ric1";
393         String managedElementId = "kista_1";
394         addRic(ricId, managedElementId);
395
396         String url = "/rics/ric?managed_element_id=" + managedElementId;
397         String rsp = restClient().get(url).block();
398         RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
399         assertThat(ricInfo.ricId).isEqualTo(ricId);
400
401         url = "/rics/ric?ric_id=" + ricId;
402         rsp = restClient().get(url).block();
403         ricInfo = gson.fromJson(rsp, RicInfo.class);
404         assertThat(ricInfo.ricId).isEqualTo(ricId);
405
406         // test GET RIC for ManagedElement that does not exist
407         url = "/rics/ric?managed_element_id=" + "junk";
408         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
409
410         url = "/rics/ric";
411         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
412     }
413
414     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
415             boolean isTransient) {
416         PolicyInfo info = new PolicyInfo();
417         info.policyId = policyInstanceId;
418         info.policyTypeId = policyTypeName;
419         info.ricId = ricId;
420         info.serviceId = serviceName;
421         info.policyData = gson.fromJson(jsonString(), Object.class);
422
423         if (isTransient) {
424             info.isTransient = isTransient;
425         }
426         info.statusNotificationUri = "statusNotificationUri";
427         return gson.toJson(info);
428     }
429
430     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
431         return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false);
432     }
433
434     @Test
435     void testPutPolicy() throws Exception {
436         String serviceName = "service.1";
437         String ricId = "ric.1";
438         String policyTypeName = "type1_1.2.3";
439         String policyInstanceId = "instance_1.2.3";
440
441         putService(serviceName);
442         addPolicyType(policyTypeName, ricId);
443
444         // PUT a transient policy
445         String url = "/policies";
446         String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true);
447         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
448
449         restClient().put(url, policyBody).block();
450
451         Policy policy = policies.getPolicy(policyInstanceId);
452         assertThat(policy).isNotNull();
453         assertThat(policy.getId()).isEqualTo(policyInstanceId);
454         assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
455         assertThat(policy.getRic().id()).isEqualTo(ricId);
456         assertThat(policy.isTransient()).isTrue();
457
458         // Put a non transient policy
459         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
460         restClient().put(url, policyBody).block();
461         policy = policies.getPolicy(policyInstanceId);
462         assertThat(policy.isTransient()).isFalse();
463
464         url = "/policy-instances";
465         String rsp = restClient().get(url).block();
466         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
467
468         url = "/policies/" + policyInstanceId;
469         rsp = restClient().get(url).block();
470         assertThat(rsp).contains(policyBody);
471
472         // Test of error codes
473         url = "/policies";
474         policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
475         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
476
477         policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
478         addPolicyType(policyTypeName + "XX", "otherRic");
479         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
480
481         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
482         this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
483         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
484         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
485     }
486
487     @Test
488     /**
489      * Test that HttpStatus and body from failing REST call to A1 is passed on to
490      * the caller.
491      *
492      * @throws ServiceException
493      */
494     void testErrorFromRic() throws ServiceException {
495         putService("service1");
496         addPolicyType("type1", "ric1");
497
498         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
499         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
500         String responseBody = "Refused";
501         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
502
503         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
504                 responseBodyBytes, StandardCharsets.UTF_8, null);
505         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
506
507         // PUT Policy
508         String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
509         String url = "/policies";
510         testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
511
512         // DELETE POLICY
513         this.addPolicy("instance1", "type1", "service1", "ric1");
514         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
515         testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
516
517     }
518
519     @Test
520     void testPutTypelessPolicy() throws Exception {
521         putService("service1");
522         addPolicyType("", "ric1");
523         String body = putPolicyBody("service1", "ric1", "", "id1");
524         restClient().put("/policies", body).block();
525
526         String rsp = restClient().get("/policy-instances").block();
527         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
528         assertThat(info.policies).hasSize(1);
529         PolicyInfo policyInfo = info.policies.iterator().next();
530         assertThat(policyInfo.policyId).isEqualTo("id1");
531         assertThat(policyInfo.policyTypeId).isEmpty();
532     }
533
534     @Test
535     void testRefuseToUpdatePolicy() throws Exception {
536         // Test that only the json can be changed for a already created policy
537         // In this case service is attempted to be changed
538         this.addRic("ric1");
539         this.addRic("ricXXX");
540         this.addPolicy("instance1", "type1", "service1", "ric1");
541         this.addPolicy("instance2", "type1", "service1", "ricXXX");
542
543         // Try change ric1 -> ricXXX
544         String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
545         testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
546     }
547
548     @Test
549     void testGetPolicy() throws Exception {
550         String url = "/policies/id";
551         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
552         {
553             String rsp = restClient().get(url).block();
554             PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
555             String policyStr = gson.toJson(info.policyData);
556             assertThat(policyStr).isEqualTo(policy.getJson());
557         }
558         {
559             policies.remove(policy);
560             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
561         }
562     }
563
564     @Test
565     void testDeletePolicy() throws Exception {
566         String policyId = "id.1";
567         addPolicy(policyId, "typeName", "service1", "ric1");
568         assertThat(policies.size()).isEqualTo(1);
569
570         String url = "/policies/" + policyId;
571         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
572
573         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
574         assertThat(policies.size()).isZero();
575
576         // Delete a non existing policy
577         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
578     }
579
580     @Test
581     void testGetPolicyType() throws Exception {
582         String typeId = "AC.D";
583         addPolicyType(typeId, "ric1");
584
585         waitForRicState("ric1", RicState.AVAILABLE);
586
587         String url = "/policy-types/" + typeId;
588
589         String rsp = this.restClient().get(url).block();
590
591         PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
592         assertThat(info.schema).isNotNull();
593
594         // Get non existing schema
595         url = "/policy-types/JUNK";
596         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
597     }
598
599     String createPolicyTypesJson(String... types) {
600         List<String> list = new ArrayList<>();
601         Collections.addAll(list, types);
602         PolicyTypeIdList ids = new PolicyTypeIdList(list);
603         return gson.toJson(ids);
604     }
605
606     @Test
607     void testGetPolicyTypes() throws Exception {
608         String TYPE_ID_1 = "A_type1_1.9.0";
609         String TYPE_ID_2 = "A_type1_2.0.0";
610         String TYPE_ID_3 = "A_type1_1.5.0";
611         String TYPE_ID_4 = "type3_1.9.0";
612         addPolicyType(TYPE_ID_1, "ric1");
613         addPolicyType(TYPE_ID_2, "ric2");
614         addPolicyType(TYPE_ID_3, "ric2");
615         addPolicyType(TYPE_ID_4, "ric2");
616
617         addPolicyType("junk", "ric2");
618         addPolicyType("junk_a.b.c", "ric2");
619
620         String url = "/policy-types";
621         String rsp = restClient().get(url).block();
622         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
623
624         url = "/policy-types?ric_id=ric1";
625         rsp = restClient().get(url).block();
626         String expResp = createPolicyTypesJson(TYPE_ID_1);
627         assertThat(rsp).isEqualTo(expResp);
628
629         // Get policy types for non existing RIC
630         url = "/policy-types?ric_id=ric1XXX";
631         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
632
633         // All types with a type_name
634         url = "/policy-types?type_name=A_type1";
635         rsp = restClient().get(url).block();
636         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
637
638         // All types compatible with type1_1.5.0 (which is type1_1.9.0)
639         url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
640         rsp = restClient().get(url).block();
641         expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
642         assertThat(rsp).isEqualTo(expResp);
643
644         url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
645         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
646
647         url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
648         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
649
650         url = "/policy-types?compatible_with_version=1.5.0";
651         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
652     }
653
654     @Test
655     void testGetPolicyInstances() throws Exception {
656         addPolicy("id1", "type1", "service1");
657
658         String url = "/policy-instances";
659         String rsp = restClient().get(url).block();
660         logger.info(rsp);
661         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
662         assertThat(info.policies).hasSize(1);
663         PolicyInfo policyInfo = info.policies.iterator().next();
664         assert (policyInfo.validate());
665         assertThat(policyInfo.policyId).isEqualTo("id1");
666         assertThat(policyInfo.policyTypeId).isEqualTo("type1");
667         assertThat(policyInfo.serviceId).isEqualTo("service1");
668     }
669
670     @Test
671     void testGetPolicyInstancesFilter() throws Exception {
672         addPolicy("id1", "type1", "service1");
673         addPolicy("id2", "type1", "service2");
674         addPolicy("id3", "type2", "service1");
675         addPolicy("id4", "type1_1.0.0", "service1");
676
677         String url = "/policy-instances?policytype_id=type1";
678         String rsp = restClient().get(url).block();
679         logger.info(rsp);
680         assertThat(rsp).contains("id1") //
681                 .contains("id2") //
682                 .doesNotContain("id3");
683
684         url = "/policy-instances?policytype_id=type1&service_id=service2";
685         rsp = restClient().get(url).block();
686         logger.info(rsp);
687         assertThat(rsp).doesNotContain("id1") //
688                 .contains("id2") //
689                 .doesNotContain("id3");
690
691         url = "/policy-instances?type_name=type1";
692         rsp = restClient().get(url).block();
693         assertThat(rsp).contains("id1") //
694                 .contains("id2") //
695                 .doesNotContain("id3") //
696                 .contains("id4");
697
698         // Test get policies for non existing type
699         url = "/policy-instances?policytype_id=type1XXX";
700         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
701
702         // Test get policies for non existing RIC
703         url = "/policy-instances?ric_id=XXX";
704         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
705     }
706
707     @Test
708     void testGetPolicyIdsFilter() throws Exception {
709         addPolicy("id1", "type1", "service1", "ric1");
710         addPolicy("id2", "type1", "service2", "ric1");
711         addPolicy("id3", "type2", "service1", "ric1");
712         addPolicy("id4", "type1_1.0.0", "service1");
713
714         String url = "/policies?policytype_id=type1";
715         String rsp = restClient().get(url).block();
716         logger.info(rsp);
717         assertThat(rsp).contains("id1") //
718                 .contains("id2") //
719                 .doesNotContain("id3");
720
721         url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
722         rsp = restClient().get(url).block();
723         PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
724         assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
725
726         url = "/policies?policytype_name=type1&service_id=service1";
727         rsp = restClient().get(url).block();
728         assertThat(rsp).contains("id1") //
729                 .contains("id3") //
730                 .contains("id4");
731         assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
732
733         // Test get policy ids for non existing type
734         url = "/policies?policytype_id=type1XXX";
735         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
736
737         // Test get policy ids for non existing RIC
738         url = "/policies?ric_id=XXX";
739         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
740     }
741
742     @Test
743     void testPutAndGetService() throws Exception {
744         // PUT
745         String serviceName = "ac.dc";
746         putService(serviceName, 0, HttpStatus.CREATED);
747         putService(serviceName, 0, HttpStatus.OK);
748
749         // GET one service
750         String url = "/services?service_id=" + serviceName;
751         String rsp = restClient().get(url).block();
752         ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
753         assertThat(info.statusList).hasSize(1);
754         ServiceStatus status = info.statusList.iterator().next();
755         assertThat(status.keepAliveIntervalSeconds).isZero();
756         assertThat(status.serviceId).isEqualTo(serviceName);
757
758         // GET (all)
759         url = "/services";
760         rsp = restClient().get(url).block();
761         assertThat(rsp).as("Response contains service name").contains(serviceName);
762         logger.info(rsp);
763
764         // Keep alive
765         url = "/services/" + serviceName + "/keepalive";
766         ResponseEntity<?> entity = restClient().putForEntity(url).block();
767         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
768
769         // DELETE service
770         assertThat(services.size()).isEqualTo(1);
771         url = "/services/" + serviceName;
772         restClient().delete(url).block();
773         assertThat(services.size()).isZero();
774
775         // Keep alive, no registered service
776         testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
777
778         // PUT service with bad payload
779         testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
780         testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
781         testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
782                 false);
783         testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
784                 HttpStatus.BAD_REQUEST, false);
785
786         // GET non existing service
787         testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
788     }
789
790     @Test
791     void testServiceSupervision() throws Exception {
792         putService("service1", 1, HttpStatus.CREATED);
793         addPolicyType("type1", "ric1");
794
795         String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
796         restClient().put("/policies", policyBody).block();
797
798         assertThat(policies.size()).isEqualTo(1);
799         assertThat(services.size()).isEqualTo(1);
800
801         // Timeout after ~1 second
802         await().untilAsserted(() -> assertThat(policies.size()).isZero());
803         assertThat(services.size()).isZero();
804     }
805
806     @Test
807     void testGetPolicyStatus() throws Exception {
808         addPolicy("id", "typeName", "service1", "ric1");
809         assertThat(policies.size()).isEqualTo(1);
810
811         String url = "/policies/id/status";
812         String rsp = restClient().get(url).block();
813         PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
814         assertThat(info.status).isEqualTo("OK");
815
816         // GET non existing policy status
817         url = "/policies/XXX/status";
818         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
819
820         // GET STATUS, the NearRT RIC returns error
821         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
822         url = "/policies/id/status";
823         WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
824         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
825         rsp = restClient().get(url).block();
826         info = gson.fromJson(rsp, PolicyStatusInfo.class);
827         assertThat(info.status).hasToString("{}");
828     }
829
830     @Test
831     void testGetServiceStatus() throws Exception {
832         String url = "/status";
833         String rsp = restClient().get(url).block();
834         assertThat(rsp).contains("hunky dory");
835
836         rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
837         assertThat(rsp).isEqualTo("hunky dory");
838     }
839
840     @Test
841     void testServiceNotification() throws Exception {
842
843         final String AUTH_TOKEN = "testToken";
844         Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
845         Files.write(authFile, AUTH_TOKEN.getBytes());
846         this.securityContext.setAuthTokenFilePath(authFile);
847
848         putService("junkService");
849         Service junkService = this.services.get("junkService");
850         junkService.setCallbackUrl("https://junk");
851         putService("service");
852
853         Ric ric = addRic("ric1");
854         ric.setState(Ric.RicState.UNAVAILABLE);
855         supervision.checkAllRics();
856         waitForRicState("ric1", RicState.AVAILABLE);
857
858         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
859         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
860         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
861         assertThat(callbackInfo.ricId).isEqualTo("ric1");
862         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
863
864         var headers = receivedCallbacks.receivedHeaders.get(0);
865         assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
866
867         Files.delete(authFile);
868     }
869
870     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
871         addRic(ric);
872         Policy policy = Policy.builder() //
873                 .id(id) //
874                 .json(jsonString()) //
875                 .ownerServiceId(service) //
876                 .ric(rics.getRic(ric)) //
877                 .type(addPolicyType(typeName, ric)) //
878                 .lastModified(Instant.now()) //
879                 .isTransient(false) //
880                 .statusNotificationUri("/policy-status?id=XXX") //
881                 .build();
882         policies.put(policy);
883         return policy;
884     }
885
886     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
887         return addPolicy(id, typeName, service, "ric");
888     }
889
890     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
891         String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
892         return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
893     }
894
895     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
896         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
897
898         String json = gson.toJson(service);
899         return json;
900     }
901
902     private void putService(String name) {
903         putService(name, 0, null);
904     }
905
906     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
907         String url = "/services";
908         String body = createServiceJson(name, keepAliveIntervalSeconds);
909         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
910         if (expectedStatus != null) {
911             assertEquals(expectedStatus, resp.getStatusCode(), "");
912         }
913     }
914
915     private String jsonString() {
916         return "{\"servingCellNrcgi\":\"1\"}";
917     }
918
919     @Test
920     void testConcurrency() throws Exception {
921         logger.info("Concurrency test starting");
922         final Instant startTime = Instant.now();
923         List<Thread> threads = new ArrayList<>();
924         List<ConcurrencyTestRunnable> tests = new ArrayList<>();
925         a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
926         addRic("ric");
927         addPolicyType("type1", "ric");
928         addPolicyType("type2", "ric");
929
930         for (int i = 0; i < 10; ++i) {
931             AsyncRestClient restClient = restClient();
932             ConcurrencyTestRunnable test =
933                     new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
934             Thread thread = new Thread(test, "TestThread_" + i);
935             thread.start();
936             threads.add(thread);
937             tests.add(test);
938         }
939         for (Thread t : threads) {
940             t.join();
941         }
942         for (ConcurrencyTestRunnable test : tests) {
943             assertThat(test.isFailed()).isFalse();
944         }
945         assertThat(policies.size()).isZero();
946         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
947     }
948
949     private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
950         WebClientConfig config = this.applicationConfig.getWebClientConfig();
951         config = ImmutableWebClientConfig.builder() //
952                 .keyStoreType(config.keyStoreType()) //
953                 .keyStorePassword(config.keyStorePassword()) //
954                 .keyStore(config.keyStore()) //
955                 .keyPassword(config.keyPassword()) //
956                 .isTrustStoreUsed(useTrustValidation) //
957                 .trustStore(config.trustStore()) //
958                 .trustStorePassword(config.trustStorePassword()) //
959                 .httpProxyConfig(config.httpProxyConfig()) //
960                 .build();
961
962         AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
963         return f.createRestClientNoHttpProxy(baseUrl);
964
965     }
966
967     private String baseUrl() {
968         return "https://localhost:" + port;
969     }
970
971     private AsyncRestClient restClient(boolean useTrustValidation) {
972         return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
973     }
974
975     private AsyncRestClient restClient() {
976         return restClient(false);
977     }
978
979     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
980         testErrorCode(request, expStatus, "", true);
981     }
982
983     private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
984         testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
985     }
986
987     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
988         testErrorCode(request, expStatus, responseContains, true);
989     }
990
991     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
992             boolean expectApplicationProblemJsonMediaType) {
993         StepVerifier.create(request) //
994                 .expectSubscription() //
995                 .expectErrorMatches(
996                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
997                 .verify();
998     }
999
1000     private void waitForRicState(String ricId, RicState state) throws ServiceException {
1001         Ric ric = rics.getRic(ricId);
1002         await().untilAsserted(() -> state.equals(ric.getState()));
1003     }
1004
1005     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1006             boolean expectApplicationProblemJsonMediaType) {
1007         assertTrue(throwable instanceof WebClientResponseException);
1008         WebClientResponseException responseException = (WebClientResponseException) throwable;
1009         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1010         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
1011         if (expectApplicationProblemJsonMediaType) {
1012             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1013         }
1014         return true;
1015     }
1016
1017     private MockA1Client getA1Client(String ricId) throws ServiceException {
1018         return a1ClientFactory.getOrCreateA1Client(ricId);
1019     }
1020
1021     private PolicyType createPolicyType(String policyTypeName) {
1022         return PolicyType.builder() //
1023                 .id(policyTypeName) //
1024                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1025                 .build();
1026     }
1027
1028     private PolicyType addPolicyType(String policyTypeName, String ricId) {
1029         PolicyType type = createPolicyType(policyTypeName);
1030         policyTypes.put(type);
1031         addRic(ricId).addSupportedPolicyType(type);
1032         return type;
1033     }
1034
1035     private Ric addRic(String ricId) {
1036         return addRic(ricId, null);
1037     }
1038
1039     private RicConfig ricConfig(String ricId, String managedElement) {
1040         List<String> mes = new ArrayList<>();
1041         if (managedElement != null) {
1042             mes.add(managedElement);
1043         }
1044         return ImmutableRicConfig.builder() //
1045                 .ricId(ricId) //
1046                 .baseUrl(ricId) //
1047                 .managedElementIds(mes) //
1048                 .controllerName("") //
1049                 .customAdapterClass("") //
1050                 .build();
1051     }
1052
1053     private Ric addRic(String ricId, String managedElement) {
1054         if (rics.get(ricId) != null) {
1055             return rics.get(ricId);
1056         }
1057
1058         RicConfig conf = ricConfig(ricId, managedElement);
1059         Ric ric = new Ric(conf);
1060         ric.setState(Ric.RicState.AVAILABLE);
1061         this.rics.put(ric);
1062         return ric;
1063     }
1064
1065 }