Added a test for service supervision
[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
35 import java.time.Duration;
36 import java.util.Collections;
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.oransc.policyagent.clients.A1Client;
44 import org.oransc.policyagent.clients.A1ClientFactory;
45 import org.oransc.policyagent.configuration.ImmutableRicConfig;
46 import org.oransc.policyagent.configuration.RicConfig;
47 import org.oransc.policyagent.repository.ImmutablePolicy;
48 import org.oransc.policyagent.repository.ImmutablePolicyType;
49 import org.oransc.policyagent.repository.Policies;
50 import org.oransc.policyagent.repository.Policy;
51 import org.oransc.policyagent.repository.PolicyType;
52 import org.oransc.policyagent.repository.Ric;
53 import org.oransc.policyagent.repository.Service;
54 import org.oransc.policyagent.repository.Services;
55 import org.oransc.policyagent.utils.LoggingUtils;
56 import reactor.core.publisher.Mono;
57
58 @ExtendWith(MockitoExtension.class)
59 public 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         .name(RIC_NAME) //
75         .baseUrl("baseUrl") //
76         .managedElementIds(Collections.emptyList()) //
77         .build();
78     private Ric ric = new Ric(ricConfig);
79     private PolicyType policyType = ImmutablePolicyType.builder() //
80         .name("plicyTypeName") //
81         .schema("schema") //
82         .build();
83     private Policy policy = ImmutablePolicy.builder() //
84         .id(POLICY_ID) //
85         .json("json") //
86         .ownerServiceName(SERVICE_NAME) //
87         .ric(ric) //
88         .type(policyType) //
89         .lastModified("lastModified") //
90         .build();
91
92     @Test
93     public void serviceExpired_policyAndServiceAreDeletedInRepoAndPolicyIsDeletedInRic() {
94         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
95
96         setUpCreationOfA1Client();
97         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.just("Policy deleted"));
98
99         ServiceSupervision serviceSupervisionUnderTest =
100             new ServiceSupervision(services, policies, a1ClientFactoryMock);
101
102         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
103
104         serviceSupervisionUnderTest.checkAllServices().blockLast();
105
106         assertThat(policies.size()).isEqualTo(0);
107         assertThat(services.size()).isEqualTo(0);
108
109         verify(a1ClientMock).deletePolicy(policy);
110         verifyNoMoreInteractions(a1ClientMock);
111     }
112
113     @Test
114     public void serviceExpiredButDeleteInRicFails_policyAndServiceAreDeletedInRepoAndErrorLoggedForRic() {
115         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
116
117         setUpCreationOfA1Client();
118         String originalErrorMessage = "Failed";
119         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.error(new Exception(originalErrorMessage)));
120
121         ServiceSupervision serviceSupervisionUnderTest =
122             new ServiceSupervision(services, policies, a1ClientFactoryMock);
123
124         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
125
126         final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ServiceSupervision.class, WARN);
127
128         serviceSupervisionUnderTest.checkAllServices().blockLast();
129
130         assertThat(policies.size()).isEqualTo(0);
131         assertThat(services.size()).isEqualTo(0);
132
133         ILoggingEvent loggingEvent = logAppender.list.get(0);
134         assertThat(loggingEvent.getThrowableProxy().getMessage()).isEqualTo(originalErrorMessage);
135         String expectedLogMessage = "Could not delete policy: " + POLICY_ID + " from ric: " + RIC_NAME;
136         assertThat(loggingEvent.toString().contains(expectedLogMessage)).isTrue();
137     }
138
139     @Test
140     public 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     public 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         services = new Services();
177         service = new Service(SERVICE_NAME, keepAliveInterval, "callbackUrl");
178         services.put(service);
179
180         policies = new Policies();
181         policies.put(policy);
182     }
183 }