Merge "New version of NearRT-RIC simulator"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / configuration / ApplicationConfigTest.java
index c8bbdfd..6a4f8b3 100644 (file)
  * limitations under the License.
  * ========================LICENSE_END===================================
  */
+
 package org.oransc.policyagent.configuration;
 
-import static org.junit.Assert.assertTrue;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.read.ListAppender;
@@ -41,23 +44,41 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
 import java.util.Properties;
+import java.util.Vector;
 
-import org.junit.Test;
 import org.junit.jupiter.api.Assertions;
+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.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
+import org.oransc.policyagent.exceptions.ServiceException;
 import org.oransc.policyagent.utils.LoggingUtils;
 
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
+@ExtendWith(MockitoExtension.class)
+@RunWith(MockitoJUnitRunner.class)
 public class ApplicationConfigTest {
 
     private ApplicationConfig appConfigUnderTest;
-    CbsClient cbsClient = mock(CbsClient.class);
+
+    @Mock
+    CbsClient cbsClient;
+
+    public static final ImmutableRicConfig CORRECT_RIC_CONIFG = ImmutableRicConfig.builder() //
+        .name("ric1") //
+        .baseUrl("http://localhost:8080/") //
+        .managedElementIds(new Vector<String>(Arrays.asList("kista_1", "kista_2"))) //
+        .build();
 
     private static EnvProperties properties() {
         return ImmutableEnvProperties.builder() //
@@ -68,6 +89,41 @@ public class ApplicationConfigTest {
             .build();
     }
 
+    @Test
+    public void whenTheConfigurationFits() throws IOException, ServiceException {
+
+        appConfigUnderTest = spy(ApplicationConfig.class);
+        appConfigUnderTest.systemEnvironment = new Properties();
+        // When
+        doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any());
+        doReturn("fileName").when(appConfigUnderTest).getLocalConfigurationFilePath();
+        appConfigUnderTest.initialize();
+
+        // Then
+        verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
+
+        Iterable<RicConfig> ricConfigs = appConfigUnderTest.getRicConfigs();
+        RicConfig ricConfig = ricConfigs.iterator().next();
+        assertThat(ricConfigs).isNotNull();
+        assertThat(ricConfig).isEqualTo(CORRECT_RIC_CONIFG);
+    }
+
+    @Test
+    public void whenFileIsExistsButJsonIsIncorrect() throws IOException, ServiceException {
+
+        appConfigUnderTest = spy(ApplicationConfig.class);
+        appConfigUnderTest.systemEnvironment = new Properties();
+
+        // When
+        doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any());
+        doReturn("fileName").when(appConfigUnderTest).getLocalConfigurationFilePath();
+        appConfigUnderTest.loadConfigurationFromFile();
+
+        // Then
+        verify(appConfigUnderTest, times(1)).loadConfigurationFromFile();
+        Assertions.assertEquals(0, appConfigUnderTest.getRicConfigs().size());
+    }
+
     @Test
     public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
 
@@ -140,4 +196,11 @@ public class ApplicationConfigTest {
         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
     }
 
+    private static InputStream getIncorrectJson() {
+        String string = "{" + //
+            "    \"config\": {" + //
+            "        \"ric\": {"; //
+        return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
+    }
+
 }