diff options
Diffstat (limited to 'rest-services/cbs-client/src/test')
5 files changed, 285 insertions, 14 deletions
diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java new file mode 100644 index 00000000..761cc5c1 --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java @@ -0,0 +1,96 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; + +import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendResource; +import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendString; + +import com.google.gson.JsonObject; +import java.time.Duration; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +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.CbsClientFactory; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.ImmutableEnvProperties; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +/** + * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> + * @since February 2019 + */ +class CbsClientImplIT { + + private static final String CONSUL_RESP = "[\n" + + " {\n" + + " \"ServiceAddress\": \"HOST\",\n" + + " \"ServiceName\": \"the_cbs\",\n" + + " \"ServicePort\": PORT\n" + + " }\n" + + "]\n"; + private static final String RES_CONFIG = "/sample_config.json"; + private static DummyHttpServer server; + + @BeforeAll + static void setUp() { + server = DummyHttpServer.start(routes -> + routes.get("/v1/catalog/service/the_cbs", (req, resp) -> sendString(resp, lazyConsulResponse())) + .get("/service_component/dcae-component", (req, resp) -> sendResource(resp, RES_CONFIG))); + } + + @AfterAll + static void tearDown() { + server.close(); + } + + @Test + void testCbsClient() { + // given + final EnvProperties env = ImmutableEnvProperties.builder() + .appName("dcae-component") + .cbsName("the_cbs") + .consulHost(server.host()) + .consulPort(server.port()) + .build(); + final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(env); + + // when + final Mono<JsonObject> result = sut.flatMap(CbsClient::get); + + // then + StepVerifier.create(result.map(obj -> obj.get("keystore.path").getAsString())) + .expectNext("/var/run/security/keystore.p12") + .expectComplete() + .verify(Duration.ofSeconds(5)); + } + + private static Mono<String> lazyConsulResponse() { + return Mono.just(CONSUL_RESP) + .map(CbsClientImplIT::processConsulResponseTemplate); + } + + private static String processConsulResponseTemplate(String resp) { + return resp.replaceAll("HOST", server.host()) + .replaceAll("PORT", Integer.toString(server.port())); + } +} diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java new file mode 100644 index 00000000..65284c5f --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java @@ -0,0 +1,59 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import com.google.gson.JsonObject; +import java.net.InetSocketAddress; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.adapters.CloudHttpClient; +import reactor.core.publisher.Mono; + +/** + * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> + * @since February 2019 + */ +class CbsClientImplTest { + private final CloudHttpClient httpClient = mock(CloudHttpClient.class); + + @Test + void shouldFetchUsingProperUrl() { + // given + InetSocketAddress cbsAddress = InetSocketAddress.createUnresolved("cbshost", 6969); + String serviceName = "dcaegen2-ves-collector"; + final CbsClientImpl cut = CbsClientImpl.create(httpClient, cbsAddress, serviceName); + final JsonObject httpResponse = new JsonObject(); + given(httpClient.callHttpGet(anyString(), any(Class.class))).willReturn(Mono.just(httpResponse)); + + // when + final JsonObject result = cut.get().block(); + + // then + verify(httpClient).callHttpGet("http://cbshost:6969/service_component/dcaegen2-ves-collector", JsonObject.class); + assertThat(result).isSameAs(httpResponse); + } +}
\ No newline at end of file diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java new file mode 100644 index 00000000..d0485f57 --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java @@ -0,0 +1,91 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2019 Nokia. All rights reserved. + * ========================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; + +import io.vavr.CheckedFunction0; +import io.vavr.Function0; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.function.Consumer; +import org.reactivestreams.Publisher; +import reactor.core.publisher.Mono; +import reactor.netty.DisposableServer; +import reactor.netty.http.server.HttpServer; +import reactor.netty.http.server.HttpServerResponse; +import reactor.netty.http.server.HttpServerRoutes; + +/** + * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> + * @since February 2019 + */ +public class DummyHttpServer { + + private final DisposableServer server; + + private DummyHttpServer(DisposableServer server) { + this.server = server; + } + + public static DummyHttpServer start(Consumer<HttpServerRoutes> routes) { + return new DummyHttpServer(HttpServer.create() + .host("127.0.0.1") + .route(routes) + .bind() + .block()); + } + + public static Publisher<Void> sendResource(HttpServerResponse httpServerResponse, String resourcePath) { + return sendString(httpServerResponse, Mono.fromCallable(() -> readResource(resourcePath))); + } + + public static Publisher<Void> sendString(HttpServerResponse httpServerResponse, Publisher<String> content) { + return httpServerResponse.sendString(content); + } + + public void close() { + server.disposeNow(); + } + + public String host() { + return server.host(); + } + + public int port() { + return server.port(); + } + + private static String readResource(String resourcePath) { + try { + return CheckedFunction0.constant(resourcePath) + .andThen(DummyHttpServer.class::getResource) + .andThen(URL::toURI) + .andThen(Paths::get) + .andThen(Files::readAllBytes) + .andThen(String::new) + .apply(); + } catch (Throwable throwable) { + throw new RuntimeException(throwable); + } + } +} diff --git a/rest-services/cbs-client/src/test/resources/logback-test.xml b/rest-services/cbs-client/src/test/resources/logback-test.xml index c1f00665..8e468cfb 100644 --- a/rest-services/cbs-client/src/test/resources/logback-test.xml +++ b/rest-services/cbs-client/src/test/resources/logback-test.xml @@ -1,21 +1,43 @@ <?xml version="1.0" encoding="UTF-8"?> <!-- - ~ ===============================LICENSE_START====================================== - ~ Copyright © 2017 AT&T Intellectual Property. All rights reserved. + ~ ============LICENSE_START======================================================= + ~ dcaegen2-collectors-veshv ~ ================================================================================ - ~ Licensed under the Apache License, Version 2.0 (the "License"); - ~ you may not use this file except in compliance with the License. - ~ You may obtain a copy of the License at + ~ Copyright (C) 2018 NOKIA + ~ ================================================================================ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at ~ - ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ http://www.apache.org/licenses/LICENSE-2.0 ~ - ~ Unless required by applicable law or agreed to in writing, software - ~ distributed under the License is distributed on an "AS IS" BASIS, - ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - ~ See the License for the specific language governing permissions and - ~ limitations under the License. - ~ ============================LICENSE_END=========================================== - --> + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + ~ ============LICENSE_END========================================================= +--> <configuration> - <root level="OFF"/> + <property name="p_tim" value="%date{"yyyy-MM-dd'T'HH:mm:ss.SSSXXX", UTC}"/> + <property name="p_lvl" value="%highlight(%-5level)"/> + <property name="p_log" value="%50.50logger"/> + <property name="SIMPLE_LOG_PATTERN" value=" +%nopexception +| ${p_tim}\t +| ${p_log}\t +| ${p_lvl}\t +| %msg%n"/> + + <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>${SIMPLE_LOG_PATTERN}</pattern> + </encoder> + </appender> + + <logger name="org.onap.dcaegen2.services.sdk" level="TRACE"/> + + <root level="INFO"> + <appender-ref ref="CONSOLE"/> + </root> </configuration> diff --git a/rest-services/cbs-client/src/test/resources/sample_config.json b/rest-services/cbs-client/src/test/resources/sample_config.json new file mode 100644 index 00000000..a95b723f --- /dev/null +++ b/rest-services/cbs-client/src/test/resources/sample_config.json @@ -0,0 +1,3 @@ +{ + "keystore.path": "/var/run/security/keystore.p12" +} |