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.Vector;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.oransc.policyagent.clients.A1Client;
44 import org.oransc.policyagent.configuration.ApplicationConfig;
45 import org.oransc.policyagent.configuration.ImmutableRicConfig;
46 import org.oransc.policyagent.configuration.RicConfig;
47 import org.oransc.policyagent.repository.PolicyTypes;
48 import org.oransc.policyagent.repository.Ric;
49 import org.oransc.policyagent.repository.Rics;
51 import reactor.core.publisher.Flux;
52 import reactor.core.publisher.Mono;
54 @ExtendWith(MockitoExtension.class)
55 @RunWith(MockitoJUnitRunner.class)
56 public class StartupServiceTest {
57 private static final String FIRST_RIC_NAME = "first";
58 private static final String FIRST_RIC_URL = "firstUrl";
59 private static final String SECOND_RIC_NAME = "second";
60 private static final String SECOND_RIC_URL = "secondUrl";
61 private static final String MANAGED_NODE_A = "nodeA";
62 private static final String MANAGED_NODE_B = "nodeB";
63 private static final String MANAGED_NODE_C = "nodeC";
65 private static final String POLICY_TYPE_1_NAME = "type1";
66 private static final String POLICY_TYPE_2_NAME = "type2";
67 private static final String POLICY_ID_1 = "policy1";
68 private static final String POLICY_ID_2 = "policy2";
71 ApplicationConfig appConfigMock;
74 A1Client a1ClientMock;
77 public void startup_allOk() {
78 Vector<RicConfig> ricConfigs = new Vector<>(2);
79 ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A));
80 ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C));
81 when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs);
83 Flux<String> fluxType1 = Flux.just(POLICY_TYPE_1_NAME);
84 Flux<String> fluxType2 = Flux.just(POLICY_TYPE_2_NAME);
85 when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(fluxType1)
86 .thenReturn(fluxType1.concatWith(fluxType2));
87 Flux<String> policies = Flux.just(new String[] {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 = new StartupService(appConfigMock, rics, policyTypes, a1ClientMock);
96 serviceUnderTest.startup();
98 await().untilAsserted(() -> assertThat(policyTypes.size()).isEqualTo(2));
100 verify(a1ClientMock).getPolicyTypeIdentities(FIRST_RIC_URL);
101 verify(a1ClientMock).deletePolicy(FIRST_RIC_URL, POLICY_ID_1);
102 verify(a1ClientMock).deletePolicy(FIRST_RIC_URL, POLICY_ID_2);
104 verify(a1ClientMock).getPolicyTypeIdentities(SECOND_RIC_URL);
105 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
106 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
108 assertTrue(policyTypes.contains(POLICY_TYPE_1_NAME), POLICY_TYPE_1_NAME + " not added to PolicyTypes.");
109 assertTrue(policyTypes.contains(POLICY_TYPE_2_NAME), POLICY_TYPE_2_NAME + " not added to PolicyTypes.");
110 assertEquals(2, rics.size(), "Correct number of Rics not added to Rics");
112 Ric firstRic = rics.get(FIRST_RIC_NAME);
113 assertNotNull(firstRic, "Ric " + FIRST_RIC_NAME + " not added to repository");
114 assertEquals(FIRST_RIC_NAME, firstRic.name(), FIRST_RIC_NAME + " not added to Rics");
115 assertEquals(ACTIVE, firstRic.state(), "Not correct state for ric " + FIRST_RIC_NAME);
116 assertEquals(1, firstRic.getSupportedPolicyTypes().size(),
117 "Not correct no of types supported for ric " + FIRST_RIC_NAME);
118 assertTrue(firstRic.isSupportingType(POLICY_TYPE_1_NAME),
119 POLICY_TYPE_1_NAME + " not supported by ric " + FIRST_RIC_NAME);
120 assertEquals(1, firstRic.getManagedNodes().size(), "Not correct no of managed nodes for ric " + FIRST_RIC_NAME);
121 assertTrue(firstRic.isManaging(MANAGED_NODE_A), MANAGED_NODE_A + " not managed by ric " + FIRST_RIC_NAME);
123 Ric secondRic = rics.get(SECOND_RIC_NAME);
124 assertNotNull(secondRic, "Ric " + SECOND_RIC_NAME + " not added to repository");
125 assertEquals(SECOND_RIC_NAME, secondRic.name(), SECOND_RIC_NAME + " not added to Rics");
126 assertEquals(ACTIVE, secondRic.state(), "Not correct state for " + SECOND_RIC_NAME);
127 assertEquals(2, secondRic.getSupportedPolicyTypes().size(),
128 "Not correct no of types supported for ric " + SECOND_RIC_NAME);
129 assertTrue(secondRic.isSupportingType(POLICY_TYPE_1_NAME),
130 POLICY_TYPE_1_NAME + " not supported by ric " + SECOND_RIC_NAME);
131 assertTrue(secondRic.isSupportingType(POLICY_TYPE_2_NAME),
132 POLICY_TYPE_2_NAME + " not supported by ric " + SECOND_RIC_NAME);
133 assertEquals(2, secondRic.getManagedNodes().size(),
134 "Not correct no of managed nodes for ric " + SECOND_RIC_NAME);
135 assertTrue(secondRic.isManaging(MANAGED_NODE_B), MANAGED_NODE_B + " not managed by ric " + SECOND_RIC_NAME);
136 assertTrue(secondRic.isManaging(MANAGED_NODE_C), MANAGED_NODE_C + " not managed by ric " + SECOND_RIC_NAME);
140 public void startup_unableToConnectToGetTypes() {
141 Vector<RicConfig> ricConfigs = new Vector<>(2);
142 ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A));
143 ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C));
144 when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs);
146 Flux<String> fluxType1 = Flux.just(POLICY_TYPE_1_NAME);
147 doReturn(Flux.error(new Exception("Unable to contact ric.")), fluxType1).when(a1ClientMock)
148 .getPolicyTypeIdentities(anyString());
150 Flux<String> policies = Flux.just(new String[] {POLICY_ID_1, POLICY_ID_2});
151 doReturn(policies).when(a1ClientMock).getPolicyIdentities(anyString());
152 when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema"));
153 when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.just("OK"));
155 Rics rics = new Rics();
156 PolicyTypes policyTypes = new PolicyTypes();
157 StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, a1ClientMock);
159 serviceUnderTest.startup();
161 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
162 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
164 assertEquals(NOT_REACHABLE, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
166 assertEquals(ACTIVE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME);
170 public void startup_unableToConnectToGetPolicies() {
171 Vector<RicConfig> ricConfigs = new Vector<>(2);
172 ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A));
173 ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C));
174 when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs);
176 Flux<String> fluxType1 = Flux.just(POLICY_TYPE_1_NAME);
177 Flux<String> fluxType2 = Flux.just(POLICY_TYPE_2_NAME);
178 when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(fluxType1)
179 .thenReturn(fluxType1.concatWith(fluxType2));
180 when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema"));
181 Flux<String> policies = Flux.just(new String[] {POLICY_ID_1, POLICY_ID_2});
182 doReturn(Flux.error(new Exception("Unable to contact ric.")), policies).when(a1ClientMock)
183 .getPolicyIdentities(anyString());
184 when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.just("OK"));
186 Rics rics = new Rics();
187 PolicyTypes policyTypes = new PolicyTypes();
188 StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, a1ClientMock);
190 serviceUnderTest.startup();
192 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
193 verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
195 assertEquals(NOT_REACHABLE, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
197 assertEquals(ACTIVE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME);
200 private RicConfig getRicConfig(String name, String baseUrl, String... nodeNames) {
201 Vector<String> managedNodes = new Vector<String>(1);
202 for (String nodeName : nodeNames) {
203 managedNodes.add(nodeName);
205 ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
207 .managedElementIds(managedNodes) //