[CBS-CLIENT] Increase code coverage 09/132309/2
authorPiotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Fri, 18 Nov 2022 14:34:05 +0000 (15:34 +0100)
committerPiotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Fri, 18 Nov 2022 14:43:35 +0000 (14:43 +0000)
- Add tests for CbsClientConfigMap

Issue-ID: DCAEGEN2-3165
Signed-off-by: Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Change-Id: I0e2f649cd27ad23608cb74aa4035854642236a61

rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientConfigMapTest.java

index aa5655f..ef5f158 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START====================================
  * DCAEGEN2-SERVICES-SDK
  * =========================================================
- * Copyright (C) 2021 Nokia. All rights reserved.
+ * Copyright (C) 2021-2022 Nokia. All rights reserved.
  * Copyright (C) 2021 Wipro Limited.
  * =========================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -33,6 +33,7 @@ import java.io.FileReader;
 
 import org.junit.Rule;
 import org.junit.contrib.java.lang.system.EnvironmentVariables;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
@@ -43,18 +44,22 @@ public class CbsClientConfigMapTest {
     private static final String SAMPLE_EXPECTED_CONFIG = "src/test/resources/sample_expected_service_config.json";
     private static final String SAMPLE_EXPECTED_POLICY_CONFIG = "src/test/resources/sample_expected_policy_config.json";
     private static final String SAMPLE_EXPECTED_ALL_CONFIG = "src/test/resources/sample_expected_all_config.json";
+    private static final String CONFIG_MAP_FILE_PATH = "src/test/resources/application_config.yaml";
     @Rule
     public final EnvironmentVariables envs = new EnvironmentVariables();
 
+    @BeforeEach
+    void setUp() {
+        envs.set("AAF_USER", "admin");
+        envs.set("AAF_PASSWORD", "admin_secret");
+    }
+
     @Test
     void shouldFetchUsingProperConfigMapFile() throws FileNotFoundException {
         // given
-        envs.set("AAF_USER", "admin");
-        envs.set("AAF_PASSWORD", "admin_secret");
-        String configMapFilePath = "src/test/resources/application_config.yaml";
         String policySyncFilePath = "src/test/resources/policies.json";
         String requestPath = "/service_component/app-name";
-        final CbsClient cut = new CbsClientConfigMap(configMapFilePath, policySyncFilePath, requestPath);
+        final CbsClient cut = new CbsClientConfigMap(CONFIG_MAP_FILE_PATH, policySyncFilePath, requestPath);
 
         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
 
@@ -69,12 +74,9 @@ public class CbsClientConfigMapTest {
     @Test
     void shouldFetchUsingConfigMapFileAndPolicySyncFile() throws FileNotFoundException {
         // given
-        envs.set("AAF_USER", "admin");
-        envs.set("AAF_PASSWORD", "admin_secret");
-        String configMapFilePath = "src/test/resources/application_config.yaml";
         String policySyncFilePath = "src/test/resources/policies.json";
         String requestPath = "/service_component_all/app-name";
-        final CbsClient cut = new CbsClientConfigMap(configMapFilePath, policySyncFilePath, requestPath);
+        final CbsClient cut = new CbsClientConfigMap(CONFIG_MAP_FILE_PATH, policySyncFilePath, requestPath);
 
         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
 
@@ -89,12 +91,9 @@ public class CbsClientConfigMapTest {
     @Test
     void shouldFetchUsingConfigMapFileWhenPolicySyncFileAbsent() throws FileNotFoundException {
         // given
-        envs.set("AAF_USER", "admin");
-        envs.set("AAF_PASSWORD", "admin_secret");
-        String configMapFilePath = "src/test/resources/application_config.yaml";
         String policySyncFilePath = "";
         String requestPath = "/service_component_all/app-name";
-        final CbsClient cut = new CbsClientConfigMap(configMapFilePath, policySyncFilePath, requestPath);
+        final CbsClient cut = new CbsClientConfigMap(CONFIG_MAP_FILE_PATH, policySyncFilePath, requestPath);
 
         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
 
@@ -106,6 +105,34 @@ public class CbsClientConfigMapTest {
         assertThat(result).isEqualTo(convertToJson(new JsonReader(new FileReader(SAMPLE_EXPECTED_ALL_CONFIG))));
     }
 
+    @Test
+    void shouldVerifyConfigMapFileWhenFileIsCorrect() {
+        // given
+        String policySyncFilePath = "src/test/resources/policies.json";
+        String requestPath = "/service_component/app-name";
+        final CbsClientConfigMap cut = new CbsClientConfigMap(CONFIG_MAP_FILE_PATH, policySyncFilePath, requestPath);
+
+        //when
+        boolean isCorrect = cut.verifyConfigMapFile();
+
+        // then
+        assertThat(isCorrect).isTrue();
+    }
+
+    @Test
+    void shouldVerifyConfigMapFileWhenFileDoesNotExist() {
+        // given
+        String notExistingFilePath = "";
+        String requestPath = "/service_component/app-name";
+        final CbsClientConfigMap cut = new CbsClientConfigMap(notExistingFilePath, notExistingFilePath, requestPath);
+
+        //when
+        boolean isCorrect = cut.verifyConfigMapFile();
+
+        // then
+        assertThat(isCorrect).isFalse();
+    }
+
     private JsonObject convertToJson(JsonReader jsonReader) {
         Gson gson = new GsonBuilder().create();
         return gson.fromJson(jsonReader, JsonObject.class);