2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.tasks;
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;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.Vector;
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;
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";
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";
72 ApplicationConfig appConfigMock;
75 A1Client a1ClientMock;
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);
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"));
92 Rics rics = new Rics();
93 PolicyTypes policyTypes = new PolicyTypes();
94 StartupService serviceUnderTest =
95 new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies());
97 serviceUnderTest.startup();
99 await().untilAsserted(() -> assertThat(policyTypes.size()).isEqualTo(2));
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);
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);
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");
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);
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);
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);
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());
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"));
157 Rics rics = new Rics();
158 PolicyTypes policyTypes = new PolicyTypes();
159 StartupService serviceUnderTest =
160 new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies());
162 serviceUnderTest.startup();
164 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
165 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
167 assertEquals(NOT_REACHABLE, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
169 assertEquals(ACTIVE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME);
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);
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"));
188 Rics rics = new Rics();
189 PolicyTypes policyTypes = new PolicyTypes();
190 StartupService serviceUnderTest =
191 new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies());
193 serviceUnderTest.startup();
195 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
196 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
198 assertEquals(NOT_REACHABLE, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
200 assertEquals(ACTIVE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME);
204 private <T> Vector<T> toVector(T... objs) {
205 Vector<T> result = new Vector<>();
212 private RicConfig getRicConfig(String name, String baseUrl, String... managedElementIds) {
213 ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
215 .managedElementIds(toVector(managedElementIds)) //