2  * ========================LICENSE_START=================================
 
   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
 
  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.onap.ccsdk.oran.a1policymanagementservice.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.time.Instant;
 
  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;
 
  57 @ExtendWith(MockitoExtension.class)
 
  58 class ServiceSupervisionTest {
 
  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";
 
  65     A1ClientFactory a1ClientFactoryMock;
 
  67     A1Client a1ClientMock;
 
  69     private Services services;
 
  70     private Service service;
 
  71     private Policies policies;
 
  72     private RicConfig ricConfig = RicConfig.builder() //
 
  74             .baseUrl("baseUrl") //
 
  76     private Ric ric = new Ric(ricConfig);
 
  77     private PolicyType policyType = PolicyType.builder() //
 
  78             .id("policyTypeName") //
 
  81     private Policy policy = Policy.builder() //
 
  84             .ownerServiceId(SERVICE_NAME) //
 
  87             .lastModified(Instant.now()) //
 
  88             .isTransient(false) //
 
  89             .statusNotificationUri("statusNotificationUri") //
 
  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));
 
  97         setUpCreationOfA1Client();
 
  98         when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.just("Policy deleted"));
 
 100         ServiceSupervision serviceSupervisionUnderTest =
 
 101                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
 
 103         await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);
 
 105         serviceSupervisionUnderTest.checkAllServices().blockLast();
 
 107         assertThat(policies.size()).isZero();
 
 108         assertThat(services.size()).isZero();
 
 110         verify(a1ClientMock).deletePolicy(policy);
 
 111         verifyNoMoreInteractions(a1ClientMock);
 
 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));
 
 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()).isZero();
 
 133         assertThat(services.size()).isZero();
 
 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);
 
 143     @DisplayName("test service Not Expired should Not Be Checked")
 
 144     void serviceNotExpired_shouldNotBeChecked() {
 
 145         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));
 
 147         ServiceSupervision serviceSupervisionUnderTest =
 
 148                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
 
 150         serviceSupervisionUnderTest.checkAllServices().blockLast();
 
 152         assertThat(policies.size()).isEqualTo(1);
 
 153         assertThat(services.size()).isEqualTo(1);
 
 155         verifyNoInteractions(a1ClientFactoryMock);
 
 156         verifyNoInteractions(a1ClientMock);
 
 160     @DisplayName("test service Without Keep Alive Interval should Not Be Checked")
 
 161     void serviceWithoutKeepAliveInterval_shouldNotBeChecked() {
 
 162         setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(0));
 
 164         ServiceSupervision serviceSupervisionUnderTest =
 
 165                 new ServiceSupervision(services, policies, a1ClientFactoryMock);
 
 167         serviceSupervisionUnderTest.checkAllServices().blockLast();
 
 169         assertThat(policies.size()).isEqualTo(1);
 
 170         assertThat(services.size()).isEqualTo(1);
 
 172         verifyNoInteractions(a1ClientFactoryMock);
 
 173         verifyNoInteractions(a1ClientMock);
 
 176     private void setUpCreationOfA1Client() {
 
 177         when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
 
 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);