c8476221d9200b1f96c13c338c2ab58600be0bcc
[ccsdk/oran.git] /
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.ImmutableRicConfig;
47 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
48 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
49 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
50 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
51 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
52 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
53 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
54 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
55 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
56 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
57 import reactor.core.publisher.Mono;
58
59 @ExtendWith(MockitoExtension.class)
60 class ServiceSupervisionTest {
61
62     private static final String SERVICE_NAME = "Service name";
63     private static final String RIC_NAME = "name";
64     private static final String POLICY_ID = "policy";
65
66     @Mock
67     A1ClientFactory a1ClientFactoryMock;
68     @Mock
69     A1Client a1ClientMock;
70
71     private Services services;
72     private Service service;
73     private Policies policies;
74     private RicConfig ricConfig = ImmutableRicConfig.builder() //
75             .ricId(RIC_NAME) //
76             .baseUrl("baseUrl") //
77             .managedElementIds(Collections.emptyList()) //
78             .controllerName("") //
79             .build();
80     private Ric ric = new Ric(ricConfig);
81     private PolicyType policyType = ImmutablePolicyType.builder() //
82             .id("policyTypeName") //
83             .schema("schema") //
84             .build();
85     private Policy policy = ImmutablePolicy.builder() //
86             .id(POLICY_ID) //
87             .json("json") //
88             .ownerServiceId(SERVICE_NAME) //
89             .ric(ric) //
90             .type(policyType) //
91             .lastModified(Instant.now()) //
92             .isTransient(false) //
93             .statusNotificationUri("statusNotificationUri") //
94             .build();
95
96     @Test
97     void serviceExpired_policyAndServiceAreDeletedInRepoAndPolicyIsDeletedInRic() {
98         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
99
100         setUpCreationOfA1Client();
101         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.just("Policy deleted"));
102
103         ServiceSupervision serviceSupervisionUnderTest =
104                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
105
106         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
107
108         serviceSupervisionUnderTest.checkAllServices().blockLast();
109
110         assertThat(policies.size()).isZero();
111         assertThat(services.size()).isZero();
112
113         verify(a1ClientMock).deletePolicy(policy);
114         verifyNoMoreInteractions(a1ClientMock);
115     }
116
117     @Test
118     void serviceExpiredButDeleteInRicFails_policyAndServiceAreDeletedInRepoAndErrorLoggedForRic() {
119         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
120
121         setUpCreationOfA1Client();
122         String originalErrorMessage = "Failed";
123         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.error(new Exception(originalErrorMessage)));
124
125         ServiceSupervision serviceSupervisionUnderTest =
126                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
127
128         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
129
130         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ServiceSupervision.class, WARN);
131
132         serviceSupervisionUnderTest.checkAllServices().blockLast();
133
134         assertThat(policies.size()).isZero();
135         assertThat(services.size()).isZero();
136
137         ILoggingEvent loggingEvent = logAppender.list.get(0);
138         assertThat(loggingEvent.getLevel()).isEqualTo(WARN);
139         String expectedLogMessage =
140                 "Could not delete policy: " + POLICY_ID + " from ric: " + RIC_NAME + ". Cause: " + originalErrorMessage;
141         assertThat(loggingEvent.getFormattedMessage()).isEqualTo(expectedLogMessage);
142     }
143
144     @Test
145     void serviceNotExpired_shouldNotBeChecked() {
146         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
147
148         ServiceSupervision serviceSupervisionUnderTest =
149                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
150
151         serviceSupervisionUnderTest.checkAllServices().blockLast();
152
153         assertThat(policies.size()).isEqualTo(1);
154         assertThat(services.size()).isEqualTo(1);
155
156         verifyNoInteractions(a1ClientFactoryMock);
157         verifyNoInteractions(a1ClientMock);
158     }
159
160     @Test
161     void serviceWithoutKeepAliveInterval_shouldNotBeChecked() {
162         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(0));
163
164         ServiceSupervision serviceSupervisionUnderTest =
165                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
166
167         serviceSupervisionUnderTest.checkAllServices().blockLast();
168
169         assertThat(policies.size()).isEqualTo(1);
170         assertThat(services.size()).isEqualTo(1);
171
172         verifyNoInteractions(a1ClientFactoryMock);
173         verifyNoInteractions(a1ClientMock);
174     }
175
176     private void setUpCreationOfA1Client() {
177         when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
178     }
179
180     private void setUpRepositoryWithKeepAliveInterval(Duration keepAliveInterval) {
181         services = new Services();
182         service = new Service(SERVICE_NAME, keepAliveInterval, "callbackUrl");
183         services.put(service);
184
185         policies = new Policies();
186         policies.put(policy);
187     }
188 }