Improve tests in PrhAppConfig 63/48463/1
authorwasala <przemyslaw.wasala@nokia.com>
Tue, 22 May 2018 10:42:16 +0000 (12:42 +0200)
committerwasala <przemyslaw.wasala@nokia.com>
Tue, 22 May 2018 10:42:16 +0000 (12:42 +0200)
Change-Id: I23b3bd3ba89d9df7451a7e2251b1bfc98adf3a2b
Issue-ID: DCAEGEN2-396
Signed-off-by: wasala <przemyslaw.wasala@nokia.com>
pom.xml
prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java
prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java

diff --git a/pom.xml b/pom.xml
index 12c8e51..41f6f8c 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                 <goal>check</goal>
               </goals>
               <configuration>
+                <excludes>
+                  <exclude>**/Immutable*</exclude>
+                  <exclude>**/GsonAdapters*</exclude>
+                  <exclude>**/*ForUnitTest*</exclude>
+                </excludes>
                 <rules>
                   <rule>
                     <element>CLASS</element>
                     <limits>
                       <limit>
-                        <counter>LINE</counter>
                         <value>COVEREDRATIO</value>
                         <!--<minimum>0.70</minimum>-->
                       </limit>
index ece1621..6d24ade 100644 (file)
@@ -84,7 +84,7 @@ public abstract class PrhAppConfig implements Config {
         JsonParser parser = new JsonParser();
         JsonObject jsonObject;
         try (InputStream inputStream = getInputStream(filepath)) {
-            JsonElement rootElement = parser.parse(new InputStreamReader(inputStream));
+            JsonElement rootElement = getJsonElement(parser, inputStream);
             if (rootElement.isJsonObject()) {
                 jsonObject = rootElement.getAsJsonObject();
                 aaiClientConfiguration = deserializeType(gsonBuilder,
@@ -99,8 +99,6 @@ public abstract class PrhAppConfig implements Config {
                     jsonObject.getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
                     DmaapPublisherConfiguration.class);
             }
-        } catch (FileNotFoundException e) {
-            logger.warn("File doesn't exist in filepath: {}", filepath, e);
         } catch (IOException e) {
             logger.warn("Problem with file loading, file: {}", filepath, e);
         } catch (JsonSyntaxException e) {
@@ -108,12 +106,16 @@ public abstract class PrhAppConfig implements Config {
         }
     }
 
+    JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
+        return parser.parse(new InputStreamReader(inputStream));
+    }
+
     private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
         @NotNull Class<T> type) {
         return gsonBuilder.create().fromJson(jsonObject, type);
     }
 
-    InputStream getInputStream(@NotNull String filepath) throws FileNotFoundException {
+    InputStream getInputStream(@NotNull String filepath) throws IOException {
         return new BufferedInputStream(new FileInputStream(filepath));
     }
 
index 2efffd8..8a1d0ec 100644 (file)
@@ -22,12 +22,16 @@ package org.onap.dcaegen2.services.prh.configuration;
 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.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
 import java.io.ByteArrayInputStream;
-import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.Objects;
@@ -35,6 +39,7 @@ import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.function.Executable;
 import org.onap.dcaegen2.services.prh.IT.junit5.mockito.MockitoExtension;
 
 /**
@@ -74,7 +79,7 @@ class PrhAppConfigTest {
 
     @Test
     public void whenTheConfigurationFits_GetAaiAndDmaapObjectRepresentationConfiguration()
-        throws FileNotFoundException {
+        throws IOException {
         //
         // Given
         //
@@ -107,7 +112,7 @@ class PrhAppConfigTest {
     }
 
     @Test
-    public void whenFileIsNotExist_ThrowFileNotFoundException() {
+    public void whenFileIsNotExist_ThrowIOException() {
         //
         // Given
         //
@@ -129,7 +134,7 @@ class PrhAppConfigTest {
     }
 
     @Test
-    public void whenFileIsExistsButJsonIsIncorrect() throws FileNotFoundException {
+    public void whenFileIsExistsButJsonIsIncorrect() throws IOException {
         //
         // Given
         //
@@ -151,6 +156,31 @@ class PrhAppConfigTest {
         Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration());
         Assertions.assertNull(prhAppConfig.getDmaapPublisherConfiguration());
 
+    }
+
+
+    @Test
+    public void whenTheConfigurationFits_ButRootElementIsNotAJsonObject()
+        throws IOException {
+        // Given
+        InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes(
+            StandardCharsets.UTF_8)));
+        // When
+        prhAppConfig.setFilepath(filePath);
+        doReturn(inputStream).when(prhAppConfig).getInputStream(any());
+        JsonElement jsonElement = mock(JsonElement.class);
+        when(jsonElement.isJsonObject()).thenReturn(false);
+        doReturn(jsonElement).when(prhAppConfig).getJsonElement(any(JsonParser.class), any(InputStream.class));
+        prhAppConfig.initFileStreamReader();
+        appConfig.dmaapConsumerConfiguration = prhAppConfig.getDmaapConsumerConfiguration();
+        appConfig.dmaapPublisherConfiguration = prhAppConfig.getDmaapPublisherConfiguration();
+        appConfig.aaiClientConfiguration = prhAppConfig.getAAIClientConfiguration();
 
+        // Then
+        verify(prhAppConfig, times(1)).setFilepath(anyString());
+        verify(prhAppConfig, times(1)).initFileStreamReader();
+        Assertions.assertNull(prhAppConfig.getAAIClientConfiguration());
+        Assertions.assertNull(prhAppConfig.getDmaapConsumerConfiguration());
+        Assertions.assertNull(prhAppConfig.getDmaapPublisherConfiguration());
     }
 }
\ No newline at end of file