package org.oransc.policyagent.tasks;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
+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.anyString;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.oransc.policyagent.repository.Ric.RicState.ACTIVE;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
import java.util.Vector;
-import org.junit.Test;
-import org.oransc.policyagent.clients.RicClient;
+
+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.configuration.ApplicationConfig;
import org.oransc.policyagent.configuration.ImmutableRicConfig;
import org.oransc.policyagent.configuration.RicConfig;
import org.oransc.policyagent.repository.Ric;
import org.oransc.policyagent.repository.Rics;
+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;
+ @Mock
+ A1Client a1ClientMock;
+
+ private static Gson gson = new GsonBuilder() //
+ .serializeNulls() //
+ .create(); //
+
@Test
public void startup_allOk() throws ServiceException {
- ApplicationConfig appConfigMock = mock(ApplicationConfig.class);
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);
- Vector<PolicyType> firstTypes = new Vector<>();
PolicyType type1 = ImmutablePolicyType.builder().name(POLICY_TYPE_1_NAME).jsonSchema("{}").build();
- firstTypes.add(type1);
- Vector<PolicyType> secondTypes = new Vector<>();
- secondTypes.add(type1);
+ Flux<String> fluxType1 = Flux.just(gson.toJson(type1));
PolicyType type2 = ImmutablePolicyType.builder().name(POLICY_TYPE_2_NAME).jsonSchema("{}").build();
- secondTypes.add(type2);
- RicClient ricClientMock = mock(RicClient.class);
- when(ricClientMock.getPolicyTypes(FIRST_RIC_URL)).thenReturn(firstTypes);
- when(ricClientMock.getPolicyTypes("secondUrl")).thenReturn(secondTypes);
+ Flux<String> fluxType2 = Flux.just(gson.toJson(type2));
+ when(a1ClientMock.getAllPolicyTypes(anyString())).thenReturn(fluxType1)
+ .thenReturn(fluxType1.concatWith(fluxType2));
+ Flux<String> policies = Flux.just(new String[] {POLICY_ID_1, POLICY_ID_2});
+ when(a1ClientMock.getPoliciesForType(anyString(), anyString())).thenReturn(policies);
+ when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.empty());
Rics rics = new Rics();
PolicyTypes policyTypes = new PolicyTypes();
- StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, ricClientMock);
+ StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, a1ClientMock);
serviceUnderTest.startup();
- verify(ricClientMock).deleteAllPolicies(FIRST_RIC_URL);
- verify(ricClientMock).getPolicyTypes(FIRST_RIC_URL);
- verify(ricClientMock).deleteAllPolicies(SECOND_RIC_URL);
- verify(ricClientMock).getPolicyTypes(SECOND_RIC_URL);
- verifyNoMoreInteractions(ricClientMock);
+ await().untilAsserted(() -> assertThat(policyTypes.size()).isEqualTo(2));
+
+ verify(a1ClientMock).getAllPolicyTypes(FIRST_RIC_URL);
+ verify(a1ClientMock).deletePolicy(FIRST_RIC_URL, POLICY_ID_1);
+ verify(a1ClientMock).deletePolicy(FIRST_RIC_URL, POLICY_ID_2);
+
+ verify(a1ClientMock).getAllPolicyTypes(SECOND_RIC_URL);
+ verify(a1ClientMock, times(2)).deletePolicy(SECOND_RIC_URL, POLICY_ID_1);
+ verify(a1ClientMock, times(2)).deletePolicy(SECOND_RIC_URL, POLICY_ID_2);
- assertEquals("Not correct number of policy types added.", 2, policyTypes.size());
- assertEquals("Not correct type added.", type1, policyTypes.getType(POLICY_TYPE_1_NAME));
- assertEquals("Not correct type added.", type2, policyTypes.getType(POLICY_TYPE_2_NAME));
- assertEquals("Correct nymber of Rics not added to Rics", 2, rics.size());
+ assertEquals(type1, policyTypes.getType(POLICY_TYPE_1_NAME), "Not correct type added.");
+ assertEquals(type2, policyTypes.getType(POLICY_TYPE_2_NAME), "Not correct type added.");
+ assertEquals(2, rics.size(), "Correct number of Rics not added to Rics");
Ric firstRic = rics.getRic(FIRST_RIC_NAME);
- assertNotNull("Ric \"" + FIRST_RIC_NAME + "\" not added to repositpry", firstRic);
- assertEquals("Not correct Ric \"" + FIRST_RIC_NAME + "\" added to Rics", FIRST_RIC_NAME, firstRic.name());
- assertEquals("Not correct state for \"" + FIRST_RIC_NAME + "\"", ACTIVE, firstRic.state());
- assertEquals("Not correct no of types supported", 1, firstRic.getSupportedPolicyTypes().size());
- assertTrue("Not correct type supported", firstRic.isSupportingType(type1));
- assertEquals("Not correct no of managed nodes", 1, firstRic.getManagedNodes().size());
- assertTrue("Not managed by node", firstRic.isManaging(MANAGED_NODE_A));
+ assertNotNull(firstRic, "Ric \"" + FIRST_RIC_NAME + "\" not added to repositpry");
+ assertEquals(FIRST_RIC_NAME, firstRic.name(), "Not correct Ric \"" + FIRST_RIC_NAME + "\" added to Rics");
+ assertEquals(ACTIVE, firstRic.state(), "Not correct state for \"" + FIRST_RIC_NAME + "\"");
+ assertEquals(1, firstRic.getSupportedPolicyTypes().size(), "Not correct no of types supported");
+ assertTrue(firstRic.isSupportingType(type1.name()), "Not correct type supported");
+ assertEquals(1, firstRic.getManagedNodes().size(), "Not correct no of managed nodes");
+ assertTrue(firstRic.isManaging(MANAGED_NODE_A), "Not managed by node");
Ric secondRic = rics.getRic(SECOND_RIC_NAME);
- assertNotNull("Ric \"" + SECOND_RIC_NAME + "\" not added to repositpry", secondRic);
- assertEquals("Not correct Ric \"" + SECOND_RIC_NAME + "\" added to Rics", SECOND_RIC_NAME, secondRic.name());
- assertEquals("Not correct state for \"" + SECOND_RIC_NAME + "\"", ACTIVE, secondRic.state());
- assertEquals("Not correct no of types supported", 2, secondRic.getSupportedPolicyTypes().size());
- assertTrue("Not correct type supported", secondRic.isSupportingType(type1));
- assertTrue("Not correct type supported", secondRic.isSupportingType(type2));
- assertEquals("Not correct no of managed nodes", 2, secondRic.getManagedNodes().size());
- assertTrue("Not correct managed node", secondRic.isManaging(MANAGED_NODE_B));
- assertTrue("Not correct managed node", secondRic.isManaging(MANAGED_NODE_C));
+ assertNotNull(secondRic, "Ric \"" + SECOND_RIC_NAME + "\" not added to repositpry");
+ assertEquals(SECOND_RIC_NAME, secondRic.name(), "Not correct Ric \"" + SECOND_RIC_NAME + "\" added to Rics");
+ assertEquals(ACTIVE, secondRic.state(), "Not correct state for \"" + SECOND_RIC_NAME + "\"");
+ assertEquals(2, secondRic.getSupportedPolicyTypes().size(), "Not correct no of types supported");
+ assertTrue(secondRic.isSupportingType(type1.name()), "Not correct type supported");
+ assertTrue(secondRic.isSupportingType(type2.name()), "Not correct type supported");
+ assertEquals(2, secondRic.getManagedNodes().size(), "Not correct no of managed nodes");
+ assertTrue(secondRic.isManaging(MANAGED_NODE_B), "Not correct managed node");
+ assertTrue(secondRic.isManaging(MANAGED_NODE_C), "Not correct managed node");
}
private RicConfig getRicConfig(String name, String baseUrl, String... nodeNames) {