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.Dmaap;
39 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
40 import org.onap.blueprintgenerator.model.componentspec.common.Publishes;
41 import org.onap.blueprintgenerator.model.componentspec.common.Streams;
42 import org.onap.blueprintgenerator.model.componentspec.common.Subscribes;
43 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
45 class StreamServiceTest {
47 private StreamService streamService;
49 OnapComponentSpec onapComponentSpecMock;
50 BlueprintHelperService blueprintHelperServiceMock;
51 DmaapService dmaapServiceMock;
57 streamService = new StreamService();
58 onapComponentSpecMock = mock(OnapComponentSpec.class);
59 blueprintHelperServiceMock = mock(BlueprintHelperService.class);
60 dmaapServiceMock = mock(DmaapService.class);
62 streamsMock = mock(Streams.class);
66 void whenStreamsIsNullCreateStreamPublishesShouldReturnEmptyMap() {
67 when(onapComponentSpecMock.getStreams()).thenReturn(null);
69 Map<String, Dmaap> streamPublishes = streamService.createStreamPublishes(
70 onapComponentSpecMock,
71 blueprintHelperServiceMock,
76 assertTrue(streamPublishes.isEmpty());
80 void whenPublishesIsNullCreateStreamPublishesShouldReturnEmptyMap() {
81 when(streamsMock.getPublishes()).thenReturn(null);
82 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
84 Map<String, Dmaap> streamPublishes = streamService.createStreamPublishes(
85 onapComponentSpecMock,
86 blueprintHelperServiceMock,
91 assertTrue(streamPublishes.isEmpty());
95 void whenPublishesIsNotEmptyDRCreateStreamPublishesShouldReturnNonEmptyMap() {
96 when(streamsMock.getPublishes()).thenReturn(createPublishesArray());
97 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
98 when(blueprintHelperServiceMock.isDataRouterType(anyString())).thenReturn(true);
100 DmaapService dmaapService = new DmaapService();
102 Map<String, Dmaap> streamPublishes = streamService.createStreamPublishes(
103 onapComponentSpecMock,
104 blueprintHelperServiceMock,
109 Map<String, Dmaap> expectedMap = createExpectedMap("_feed");
111 assertNotNull(streamPublishes);
112 assertEquals(expectedMap.size(), streamPublishes.size());
113 for(Map.Entry<String, Dmaap> entry : expectedMap.entrySet()) {
114 assertTrue(streamPublishes.containsKey(entry.getKey()));
115 assertTrue(streamPublishes.get(entry.getKey()).getType().equals(entry.getValue().getType()));
116 assertTrue(streamPublishes.get(entry.getKey()).getDmaap_info().equals(entry.getValue().getDmaap_info()));
121 void whenPublishesIsNotEmptyMRCreateStreamPublishesShouldReturnNonEmptyMap() {
122 when(streamsMock.getPublishes()).thenReturn(createPublishesArray());
123 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
124 when(blueprintHelperServiceMock.isMessageRouterType(anyString())).thenReturn(true);
126 DmaapService dmaapService = new DmaapService();
128 Map<String, Dmaap> streamPublishes = streamService.createStreamPublishes(
129 onapComponentSpecMock,
130 blueprintHelperServiceMock,
135 Map<String, Dmaap> expectedMap = createExpectedMap("_topic");
137 assertNotNull(streamPublishes);
138 assertEquals(expectedMap.size(), streamPublishes.size());
139 for(Map.Entry<String, Dmaap> entry : expectedMap.entrySet()) {
140 assertTrue(streamPublishes.containsKey(entry.getKey()));
141 assertTrue(streamPublishes.get(entry.getKey()).getType().equals(entry.getValue().getType()));
142 assertTrue(streamPublishes.get(entry.getKey()).getDmaap_info().equals(entry.getValue().getDmaap_info()));
147 void whenStreamsIsNullCreateStreamSubscribesShouldReturnEmptyMap() {
148 when(onapComponentSpecMock.getStreams()).thenReturn(null);
150 Map<String, Dmaap> streamSubscribes = streamService.createStreamSubscribes(
151 onapComponentSpecMock,
152 blueprintHelperServiceMock,
157 assertTrue(streamSubscribes.isEmpty());
161 void whenSubscribesIsNullCreateStreamSubscribesShouldReturnEmptyMap() {
162 when(streamsMock.getPublishes()).thenReturn(null);
163 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
165 Map<String, Dmaap> streamSubscribes = streamService.createStreamSubscribes(
166 onapComponentSpecMock,
167 blueprintHelperServiceMock,
172 assertTrue(streamSubscribes.isEmpty());
176 void whenSubscribesIsNotEmptyDRCreateStreamSubscribesShouldReturnNonEmptyMap() {
177 when(streamsMock.getSubscribes()).thenReturn(createSubscribesArray());
178 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
179 when(blueprintHelperServiceMock.isDataRouterType(anyString())).thenReturn(true);
181 DmaapService dmaapService = new DmaapService();
183 Map<String, Dmaap> streamSubscribes = streamService.createStreamSubscribes(
184 onapComponentSpecMock,
185 blueprintHelperServiceMock,
190 Map<String, Dmaap> expectedMap = createExpectedMap("_feed");
192 assertNotNull(streamSubscribes);
193 assertEquals(expectedMap.size(), streamSubscribes.size());
194 for(Map.Entry<String, Dmaap> entry : expectedMap.entrySet()) {
195 assertTrue(streamSubscribes.containsKey(entry.getKey()));
196 assertTrue(streamSubscribes.get(entry.getKey()).getType().equals(entry.getValue().getType()));
197 assertTrue(streamSubscribes.get(entry.getKey()).getDmaap_info().equals(entry.getValue().getDmaap_info()));
202 void whenSubscribesIsNotEmptyMRCreateStreamSubscribesShouldReturnNonEmptyMap() {
203 when(streamsMock.getSubscribes()).thenReturn(createSubscribesArray());
204 when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
205 when(blueprintHelperServiceMock.isMessageRouterType(anyString())).thenReturn(true);
207 DmaapService dmaapService = new DmaapService();
209 Map<String, Dmaap> streamSubscribes = streamService.createStreamSubscribes(
210 onapComponentSpecMock,
211 blueprintHelperServiceMock,
216 Map<String, Dmaap> expectedMap = createExpectedMap("_topic");
218 assertNotNull(streamSubscribes);
219 assertEquals(expectedMap.size(), streamSubscribes.size());
220 for(Map.Entry<String, Dmaap> entry : expectedMap.entrySet()) {
221 assertTrue(streamSubscribes.containsKey(entry.getKey()));
222 assertTrue(streamSubscribes.get(entry.getKey()).getType().equals(entry.getValue().getType()));
223 assertTrue(streamSubscribes.get(entry.getKey()).getDmaap_info().equals(entry.getValue().getDmaap_info()));
227 private Map<String, Dmaap> createExpectedMap(String suffix) {
228 Map<String, Dmaap> expectedMap = new HashMap<>();
229 Dmaap dmaap1 = new Dmaap();
230 dmaap1.setType("t1");
231 dmaap1.setDmaap_info("<<k1" + suffix + ">>");
233 Dmaap dmaap2 = new Dmaap();
234 dmaap2.setType("t2");
235 dmaap2.setDmaap_info("<<k2" + suffix + ">>");
237 Dmaap dmaap3 = new Dmaap();
238 dmaap3.setType("t3");
239 dmaap3.setDmaap_info("<<k3" + suffix + ">>");
241 expectedMap.put("k1", dmaap1);
242 expectedMap.put("k2", dmaap2);
243 expectedMap.put("k3", dmaap3);
247 private Publishes[] createPublishesArray() {
248 Publishes pub1 = createPublishes("k1", "t1");
249 Publishes pub2 = createPublishes("k2", "t2");
250 Publishes pub3 = createPublishes("k3", "t3");
252 return new Publishes[]{pub1, pub2, pub3};
255 private Subscribes[] createSubscribesArray() {
256 Subscribes sub1 = createSubscribes("k1", "t1");
257 Subscribes sub2 = createSubscribes("k2", "t2");
258 Subscribes sub3 = createSubscribes("k3", "t3");
260 return new Subscribes[]{sub1, sub2, sub3};
263 private Publishes createPublishes(String key, String type){
264 Publishes publishes = new Publishes();
265 publishes.setConfig_key(key);
266 publishes.setType(type);
270 private Subscribes createSubscribes(String key, String type){
271 Subscribes subscribes = new Subscribes();
272 subscribes.setConfig_key(key);
273 subscribes.setType(type);
277 private Map<String, LinkedHashMap<String, Object>> createInputs(){
278 LinkedHashMap<String, Object> map = new LinkedHashMap<>();
279 map.put("key-1", "obj-1");
281 Map<String, LinkedHashMap<String, Object>> mapsMap = new HashMap<>();
282 mapsMap.put("inputs", map);