[DCAEGEN2] Release SDK libraries version 1.9.5
[dcaegen2/services/sdk.git] / rest-services / cbs-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / cbs / client / impl / CbsClientImplIT.java
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019-2021 Nokia. All rights reserved.
6  * Copyright (C) 2022 AT&T Intellectual Property. All rights reserved.
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 com.google.gson.JsonObject;
25 import io.vavr.collection.Stream;
26 import org.jetbrains.annotations.NotNull;
27 import org.junit.Rule;
28 import org.junit.contrib.java.lang.system.EnvironmentVariables;
29 import org.junit.jupiter.api.AfterAll;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32 import org.onap.dcaegen2.services.sdk.model.streams.RawDataStream;
33 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.KafkaSink;
34 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.KafkaSource;
35 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSink;
36 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.exceptions.HttpException;
37 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer;
38 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
39 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
40 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
41 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.EnvironmentParsingException;
42 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.StreamParsingException;
43 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.DataStreams;
44 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParser;
45 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParsers;
46 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
47 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
48 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration;
49 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
50 import reactor.core.publisher.Flux;
51 import reactor.core.publisher.Mono;
52 import reactor.test.StepVerifier;
53
54 import java.time.Duration;
55
56 import static org.assertj.core.api.Assertions.assertThat;
57 import static org.onap.dcaegen2.services.sdk.model.streams.StreamType.KAFKA;
58 import static org.onap.dcaegen2.services.sdk.model.streams.StreamType.MESSAGE_ROUTER;
59 import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.test.DummyHttpServer.sendResource;
60 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamPredicates.streamOfType;
61
62 /**
63  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
64  * @since February 2019
65  */
66 class CbsClientImplIT {
67
68     private static final String SAMPLE_CONFIG = "/sample_service_config.json";
69     private static final String SAMPLE_ALL = "/sample_all.json";
70     private static final String SAMPLE_KEY = "/sample_key.json";
71     private static final String SAMPLE_CONFIG_KEY = "keystore.path";
72     private static final String EXPECTED_CONFIG_VALUE_FROM_CBS = "/var/run/security/keystore.p12";
73     private static final String CONFIG_MAP_FILE_PATH = "src/test/resources/application_config.yaml";
74     private static CbsClientConfiguration sampleConfigurationCbsSource;
75     private static CbsClientConfiguration sampleConfigurationFileSource;
76     private static DummyHttpServer server;
77
78
79     @Rule
80     public final EnvironmentVariables envs = new EnvironmentVariables();
81
82     @BeforeAll
83     static void setUp() {
84         server = DummyHttpServer.start(routes ->
85                 routes.get("/service_component/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_CONFIG))
86                         .get("/service_component_all/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_ALL))
87                         .get("/sampleKey/dcae-component", (req, resp) -> sendResource(resp, SAMPLE_KEY))
88         );
89         ImmutableCbsClientConfiguration.Builder configBuilder = getConfigBuilder();
90         sampleConfigurationCbsSource = configBuilder.build();
91         sampleConfigurationFileSource = configBuilder.configMapFilePath(CONFIG_MAP_FILE_PATH).build();
92     }
93
94     @AfterAll
95     static void tearDown() {
96         server.close();
97     }
98
99
100     @Test
101     void testCbsClientWithConfigRetrievedFromFileMissingEnv() {
102         // given
103         envs.set("AAF_USER", "");
104         final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfigurationFileSource);
105         final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
106
107         // when
108         final Mono<JsonObject> result = sut.flatMap(cbsClient -> cbsClient.get(request));
109
110         // then
111         StepVerifier.create(result)
112                 .expectErrorSatisfies(ex -> {
113                     assertThat(ex).isInstanceOf(EnvironmentParsingException.class);
114                     assertThat(ex).hasMessageContaining("Cannot read AAF_USER from environment.");
115                 })
116                 .verify(Duration.ofSeconds(5));
117     }
118
119     @Test
120     void testCbsClientWithConfigRetrievedFromFile() {
121         // given
122         envs.set("AAF_USER", "admin");
123         envs.set("AAF_PASSWORD", "admin_secret");
124         final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(sampleConfigurationFileSource);
125         final CbsRequest request = CbsRequests.getConfiguration(RequestDiagnosticContext.create());
126
127         // when
128         final Mono<JsonObject> result = sut.flatMap(cbsClient -> cbsClient.get(request));
129
130         // then
131         StepVerifier.create(result.map(this::sampleConfigValue))
132                 .expectNext(EXPECTED_CONFIG_VALUE_FROM_CBS)
133                 .expectComplete()
134                 .verify(Duration.ofSeconds(5));
135     }
136
137
138     @NotNull
139     private static ImmutableCbsClientConfiguration.Builder getConfigBuilder() {
140         return ImmutableCbsClientConfiguration.builder()
141                 .appName("dcae-component");
142     }
143
144     private String sampleConfigValue(JsonObject obj) {
145         return obj.get(SAMPLE_CONFIG_KEY).getAsString();
146     }
147
148 }