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