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