2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 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=========================================================
20 package org.onap.dcae.common.publishing;
23 import com.google.gson.JsonElement;
24 import io.vavr.collection.List;
25 import io.vavr.control.Option;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockserver.integration.ClientAndServer;
29 import org.mockserver.junit.jupiter.MockServerExtension;
30 import org.mockserver.junit.jupiter.MockServerSettings;
31 import org.mockserver.matchers.Times;
32 import org.mockserver.verify.VerificationTimes;
33 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishResponse;
34 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
35 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.ImmutableDmaapConnectionPoolConfig;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.ImmutableMessageRouterPublisherConfig;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.MessageRouterPublisherConfig;
38 import reactor.core.publisher.Flux;
39 import reactor.test.StepVerifier;
41 import java.time.Duration;
42 import java.util.concurrent.TimeUnit;
44 import static org.mockserver.model.HttpRequest.request;
45 import static org.mockserver.model.HttpResponse.response;
46 import static org.onap.dcae.common.publishing.DmaapRequestConfiguration.createPublishRequest;
47 import static org.onap.dcae.common.publishing.DmaapRequestConfiguration.getAsJsonElements;
49 @ExtendWith(MockServerExtension.class)
50 @MockServerSettings(ports = {1080, 8888})
51 class PublisherTestMockServer {
53 private static final int MAX_IDLE_TIME = 10;
54 private static final int MAX_LIFE_TIME = 20;
55 private static final int CONNECTION_POOL = 1;
56 private static final String TOPIC = "TOPIC10";
57 private static final String PATH = String.format("/events/%s/", TOPIC);
59 private static final String TIMEOUT_ERROR_MESSAGE = "408 Request Timeout\n"
63 + "\"serviceException\":"
65 + "\"messageId\":\"SVC0001\","
66 + "\"text\":\"Client timeout exception occurred, Error code is %1\","
67 + "\"variables\":[\"408\"]"
72 private final ClientAndServer client;
74 public PublisherTestMockServer(ClientAndServer client) {
79 void publisher_shouldHandleClientTimeoutErrorWhenTimeoutDefined() {
81 final Long timeoutSec = 1L;
82 final Publisher publisher = new Publisher(connectionPoolConfiguration());
83 final String simpleEvent = "{\"message\":\"message1\"}";
84 final MessageRouterPublishResponse expectedResponse = errorPublishResponse(TIMEOUT_ERROR_MESSAGE);
86 final String path = String.format("/events/%s/", TOPIC);
87 client.when(request().withPath(path), Times.once())
88 .respond(response().withDelay(TimeUnit.SECONDS, 2));
89 List<String> events = List.of(simpleEvent);
92 final Flux<MessageRouterPublishResponse> result = publisher.publishEvents(events, createPublishRequest(createPublishConfig(), timeoutSec));
96 StepVerifier.create(result)
97 .expectNext(expectedResponse)
99 .verify(Duration.ofSeconds(10));
102 client.verify(request().withPath(path), VerificationTimes.exactly(1));
107 void publishEvent_shouldSuccessfullyPublishSingleMessage() {
109 final Publisher publisher = new Publisher();
110 final String simpleEvent = "{\"message\":\"message1\"}";
111 final List<String> twoJsonMessages = List.of(simpleEvent);
112 final MessageRouterPublishResponse expectedResponse = successPublishResponse(getAsJsonElements(twoJsonMessages));
113 client.when(request().withPath(PATH), Times.once())
114 .respond(response());
117 final Flux<MessageRouterPublishResponse> result = publisher.publishEvents(List.of(simpleEvent), createPublishConfig());
120 StepVerifier.create(result)
121 .expectNext(expectedResponse)
123 .verify(Duration.ofSeconds(10));
126 private Option<PublisherConfig> createPublishConfig() {
127 List<String> desc = List.of("localhost:1080");
128 PublisherConfig conf = new PublisherConfig(desc, TOPIC);
129 return Option.of(conf);
132 private MessageRouterPublishResponse successPublishResponse(List<JsonElement> items) {
133 return ImmutableMessageRouterPublishResponse
139 public static MessageRouterPublishResponse errorPublishResponse(String failReasonFormat, Object... formatArgs) {
140 String failReason = formatArgs.length == 0 ? failReasonFormat : String.format(failReasonFormat, formatArgs);
141 return ImmutableMessageRouterPublishResponse
143 .failReason(failReason)
147 public MessageRouterPublisherConfig connectionPoolConfiguration() {
148 return ImmutableMessageRouterPublisherConfig.builder()
149 .connectionPoolConfig(ImmutableDmaapConnectionPoolConfig.builder()
150 .connectionPool(CONNECTION_POOL)
151 .maxIdleTime(MAX_IDLE_TIME)
152 .maxLifeTime(MAX_LIFE_TIME)