Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / configuration / ApplicationConfigTest.java
index 257776d..5667fd2 100644 (file)
@@ -2,7 +2,7 @@
  * ========================LICENSE_START=================================
  * O-RAN-SC
  * %%
- * Copyright (C) 2019 Nordix Foundation
+ * Copyright (C) 2020 Nordix Foundation
  * %%
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.oransc.policyagent.configuration;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.verify;
 
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.Vector;
 
 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.configuration.ApplicationConfig.Observer;
+import org.oransc.policyagent.configuration.ApplicationConfig.RicConfigUpdate;
+import org.oransc.policyagent.configuration.ApplicationConfigParser.ConfigParserResult;
 import org.oransc.policyagent.exceptions.ServiceException;
 
 @ExtendWith(MockitoExtension.class)
-public class ApplicationConfigTest {
-    @Mock
-    Observer observerMock1;
-
-    @Mock
-    Observer observerMock2;
+class ApplicationConfigTest {
 
     private static final ImmutableRicConfig RIC_CONFIG_1 = ImmutableRicConfig.builder() //
         .name("ric1") //
         .baseUrl("ric1_url") //
         .managedElementIds(new Vector<>()) //
+        .controllerName("") //
         .build();
 
+    ConfigParserResult configParserResult(RicConfig... rics) {
+        return ImmutableConfigParserResult.builder() //
+            .ricConfigs(Arrays.asList(rics)) //
+            .dmaapConsumerTopicUrl("dmaapConsumerTopicUrl") //
+            .dmaapProducerTopicUrl("dmaapProducerTopicUrl") //
+            .controllerConfigs(new HashMap<>()) //
+            .build();
+    }
+
     @Test
-    public void addRicShouldNotifyAllObserversOfRicAdded() throws Exception {
+    void gettingNotAddedRicShouldThrowException() {
         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
 
-        appConfigUnderTest.addObserver(observerMock1);
-        appConfigUnderTest.addObserver(observerMock2);
+        appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1));
 
-        appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
+        Exception exception = assertThrows(ServiceException.class, () -> {
+            appConfigUnderTest.getRic("name");
+        });
 
-        verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
-        verify(observerMock2).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
+        assertEquals("Could not find ric configuration: name", exception.getMessage());
+    }
+
+    @Test
+    void addRicShouldNotifyAllObserversOfRicAdded() throws Exception {
+        ApplicationConfig appConfigUnderTest = new ApplicationConfig();
 
-        assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configuraions.");
+        RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst();
+        assertEquals(RicConfigUpdate.Type.ADDED, update.getType());
+        assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configurations.");
 
         assertEquals(RIC_CONFIG_1, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
             "Not correct Ric retrieved from configurations.");
+
+        update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst();
+        assertNull(update, "Nothing should be updated");
+        assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric should remain.");
+
     }
 
     @Test
-    public void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception {
+    void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception {
         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
 
-        appConfigUnderTest.addObserver(observerMock1);
-
-        appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
+        appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1));
 
         ImmutableRicConfig changedRicConfig = ImmutableRicConfig.builder() //
             .name("ric1") //
             .baseUrl("changed_ric1_url") //
             .managedElementIds(new Vector<>()) //
+            .controllerName("") //
             .build();
 
-        appConfigUnderTest.setConfiguration(Arrays.asList(changedRicConfig), null, null);
-
-        verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.ADDED);
-        verify(observerMock1).onRicConfigUpdate(changedRicConfig, ApplicationConfig.RicConfigUpdate.CHANGED);
+        RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(changedRicConfig)).blockFirst();
 
+        assertEquals(RicConfigUpdate.Type.CHANGED, update.getType());
         assertEquals(changedRicConfig, appConfigUnderTest.getRic(RIC_CONFIG_1.name()),
             "Changed Ric not retrieved from configurations.");
     }
 
     @Test
-    public void removedRicShouldNotifyAllObserversOfRicRemoved() {
+    void removedRicShouldNotifyAllObserversOfRicRemoved() {
         ApplicationConfig appConfigUnderTest = new ApplicationConfig();
 
-        appConfigUnderTest.addObserver(observerMock1);
-
         ImmutableRicConfig ricConfig2 = ImmutableRicConfig.builder() //
             .name("ric2") //
             .baseUrl("ric2_url") //
             .managedElementIds(new Vector<>()) //
+            .controllerName("") //
             .build();
 
-        appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1, ricConfig2), null, null);
-
-        appConfigUnderTest.setConfiguration(Arrays.asList(ricConfig2), null, null);
+        appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1, ricConfig2));
 
-        verify(observerMock1).onRicConfigUpdate(RIC_CONFIG_1, ApplicationConfig.RicConfigUpdate.REMOVED);
+        RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(ricConfig2)).blockFirst();
 
+        assertEquals(RicConfigUpdate.Type.REMOVED, update.getType());
         assertEquals(1, appConfigUnderTest.getRicConfigs().size(), "Ric not deleted from configurations.");
     }
 
-    @Test
-    public void gettingNotAddedRicShouldThrowException() {
-        ApplicationConfig appConfigUnderTest = new ApplicationConfig();
-
-        appConfigUnderTest.setConfiguration(Arrays.asList(RIC_CONFIG_1), null, null);
-
-        Exception exception = assertThrows(ServiceException.class, () -> {
-            appConfigUnderTest.getRic("name");
-        });
-
-        assertEquals("Could not find ric: name", exception.getMessage());
-    }
 }