681fa147f2428e58eeb903ea895decfc1769aa4b
[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.GsonBuilder;
24 import com.google.gson.JsonObject;
25 import io.vavr.control.Either;
26 import org.junit.jupiter.api.Test;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.StreamParserError;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParser;
29 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParsers;
30 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamParser;
31 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.DataStream;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.dmaap.*;
33
34 import java.io.IOException;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.StreamsConstants.DATA_ROUTER_TYPE;
38 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.StreamsConstants.MESSAGE_ROUTER_TYPE;
39
40 public class DataRouterSourceParserTest {
41     private static final String SAMPLE_LOCATION = "mtc00";
42     private static final String SAMPLE_DELIVERY_URL = "https://my-subscriber-app.dcae:8080/target-path";
43     private static final String SAMPLE_USER = "some-user";
44     private static final String SAMPLE_PASSWORD = "some-password";
45     private static final String SAMPLE_SUBSCRIBER_ID = "789012";
46
47     private static final Gson gson = new Gson();
48
49     private static final DataRouterSource fullConfigurationStream = ImmutableDataRouterSource.builder()
50             .location(SAMPLE_LOCATION)
51             .deliveryUrl(SAMPLE_DELIVERY_URL)
52             .username(SAMPLE_USER)
53             .password(SAMPLE_PASSWORD)
54             .subscriberId(SAMPLE_SUBSCRIBER_ID)
55             .build();
56
57     private static final DataRouterSource minimalConfigurationStream = ImmutableDataRouterSource.builder()
58             .build();
59
60
61     private final StreamFromGsonParser<DataRouterSource> streamParser = StreamFromGsonParsers.dataRouterSourceParser();
62
63     @Test
64     void fullConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
65         // given
66         JsonObject input = GsonUtils.readObjectFromResource("/streams/data_router_source_full.json");
67         // when
68         DataRouterSource result = streamParser.unsafeParse(input);
69         // then
70         assertThat(result).isEqualTo(fullConfigurationStream);
71     }
72
73     @Test
74     void minimalConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
75         // given
76         JsonObject input = GsonUtils.readObjectFromResource("/streams/data_router_source_minimal.json");
77         // when
78         DataRouterSource result = streamParser.unsafeParse(input);
79         // then
80         assertThat(result).isEqualTo(minimalConfigurationStream);
81     }
82
83     @Test
84     void incorrectConfiguration_shouldParseToStreamParserError() throws IOException {
85         // given
86         JsonObject input = GsonUtils.readObjectFromResource("/streams/message_router_full.json");
87         // when
88         Either<StreamParserError, DataRouterSource> result = streamParser.parse(input);
89         // then
90         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
91         result.peekLeft(error -> {
92                     assertThat(error.message()).contains("Invalid stream type");
93                     assertThat(error.message()).contains("Expected '" + DATA_ROUTER_TYPE + "', but was '"
94                             + MESSAGE_ROUTER_TYPE + "'");
95                 }
96         );
97     }
98
99     @Test
100     void emptyConfiguration_shouldBeParsedToStreamParserError() {
101         // given
102         JsonObject input = gson.fromJson("{}", JsonObject.class);
103         // when
104         Either<StreamParserError, DataRouterSource> result = streamParser.parse(input);
105         // then
106         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
107     }
108 }