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.dmaap.client.api;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import com.google.gson.JsonPrimitive;
28 import io.vavr.collection.List;
29 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.ImmutableMessageRouterSink;
30 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.ImmutableMessageRouterSource;
31 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSink;
32 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishRequest;
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.ImmutableMessageRouterSubscribeRequest;
35 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeResponse;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
38 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse;
40 import reactor.core.publisher.Flux;
42 final class MessageRouterTestsUtils {
43 private static final JsonParser parser = new JsonParser();
44 private MessageRouterTestsUtils() {}
46 static MessageRouterPublishRequest createPublishRequest(String topicUrl){
47 MessageRouterSink sinkDefinition = ImmutableMessageRouterSink.builder()
52 return ImmutableMessageRouterPublishRequest.builder()
53 .sinkDefinition(sinkDefinition)
57 static MessageRouterSubscribeRequest createMRSubscribeRequest(String topicUrl,
58 String consumerGroup, String consumerId) {
59 ImmutableMessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
64 return ImmutableMessageRouterSubscribeRequest
66 .sourceDefinition(sourceDefinition)
67 .consumerGroup(consumerGroup)
68 .consumerId(consumerId)
72 static List<JsonElement> getAsJsonElements(List<String> messages){
73 return messages.map(parser::parse);
76 static JsonObject getAsJsonObject(String item){
77 return new Gson().fromJson(item, JsonObject.class);
80 static Flux<JsonObject> jsonBatch(List<String> messages){
81 return Flux.fromIterable(messages).map(parser::parse).map(JsonElement::getAsJsonObject);
84 static Flux<JsonPrimitive> plainBatch(List<String> messages){
85 return Flux.fromIterable(messages).map(JsonPrimitive::new);
88 static MessageRouterSubscribeResponse errorSubscribeResponse(String failReasonFormat, Object... formatArgs){
89 return ImmutableMessageRouterSubscribeResponse
91 .failReason(String.format(failReasonFormat, formatArgs))
95 static MessageRouterSubscribeResponse successSubscribeResponse(List<JsonElement> items){
96 return ImmutableMessageRouterSubscribeResponse
102 static MessageRouterPublishResponse errorPublishResponse(String failReasonFormat, Object... formatArgs){
103 return ImmutableMessageRouterPublishResponse
105 .failReason(String.format(failReasonFormat, formatArgs))
109 static MessageRouterPublishResponse successPublishResponse(List<JsonElement> items){
110 return ImmutableMessageRouterPublishResponse
116 static void registerTopic(MessageRouterPublisher publisher, MessageRouterPublishRequest publishRequest,
117 MessageRouterSubscriber subscriber, MessageRouterSubscribeRequest subscribeRequest) {
118 final List<String> sampleJsonMessages = List.of("{\"message\":\"message1\"}",
119 "{\"differentMessage\":\"message2\"}");
120 final Flux<JsonObject> jsonMessageBatch = MessageRouterTestsUtils.jsonBatch(sampleJsonMessages);
122 publisher.put(publishRequest, jsonMessageBatch).blockLast();
123 subscriber.get(subscribeRequest).block();