e8f28195ddb622c11035a3af00eee4a49ab2b6f4
[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.DisplayName;
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.RicConfig;
48 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
49 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
50 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
51 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
52 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
53 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
54 import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
55 import reactor.core.publisher.Mono;
56
57 @ExtendWith(MockitoExtension.class)
58 class ServiceSupervisionTest {
59
60     private static final String SERVICE_NAME = "Service name";
61     private static final String RIC_NAME = "name";
62     private static final String POLICY_ID = "policy";
63
64     @Mock
65     A1ClientFactory a1ClientFactoryMock;
66     @Mock
67     A1Client a1ClientMock;
68
69     private Services services;
70     private Service service;
71     private Policies policies;
72     private RicConfig ricConfig = RicConfig.builder() //
73             .ricId(RIC_NAME) //
74             .baseUrl("baseUrl") //
75             .build();
76     private Ric ric = new Ric(ricConfig);
77     private PolicyType policyType = PolicyType.builder() //
78             .id("policyTypeName") //
79             .schema("schema") //
80             .build();
81     private Policy policy = Policy.builder() //
82             .id(POLICY_ID) //
83             .json("json") //
84             .ownerServiceId(SERVICE_NAME) //
85             .ric(ric) //
86             .type(policyType) //
87             .lastModified(Instant.now()) //
88             .isTransient(false) //
89             .statusNotificationUri("statusNotificationUri") //
90             .build();
91
92     @Test
93     @DisplayName("test service Expired policy And Service Are Deleted In Repo And Policy Is Deleted In Ric")
94     void serviceExpired_policyAndServiceAreDeletedInRepoAndPolicyIsDeletedInRic() {
95         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
96
97         setUpCreationOfA1Client();
98         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.just("Policy deleted"));
99
100         ServiceSupervision serviceSupervisionUnderTest =
101                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
102
103         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
104
105         serviceSupervisionUnderTest.checkAllServices().blockLast();
106
107         assertThat(policies.size()).isZero();
108         assertThat(services.size()).isZero();
109
110         verify(a1ClientMock).deletePolicy(policy);
111         verifyNoMoreInteractions(a1ClientMock);
112     }
113
114     @Test
115     @DisplayName("test service Expired But Delete In Ric Fails policy And Service Are Deleted In Repo And Error Logged For Ric")
116     void serviceExpiredButDeleteInRicFails_policyAndServiceAreDeletedInRepoAndErrorLoggedForRic() {
117         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
118
119         setUpCreationOfA1Client();
120         String originalErrorMessage = "Failed";
121         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.error(new Exception(originalErrorMessage)));
122
123         ServiceSupervision serviceSupervisionUnderTest =
124                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
125
126         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
127
128         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ServiceSupervision.class, WARN);
129
130         serviceSupervisionUnderTest.checkAllServices().blockLast();
131
132         assertThat(policies.size()).isZero();
133         assertThat(services.size()).isZero();
134
135         ILoggingEvent loggingEvent = logAppender.list.get(0);
136         assertThat(loggingEvent.getLevel()).isEqualTo(WARN);
137         String expectedLogMessage =
138                 "Could not delete policy: " + POLICY_ID + " from ric: " + RIC_NAME + ". Cause: " + originalErrorMessage;
139         assertThat(loggingEvent.getFormattedMessage()).isEqualTo(expectedLogMessage);
140     }
141
142     @Test
143     @DisplayName("test service Not Expired should Not Be Checked")
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     @DisplayName("test service Without Keep Alive Interval should Not Be Checked")
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         ApplicationConfig appConfig = new ApplicationConfig();
182         services = new Services(appConfig);
183         service = new Service(SERVICE_NAME, keepAliveInterval, "callbackUrl");
184         services.put(service);
185         policies = new Policies(appConfig);
186         policies.put(policy);
187     }
188 }