729fc7b3b71764d756d8a9b799b3630e11fea94f
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / tasks / StartupServiceTest.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.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 import static org.oransc.policyagent.repository.Ric.RicState.IDLE;
33
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Vector;
37
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.extension.ExtendWith;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.mockito.junit.jupiter.MockitoExtension;
44 import org.oransc.policyagent.clients.A1Client;
45 import org.oransc.policyagent.configuration.ApplicationConfig;
46 import org.oransc.policyagent.configuration.ImmutableRicConfig;
47 import org.oransc.policyagent.configuration.RicConfig;
48 import org.oransc.policyagent.repository.Policies;
49 import org.oransc.policyagent.repository.PolicyTypes;
50 import org.oransc.policyagent.repository.Ric;
51 import org.oransc.policyagent.repository.Ric.RicState;
52 import org.oransc.policyagent.repository.Rics;
53 import org.oransc.policyagent.repository.Services;
54 import reactor.core.publisher.Mono;
55
56 @ExtendWith(MockitoExtension.class)
57 @RunWith(MockitoJUnitRunner.class)
58 public class StartupServiceTest {
59     private static final String FIRST_RIC_NAME = "first";
60     private static final String FIRST_RIC_URL = "firstUrl";
61     private static final String SECOND_RIC_NAME = "second";
62     private static final String SECOND_RIC_URL = "secondUrl";
63     private static final String MANAGED_NODE_A = "nodeA";
64     private static final String MANAGED_NODE_B = "nodeB";
65     private static final String MANAGED_NODE_C = "nodeC";
66
67     private static final String POLICY_TYPE_1_NAME = "type1";
68     private static final String POLICY_TYPE_2_NAME = "type2";
69     private static final String POLICY_ID_1 = "policy1";
70     private static final String POLICY_ID_2 = "policy2";
71
72     @Mock
73     ApplicationConfig appConfigMock;
74
75     @Mock
76     A1Client a1ClientMock;
77
78     @Test
79     public void startup_allOk() {
80         Mono<Collection<String>> policyTypes1 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME));
81         Mono<Collection<String>> policyTypes2 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME));
82         when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(policyTypes1).thenReturn(policyTypes2);
83         Mono<Collection<String>> policies = Mono.just(Arrays.asList(POLICY_ID_1, POLICY_ID_2));
84         when(a1ClientMock.getPolicyIdentities(anyString())).thenReturn(policies);
85         when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema"));
86         when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.just("OK"));
87
88         Rics rics = new Rics();
89         PolicyTypes policyTypes = new PolicyTypes();
90         StartupService serviceUnderTest =
91             new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies(), new Services());
92
93         serviceUnderTest.startup();
94
95         serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
96             ApplicationConfig.RicConfigUpdate.ADDED);
97         serviceUnderTest.onRicConfigUpdate(
98             getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C),
99             ApplicationConfig.RicConfigUpdate.ADDED);
100
101         await().untilAsserted(() -> assertThat(policyTypes.size()).isEqualTo(2));
102
103         verify(a1ClientMock).getPolicyTypeIdentities(FIRST_RIC_URL);
104         verify(a1ClientMock).deletePolicy(FIRST_RIC_URL, POLICY_ID_1);
105         verify(a1ClientMock).deletePolicy(FIRST_RIC_URL, POLICY_ID_2);
106
107         verify(a1ClientMock).getPolicyTypeIdentities(SECOND_RIC_URL);
108         verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
109         verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
110
111         assertTrue(policyTypes.contains(POLICY_TYPE_1_NAME), POLICY_TYPE_1_NAME + " not added to PolicyTypes.");
112         assertTrue(policyTypes.contains(POLICY_TYPE_2_NAME), POLICY_TYPE_2_NAME + " not added to PolicyTypes.");
113         assertEquals(2, rics.size(), "Correct number of Rics not added to Rics");
114
115         Ric firstRic = rics.get(FIRST_RIC_NAME);
116         assertNotNull(firstRic, "Ric " + FIRST_RIC_NAME + " not added to repository");
117         assertEquals(FIRST_RIC_NAME, firstRic.name(), FIRST_RIC_NAME + " not added to Rics");
118         assertEquals(IDLE, firstRic.state(), "Not correct state for ric " + FIRST_RIC_NAME);
119         assertEquals(1, firstRic.getSupportedPolicyTypes().size(),
120             "Not correct no of types supported for ric " + FIRST_RIC_NAME);
121         assertTrue(firstRic.isSupportingType(POLICY_TYPE_1_NAME),
122             POLICY_TYPE_1_NAME + " not supported by ric " + FIRST_RIC_NAME);
123         assertEquals(1, firstRic.getManagedElementIds().size(),
124             "Not correct no of managed nodes for ric " + FIRST_RIC_NAME);
125         assertTrue(firstRic.isManaging(MANAGED_NODE_A), MANAGED_NODE_A + " not managed by ric " + FIRST_RIC_NAME);
126
127         Ric secondRic = rics.get(SECOND_RIC_NAME);
128         assertNotNull(secondRic, "Ric " + SECOND_RIC_NAME + " not added to repository");
129         assertEquals(SECOND_RIC_NAME, secondRic.name(), SECOND_RIC_NAME + " not added to Rics");
130         assertEquals(IDLE, secondRic.state(), "Not correct state for " + SECOND_RIC_NAME);
131         assertEquals(2, secondRic.getSupportedPolicyTypes().size(),
132             "Not correct no of types supported for ric " + SECOND_RIC_NAME);
133         assertTrue(secondRic.isSupportingType(POLICY_TYPE_1_NAME),
134             POLICY_TYPE_1_NAME + " not supported by ric " + SECOND_RIC_NAME);
135         assertTrue(secondRic.isSupportingType(POLICY_TYPE_2_NAME),
136             POLICY_TYPE_2_NAME + " not supported by ric " + SECOND_RIC_NAME);
137         assertEquals(2, secondRic.getManagedElementIds().size(),
138             "Not correct no of managed nodes for ric " + SECOND_RIC_NAME);
139         assertTrue(secondRic.isManaging(MANAGED_NODE_B), MANAGED_NODE_B + " not managed by ric " + SECOND_RIC_NAME);
140         assertTrue(secondRic.isManaging(MANAGED_NODE_C), MANAGED_NODE_C + " not managed by ric " + SECOND_RIC_NAME);
141     }
142
143     @Test
144     public void startup_unableToConnectToGetTypes() {
145         Mono<Collection<String>> policyIdentities = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME));
146         Mono<?> error = Mono.error(new Exception("Unable to contact ric."));
147         doReturn(error, policyIdentities).when(a1ClientMock).getPolicyTypeIdentities(anyString());
148
149         Mono<Collection<String>> policies = Mono.just(Arrays.asList(POLICY_ID_1, POLICY_ID_2));
150         doReturn(policies).when(a1ClientMock).getPolicyIdentities(anyString());
151         when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema"));
152         when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.just("OK"));
153
154         Rics rics = new Rics();
155         PolicyTypes policyTypes = new PolicyTypes();
156         StartupService serviceUnderTest =
157             new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies(), new Services());
158
159         serviceUnderTest.startup();
160         serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
161             ApplicationConfig.RicConfigUpdate.ADDED);
162         serviceUnderTest.onRicConfigUpdate(
163             getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C),
164             ApplicationConfig.RicConfigUpdate.ADDED);
165
166         verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
167         verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
168
169         assertEquals(RicState.UNDEFINED, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
170
171         assertEquals(IDLE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME);
172     }
173
174     @Test
175     public void startup_unableToConnectToGetPolicies() {
176
177         Mono<Collection<String>> policyTypes1 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME));
178         Mono<Collection<String>> policyTypes2 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME));
179         when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(policyTypes1).thenReturn(policyTypes2);
180         when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema"));
181         Mono<Collection<String>> policies = Mono.just(Arrays.asList(POLICY_ID_1, POLICY_ID_2));
182         doReturn(Mono.error(new Exception("Unable to contact ric.")), policies).when(a1ClientMock)
183             .getPolicyIdentities(anyString());
184         when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.just("OK"));
185
186         Rics rics = new Rics();
187         PolicyTypes policyTypes = new PolicyTypes();
188         StartupService serviceUnderTest =
189             new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies(), new Services());
190
191         serviceUnderTest.startup();
192         serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
193             ApplicationConfig.RicConfigUpdate.ADDED);
194         serviceUnderTest.onRicConfigUpdate(
195             getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C),
196             ApplicationConfig.RicConfigUpdate.ADDED);
197
198         verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
199         verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
200
201         assertEquals(RicState.UNDEFINED, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
202
203         assertEquals(IDLE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME);
204     }
205
206     @SafeVarargs
207     private <T> Vector<T> toVector(T... objs) {
208         Vector<T> result = new Vector<>();
209         for (T o : objs) {
210             result.add(o);
211         }
212         return result;
213     }
214
215     private RicConfig getRicConfig(String name, String baseUrl, String... managedElementIds) {
216         ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
217             .name(name) //
218             .managedElementIds(toVector(managedElementIds)) //
219             .baseUrl(baseUrl) //
220             .build();
221         return ricConfig;
222     }
223 }