7092de5a6565a3ca18f32585844664f5185342b3
[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.dr;
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.DataRouterSink;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.dmaap.ImmutableDataRouterSink;
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 class DataRouterSinkParserTest {
44
45     private static final String SAMPLE_LOCATION = "mtc00";
46     private static final String SAMPLE_PUBLISH_URL = "https://we-are-data-router.us/feed/xyz";
47     private static final String SAMPLE_LOG_URL = "https://we-are-data-router.us/feed/xyz/logs";
48     private static final String SAMPLE_USER = "some-user";
49     private static final String SAMPLE_PASSWORD = "some-password";
50     private static final String SAMPLE_PUBLISHER_ID = "123456";
51
52     private final StreamFromGsonParser<DataRouterSink> streamParser = StreamFromGsonParsers.dataRouterSinkParser();
53
54     @Test
55     void fullConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
56         // given
57         RawDataStream<JsonObject> input = DataStreamUtils.readSinkFromResource("/streams/data_router_sink_full.json");
58
59         // when
60         DataRouterSink result = streamParser.unsafeParse(input);
61
62         // then
63         final DataRouterSink fullConfigurationStream = ImmutableDataRouterSink.builder()
64                 .name(input.name())
65                 .location(SAMPLE_LOCATION)
66                 .publishUrl(SAMPLE_PUBLISH_URL)
67                 .logUrl(SAMPLE_LOG_URL)
68                 .username(SAMPLE_USER)
69                 .password(SAMPLE_PASSWORD)
70                 .publisherId(SAMPLE_PUBLISHER_ID)
71                 .build();
72         assertThat(result).isEqualTo(fullConfigurationStream);
73     }
74
75     @Test
76     void minimalConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
77         //given
78         RawDataStream<JsonObject> input = DataStreamUtils
79                 .readSinkFromResource("/streams/data_router_sink_minimal.json");
80
81         // when
82         DataRouterSink result = streamParser.unsafeParse(input);
83
84         // then
85         final DataRouterSink minimalConfigurationStream = ImmutableDataRouterSink.builder()
86                 .name(input.name())
87                 .publishUrl(SAMPLE_PUBLISH_URL)
88                 .build();
89         assertThat(result).isEqualTo(minimalConfigurationStream);
90     }
91
92     @Test
93     void incorrectConfiguration_shouldParseToStreamParserError() throws IOException {
94         // given
95         RawDataStream<JsonObject> input = DataStreamUtils.readSinkFromResource("/streams/message_router_full.json");
96
97         // when
98         Either<StreamParserError, DataRouterSink> result = streamParser.parse(input);
99
100         // then
101         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
102         result.peekLeft(error -> {
103                     assertThat(error.message()).contains("Invalid stream type");
104                     assertThat(error.message()).contains("Expected '" + DATA_ROUTER_TYPE + "', but was '"
105                             + MESSAGE_ROUTER_TYPE + "'");
106                 }
107         );
108     }
109
110     @Test
111     void emptyConfiguration_shouldParseToStreamParserError() {
112         // given
113         JsonObject json = new JsonObject();
114         final ImmutableRawDataStream<JsonObject> input = ImmutableRawDataStream.<JsonObject>builder()
115                 .name("empty")
116                 .type("data_router")
117                 .descriptor(json)
118                 .direction(DataStreamDirection.SINK)
119                 .build();
120
121         // when
122         Either<StreamParserError, DataRouterSink> result = streamParser.parse(input);
123
124         // then
125         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
126     }
127
128 }