2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.tasks;
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;
32 import ch.qos.logback.classic.spi.ILoggingEvent;
33 import ch.qos.logback.core.read.ListAppender;
35 import java.time.Duration;
36 import java.util.Collections;
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;
58 @ExtendWith(MockitoExtension.class)
59 class ServiceSupervisionTest {
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";
66 A1ClientFactory a1ClientFactoryMock;
68 A1Client a1ClientMock;
70 private Services services;
71 private Service service;
72 private Policies policies;
73 private RicConfig ricConfig = ImmutableRicConfig.builder() //
75 .baseUrl("baseUrl") //
76 .managedElementIds(Collections.emptyList()) //
77 .controllerName("") //
79 private Ric ric = new Ric(ricConfig);
80 private PolicyType policyType = ImmutablePolicyType.builder() //
81 .name("plicyTypeName") //
84 private Policy policy = ImmutablePolicy.builder() //
87 .ownerServiceName(SERVICE_NAME) //
90 .lastModified("lastModified") //
91 .isTransient(false) //
95 void serviceExpired_policyAndServiceAreDeletedInRepoAndPolicyIsDeletedInRic() {
96 setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
98 setUpCreationOfA1Client();
99 when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.just("Policy deleted"));
101 ServiceSupervision serviceSupervisionUnderTest =
102 new ServiceSupervision(services, policies, a1ClientFactoryMock);
104 await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
106 serviceSupervisionUnderTest.checkAllServices().blockLast();
108 assertThat(policies.size()).isEqualTo(0);
109 assertThat(services.size()).isEqualTo(0);
111 verify(a1ClientMock).deletePolicy(policy);
112 verifyNoMoreInteractions(a1ClientMock);
116 void serviceExpiredButDeleteInRicFails_policyAndServiceAreDeletedInRepoAndErrorLoggedForRic() {
117 setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
119 setUpCreationOfA1Client();
120 String originalErrorMessage = "Failed";
121 when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.error(new Exception(originalErrorMessage)));
123 ServiceSupervision serviceSupervisionUnderTest =
124 new ServiceSupervision(services, policies, a1ClientFactoryMock);
126 await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
128 final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ServiceSupervision.class, WARN);
130 serviceSupervisionUnderTest.checkAllServices().blockLast();
132 assertThat(policies.size()).isEqualTo(0);
133 assertThat(services.size()).isEqualTo(0);
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.toString().contains(expectedLogMessage)).isTrue();
143 void serviceNotExpired_shouldNotBeChecked() {
144 setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
146 ServiceSupervision serviceSupervisionUnderTest =
147 new ServiceSupervision(services, policies, a1ClientFactoryMock);
149 serviceSupervisionUnderTest.checkAllServices().blockLast();
151 assertThat(policies.size()).isEqualTo(1);
152 assertThat(services.size()).isEqualTo(1);
154 verifyNoInteractions(a1ClientFactoryMock);
155 verifyNoInteractions(a1ClientMock);
159 void serviceWithoutKeepAliveInterval_shouldNotBeChecked() {
160 setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(0));
162 ServiceSupervision serviceSupervisionUnderTest =
163 new ServiceSupervision(services, policies, a1ClientFactoryMock);
165 serviceSupervisionUnderTest.checkAllServices().blockLast();
167 assertThat(policies.size()).isEqualTo(1);
168 assertThat(services.size()).isEqualTo(1);
170 verifyNoInteractions(a1ClientFactoryMock);
171 verifyNoInteractions(a1ClientMock);
174 private void setUpCreationOfA1Client() {
175 when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
178 private void setUpRepositoryWithKeepAliveInterval(Duration keepAliveInterval) {
179 services = new Services();
180 service = new Service(SERVICE_NAME, keepAliveInterval, "callbackUrl");
181 services.put(service);
183 policies = new Policies();
184 policies.put(policy);