3 * * ============LICENSE_START=======================================================
5 * * ================================================================================
6 * * Copyright (c) 2021 Nokia Intellectual Property. All rights reserved.
7 * * ================================================================================
8 * * Licensed under the Apache License, Version 2.0 (the "License");
9 * * you may not use this file except in compliance with the License.
10 * * You may obtain a copy of the License at
12 * * http://www.apache.org/licenses/LICENSE-2.0
14 * * Unless required by applicable law or agreed to in writing, software
15 * * distributed under the License is distributed on an "AS IS" BASIS,
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * * See the License for the specific language governing permissions and
18 * * limitations under the License.
19 * * ============LICENSE_END=========================================================
24 package org.onap.blueprintgenerator.service.common;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31 import static org.junit.Assert.assertEquals;
33 import java.util.HashMap;
34 import java.util.LinkedHashMap;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.blueprintgenerator.model.common.BaseStream;
39 import org.onap.blueprintgenerator.model.common.Dmaap;
40 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
41 import org.onap.blueprintgenerator.model.componentspec.common.Publishes;
42 import org.onap.blueprintgenerator.model.componentspec.common.Streams;
43 import org.onap.blueprintgenerator.model.componentspec.common.Subscribes;
44 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
46 class StreamServiceTest {
48 private StreamService streamService;
50 OnapComponentSpec onapComponentSpecMock;
51 BlueprintHelperService blueprintHelperServiceMock;
52 DmaapService dmaapServiceMock;
58 streamService = new StreamService();
59 onapComponentSpecMock = mock(OnapComponentSpec.class);
60 blueprintHelperServiceMock = mock(BlueprintHelperService.class);
61 dmaapServiceMock = mock(DmaapService.class);
63 streamsMock = mock(Streams.class);
67 void whenStreamsIsNullCreateStreamPublishesShouldReturnEmptyMap() {
68 when(onapComponentSpecMock.getStreams()).thenReturn(null);
70 Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
71 onapComponentSpecMock,
72 blueprintHelperServiceMock,
77 assertTrue(streamPublishes.isEmpty());
81 void whenPublishesIsNullCreateStreamPublishesShouldReturnEmptyMap() {
82 when(streamsMock.getPublishes()).thenReturn(null);
83 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
85 Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
86 onapComponentSpecMock,
87 blueprintHelperServiceMock,
92 assertTrue(streamPublishes.isEmpty());
96 void whenPublishesIsNotEmptyDRCreateStreamPublishesShouldReturnNonEmptyMap() {
97 when(streamsMock.getPublishes()).thenReturn(createPublishesArray());
98 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
99 when(blueprintHelperServiceMock.isDataRouterType(anyString())).thenReturn(true);
101 DmaapService dmaapService = new DmaapService();
103 Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
104 onapComponentSpecMock,
105 blueprintHelperServiceMock,
110 Map<String, BaseStream> expectedMap = createExpectedMap("_feed");
112 assertNotNull(streamPublishes);
113 assertEquals(expectedMap.size(), streamPublishes.size());
114 for (Map.Entry<String, BaseStream> entry : expectedMap.entrySet()) {
115 assertTrue(streamPublishes.containsKey(entry.getKey()));
116 assertEquals(streamPublishes.get(entry.getKey()).getType(), entry.getValue().getType());
117 assertEquals(((Dmaap) streamPublishes.get(entry.getKey())).getDmaap_info(),
118 ((Dmaap) entry.getValue()).getDmaap_info());
123 void whenPublishesIsNotEmptyMRCreateStreamPublishesShouldReturnNonEmptyMap() {
124 when(streamsMock.getPublishes()).thenReturn(createPublishesArray());
125 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
126 when(blueprintHelperServiceMock.isMessageRouterType(anyString())).thenReturn(true);
128 DmaapService dmaapService = new DmaapService();
130 Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
131 onapComponentSpecMock,
132 blueprintHelperServiceMock,
137 Map<String, BaseStream> expectedMap = createExpectedMap("_topic");
139 assertNotNull(streamPublishes);
140 assertEquals(expectedMap.size(), streamPublishes.size());
141 for (Map.Entry<String, BaseStream> entry : expectedMap.entrySet()) {
142 assertTrue(streamPublishes.containsKey(entry.getKey()));
143 assertEquals(streamPublishes.get(entry.getKey()).getType(), entry.getValue().getType());
144 assertEquals(((Dmaap) streamPublishes.get(entry.getKey())).getDmaap_info(),
145 ((Dmaap) entry.getValue()).getDmaap_info());
150 void whenStreamsIsNullCreateStreamSubscribesShouldReturnEmptyMap() {
151 when(onapComponentSpecMock.getStreams()).thenReturn(null);
153 Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
154 onapComponentSpecMock,
155 blueprintHelperServiceMock,
160 assertTrue(streamSubscribes.isEmpty());
164 void whenSubscribesIsNullCreateStreamSubscribesShouldReturnEmptyMap() {
165 when(streamsMock.getPublishes()).thenReturn(null);
166 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
168 Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
169 onapComponentSpecMock,
170 blueprintHelperServiceMock,
175 assertTrue(streamSubscribes.isEmpty());
179 void whenSubscribesIsNotEmptyDRCreateStreamSubscribesShouldReturnNonEmptyMap() {
180 when(streamsMock.getSubscribes()).thenReturn(createSubscribesArray());
181 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
182 when(blueprintHelperServiceMock.isDataRouterType(anyString())).thenReturn(true);
184 DmaapService dmaapService = new DmaapService();
186 Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
187 onapComponentSpecMock,
188 blueprintHelperServiceMock,
193 Map<String, BaseStream> expectedMap = createExpectedMap("_feed");
195 assertNotNull(streamSubscribes);
196 assertEquals(expectedMap.size(), streamSubscribes.size());
197 for (Map.Entry<String, BaseStream> entry : expectedMap.entrySet()) {
198 assertTrue(streamSubscribes.containsKey(entry.getKey()));
199 assertEquals(streamSubscribes.get(entry.getKey()).getType(), entry.getValue().getType());
200 assertEquals(((Dmaap) streamSubscribes.get(entry.getKey())).getDmaap_info(),
201 ((Dmaap) entry.getValue()).getDmaap_info());
206 void whenSubscribesIsNotEmptyMRCreateStreamSubscribesShouldReturnNonEmptyMap() {
207 when(streamsMock.getSubscribes()).thenReturn(createSubscribesArray());
208 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
209 when(blueprintHelperServiceMock.isMessageRouterType(anyString())).thenReturn(true);
211 DmaapService dmaapService = new DmaapService();
213 Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
214 onapComponentSpecMock,
215 blueprintHelperServiceMock,
220 Map<String, BaseStream> expectedMap = createExpectedMap("_topic");
222 assertNotNull(streamSubscribes);
223 assertEquals(expectedMap.size(), streamSubscribes.size());
224 for (Map.Entry<String, BaseStream> entry : expectedMap.entrySet()) {
225 assertTrue(streamSubscribes.containsKey(entry.getKey()));
226 assertEquals(streamSubscribes.get(entry.getKey()).getType(), entry.getValue().getType());
227 assertEquals(((Dmaap) streamSubscribes.get(entry.getKey())).getDmaap_info(),
228 ((Dmaap) entry.getValue()).getDmaap_info());
232 private Map<String, BaseStream> createExpectedMap(String suffix) {
233 Map<String, BaseStream> expectedMap = new HashMap<>();
234 Dmaap dmaap1 = new Dmaap();
235 dmaap1.setType("t1");
236 dmaap1.setDmaap_info("<<k1" + suffix + ">>");
238 Dmaap dmaap2 = new Dmaap();
239 dmaap2.setType("t2");
240 dmaap2.setDmaap_info("<<k2" + suffix + ">>");
242 Dmaap dmaap3 = new Dmaap();
243 dmaap3.setType("t3");
244 dmaap3.setDmaap_info("<<k3" + suffix + ">>");
246 expectedMap.put("k1", dmaap1);
247 expectedMap.put("k2", dmaap2);
248 expectedMap.put("k3", dmaap3);
252 private Publishes[] createPublishesArray() {
253 Publishes pub1 = createPublishes("k1", "t1");
254 Publishes pub2 = createPublishes("k2", "t2");
255 Publishes pub3 = createPublishes("k3", "t3");
257 return new Publishes[]{pub1, pub2, pub3};
260 private Subscribes[] createSubscribesArray() {
261 Subscribes sub1 = createSubscribes("k1", "t1");
262 Subscribes sub2 = createSubscribes("k2", "t2");
263 Subscribes sub3 = createSubscribes("k3", "t3");
265 return new Subscribes[]{sub1, sub2, sub3};
268 private Publishes createPublishes(String key, String type) {
269 Publishes publishes = new Publishes();
270 publishes.setConfig_key(key);
271 publishes.setType(type);
275 private Subscribes createSubscribes(String key, String type) {
276 Subscribes subscribes = new Subscribes();
277 subscribes.setConfig_key(key);
278 subscribes.setType(type);
282 private Map<String, Map<String, Object>> createInputs() {
283 Map<String, Object> map = new LinkedHashMap<>();
284 map.put("key-1", "obj-1");
286 Map<String, Map<String, Object>> mapsMap = new HashMap<>();
287 mapsMap.put("inputs", map);