PMS Persistent storage of service definitions - A1 Istanbul
[ccsdk/oran.git] / a1-policy-management / src / test / java / org / onap / ccsdk / oran / a1policymanagementservice / tasks / ServiceSupervisionTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 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.tasks;
22
23 import static ch.qos.logback.classic.Level.WARN;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.awaitility.Awaitility.await;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyNoInteractions;
29 import static org.mockito.Mockito.verifyNoMoreInteractions;
30 import static org.mockito.Mockito.when;
31
32 import ch.qos.logback.classic.spi.ILoggingEvent;
33 import ch.qos.logback.core.read.ListAppender;
34
35 import java.time.Duration;
36 import java.time.Instant;
37 import java.util.Collections;
38
39 import org.awaitility.Durations;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.mockito.Mock;
43 import org.mockito.junit.jupiter.MockitoExtension;
44 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client;
45 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
46 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
47 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
48 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
49 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
50 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
51 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
52 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
53 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
54 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
55 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
56 import reactor.core.publisher.Mono;
57
58 @ExtendWith(MockitoExtension.class)
59 class ServiceSupervisionTest {
60
61     private static final String SERVICE_NAME = "Service name";
62     private static final String RIC_NAME = "name";
63     private static final String POLICY_ID = "policy";
64
65     @Mock
66     A1ClientFactory a1ClientFactoryMock;
67     @Mock
68     A1Client a1ClientMock;
69
70     private Services services;
71     private Service service;
72     private Policies policies;
73     private RicConfig ricConfig = ImmutableRicConfig.builder() //
74             .ricId(RIC_NAME) //
75             .baseUrl("baseUrl") //
76             .managedElementIds(Collections.emptyList()) //
77             .controllerName("") //
78             .build();
79     private Ric ric = new Ric(ricConfig);
80     private PolicyType policyType = PolicyType.builder() //
81             .id("policyTypeName") //
82             .schema("schema") //
83             .build();
84     private Policy policy = Policy.builder() //
85             .id(POLICY_ID) //
86             .json("json") //
87             .ownerServiceId(SERVICE_NAME) //
88             .ric(ric) //
89             .type(policyType) //
90             .lastModified(Instant.now()) //
91             .isTransient(false) //
92             .statusNotificationUri("statusNotificationUri") //
93             .build();
94
95     @Test
96     void serviceExpired_policyAndServiceAreDeletedInRepoAndPolicyIsDeletedInRic() {
97         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
98
99         setUpCreationOfA1Client();
100         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.just("Policy deleted"));
101
102         ServiceSupervision serviceSupervisionUnderTest =
103                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
104
105         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
106
107         serviceSupervisionUnderTest.checkAllServices().blockLast();
108
109         assertThat(policies.size()).isZero();
110         assertThat(services.size()).isZero();
111
112         verify(a1ClientMock).deletePolicy(policy);
113         verifyNoMoreInteractions(a1ClientMock);
114     }
115
116     @Test
117     void serviceExpiredButDeleteInRicFails_policyAndServiceAreDeletedInRepoAndErrorLoggedForRic() {
118         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
119
120         setUpCreationOfA1Client();
121         String originalErrorMessage = "Failed";
122         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.error(new Exception(originalErrorMessage)));
123
124         ServiceSupervision serviceSupervisionUnderTest =
125                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
126
127         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
128
129         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ServiceSupervision.class, WARN);
130
131         serviceSupervisionUnderTest.checkAllServices().blockLast();
132
133         assertThat(policies.size()).isZero();
134         assertThat(services.size()).isZero();
135
136         ILoggingEvent loggingEvent = logAppender.list.get(0);
137         assertThat(loggingEvent.getLevel()).isEqualTo(WARN);
138         String expectedLogMessage =
139                 "Could not delete policy: " + POLICY_ID + " from ric: " + RIC_NAME + ". Cause: " + originalErrorMessage;
140         assertThat(loggingEvent.getFormattedMessage()).isEqualTo(expectedLogMessage);
141     }
142
143     @Test
144     void serviceNotExpired_shouldNotBeChecked() {
145         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
146
147         ServiceSupervision serviceSupervisionUnderTest =
148                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
149
150         serviceSupervisionUnderTest.checkAllServices().blockLast();
151
152         assertThat(policies.size()).isEqualTo(1);
153         assertThat(services.size()).isEqualTo(1);
154
155         verifyNoInteractions(a1ClientFactoryMock);
156         verifyNoInteractions(a1ClientMock);
157     }
158
159     @Test
160     void serviceWithoutKeepAliveInterval_shouldNotBeChecked() {
161         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(0));
162
163         ServiceSupervision serviceSupervisionUnderTest =
164                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
165
166         serviceSupervisionUnderTest.checkAllServices().blockLast();
167
168         assertThat(policies.size()).isEqualTo(1);
169         assertThat(services.size()).isEqualTo(1);
170
171         verifyNoInteractions(a1ClientFactoryMock);
172         verifyNoInteractions(a1ClientMock);
173     }
174
175     private void setUpCreationOfA1Client() {
176         when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
177     }
178
179     private void setUpRepositoryWithKeepAliveInterval(Duration keepAliveInterval) {
180         ApplicationConfig appConfig = new ApplicationConfig();
181         services = new Services(appConfig);
182         service = new Service(SERVICE_NAME, keepAliveInterval, "callbackUrl");
183         services.put(service);
184         policies = new Policies(appConfig);
185         policies.put(policy);
186     }
187 }