Replace cambria with DmaaP client
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / common / publishing / PublisherTestMockServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VES Collector
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.common.publishing;
21
22
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;
40
41 import java.time.Duration;
42 import java.util.concurrent.TimeUnit;
43
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;
48
49 @ExtendWith(MockServerExtension.class)
50 @MockServerSettings(ports = {1080, 8888})
51 class PublisherTestMockServer {
52
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);
58
59     private static final String TIMEOUT_ERROR_MESSAGE = "408 Request Timeout\n"
60             + "{"
61             + "\"requestError\":"
62             + "{"
63             + "\"serviceException\":"
64             + "{"
65             + "\"messageId\":\"SVC0001\","
66             + "\"text\":\"Client timeout exception occurred, Error code is %1\","
67             + "\"variables\":[\"408\"]"
68             + "}"
69             + "}"
70             + "}";
71
72     private final ClientAndServer client;
73
74     public PublisherTestMockServer(ClientAndServer client) {
75         this.client = client;
76     }
77
78     @Test
79     void publisher_shouldHandleClientTimeoutErrorWhenTimeoutDefined() {
80         //given
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);
85
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);
90
91         //when
92         final Flux<MessageRouterPublishResponse> result = publisher.publishEvents(events, createPublishRequest(createPublishConfig(), timeoutSec));
93
94
95
96         StepVerifier.create(result)
97                 .expectNext(expectedResponse)
98                 .expectComplete()
99                 .verify(Duration.ofSeconds(10));
100
101         //then
102         client.verify(request().withPath(path), VerificationTimes.exactly(1));
103
104     }
105
106     @Test
107     void publishEvent_shouldSuccessfullyPublishSingleMessage() {
108         //given
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());
115
116         //when
117         final Flux<MessageRouterPublishResponse> result = publisher.publishEvents(List.of(simpleEvent), createPublishConfig());
118
119         //then
120         StepVerifier.create(result)
121                 .expectNext(expectedResponse)
122                 .expectComplete()
123                 .verify(Duration.ofSeconds(10));
124     }
125
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);
130     }
131
132     private MessageRouterPublishResponse successPublishResponse(List<JsonElement> items) {
133         return ImmutableMessageRouterPublishResponse
134                 .builder()
135                 .items(items)
136                 .build();
137     }
138
139     public static MessageRouterPublishResponse errorPublishResponse(String failReasonFormat, Object... formatArgs) {
140         String failReason = formatArgs.length == 0 ? failReasonFormat : String.format(failReasonFormat, formatArgs);
141         return ImmutableMessageRouterPublishResponse
142                 .builder()
143                 .failReason(failReason)
144                 .build();
145     }
146
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)
153                         .build())
154                 .build();
155     }
156 }