import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.oransc.policyagent.repository.Ric.RicState.ACTIVE;
-import static org.oransc.policyagent.repository.Ric.RicState.NOT_REACHABLE;
+import static org.oransc.policyagent.repository.Ric.RicState.IDLE;
+import com.google.common.collect.ImmutableList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
import java.util.Vector;
+
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.oransc.policyagent.clients.A1Client;
+import org.oransc.policyagent.clients.A1ClientFactory;
import org.oransc.policyagent.configuration.ApplicationConfig;
import org.oransc.policyagent.configuration.ImmutableRicConfig;
import org.oransc.policyagent.configuration.RicConfig;
+import org.oransc.policyagent.repository.Policies;
+import org.oransc.policyagent.repository.Policy;
+import org.oransc.policyagent.repository.PolicyType;
import org.oransc.policyagent.repository.PolicyTypes;
import org.oransc.policyagent.repository.Ric;
+import org.oransc.policyagent.repository.Ric.RicState;
import org.oransc.policyagent.repository.Rics;
+import org.oransc.policyagent.repository.Services;
+
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@ExtendWith(MockitoExtension.class)
-@RunWith(MockitoJUnitRunner.class)
public class StartupServiceTest {
private static final String FIRST_RIC_NAME = "first";
private static final String FIRST_RIC_URL = "firstUrl";
private static final String POLICY_TYPE_1_NAME = "type1";
private static final String POLICY_TYPE_2_NAME = "type2";
- private static final String POLICY_ID_1 = "policy1";
- private static final String POLICY_ID_2 = "policy2";
- @Mock
ApplicationConfig appConfigMock;
+ RefreshConfigTask refreshTaskMock;
- @Mock
A1Client a1ClientMock;
+ A1ClientFactory a1ClientFactory;
+
+ @BeforeEach
+ public void init() throws Exception {
+ a1ClientMock = mock(A1Client.class);
+ a1ClientFactory = mock(A1ClientFactory.class);
+ appConfigMock = mock(ApplicationConfig.class);
+ refreshTaskMock = mock(RefreshConfigTask.class);
+ doReturn(Mono.just(a1ClientMock)).when(a1ClientFactory).createA1Client(any());
+ }
@Test
- public void startup_allOk() {
- Vector<RicConfig> ricConfigs = new Vector<>(2);
- ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A));
- ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C));
- when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs);
-
- Flux<String> fluxType1 = Flux.just(POLICY_TYPE_1_NAME);
- Flux<String> fluxType2 = Flux.just(POLICY_TYPE_2_NAME);
- when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(fluxType1)
- .thenReturn(fluxType1.concatWith(fluxType2));
- Flux<String> policies = Flux.just(new String[] {POLICY_ID_1, POLICY_ID_2});
- when(a1ClientMock.getPolicyIdentities(anyString())).thenReturn(policies);
- when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.empty());
+ public void startupAndAllOk_thenRicsAreConfiguredInRepository() {
+ Mono<List<String>> policyTypes1 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME));
+ Mono<List<String>> policyTypes2 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME));
+ doReturn(policyTypes1, policyTypes2).when(a1ClientMock).getPolicyTypeIdentities();
+ doReturn(Mono.just("Schema")).when(a1ClientMock).getPolicyTypeSchema(anyString());
Rics rics = new Rics();
PolicyTypes policyTypes = new PolicyTypes();
- StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, a1ClientMock);
+ StartupService serviceUnderTest = new StartupService(appConfigMock, refreshTaskMock, rics, policyTypes,
+ a1ClientFactory, new Policies(), new Services());
serviceUnderTest.startup();
- await().untilAsserted(() -> assertThat(policyTypes.size()).isEqualTo(2));
-
- verify(a1ClientMock).getPolicyTypeIdentities(FIRST_RIC_URL);
- verify(a1ClientMock).deletePolicy(FIRST_RIC_URL, POLICY_ID_1);
- verify(a1ClientMock).deletePolicy(FIRST_RIC_URL, POLICY_ID_2);
+ serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
+ ApplicationConfig.RicConfigUpdate.ADDED);
+ serviceUnderTest.onRicConfigUpdate(
+ getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C),
+ ApplicationConfig.RicConfigUpdate.ADDED);
- verify(a1ClientMock).getPolicyTypeIdentities(SECOND_RIC_URL);
- verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
- verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
+ await().untilAsserted(() -> assertThat(policyTypes.size()).isEqualTo(2));
assertTrue(policyTypes.contains(POLICY_TYPE_1_NAME), POLICY_TYPE_1_NAME + " not added to PolicyTypes.");
assertTrue(policyTypes.contains(POLICY_TYPE_2_NAME), POLICY_TYPE_2_NAME + " not added to PolicyTypes.");
Ric firstRic = rics.get(FIRST_RIC_NAME);
assertNotNull(firstRic, "Ric " + FIRST_RIC_NAME + " not added to repository");
assertEquals(FIRST_RIC_NAME, firstRic.name(), FIRST_RIC_NAME + " not added to Rics");
- assertEquals(ACTIVE, firstRic.state(), "Not correct state for ric " + FIRST_RIC_NAME);
+ assertEquals(IDLE, firstRic.getState(), "Not correct state for ric " + FIRST_RIC_NAME);
assertEquals(1, firstRic.getSupportedPolicyTypes().size(),
"Not correct no of types supported for ric " + FIRST_RIC_NAME);
assertTrue(firstRic.isSupportingType(POLICY_TYPE_1_NAME),
POLICY_TYPE_1_NAME + " not supported by ric " + FIRST_RIC_NAME);
- assertEquals(1, firstRic.getManagedNodes().size(), "Not correct no of managed nodes for ric " + FIRST_RIC_NAME);
+ assertEquals(1, firstRic.getManagedElementIds().size(),
+ "Not correct no of managed nodes for ric " + FIRST_RIC_NAME);
assertTrue(firstRic.isManaging(MANAGED_NODE_A), MANAGED_NODE_A + " not managed by ric " + FIRST_RIC_NAME);
Ric secondRic = rics.get(SECOND_RIC_NAME);
assertNotNull(secondRic, "Ric " + SECOND_RIC_NAME + " not added to repository");
assertEquals(SECOND_RIC_NAME, secondRic.name(), SECOND_RIC_NAME + " not added to Rics");
- assertEquals(ACTIVE, secondRic.state(), "Not correct state for " + SECOND_RIC_NAME);
+ assertEquals(IDLE, secondRic.getState(), "Not correct state for " + SECOND_RIC_NAME);
assertEquals(2, secondRic.getSupportedPolicyTypes().size(),
"Not correct no of types supported for ric " + SECOND_RIC_NAME);
assertTrue(secondRic.isSupportingType(POLICY_TYPE_1_NAME),
POLICY_TYPE_1_NAME + " not supported by ric " + SECOND_RIC_NAME);
assertTrue(secondRic.isSupportingType(POLICY_TYPE_2_NAME),
POLICY_TYPE_2_NAME + " not supported by ric " + SECOND_RIC_NAME);
- assertEquals(2, secondRic.getManagedNodes().size(),
+ assertEquals(2, secondRic.getManagedElementIds().size(),
"Not correct no of managed nodes for ric " + SECOND_RIC_NAME);
assertTrue(secondRic.isManaging(MANAGED_NODE_B), MANAGED_NODE_B + " not managed by ric " + SECOND_RIC_NAME);
assertTrue(secondRic.isManaging(MANAGED_NODE_C), MANAGED_NODE_C + " not managed by ric " + SECOND_RIC_NAME);
}
@Test
- public void startup_unableToConnectToGetTypes() {
- Vector<RicConfig> ricConfigs = new Vector<>(2);
- ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A));
- ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C));
- when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs);
-
- Flux<String> fluxType1 = Flux.just(POLICY_TYPE_1_NAME);
- doReturn(Flux.error(new Exception("Unable to contact ric.")), fluxType1).when(a1ClientMock)
- .getPolicyTypeIdentities(anyString());
-
- Flux<String> policies = Flux.just(new String[] {POLICY_ID_1, POLICY_ID_2});
- doReturn(policies).when(a1ClientMock).getPolicyIdentities(anyString());
- when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.empty());
+ public void startupAndUnableToConnectToGetTypes_thenRicStateSetToUndefined() {
+ Mono<?> error = Mono.error(new Exception("Unable to contact ric."));
+ doReturn(error, error).when(a1ClientMock).getPolicyTypeIdentities();
Rics rics = new Rics();
PolicyTypes policyTypes = new PolicyTypes();
- StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, a1ClientMock);
+ StartupService serviceUnderTest = new StartupService(appConfigMock, refreshTaskMock, rics, policyTypes,
+ a1ClientFactory, new Policies(), new Services());
serviceUnderTest.startup();
+ serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
+ ApplicationConfig.RicConfigUpdate.ADDED);
- verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
- verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
-
- assertEquals(NOT_REACHABLE, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
-
- assertEquals(ACTIVE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME);
+ assertEquals(RicState.UNDEFINED, rics.get(FIRST_RIC_NAME).getState(),
+ "Not correct state for " + FIRST_RIC_NAME);
}
@Test
- public void startup_unableToConnectToGetPolicies() {
- Vector<RicConfig> ricConfigs = new Vector<>(2);
- ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A));
- ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C));
- when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs);
-
- Flux<String> fluxType1 = Flux.just(POLICY_TYPE_1_NAME);
- Flux<String> fluxType2 = Flux.just(POLICY_TYPE_2_NAME);
- when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(fluxType1)
- .thenReturn(fluxType1.concatWith(fluxType2));
- Flux<String> policies = Flux.just(new String[] {POLICY_ID_1, POLICY_ID_2});
- doReturn(Flux.error(new Exception("Unable to contact ric.")), policies).when(a1ClientMock)
- .getPolicyIdentities(anyString());
- when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.empty());
+ public void startupAndUnableToConnectToDeleteAllPolicies_thenRicStateSetToUndefined() {
- Rics rics = new Rics();
- PolicyTypes policyTypes = new PolicyTypes();
- StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, a1ClientMock);
+ Mono<List<String>> policyTypes = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME));
+ when(a1ClientMock.getPolicyTypeIdentities()).thenReturn(policyTypes);
+ when(a1ClientMock.getPolicyTypeSchema(anyString())).thenReturn(Mono.just("Schema"));
+ Flux<?> error = Flux.error(new Exception("Unable to contact ric."));
+ doReturn(error).when(a1ClientMock).deleteAllPolicies();
- serviceUnderTest.startup();
+ RicConfig ricConfig = mock(RicConfig.class);
+ when(ricConfig.name()).thenReturn(FIRST_RIC_NAME);
+ when(ricConfig.managedElementIds()).thenReturn(ImmutableList.copyOf(Collections.emptyList()));
+ Ric ric = new Ric(ricConfig);
+
+ PolicyType policyType = mock(PolicyType.class);
+ when(policyType.name()).thenReturn(POLICY_TYPE_1_NAME);
- verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
- verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
+ Policy policy = mock(Policy.class);
+ when(policy.ric()).thenReturn(ric);
+ when(policy.type()).thenReturn(policyType);
+ Policies policies = new Policies();
+ policies.put(policy);
- assertEquals(NOT_REACHABLE, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME);
+ StartupService serviceUnderTest = new StartupService(appConfigMock, refreshTaskMock, new Rics(),
+ new PolicyTypes(), a1ClientFactory, policies, new Services());
- assertEquals(ACTIVE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME);
+ serviceUnderTest.startup();
+ serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
+ ApplicationConfig.RicConfigUpdate.ADDED);
+
+ assertEquals(RicState.UNDEFINED, ric.getState(), "Not correct state for " + FIRST_RIC_NAME);
}
- private RicConfig getRicConfig(String name, String baseUrl, String... nodeNames) {
- Vector<String> managedNodes = new Vector<String>(1);
- for (String nodeName : nodeNames) {
- managedNodes.add(nodeName);
+ @SafeVarargs
+ private <T> Vector<T> toVector(T... objs) {
+ Vector<T> result = new Vector<>();
+ for (T o : objs) {
+ result.add(o);
}
+ return result;
+ }
+
+ private RicConfig getRicConfig(String name, String baseUrl, String... managedElementIds) {
ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
.name(name) //
- .managedElementIds(managedNodes) //
+ .managedElementIds(toVector(managedElementIds)) //
.baseUrl(baseUrl) //
.build();
return ricConfig;