d497817f9fb782d3e291bc7fd38f07385b302408
[dcaegen2/services/sdk.git] /
1 /*
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
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.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.dmaap.mr;
21
22 import com.google.gson.Gson;
23 import com.google.gson.JsonObject;
24 import io.vavr.control.Either;
25 import org.junit.jupiter.api.Test;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.StreamParserError;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParser;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParsers;
29 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.DataStreamUtils;
30 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.DataStreamDirection;
31 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.ImmutableRawDataStream;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.RawDataStream;
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.dmaap.MessageRouterSink;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.dmaap.MessageRouterSource;
35
36 import java.io.IOException;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.StreamsConstants.DATA_ROUTER_TYPE;
40 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.StreamsConstants.MESSAGE_ROUTER_TYPE;
41
42 /**
43  * @author <a href="mailto:kornel.janiak@nokia.com">Kornel Janiak</a>
44  */
45
46 public class MessageRouterSourceParserTest {
47
48     private static final String SAMPLE_AAF_USERNAME = "some-user";
49     private static final String SAMPLE_AAF_PASSWORD = "some-password";
50     private static final String SAMPLE_LOCATION = "mtc00";
51     private static final String SAMPLE_CLIENT_ROLE = "com.dcae.member";
52     private static final String SAMPLE_CLIENT_ID = "1500462518108";
53     private static final String SAMPLE_TOPIC_URL = "https://we-are-message-router.us:3905/events/some-topic";
54
55     private final StreamFromGsonParser<MessageRouterSource> streamParser = StreamFromGsonParsers.messageRouterSourceParser();
56
57     @Test
58     void fullConfiguration_shouldGenerateDataRouterSourceObject() throws IOException {
59         // given
60         RawDataStream<JsonObject> input = DataStreamUtils.readSourceFromResource("/streams/message_router_full.json");
61
62         // when
63         MessageRouterSource result = streamParser.unsafeParse(input);
64
65         // then
66         assertThat(result.aafCredentials().username()).isEqualTo(SAMPLE_AAF_USERNAME);
67         assertThat(result.aafCredentials().password()).isEqualTo(SAMPLE_AAF_PASSWORD);
68         assertThat(result.location()).isEqualTo(SAMPLE_LOCATION);
69         assertThat(result.clientRole()).isEqualTo(SAMPLE_CLIENT_ROLE);
70         assertThat(result.clientId()).isEqualTo(SAMPLE_CLIENT_ID);
71         assertThat(result.topicUrl()).isEqualTo(SAMPLE_TOPIC_URL);
72     }
73
74     @Test
75     void minimalConfiguration_shouldGenerateDataRouterSourceObject() throws IOException {
76         // given
77         RawDataStream<JsonObject> input = DataStreamUtils.readSourceFromResource("/streams/message_router_minimal.json");
78
79         // when
80         MessageRouterSource result = streamParser.unsafeParse(input);
81
82         // then
83         assertThat(result.topicUrl()).isEqualTo(SAMPLE_TOPIC_URL);
84         assertThat(result.aafCredentials().username()).isNull();
85         assertThat(result.aafCredentials().password()).isNull();
86         assertThat(result.clientId()).isNull();
87     }
88
89     @Test
90     void incorrectConfiguration_shouldParseToStreamParserError() throws IOException {
91         // given
92         RawDataStream<JsonObject> input = DataStreamUtils.readSourceFromResource("/streams/data_router_sink_full.json");
93
94         // when
95         Either<StreamParserError, MessageRouterSource> result = streamParser.parse(input);
96
97         // then
98         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
99         result.peekLeft(error -> {
100                     assertThat(error.message()).contains("Invalid stream type");
101                     assertThat(error.message()).contains("Expected '" + MESSAGE_ROUTER_TYPE + "', but was '"
102                             + DATA_ROUTER_TYPE + "'");
103                 }
104         );
105     }
106
107     @Test
108     void emptyConfiguration_shouldParseToStreamParserError() {
109         // given
110         JsonObject json = new JsonObject();
111         final ImmutableRawDataStream<JsonObject> input = ImmutableRawDataStream.<JsonObject>builder()
112                 .name("empty")
113                 .type("data_router")
114                 .descriptor(json)
115                 .direction(DataStreamDirection.SOURCE)
116                 .build();
117         // when
118         Either<StreamParserError, MessageRouterSource> result = streamParser.parse(input);
119         // then
120         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
121     }
122
123
124 }