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