8561e0b00d0a88f3015dd18ba2cd7c6308bf4278
[dcaegen2/services/sdk.git] / rest-services / dmaap-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / dmaap / client / MessageRouterTestsUtils.java
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019-2020 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
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client;
22
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.api.MessageRouterPublisher;
33 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterSubscriber;
34 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishRequest;
35 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishResponse;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeRequest;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeResponse;
38 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse;
42 import reactor.core.publisher.Flux;
43
44
45 public final class MessageRouterTestsUtils {
46     private MessageRouterTestsUtils() {}
47
48     public static MessageRouterPublishRequest createPublishRequest(String topicUrl){
49         return createPublishRequest(topicUrl, ContentType.APPLICATION_JSON);
50     }
51
52     public static MessageRouterPublishRequest createPublishRequest(String topicUrl, ContentType contentType){
53         MessageRouterSink sinkDefinition = ImmutableMessageRouterSink.builder()
54                 .name("the topic")
55                 .topicUrl(topicUrl)
56                 .build();
57
58         return ImmutableMessageRouterPublishRequest.builder()
59                 .sinkDefinition(sinkDefinition)
60                 .contentType(contentType)
61                 .build();
62     }
63
64     public static MessageRouterSubscribeRequest createMRSubscribeRequest(String topicUrl,
65             String consumerGroup, String consumerId) {
66         ImmutableMessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
67                 .name("the topic")
68                 .topicUrl(topicUrl)
69                 .build();
70
71         return ImmutableMessageRouterSubscribeRequest
72                 .builder()
73                 .sourceDefinition(sourceDefinition)
74                 .consumerGroup(consumerGroup)
75                 .consumerId(consumerId)
76                 .build();
77     }
78
79     public static List<JsonElement> getAsJsonElements(List<String> messages){
80         return messages.map(JsonParser::parseString);
81     }
82
83     public static List<JsonObject> getAsJsonObjects(List<String> messages){
84         return getAsJsonElements(messages).map(JsonElement::getAsJsonObject);
85     }
86
87     public static List<JsonPrimitive> getAsJsonPrimitives(List<String> messages){
88         return getAsJsonElements(messages).map(JsonElement::getAsJsonPrimitive);
89     }
90
91     public static JsonObject getAsJsonObject(String item){
92         return new Gson().fromJson(item, JsonObject.class);
93     }
94
95     public static Flux<JsonElement> plainBatch(List<String> messages){
96         return Flux.fromIterable(getAsJsonElements(messages));
97     }
98
99     public static Flux<JsonObject> jsonBatch(List<String> messages){
100         return Flux.fromIterable(getAsJsonObjects(messages));
101     }
102
103     public static MessageRouterSubscribeResponse errorSubscribeResponse(String failReasonFormat, Object... formatArgs){
104         return ImmutableMessageRouterSubscribeResponse
105                 .builder()
106                 .failReason(String.format(failReasonFormat, formatArgs))
107                 .build();
108     }
109
110     public static MessageRouterSubscribeResponse successSubscribeResponse(List<JsonElement> items){
111         return ImmutableMessageRouterSubscribeResponse
112                 .builder()
113                 .items(items)
114                 .build();
115     }
116
117     public static MessageRouterPublishResponse errorPublishResponse(String failReasonFormat, Object... formatArgs){
118         return ImmutableMessageRouterPublishResponse
119                 .builder()
120                 .failReason(String.format(failReasonFormat, formatArgs))
121                 .build();
122     }
123
124     public static MessageRouterPublishResponse successPublishResponse(List<JsonElement> items){
125         return ImmutableMessageRouterPublishResponse
126                 .builder()
127                 .items(items)
128                 .build();
129     }
130
131     public static void registerTopic(MessageRouterPublisher publisher, MessageRouterPublishRequest publishRequest,
132             MessageRouterSubscriber subscriber, MessageRouterSubscribeRequest subscribeRequest) {
133         final List<String> sampleJsonMessages = List.of("{\"message\":\"message1\"}",
134                 "{\"differentMessage\":\"message2\"}");
135         final Flux<JsonObject> jsonMessageBatch = MessageRouterTestsUtils.jsonBatch(sampleJsonMessages);
136
137         publisher.put(publishRequest, jsonMessageBatch).blockLast();
138         subscriber.get(subscribeRequest).block();
139     }
140 }