PrhAppConfigTest refactor 36/72736/1
authorpkaras <piotr.karas@nokia.com>
Thu, 15 Nov 2018 08:01:27 +0000 (09:01 +0100)
committerpkaras <piotr.karas@nokia.com>
Thu, 15 Nov 2018 08:01:27 +0000 (09:01 +0100)
Change-Id: Iaa604d04139ca26fcb9a21e03484f72c56f15816
Issue-ID: DCAEGEN2-973
Signed-off-by: piotr.karas <piotr.karas@nokia.com>
prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java
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
prh-app-server/src/test/resources/not_json_object.json [new file with mode: 0644]

index 9d199a1..fd7c892 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.onap.dcaegen2.services.prh.configuration;
 
+import java.util.Objects;
 import java.util.Optional;
 import java.util.function.Predicate;
 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
@@ -139,6 +140,9 @@ public class AppConfig extends PrhAppConfig {
 
     @Override
     public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
+        if (noFileConfiguration(dmaapConsumerConfiguration)) {
+            return null;
+        }
         return new ImmutableDmaapConsumerConfiguration.Builder()
             .dmaapUserPassword(
                 Optional.ofNullable(consumerDmaapUserPassword).filter(isEmpty.negate())
@@ -190,6 +194,9 @@ public class AppConfig extends PrhAppConfig {
 
     @Override
     public AaiClientConfiguration getAaiClientConfiguration() {
+        if (noFileConfiguration(aaiClientConfiguration)) {
+            return null;
+        }
         return new ImmutableAaiClientConfiguration.Builder()
             .aaiHost(Optional.ofNullable(aaiHost).filter(isEmpty.negate()).orElse(aaiClientConfiguration.aaiHost()))
             .aaiPort(
@@ -229,6 +236,9 @@ public class AppConfig extends PrhAppConfig {
 
     @Override
     public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
+        if (noFileConfiguration(dmaapPublisherConfiguration)) {
+            return null;
+        }
         return new ImmutableDmaapPublisherConfiguration.Builder()
             .dmaapContentType(
                 Optional.ofNullable(producerDmaapContentType).filter(isEmpty.negate())
@@ -268,4 +278,8 @@ public class AppConfig extends PrhAppConfig {
                     .orElse(dmaapPublisherConfiguration.enableDmaapCertAuth()))
             .build();
     }
+
+    private boolean noFileConfiguration(Object object) {
+        return Objects.isNull(object);
+    }
 }
index 2b4b201..f66924b 100644 (file)
@@ -95,29 +95,39 @@ public abstract class PrhAppConfig implements Config {
         try (InputStream inputStream = resourceFile.getInputStream()) {
             JsonElement rootElement = getJsonElement(parser, inputStream);
             if (rootElement.isJsonObject()) {
-                aaiClientConfiguration = deserializeType(gsonBuilder,
-                    concatenateJsonObjects(
-                        rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(AAI).getAsJsonObject(AAI_CONFIG),
-                        rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
-                    AaiClientConfiguration.class);
-                dmaapConsumerConfiguration = deserializeType(gsonBuilder,
-                    concatenateJsonObjects(
-                        rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_CONSUMER),
-                        rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
-                    DmaapConsumerConfiguration.class);
-                dmaapPublisherConfiguration = deserializeType(gsonBuilder,
-                    concatenateJsonObjects(
-                        rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(DMAAP).getAsJsonObject(DMAAP_PRODUCER),
-                        rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
-                    DmaapPublisherConfiguration.class);
+                deserializeAaiConfiguration(gsonBuilder, rootElement);
+                deserializeDmaapConsumerConfiguration(gsonBuilder, rootElement);
+                deserializeDmaapPublisherConfiguration(gsonBuilder, rootElement);
             }
-        } catch (IOException e) {
+        }
+        catch (IOException e) {
             LOGGER.warn("Problem with file loading, file ", e);
-        } catch (JsonSyntaxException e) {
-            LOGGER.warn("Problem with Json deserialization", e);
         }
     }
 
+    private void deserializeDmaapPublisherConfiguration(GsonBuilder gsonBuilder, JsonElement rootElement) {
+        dmaapPublisherConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
+                rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
+                        .getAsJsonObject(DMAAP_PRODUCER),
+                    rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
+            DmaapPublisherConfiguration.class);
+    }
+
+    private void deserializeDmaapConsumerConfiguration(GsonBuilder gsonBuilder, JsonElement rootElement) {
+        dmaapConsumerConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
+                rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(DMAAP)
+                        .getAsJsonObject(DMAAP_CONSUMER),
+                rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
+            DmaapConsumerConfiguration.class);
+    }
+
+    private void deserializeAaiConfiguration(GsonBuilder gsonBuilder, JsonElement rootElement) {
+        aaiClientConfiguration = deserializeType(gsonBuilder, concatenateJsonObjects(
+                rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(AAI).getAsJsonObject(AAI_CONFIG),
+                rootElement.getAsJsonObject().getAsJsonObject(CONFIG).getAsJsonObject(SECURITY)),
+                AaiClientConfiguration.class);
+    }
+
     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
         return parser.parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
     }
@@ -130,7 +140,12 @@ public abstract class PrhAppConfig implements Config {
 
     private <T> T deserializeType(@NotNull GsonBuilder gsonBuilder, @NotNull JsonObject jsonObject,
         @NotNull Class<T> type) {
-        return gsonBuilder.create().fromJson(jsonObject, type);
+        try {
+            return gsonBuilder.create().fromJson(jsonObject, type);
+        }  catch (JsonSyntaxException e) {
+            LOGGER.warn("Problem with Json deserialization", e);
+            return null;
+        }
     }
 
     void setResourceFile(Resource resourceFile) {
index 61e17d3..42acc59 100644 (file)
@@ -22,22 +22,14 @@ package org.onap.dcaegen2.services.prh.configuration;
 
 import static java.lang.ClassLoader.getSystemResource;
 import static java.nio.file.Files.readAllBytes;
-import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
-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.verify;
 import static org.mockito.Mockito.when;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParser;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Paths;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -53,118 +45,64 @@ import org.springframework.core.io.Resource;
 @ExtendWith({MockitoExtension.class})
 class PrhAppConfigTest {
 
-    private final String jsonString =
-            new String(readAllBytes(Paths.get(getSystemResource("correct_config.json").toURI())));
-    private final String incorrectJsonString =
-            new String(readAllBytes(Paths.get(getSystemResource("incorrect_config.json").toURI())));
-    private PrhAppConfig prhAppConfig;
+    private static final String CORRECT_CONFIG_FILE = "correct_config.json";
+    private static final String INCORRECT_CONFIG_FILE = "incorrect_config.json";
+    private static final String NOT_JSON_OBJECT_FILE = "not_json_object.json";
     private AppConfig appConfig;
 
-
-    PrhAppConfigTest() throws Exception {
-    }
-
     @BeforeEach
     void setUp() {
-        prhAppConfig = spy(PrhAppConfig.class);
-        appConfig = spy(new AppConfig());
+        appConfig = new AppConfig();
     }
 
     @Test
-    void whenTheConfigurationFits_GetAaiAndDmaapObjectRepresentationConfiguration() {
-        //
-        // Given
-        //
-        InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes(
-                StandardCharsets.UTF_8)));
-        //
-        // When
-        //
-        prhAppConfig.setResourceFile(new InputStreamResource(inputStream));
-        prhAppConfig.initFileStreamReader();
-        appConfig.dmaapConsumerConfiguration = prhAppConfig.getDmaapConsumerConfiguration();
-        appConfig.dmaapPublisherConfiguration = prhAppConfig.getDmaapPublisherConfiguration();
-        appConfig.aaiClientConfiguration = prhAppConfig.getAaiClientConfiguration();
-        //
-        // Then
-        //
-        verify(prhAppConfig).initFileStreamReader();
-        assertNotNull(prhAppConfig.getDmaapConsumerConfiguration());
-        assertNotNull(prhAppConfig.getDmaapPublisherConfiguration());
-        assertNotNull(prhAppConfig.getAaiClientConfiguration());
-        assertEquals(appConfig.getDmaapPublisherConfiguration(), prhAppConfig.getDmaapPublisherConfiguration());
-        assertEquals(appConfig.getDmaapConsumerConfiguration(), prhAppConfig.getDmaapConsumerConfiguration());
-        assertEquals(appConfig.getAaiClientConfiguration(), prhAppConfig.getAaiClientConfiguration());
-
+    void whenTheConfigurationFits() throws Exception {
+        InputStream inputStream = createInputStream(CORRECT_CONFIG_FILE);
+        appConfig.setResourceFile(new InputStreamResource(inputStream));
+        appConfig.initFileStreamReader();
+
+        assertNotNull(appConfig.getDmaapConsumerConfiguration());
+        assertNotNull(appConfig.getDmaapPublisherConfiguration());
+        assertNotNull(appConfig.getAaiClientConfiguration());
     }
 
     @Test
-    void whenFileIsNotExist_ThrowIoException() throws IOException {
-        //
-        // Given
-        InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes(
-                StandardCharsets.UTF_8)));
+    void whenFileDoesNotExist() throws Exception {
+        InputStream inputStream = createInputStream(CORRECT_CONFIG_FILE);
         Resource resource = spy(new InputStreamResource(inputStream));
-        //
         when(resource.getInputStream()).thenThrow(new IOException());
-        prhAppConfig.setResourceFile(resource);
-        //
-        // When
-        //
-        prhAppConfig.initFileStreamReader();
-        //
-        // Then
-        //
-        verify(prhAppConfig).initFileStreamReader();
-        assertNull(prhAppConfig.getAaiClientConfiguration());
-        assertNull(prhAppConfig.getDmaapConsumerConfiguration());
-        assertNull(prhAppConfig.getDmaapPublisherConfiguration());
+        appConfig.setResourceFile(resource);
+        appConfig.initFileStreamReader();
 
+        assertNull(appConfig.getAaiClientConfiguration());
+        assertNull(appConfig.getDmaapConsumerConfiguration());
+        assertNull(appConfig.getDmaapPublisherConfiguration());
     }
 
     @Test
-    void whenFileIsExistsButJsonIsIncorrect() {
-        //
-        // Given
-        //
-        InputStream inputStream = new ByteArrayInputStream((incorrectJsonString.getBytes(
-                StandardCharsets.UTF_8)));
-        //
-        // When
-        //
-        prhAppConfig.setResourceFile(new InputStreamResource(inputStream));
-        prhAppConfig.initFileStreamReader();
-
-        //
-        // Then
-        //
-        verify(prhAppConfig).initFileStreamReader();
-        assertNotNull(prhAppConfig.getAaiClientConfiguration());
-        assertNotNull(prhAppConfig.getDmaapConsumerConfiguration());
-        assertNull(prhAppConfig.getDmaapPublisherConfiguration());
-
+    void whenFileExistsButDmaapPublisherJsonConfigurationIsIncorrect() throws Exception {
+        InputStream inputStream = createInputStream(INCORRECT_CONFIG_FILE);
+        appConfig.setResourceFile(new InputStreamResource(inputStream));
+        appConfig.initFileStreamReader();
+
+        assertNotNull(appConfig.getAaiClientConfiguration());
+        assertNotNull(appConfig.getDmaapConsumerConfiguration());
+        assertNull(appConfig.getDmaapPublisherConfiguration());
     }
 
-
     @Test
-    void whenTheConfigurationFits_ButRootElementIsNotAJsonObject() {
-        // Given
-        InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes(
-                StandardCharsets.UTF_8)));
-        // When
-        prhAppConfig.setResourceFile(new InputStreamResource(inputStream));
-        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).initFileStreamReader();
-        assertNull(prhAppConfig.getAaiClientConfiguration());
-        assertNull(prhAppConfig.getDmaapConsumerConfiguration());
-        assertNull(prhAppConfig.getDmaapPublisherConfiguration());
+    void whenRootElementIsNotAJsonObject() throws Exception {
+        InputStream inputStream = createInputStream(NOT_JSON_OBJECT_FILE);
+        appConfig.setResourceFile(new InputStreamResource(inputStream));
+        appConfig.initFileStreamReader();
+
+
+        assertNull(appConfig.getAaiClientConfiguration());
+        assertNull(appConfig.getDmaapConsumerConfiguration());
+        assertNull(appConfig.getDmaapPublisherConfiguration());
+    }
+
+    private InputStream createInputStream(String jsonFile) throws Exception {
+        return new ByteArrayInputStream(readAllBytes(Paths.get(getSystemResource(jsonFile).toURI())));
     }
 }
\ No newline at end of file
diff --git a/prh-app-server/src/test/resources/not_json_object.json b/prh-app-server/src/test/resources/not_json_object.json
new file mode 100644 (file)
index 0000000..f50456e
--- /dev/null
@@ -0,0 +1 @@
+["a", "d", "d"]