aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/ServiceSupervisionTest.java
blob: 294b220c6c9bbe19383997bebc5162c3c741a3d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*-
 * ========================LICENSE_START=================================
 * ONAP : ccsdk oran
 * ======================================================================
 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
 * ======================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================LICENSE_END===================================
 */

package org.onap.ccsdk.oran.a1policymanagementservice.tasks;

import static ch.qos.logback.classic.Level.WARN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;

import java.time.Duration;
import java.time.Instant;
import java.util.Collections;

import org.awaitility.Durations;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1Client;
import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
import org.onap.ccsdk.oran.a1policymanagementservice.utils.LoggingUtils;
import reactor.core.publisher.Mono;

@ExtendWith(MockitoExtension.class)
class ServiceSupervisionTest {

    private static final String SERVICE_NAME = "Service name";
    private static final String RIC_NAME = "name";
    private static final String POLICY_ID = "policy";

    @Mock
    A1ClientFactory a1ClientFactoryMock;
    @Mock
    A1Client a1ClientMock;

    private Services services;
    private Service service;
    private Policies policies;
    private RicConfig ricConfig = ImmutableRicConfig.builder() //
            .ricId(RIC_NAME) //
            .baseUrl("baseUrl") //
            .managedElementIds(Collections.emptyList()) //
            .controllerName("") //
            .build();
    private Ric ric = new Ric(ricConfig);
    private PolicyType policyType = PolicyType.builder() //
            .id("policyTypeName") //
            .schema("schema") //
            .build();
    private Policy policy = Policy.builder() //
            .id(POLICY_ID) //
            .json("json") //
            .ownerServiceId(SERVICE_NAME) //
            .ric(ric) //
            .type(policyType) //
            .lastModified(Instant.now()) //
            .isTransient(false) //
            .statusNotificationUri("statusNotificationUri") //
            .build();

    @Test
    void serviceExpired_policyAndServiceAreDeletedInRepoAndPolicyIsDeletedInRic() {
        setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));

        setUpCreationOfA1Client();
        when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.just("Policy deleted"));

        ServiceSupervision serviceSupervisionUnderTest =
                new ServiceSupervision(services, policies, a1ClientFactoryMock);

        await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);

        serviceSupervisionUnderTest.checkAllServices().blockLast();

        assertThat(policies.size()).isZero();
        assertThat(services.size()).isZero();

        verify(a1ClientMock).deletePolicy(policy);
        verifyNoMoreInteractions(a1ClientMock);
    }

    @Test
    void serviceExpiredButDeleteInRicFails_policyAndServiceAreDeletedInRepoAndErrorLoggedForRic() {
        setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));

        setUpCreationOfA1Client();
        String originalErrorMessage = "Failed";
        when(a1ClientMock.deletePolicy(any(Policy.class))).thenReturn(Mono.error(new Exception(originalErrorMessage)));

        ServiceSupervision serviceSupervisionUnderTest =
                new ServiceSupervision(services, policies, a1ClientFactoryMock);

        await().atMost(Durations.FIVE_SECONDS).with().pollInterval(Durations.ONE_SECOND).until(service::isExpired);

        final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(ServiceSupervision.class, WARN);

        serviceSupervisionUnderTest.checkAllServices().blockLast();

        assertThat(policies.size()).isZero();
        assertThat(services.size()).isZero();

        ILoggingEvent loggingEvent = logAppender.list.get(0);
        assertThat(loggingEvent.getLevel()).isEqualTo(WARN);
        String expectedLogMessage =
                "Could not delete policy: " + POLICY_ID + " from ric: " + RIC_NAME + ". Cause: " + originalErrorMessage;
        assertThat(loggingEvent.getFormattedMessage()).isEqualTo(expectedLogMessage);
    }

    @Test
    void serviceNotExpired_shouldNotBeChecked() {
        setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(2));

        ServiceSupervision serviceSupervisionUnderTest =
                new ServiceSupervision(services, policies, a1ClientFactoryMock);

        serviceSupervisionUnderTest.checkAllServices().blockLast();

        assertThat(policies.size()).isEqualTo(1);
        assertThat(services.size()).isEqualTo(1);

        verifyNoInteractions(a1ClientFactoryMock);
        verifyNoInteractions(a1ClientMock);
    }

    @Test
    void serviceWithoutKeepAliveInterval_shouldNotBeChecked() {
        setUpRepositoryWithKeepAliveInterval(Duration.ofSeconds(0));

        ServiceSupervision serviceSupervisionUnderTest =
                new ServiceSupervision(services, policies, a1ClientFactoryMock);

        serviceSupervisionUnderTest.checkAllServices().blockLast();

        assertThat(policies.size()).isEqualTo(1);
        assertThat(services.size()).isEqualTo(1);

        verifyNoInteractions(a1ClientFactoryMock);
        verifyNoInteractions(a1ClientMock);
    }

    private void setUpCreationOfA1Client() {
        when(a1ClientFactoryMock.createA1Client(any(Ric.class))).thenReturn(Mono.just(a1ClientMock));
    }

    private void setUpRepositoryWithKeepAliveInterval(Duration keepAliveInterval) {
        ApplicationConfig appConfig = new ApplicationConfig();
        services = new Services(appConfig);
        service = new Service(SERVICE_NAME, keepAliveInterval, "callbackUrl");
        services.put(service);
        policies = new Policies(appConfig);
        policies.put(policy);
    }
}