VESCollector Test optimization
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / common / publishing / PublisherTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VES Collector
4  * ================================================================================
5  * Copyright (C) 2021 Nokia. All rights reserved.
6  * Copyright (C) 2023 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 package org.onap.dcae.common.publishing;
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.Assume;
28 import org.junit.Before;
29
30 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishResponse;
31 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
32 import org.testcontainers.containers.DockerComposeContainer;
33 import org.testcontainers.junit.jupiter.Container;
34 import org.testcontainers.junit.jupiter.Testcontainers;
35 import reactor.core.publisher.Flux;
36 import reactor.test.StepVerifier;
37
38 import java.time.Duration;
39
40 import static org.onap.dcae.common.publishing.DMaapContainer.createContainerInstance;
41 import static org.onap.dcae.common.publishing.DmaapRequestConfiguration.getAsJsonElements;
42
43
44 @Testcontainers(disabledWithoutDocker = true)
45 public class PublisherTest  {
46
47     @Container
48     private final DockerComposeContainer CONTAINER = createContainerInstance();
49     
50     @Before
51     public void linuxOnly() {
52         Assume.assumeFalse
53         (System.getProperty("os.name").toLowerCase().startsWith("win"));
54     }
55
56     @Test
57     public void publishEvent_shouldSuccessfullyPublishSingleMessage() {
58         //given
59         final Publisher publisher = new Publisher();
60         final String simpleEvent = "{\"message\":\"message1\"}";
61         final List<String> twoJsonMessages = List.of(simpleEvent);
62         final MessageRouterPublishResponse expectedResponse = successPublishResponse(getAsJsonElements(twoJsonMessages));
63
64         //when
65         final Flux<MessageRouterPublishResponse> result = publisher.publishEvents(twoJsonMessages, createPublishConfig());
66
67         //then
68         StepVerifier.create(result)
69                 .expectNext(expectedResponse)
70                 .expectComplete()
71                 .verify(Duration.ofSeconds(10));
72     }
73
74
75     private Option<PublisherConfig> createPublishConfig() {
76         List<String> desc = List.of("127.0.0.1:3904");
77         PublisherConfig conf = new PublisherConfig(desc, "topic");
78         return Option.of(conf);
79     }
80
81     private MessageRouterPublishResponse successPublishResponse(List<JsonElement> items) {
82         return ImmutableMessageRouterPublishResponse
83                 .builder()
84                 .items(items)
85                 .build();
86     }
87
88 }