2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * =========================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.BDDMockito.given;
26 import static org.mockito.Mockito.mock;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonParser;
31 import java.io.InputStreamReader;
32 import java.net.InetSocketAddress;
33 import org.junit.jupiter.api.Test;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.EnvProperties;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.ImmutableEnvProperties;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.adapters.CloudHttpClient;
37 import reactor.core.publisher.Mono;
38 import reactor.test.StepVerifier;
41 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
42 * @since February 2019
46 private final EnvProperties env = ImmutableEnvProperties.builder()
47 .cbsName("cbs-service")
48 .consulHost("consul.local")
50 .appName("whatever").build();
51 private final CloudHttpClient httpClient = mock(CloudHttpClient.class);
52 private final CbsLookup cut = new CbsLookup(httpClient);
55 void lookupShouldReturnValidConfiguration() {
57 givenConsulResponse(parseResource("/consul_cbs_service.json").getAsJsonArray());
60 final InetSocketAddress result = cut.lookup(env).block();
63 assertThat(result.getHostString()).isEqualTo("config-binding-service");
64 assertThat(result.getPort()).isEqualTo(10000);
68 void lookupShouldReturnEmptyResultWhenServiceArrayIsEmpty() {
70 givenConsulResponse(new JsonArray());
73 final Mono<InetSocketAddress> result = cut.lookup(env);
76 StepVerifier.create(result).verifyComplete();
79 private JsonElement parseResource(String resource) {
80 return new JsonParser().parse(new InputStreamReader(CbsLookupTest.class.getResourceAsStream(resource)));
83 private void givenConsulResponse(JsonArray jsonArray) {
84 final String url = "http://"
88 + "/v1/catalog/service/"
90 given(httpClient.callHttpGet(url, JsonArray.class))
91 .willReturn(Mono.just(jsonArray));