398ebcd96b874ffbabf9367ccce3365e8b414852
[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;
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.model.streams.dmaap.DataRouterSink;
30 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.dmaap.ImmutableDataRouterSink;
31
32 import java.io.IOException;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.StreamsConstants.DATA_ROUTER_TYPE;
36 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.StreamsConstants.MESSAGE_ROUTER_TYPE;
37
38
39 class DataRouterSinkParserTest {
40     private static final String SAMPLE_LOCATION = "mtc00";
41     private static final String SAMPLE_PUBLISH_URL = "https://we-are-data-router.us/feed/xyz";
42     private static final String SAMPLE_LOG_URL = "https://we-are-data-router.us/feed/xyz/logs";
43     private static final String SAMPLE_USER = "some-user";
44     private static final String SAMPLE_PASSWORD = "some-password";
45     private static final String SAMPLE_PUBLISHER_ID = "123456";
46
47     private static final Gson gson = new Gson();
48
49     private final StreamFromGsonParser<DataRouterSink> streamParser = StreamFromGsonParsers.dataRouterSinkParser();
50
51     private static final DataRouterSink fullConfigurationStream = ImmutableDataRouterSink.builder()
52             .location(SAMPLE_LOCATION)
53             .publishUrl(SAMPLE_PUBLISH_URL)
54             .logUrl(SAMPLE_LOG_URL)
55             .username(SAMPLE_USER)
56             .password(SAMPLE_PASSWORD)
57             .publisherId(SAMPLE_PUBLISHER_ID)
58             .build();
59
60     private static final DataRouterSink minimalConfigurationStream = ImmutableDataRouterSink.builder()
61             .publishUrl(SAMPLE_PUBLISH_URL)
62             .build();
63
64     @Test
65     void fullConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
66         // given
67         JsonObject input = GsonUtils.readObjectFromResource("/streams/data_router_sink_full.json");
68         // when
69         DataRouterSink result = streamParser.unsafeParse(input);
70         // then
71         assertThat(result).isEqualTo(fullConfigurationStream);
72     }
73
74     @Test
75     void minimalConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
76         //given
77         JsonObject input = GsonUtils.readObjectFromResource("/streams/data_router_sink_minimal.json");
78         // when
79         DataRouterSink result = streamParser.unsafeParse(input);
80         // then
81         assertThat(result).isEqualTo(minimalConfigurationStream);
82     }
83
84     @Test
85     void incorrectConfiguration_shouldParseToStreamParserError() throws IOException {
86         // given
87         JsonObject input = GsonUtils.readObjectFromResource("/streams/message_router_full.json");
88         // when
89         Either<StreamParserError, DataRouterSink> result = streamParser.parse(input);
90         // then
91         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
92         result.peekLeft(error -> {
93                     assertThat(error.message()).contains("Invalid stream type");
94                     assertThat(error.message()).contains("Expected '" + DATA_ROUTER_TYPE + "', but was '"
95                             + MESSAGE_ROUTER_TYPE + "'");
96                 }
97         );
98     }
99
100     @Test
101     void emptyConfiguration_shouldParseToStreamParserError() {
102         // given
103         JsonObject input = gson.fromJson("{}", JsonObject.class);
104         // when
105         Either<StreamParserError, DataRouterSink> result = streamParser.parse(input);
106         // then
107         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
108     }
109
110 }