e4fd6e688ea71f109bb2507218dfc733f97d37c3
[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<?> error = Mono.error(new Exception("Unable to contact ric."));
146         doReturn(error, error).when(a1ClientMock).getPolicyTypeIdentities(anyString());
147         doReturn(error).when(a1ClientMock).getPolicyIdentities(anyString());
148
149         Rics rics = new Rics();
150         PolicyTypes policyTypes = new PolicyTypes();
151         StartupService serviceUnderTest =
152             new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies(), new Services());
153
154         serviceUnderTest.startup();
155         serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
156             ApplicationConfig.RicConfigUpdate.ADDED);
157
158         assertEquals(RicState.UNDEFINED, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
159     }
160
161     @Test
162     public void startup_unableToConnectToGetPolicies() {
163
164         Mono<Collection<String>> policyTypes = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME));
165         when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(policyTypes);
166         when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema"));
167         Mono<?> error = Mono.error(new Exception("Unable to contact ric."));
168         doReturn(error).when(a1ClientMock).getPolicyIdentities(anyString());
169
170         Rics rics = new Rics();
171         StartupService serviceUnderTest =
172             new StartupService(appConfigMock, rics, new PolicyTypes(), a1ClientMock, new Policies(), new Services());
173
174         serviceUnderTest.startup();
175         serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
176             ApplicationConfig.RicConfigUpdate.ADDED);
177
178         assertEquals(RicState.UNDEFINED, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
179     }
180
181     @SafeVarargs
182     private <T> Vector<T> toVector(T... objs) {
183         Vector<T> result = new Vector<>();
184         for (T o : objs) {
185             result.add(o);
186         }
187         return result;
188     }
189
190     private RicConfig getRicConfig(String name, String baseUrl, String... managedElementIds) {
191         ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
192             .name(name) //
193             .managedElementIds(toVector(managedElementIds)) //
194             .baseUrl(baseUrl) //
195             .build();
196         return ricConfig;
197     }
198 }