b2d013095909aecdca27c70535af212ec017929e
[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.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.impl.streams.gson.DataStreamUtils;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.DataStream;
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.DataStreamDirection;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.ImmutableRawDataStream;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.RawDataStream;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.streams.dmaap.*;
37
38 import java.io.IOException;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.StreamsConstants.DATA_ROUTER_TYPE;
42 import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.streams.gson.StreamsConstants.MESSAGE_ROUTER_TYPE;
43
44 public class DataRouterSourceParserTest {
45
46     private static final String SAMPLE_LOCATION = "mtc00";
47     private static final String SAMPLE_DELIVERY_URL = "https://my-subscriber-app.dcae:8080/target-path";
48     private static final String SAMPLE_USER = "some-user";
49     private static final String SAMPLE_PASSWORD = "some-password";
50     private static final String SAMPLE_SUBSCRIBER_ID = "789012";
51
52     private final StreamFromGsonParser<DataRouterSource> streamParser = StreamFromGsonParsers.dataRouterSourceParser();
53
54     @Test
55     void fullConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
56         // given
57         RawDataStream<JsonObject> input = DataStreamUtils.readSourceFromResource("/streams/data_router_source_full.json");
58
59         // when
60         DataRouterSource result = streamParser.unsafeParse(input);
61
62         // then
63
64         final DataRouterSource fullConfigurationStream = ImmutableDataRouterSource.builder()
65                 .name(input.name())
66                 .location(SAMPLE_LOCATION)
67                 .deliveryUrl(SAMPLE_DELIVERY_URL)
68                 .username(SAMPLE_USER)
69                 .password(SAMPLE_PASSWORD)
70                 .subscriberId(SAMPLE_SUBSCRIBER_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                 .readSourceFromResource("/streams/data_router_source_minimal.json");
80
81         // when
82         DataRouterSource result = streamParser.unsafeParse(input);
83
84         // then
85         final DataRouterSource minimalConfigurationStream = ImmutableDataRouterSource.builder()
86                 .name(input.name())
87                 .build();
88         assertThat(result).isEqualTo(minimalConfigurationStream);
89     }
90
91     @Test
92     void incorrectConfiguration_shouldParseToStreamParserError() throws IOException {
93         // given
94         RawDataStream<JsonObject> input = DataStreamUtils.readSourceFromResource("/streams/message_router_full.json");
95
96         // when
97         Either<StreamParserError, DataRouterSource> result = streamParser.parse(input);
98
99         // then
100         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
101         result.peekLeft(error -> {
102                     assertThat(error.message()).contains("Invalid stream type");
103                     assertThat(error.message()).contains("Expected '" + DATA_ROUTER_TYPE + "', but was '"
104                             + MESSAGE_ROUTER_TYPE + "'");
105                 }
106         );
107     }
108
109     @Test
110     void emptyConfiguration_shouldBeParsedToStreamParserError() {
111         // given
112         JsonObject json = new JsonObject();
113         final ImmutableRawDataStream<JsonObject> input = ImmutableRawDataStream.<JsonObject>builder()
114                 .name("empty")
115                 .type("data_router")
116                 .descriptor(json)
117                 .direction(DataStreamDirection.SOURCE)
118                 .build();
119
120         // when
121         Either<StreamParserError, DataRouterSource> result = streamParser.parse(input);
122
123         // then
124         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
125     }
126 }