Update CBS-Client to read policy configuration from a file exposed by policy-sidecar...
[dcaegen2/services/sdk.git] / rest-services / cbs-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / cbs / client / impl / CbsClientConfigMapTest.java
1 /*
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=====================================
20  */
21
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25
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;
30
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
33
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;
40
41 public class CbsClientConfigMapTest {
42
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";
46     @Rule
47     public final EnvironmentVariables envs = new EnvironmentVariables();
48
49     @Test
50     void shouldFetchUsingProperConfigMapFile() throws FileNotFoundException {
51         // given
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);
58
59         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
60
61         // when
62         final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block();
63
64         // then
65         assertThat(result).isNotNull();
66         assertThat(result).isEqualTo(convertToJson(new JsonReader(new FileReader(SAMPLE_EXPECTED_CONFIG))));
67     }
68
69     @Test
70     void shouldFetchUsingConfigMapFileAndPolicySyncFile() throws FileNotFoundException {
71         // given
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);
78
79         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
80
81         // when
82         final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block();
83
84         // then
85         assertThat(result).isNotNull();
86         assertThat(result).isEqualTo(convertToJson(new JsonReader(new FileReader(SAMPLE_EXPECTED_POLICY_CONFIG))));
87     }
88
89     @Test
90     void shouldFetchUsingConfigMapFileWhenPolicySyncFileAbsent() throws FileNotFoundException {
91         // given
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);
98
99         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
100
101         // when
102         final JsonObject result = cut.get(CbsRequests.getConfiguration(diagnosticContext)).block();
103
104         // then
105         assertThat(result).isNotNull();
106         assertThat(result).isEqualTo(convertToJson(new JsonReader(new FileReader(SAMPLE_EXPECTED_ALL_CONFIG))));
107     }
108
109     private JsonObject convertToJson(JsonReader jsonReader) {
110         Gson gson = new GsonBuilder().create();
111         return gson.fromJson(jsonReader, JsonObject.class);
112     }
113 }
114