Add more configuration unit test 93/1993/2
authorYongchaoWu <yongchao.wu@est.tech>
Tue, 10 Dec 2019 21:53:54 +0000 (22:53 +0100)
committeryongchao <yongchao.wu@est.tech>
Wed, 11 Dec 2019 12:36:12 +0000 (13:36 +0100)
Issue-ID: NONRTRIC-79
Signed-off-by: YongchaoWu <yongchao.wu@est.tech>
Change-Id: Ie98f652831f6ceebe7010cf0a9a6a3a4de4a4364

policy-agent/pom.xml
policy-agent/src/test/java/org/oransc/policyagent/configuration/ApplicationConfigTest.java

index 06036db..cca5bf6 100644 (file)
         <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter-engine</artifactId>
+            <version>${junit-jupiter.version}</version>
             <scope>test</scope>
         </dependency>
     </dependencies>
         <springfox.version>2.8.0</springfox.version>
         <immutable.version>2.7.1</immutable.version>
         <sdk.version>1.1.6</sdk.version>
+        <junit-jupiter.version>5.4.0</junit-jupiter.version>
     </properties>
     <build>
         <plugins>
index c8bbdfd..d4f9e2a 100644 (file)
  */
 package org.oransc.policyagent.configuration;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.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,13 +44,16 @@ 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.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;
@@ -59,6 +65,12 @@ public class ApplicationConfigTest {
     private ApplicationConfig appConfigUnderTest;
     CbsClient cbsClient = mock(CbsClient.class);
 
+    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() //
             .consulHost("host") //
@@ -68,6 +80,39 @@ 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());
+        appConfigUnderTest.initialize();
+
+        // Then
+        verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(any());
+
+        Vector<RicConfig> ricConfigs = appConfigUnderTest.getRicConfigs();
+        RicConfig ricConfig = ricConfigs.firstElement();
+        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());
+        appConfigUnderTest.loadConfigurationFromFile(any());
+
+        // Then
+        verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(any());
+        Assertions.assertNull(appConfigUnderTest.getRicConfigs());
+    }
+
     @Test
     public void whenPeriodicConfigRefreshNoEnvironmentVariables() {
 
@@ -140,4 +185,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)));
+    }
+
 }