Add tests for StartupService
Change-Id: I686d0acb2a0ca976923c6cfeed1502ebae52cb80
Issue-ID: NONRTRIC-140
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
diff --git a/policy-agent/src/main/java/org/oransc/policyagent/tasks/StartupService.java b/policy-agent/src/main/java/org/oransc/policyagent/tasks/StartupService.java
index d3b8b35..7068da4 100644
--- a/policy-agent/src/main/java/org/oransc/policyagent/tasks/StartupService.java
+++ b/policy-agent/src/main/java/org/oransc/policyagent/tasks/StartupService.java
@@ -22,6 +22,7 @@
import org.oransc.policyagent.clients.A1ClientFactory;
import org.oransc.policyagent.configuration.ApplicationConfig;
+import org.oransc.policyagent.configuration.ApplicationConfig.RicConfigUpdate;
import org.oransc.policyagent.configuration.RicConfig;
import org.oransc.policyagent.repository.Policies;
import org.oransc.policyagent.repository.PolicyTypes;
@@ -78,19 +79,23 @@
}
@Override
- public void onRicConfigUpdate(RicConfig ricConfig, ApplicationConfig.RicConfigUpdate event) {
+ public void onRicConfigUpdate(RicConfig ricConfig, RicConfigUpdate event) {
synchronized (this.rics) {
- if (event.equals(ApplicationConfig.RicConfigUpdate.ADDED)
- || event.equals(ApplicationConfig.RicConfigUpdate.CHANGED)) {
- Ric ric = new Ric(ricConfig);
- rics.put(ric);
- RicSynchronizationTask synchronizationTask =
- new RicSynchronizationTask(a1ClientFactory, policyTypes, policies, services);
- synchronizationTask.run(ric);
- } else if (event.equals(ApplicationConfig.RicConfigUpdate.REMOVED)) {
- rics.remove(ricConfig.name());
- } else {
- logger.debug("Unhandled event: {}", event);
+ switch (event) {
+ case ADDED:
+ case CHANGED:
+ Ric ric = new Ric(ricConfig);
+ rics.put(ric);
+ RicSynchronizationTask synchronizationTask = createSynchronizationTask();
+ synchronizationTask.run(ric);
+ break;
+
+ case REMOVED:
+ rics.remove(ricConfig.name());
+ break;
+
+ default:
+ logger.error("Unhandled ric event: {}", event);
}
}
}
@@ -104,4 +109,7 @@
refreshConfigTask.start();
}
+ RicSynchronizationTask createSynchronizationTask() {
+ return new RicSynchronizationTask(a1ClientFactory, policyTypes, policies, services);
+ }
}
diff --git a/policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java b/policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java
index eeecc1c..5590c5e 100644
--- a/policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java
+++ b/policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java
@@ -20,194 +20,118 @@
package org.oransc.policyagent.tasks;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.awaitility.Awaitility.await;
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.mock;
-import static org.mockito.Mockito.when;
-import static org.oransc.policyagent.repository.Ric.RicState.IDLE;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
-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.mockito.Mock;
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)
public class StartupServiceTest {
private static final String FIRST_RIC_NAME = "first";
- private static final String FIRST_RIC_URL = "firstUrl";
private static final String SECOND_RIC_NAME = "second";
- private static final String SECOND_RIC_URL = "secondUrl";
- private static final String MANAGED_NODE_A = "nodeA";
- private static final String MANAGED_NODE_B = "nodeB";
- private static final String MANAGED_NODE_C = "nodeC";
- private static final String POLICY_TYPE_1_NAME = "type1";
- private static final String POLICY_TYPE_2_NAME = "type2";
-
+ @Mock
ApplicationConfig appConfigMock;
+ @Mock
RefreshConfigTask refreshTaskMock;
-
- 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());
- }
+ @Mock
+ RicSynchronizationTask synchronizationTaskMock;
@Test
- 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());
- doReturn(Flux.empty()).when(a1ClientMock).deleteAllPolicies();
-
- Rics rics = new Rics();
- PolicyTypes policyTypes = new PolicyTypes();
- StartupService serviceUnderTest = new StartupService(appConfigMock, refreshTaskMock, rics, policyTypes,
- a1ClientFactory, new Policies(), new Services());
+ public void startup_thenServiceIsAddedAsObeserverAndRefreshIsStarted() {
+ StartupService serviceUnderTest =
+ new StartupService(appConfigMock, refreshTaskMock, null, null, null, null, null);
serviceUnderTest.startup();
- 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(appConfigMock).addObserver(serviceUnderTest);
+ verify(refreshTaskMock).start();
+ }
+
+ @Test
+ public void twoNewRicsAddedToConfiguration_thenSynchronizationIsStartedAndTwoRicsAreAddedInRepository() {
+
+ Rics rics = new Rics();
+ StartupService serviceUnderTest =
+ spy(new StartupService(appConfigMock, refreshTaskMock, rics, null, null, null, null));
+
+ doReturn(synchronizationTaskMock).when(serviceUnderTest).createSynchronizationTask();
+
+ serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME), ApplicationConfig.RicConfigUpdate.ADDED);
+ serviceUnderTest.onRicConfigUpdate(getRicConfig(SECOND_RIC_NAME), ApplicationConfig.RicConfigUpdate.ADDED);
Ric firstRic = rics.get(FIRST_RIC_NAME);
- Ric secondRic = rics.get(SECOND_RIC_NAME);
- await().untilAsserted(() -> assertThat(firstRic.getState()).isEqualTo(IDLE));
- await().untilAsserted(() -> assertThat(secondRic.getState()).isEqualTo(IDLE));
-
- 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.");
- assertEquals(2, rics.size(), "Correct number of Rics not added to Rics");
-
- assertNotNull(firstRic, "Ric " + FIRST_RIC_NAME + " not added to repository");
assertEquals(FIRST_RIC_NAME, firstRic.name(), FIRST_RIC_NAME + " not added to Rics");
+ verify(synchronizationTaskMock, times(1)).run(firstRic);
- 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.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);
-
- assertNotNull(secondRic, "Ric " + SECOND_RIC_NAME + " not added to repository");
+ Ric secondRic = rics.get(SECOND_RIC_NAME);
assertEquals(SECOND_RIC_NAME, secondRic.name(), SECOND_RIC_NAME + " not added to Rics");
- 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.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);
+ verify(synchronizationTaskMock).run(secondRic);
}
@Test
- public void startupAndUnableToConnectToGetTypes_thenRicStateSetToUndefined() {
- Mono<?> error = Mono.error(new Exception("Unable to contact ric."));
- doReturn(error, error).when(a1ClientMock).getPolicyTypeIdentities();
-
+ public void oneRicIsChanged_thenSynchronizationIsStartedAndRicIsUpdatedInRepository() {
Rics rics = new Rics();
- PolicyTypes policyTypes = new PolicyTypes();
- StartupService serviceUnderTest = new StartupService(appConfigMock, refreshTaskMock, rics, policyTypes,
- a1ClientFactory, new Policies(), new Services());
+ Ric originalRic = new Ric(getRicConfig(FIRST_RIC_NAME, "managedElement1"));
+ rics.put(originalRic);
- serviceUnderTest.startup();
- serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A),
- ApplicationConfig.RicConfigUpdate.ADDED);
+ StartupService serviceUnderTest =
+ spy(new StartupService(appConfigMock, refreshTaskMock, rics, null, null, null, null));
- assertEquals(RicState.UNDEFINED, rics.get(FIRST_RIC_NAME).getState(),
- "Not correct state for " + FIRST_RIC_NAME);
+ doReturn(synchronizationTaskMock).when(serviceUnderTest).createSynchronizationTask();
+
+ String updatedManagedElementName = "managedElement2";
+ serviceUnderTest.onRicConfigUpdate(getRicConfig(FIRST_RIC_NAME, updatedManagedElementName),
+ ApplicationConfig.RicConfigUpdate.CHANGED);
+
+ Ric firstRic = rics.get(FIRST_RIC_NAME);
+ assertEquals(FIRST_RIC_NAME, firstRic.name(), FIRST_RIC_NAME + " not added to Rics");
+ assertTrue(firstRic.getManagedElementIds().contains(updatedManagedElementName), "Ric not updated");
+ verify(synchronizationTaskMock).run(firstRic);
}
@Test
- public void startupAndUnableToConnectToDeleteAllPolicies_thenRicStateSetToUndefined() {
+ public void oneRicIsRemoved_thenNoSynchronizationIsStartedAndRicIsDeletedFromRepository() {
+ Rics rics = new Rics();
+ RicConfig ricConfig = getRicConfig(FIRST_RIC_NAME);
+ rics.put(new Ric(ricConfig));
- 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();
+ StartupService serviceUnderTest =
+ new StartupService(appConfigMock, refreshTaskMock, rics, null, null, null, null);
- 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);
+ serviceUnderTest.onRicConfigUpdate(ricConfig, ApplicationConfig.RicConfigUpdate.REMOVED);
- PolicyType policyType = mock(PolicyType.class);
- when(policyType.name()).thenReturn(POLICY_TYPE_1_NAME);
-
- Policy policy = mock(Policy.class);
- when(policy.ric()).thenReturn(ric);
- when(policy.type()).thenReturn(policyType);
- Policies policies = new Policies();
- policies.put(policy);
-
- StartupService serviceUnderTest = new StartupService(appConfigMock, refreshTaskMock, new Rics(),
- new PolicyTypes(), a1ClientFactory, policies, new Services());
-
- 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);
+ assertEquals(0, rics.size(), "Ric not deleted");
}
- @SafeVarargs
- private <T> Vector<T> toVector(T... objs) {
- Vector<T> result = new Vector<>();
- for (T o : objs) {
- result.add(o);
+ private RicConfig getRicConfig(String name) {
+ return getRicConfig(name, null);
+ }
+
+ private RicConfig getRicConfig(String name, String managedElementName) {
+ List<String> managedElements = Collections.emptyList();
+ if (managedElementName != null) {
+ managedElements = Collections.singletonList(managedElementName);
}
- return result;
- }
-
- private RicConfig getRicConfig(String name, String baseUrl, String... managedElementIds) {
ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
.name(name) //
- .managedElementIds(toVector(managedElementIds)) //
- .baseUrl(baseUrl) //
+ .managedElementIds(managedElements) //
+ .baseUrl("baseUrl") //
.build();
return ricConfig;
}