381c2d19a7a01ec6448b569449e05482af7ea39b
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / tasks / ServiceSupervisionTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.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 import java.time.Duration;
35 import java.util.Collections;
36 import org.awaitility.Durations;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.Mock;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.oransc.policyagent.clients.A1Client;
42 import org.oransc.policyagent.clients.A1ClientFactory;
43 import org.oransc.policyagent.configuration.ImmutableRicConfig;
44 import org.oransc.policyagent.configuration.RicConfig;
45 import org.oransc.policyagent.repository.ImmutablePolicy;
46 import org.oransc.policyagent.repository.ImmutablePolicyType;
47 import org.oransc.policyagent.repository.Policies;
48 import org.oransc.policyagent.repository.Policy;
49 import org.oransc.policyagent.repository.PolicyType;
50 import org.oransc.policyagent.repository.Ric;
51 import org.oransc.policyagent.repository.Service;
52 import org.oransc.policyagent.repository.Services;
53 import org.oransc.policyagent.utils.LoggingUtils;
54 import reactor.core.publisher.Mono;
55
56 @ExtendWith(MockitoExtension.class)
57 public 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 = ImmutableRicConfig.builder() //
72         .name(RIC_NAME) //
73         .baseUrl("baseUrl") //
74         .managedElementIds(Collections.emptyList()) //
75         .build();
76     private Ric ric = new Ric(ricConfig);
77     private PolicyType policyType = ImmutablePolicyType.builder() //
78         .name("plicyTypeName") //
79         .schema("schema") //
80         .build();
81     private Policy policy = ImmutablePolicy.builder() //
82         .id(POLICY_ID) //
83         .json("json") //
84         .ownerServiceName(SERVICE_NAME) //
85         .ric(ric) //
86         .type(policyType) //
87         .lastModified("lastModified") //
88         .build();
89
90     @Test
91     public void serviceExpired_policyAndServiceAreDeletedInRepoAndPolicyIsDeletedInRic() {
92         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
93
94         setUpCreationOfA1Client();
95         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.just("Policy deleted"));
96
97         ServiceSupervision serviceSupervisionUnderTest =
98             new ServiceSupervision(services, policies, a1ClientFactoryMock);
99
100         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
101
102         serviceSupervisionUnderTest.checkAllServices();
103
104         assertThat(policies.size()).isEqualTo(0);
105         assertThat(services.size()).isEqualTo(0);
106
107         verify(a1ClientMock).deletePolicy(policy);
108         verifyNoMoreInteractions(a1ClientMock);
109     }
110
111     @Test
112     public void serviceExpiredButDeleteInRicFails_policyAndServiceAreDeletedInRepoAndErrorLoggedForRic() {
113         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
114
115         setUpCreationOfA1Client();
116         String originalErrorMessage = "Failed";
117         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.error(new Exception(originalErrorMessage)));
118
119         ServiceSupervision serviceSupervisionUnderTest =
120             new ServiceSupervision(services, policies, a1ClientFactoryMock);
121
122         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
123
124         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ServiceSupervision.class, WARN);
125
126         serviceSupervisionUnderTest.checkAllServices();
127
128         assertThat(policies.size()).isEqualTo(0);
129         assertThat(services.size()).isEqualTo(0);
130
131         ILoggingEvent loggingEvent = logAppender.list.get(0);
132         assertThat(loggingEvent.getThrowableProxy().getMessage()).isEqualTo(originalErrorMessage);
133         String expectedLogMessage = "Could not delete policy: " + POLICY_ID + " from ric: " + RIC_NAME;
134         assertThat(loggingEvent.toString().contains(expectedLogMessage)).isTrue();
135     }
136
137     @Test
138     public void serviceNotExpired_shouldNotBeChecked() {
139         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
140
141         ServiceSupervision serviceSupervisionUnderTest =
142             new ServiceSupervision(services, policies, a1ClientFactoryMock);
143
144         serviceSupervisionUnderTest.checkAllServices();
145
146         assertThat(policies.size()).isEqualTo(1);
147         assertThat(services.size()).isEqualTo(1);
148
149         verifyNoInteractions(a1ClientFactoryMock);
150         verifyNoInteractions(a1ClientMock);
151     }
152
153     @Test
154     public void serviceWithoutKeepAliveInterval_shouldNotBeChecked() {
155         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(0));
156
157         ServiceSupervision serviceSupervisionUnderTest =
158             new ServiceSupervision(services, policies, a1ClientFactoryMock);
159
160         serviceSupervisionUnderTest.checkAllServices();
161
162         assertThat(policies.size()).isEqualTo(1);
163         assertThat(services.size()).isEqualTo(1);
164
165         verifyNoInteractions(a1ClientFactoryMock);
166         verifyNoInteractions(a1ClientMock);
167     }
168
169     private void setUpCreationOfA1Client() {
170         when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
171     }
172
173     private void setUpRepositoryWithKeepAliveInterval(Duration keepAliveInterval) {
174         services = new Services();
175         service = new Service(SERVICE_NAME, keepAliveInterval, "callbackUrl");
176         services.put(service);
177
178         policies = new Policies();
179         policies.put(policy);
180     }
181 }