63b04d1d18e4dd4ee47774ba8c2afc78cfcf24f0
[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.MessageRouterSink;
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  * @author <a href="mailto:kornel.janiak@nokia.com">Kornel Janiak</a>
40  */
41
42 public class MessageRouterSinkParserTest {
43
44     private static final String SAMPLE_AAF_USERNAME = "some-user";
45     private static final String SAMPLE_AAF_PASSWORD = "some-password";
46     private static final String SAMPLE_LOCATION = "mtc00";
47     private static final String SAMPLE_CLIENT_ROLE = "com.dcae.member";
48     private static final String SAMPLE_CLIENT_ID = "1500462518108";
49     private static final String SAMPLE_TOPIC_URL = "https://we-are-message-router.us:3905/events/some-topic";
50
51     private static final Gson gson = new Gson();
52
53     private final StreamFromGsonParser<MessageRouterSink> streamParser = StreamFromGsonParsers.messageRouterSinkParser();
54
55     @Test
56     void fullConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
57         // given
58         JsonObject input = GsonUtils.readObjectFromResource("/streams/message_router_full.json");
59         // when
60         MessageRouterSink result = streamParser.unsafeParse(input);
61         // then
62         assertThat(result).isInstanceOf(MessageRouterSink.class);
63         assertThat(result.aafCredentials().username()).isEqualTo(SAMPLE_AAF_USERNAME);
64         assertThat(result.aafCredentials().password()).isEqualTo(SAMPLE_AAF_PASSWORD);
65         assertThat(result.location()).isEqualTo(SAMPLE_LOCATION);
66         assertThat(result.clientRole()).isEqualTo(SAMPLE_CLIENT_ROLE);
67         assertThat(result.clientId()).isEqualTo(SAMPLE_CLIENT_ID);
68         assertThat(result.topicUrl()).isEqualTo(SAMPLE_TOPIC_URL);
69     }
70
71     @Test
72     void minimalConfiguration_shouldGenerateDataRouterSinkObject() throws IOException {
73         // given
74         JsonObject input = GsonUtils.readObjectFromResource("/streams/message_router_minimal.json");
75
76         // when
77         MessageRouterSink result = streamParser.unsafeParse(input);
78         // then
79         assertThat(result).isInstanceOf(MessageRouterSink.class);
80         assertThat(result.topicUrl()).isEqualTo(SAMPLE_TOPIC_URL);
81         assertThat(result.aafCredentials().username()).isNull();
82         assertThat(result.aafCredentials().password()).isNull();
83         assertThat(result.clientId()).isNull();
84     }
85
86     @Test
87     void incorrectConfiguration_shouldParseToStreamParserError() throws IOException {
88         // given
89         JsonObject input = GsonUtils.readObjectFromResource("/streams/data_router_sink_full.json");
90         // when
91         Either<StreamParserError, MessageRouterSink> result = streamParser.parse(input);
92         // then
93         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
94         result.peekLeft(error -> {
95                     assertThat(error.message()).contains("Invalid stream type");
96                     assertThat(error.message()).contains("Expected '" + MESSAGE_ROUTER_TYPE + "', but was '"
97                             + DATA_ROUTER_TYPE + "'");
98                 }
99         );
100     }
101
102     @Test
103     void emptyConfiguration_shouldParseToStreamParserError() {
104         // given
105         JsonObject input = gson.fromJson("{}", JsonObject.class);
106         // when
107         Either<StreamParserError, MessageRouterSink> result = streamParser.parse(input);
108         // then
109         assertThat(result.getLeft()).isInstanceOf(StreamParserError.class);
110     }
111
112
113 }