2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2021 Nokia. All rights reserved.
6 * Copyright (C) 2021 Wipro Limited.
7 * =========================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=====================================
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
24 import static org.assertj.core.api.Assertions.assertThat;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonObject;
29 import com.google.gson.stream.JsonReader;
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
34 import org.junit.Rule;
35 import org.junit.contrib.java.lang.system.EnvironmentVariables;
36 import org.junit.jupiter.api.Test;
37 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
38 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
39 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
41 public class CbsClientConfigMapTest {
43 private static final String SAMPLE_EXPECTED_CONFIG = "src/test/resources/sample_expected_service_config.json";
44 private static final String SAMPLE_EXPECTED_POLICY_CONFIG = "src/test/resources/sample_expected_policy_config.json";
45 private static final String SAMPLE_EXPECTED_ALL_CONFIG = "src/test/resources/sample_expected_all_config.json";
47 public final EnvironmentVariables envs = new EnvironmentVariables();
50 void shouldFetchUsingProperConfigMapFile() throws FileNotFoundException {
52 envs.set("AAF_USER", "admin");
53 envs.set("AAF_PASSWORD", "admin_secret");
54 String configMapFilePath = "src/test/resources/application_config.yaml";
55 String policySyncFilePath = "src/test/resources/policies.json";
56 String requestPath = "/service_component/app-name";
57 final CbsClient cut = new CbsClientConfigMap(configMapFilePath, policySyncFilePath, requestPath);
59 RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
62 final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block();
65 assertThat(result).isNotNull();
66 assertThat(result).isEqualTo(convertToJson(new JsonReader(new FileReader(SAMPLE_EXPECTED_CONFIG))));
70 void shouldFetchUsingConfigMapFileAndPolicySyncFile() throws FileNotFoundException {
72 envs.set("AAF_USER", "admin");
73 envs.set("AAF_PASSWORD", "admin_secret");
74 String configMapFilePath = "src/test/resources/application_config.yaml";
75 String policySyncFilePath = "src/test/resources/policies.json";
76 String requestPath = "/service_component_all/app-name";
77 final CbsClient cut = new CbsClientConfigMap(configMapFilePath, policySyncFilePath, requestPath);
79 RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
82 final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block();
85 assertThat(result).isNotNull();
86 assertThat(result).isEqualTo(convertToJson(new JsonReader(new FileReader(SAMPLE_EXPECTED_POLICY_CONFIG))));
90 void shouldFetchUsingConfigMapFileWhenPolicySyncFileAbsent() throws FileNotFoundException {
92 envs.set("AAF_USER", "admin");
93 envs.set("AAF_PASSWORD", "admin_secret");
94 String configMapFilePath = "src/test/resources/application_config.yaml";
95 String policySyncFilePath = "";
96 String requestPath = "/service_component_all/app-name";
97 final CbsClient cut = new CbsClientConfigMap(configMapFilePath, policySyncFilePath, requestPath);
99 RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
102 final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block();
105 assertThat(result).isNotNull();
106 assertThat(result).isEqualTo(convertToJson(new JsonReader(new FileReader(SAMPLE_EXPECTED_ALL_CONFIG))));
109 private JsonObject convertToJson(JsonReader jsonReader) {
110 Gson gson = new GsonBuilder().create();
111 return gson.fromJson(jsonReader, JsonObject.class);