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 org.awaitility.Awaitility.await;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.verifyNoMoreInteractions;
27 import static org.mockito.Mockito.when;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Vector;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.oransc.policyagent.clients.A1Client;
40 import org.oransc.policyagent.configuration.ImmutableRicConfig;
41 import org.oransc.policyagent.repository.ImmutablePolicy;
42 import org.oransc.policyagent.repository.ImmutablePolicyType;
43 import org.oransc.policyagent.repository.Policies;
44 import org.oransc.policyagent.repository.Policy;
45 import org.oransc.policyagent.repository.PolicyType;
46 import org.oransc.policyagent.repository.PolicyTypes;
47 import org.oransc.policyagent.repository.Ric;
48 import org.oransc.policyagent.repository.Ric.RicState;
49 import org.oransc.policyagent.repository.Rics;
50 import org.oransc.policyagent.repository.Services;
51 import reactor.core.publisher.Mono;
53 @ExtendWith(MockitoExtension.class)
54 @RunWith(MockitoJUnitRunner.class)
55 public class RepositorySupervisionTest {
57 A1Client a1ClientMock;
61 Ric ric1 = new Ric(ImmutableRicConfig.builder() //
63 .baseUrl("baseUrl1") //
64 .managedElementIds(new Vector<String>(Arrays.asList("kista_1", "kista_2"))) //
66 ric1.setState(Ric.RicState.IDLE);
67 Ric ric2 = new Ric(ImmutableRicConfig.builder() //
69 .baseUrl("baseUrl2") //
70 .managedElementIds(new Vector<String>(Arrays.asList("kista_3", "kista_4"))) //
72 ric2.setState(Ric.RicState.UNDEFINED);
73 Ric ric3 = new Ric(ImmutableRicConfig.builder() //
75 .baseUrl("baseUrl3") //
76 .managedElementIds(new Vector<String>(Arrays.asList("kista_5"))) //
78 Rics rics = new Rics();
83 PolicyType policyType = ImmutablePolicyType.builder() //
87 Policy policy1 = ImmutablePolicy.builder() //
90 .ownerServiceName("service") //
93 .lastModified("now") //
95 Policies policies = new Policies();
96 policies.put(policy1);
97 PolicyTypes types = new PolicyTypes();
98 Services services = new Services();
100 RepositorySupervision supervisorUnderTest =
101 new RepositorySupervision(rics, policies, a1ClientMock, types, services);
103 Mono<Collection<String>> policyIds = Mono.just(Arrays.asList("policyId1", "policyId2"));
104 when(a1ClientMock.getPolicyIdentities(anyString())).thenReturn(policyIds);
105 when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.empty());
106 when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(policyIds);
107 when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("schema"));
109 supervisorUnderTest.checkAllRics();
111 await().untilAsserted(() -> RicState.IDLE.equals(ric1.state()));
112 await().untilAsserted(() -> RicState.IDLE.equals(ric2.state()));
113 await().untilAsserted(() -> RicState.IDLE.equals(ric3.state()));
115 verify(a1ClientMock).deletePolicy("baseUrl1", "policyId2");
116 verify(a1ClientMock).deletePolicy("baseUrl2", "policyId2");
117 verifyNoMoreInteractions(a1ClientMock);