Changed in config will add and recover Rics
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / tasks / RepositorySupervisionTest.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 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;
28
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Vector;
32
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;
52
53 @ExtendWith(MockitoExtension.class)
54 @RunWith(MockitoJUnitRunner.class)
55 public class RepositorySupervisionTest {
56     @Mock
57     A1Client a1ClientMock;
58
59     @Test
60     public void test() {
61         Ric ric1 = new Ric(ImmutableRicConfig.builder() //
62             .name("ric1") //
63             .baseUrl("baseUrl1") //
64             .managedElementIds(new Vector<String>(Arrays.asList("kista_1", "kista_2"))) //
65             .build());
66         ric1.setState(Ric.RicState.IDLE);
67         Ric ric2 = new Ric(ImmutableRicConfig.builder() //
68             .name("ric2") //
69             .baseUrl("baseUrl2") //
70             .managedElementIds(new Vector<String>(Arrays.asList("kista_3", "kista_4"))) //
71             .build());
72         ric2.setState(Ric.RicState.UNDEFINED);
73         Ric ric3 = new Ric(ImmutableRicConfig.builder() //
74             .name("ric3") //
75             .baseUrl("baseUrl3") //
76             .managedElementIds(new Vector<String>(Arrays.asList("kista_5"))) //
77             .build());
78         Rics rics = new Rics();
79         rics.put(ric1);
80         rics.put(ric2);
81         rics.put(ric3);
82
83         PolicyType policyType = ImmutablePolicyType.builder() //
84             .name("type") //
85             .schema("") //
86             .build();
87         Policy policy1 = ImmutablePolicy.builder() //
88             .id("policyId1") //
89             .json("") //
90             .ownerServiceName("service") //
91             .ric(ric1) //
92             .type(policyType) //
93             .lastModified("now") //
94             .build();
95         Policies policies = new Policies();
96         policies.put(policy1);
97         PolicyTypes types = new PolicyTypes();
98         Services services = new Services();
99
100         RepositorySupervision supervisorUnderTest =
101             new RepositorySupervision(rics, policies, a1ClientMock, types, services);
102
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"));
108
109         supervisorUnderTest.checkAllRics();
110
111         await().untilAsserted(() -> RicState.IDLE.equals(ric1.state()));
112         await().untilAsserted(() -> RicState.IDLE.equals(ric2.state()));
113         await().untilAsserted(() -> RicState.IDLE.equals(ric3.state()));
114
115         verify(a1ClientMock).deletePolicy("baseUrl1", "policyId2");
116         verify(a1ClientMock).deletePolicy("baseUrl2", "policyId2");
117         verifyNoMoreInteractions(a1ClientMock);
118     }
119 }