fe93f5ff149b7edc3e5453ce99af909f9f08dc97
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / test / java / org / onap / blueprintgenerator / service / common / StreamServiceTest.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
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
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
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=========================================================
20  *
21  *
22  */
23
24 package org.onap.blueprintgenerator.service.common;
25
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;
32
33 import java.util.HashMap;
34 import java.util.LinkedHashMap;
35 import java.util.Map;
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;
45
46 class StreamServiceTest {
47
48     private StreamService streamService;
49
50     OnapComponentSpec onapComponentSpecMock;
51     BlueprintHelperService blueprintHelperServiceMock;
52     DmaapService dmaapServiceMock;
53
54     Streams streamsMock;
55
56     @BeforeEach
57     public void setup() {
58         streamService = new StreamService();
59         onapComponentSpecMock = mock(OnapComponentSpec.class);
60         blueprintHelperServiceMock = mock(BlueprintHelperService.class);
61         dmaapServiceMock = mock(DmaapService.class);
62
63         streamsMock = mock(Streams.class);
64     }
65
66     @Test
67     void whenStreamsIsNullCreateStreamPublishesShouldReturnEmptyMap() {
68         when(onapComponentSpecMock.getStreams()).thenReturn(null);
69
70         Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
71             onapComponentSpecMock,
72             blueprintHelperServiceMock,
73             dmaapServiceMock,
74             createInputs(),
75             true);
76
77         assertTrue(streamPublishes.isEmpty());
78     }
79
80     @Test
81     void whenPublishesIsNullCreateStreamPublishesShouldReturnEmptyMap() {
82         when(streamsMock.getPublishes()).thenReturn(null);
83         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
84
85         Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
86             onapComponentSpecMock,
87             blueprintHelperServiceMock,
88             dmaapServiceMock,
89             createInputs(),
90             true);
91
92         assertTrue(streamPublishes.isEmpty());
93     }
94
95     @Test
96     void whenPublishesIsNotEmptyDRCreateStreamPublishesShouldReturnNonEmptyMap() {
97         when(streamsMock.getPublishes()).thenReturn(createPublishesArray());
98         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
99         when(blueprintHelperServiceMock.isDataRouterType(anyString())).thenReturn(true);
100
101         DmaapService dmaapService = new DmaapService();
102
103         Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
104             onapComponentSpecMock,
105             blueprintHelperServiceMock,
106             dmaapService,
107             createInputs(),
108             true);
109
110         Map<String, BaseStream> expectedMap = createExpectedMap("_feed");
111
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());
119         }
120     }
121
122     @Test
123     void whenPublishesIsNotEmptyMRCreateStreamPublishesShouldReturnNonEmptyMap() {
124         when(streamsMock.getPublishes()).thenReturn(createPublishesArray());
125         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
126         when(blueprintHelperServiceMock.isMessageRouterType(anyString())).thenReturn(true);
127
128         DmaapService dmaapService = new DmaapService();
129
130         Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
131             onapComponentSpecMock,
132             blueprintHelperServiceMock,
133             dmaapService,
134             createInputs(),
135             true);
136
137         Map<String, BaseStream> expectedMap = createExpectedMap("_topic");
138
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());
146         }
147     }
148
149     @Test
150     void whenStreamsIsNullCreateStreamSubscribesShouldReturnEmptyMap() {
151         when(onapComponentSpecMock.getStreams()).thenReturn(null);
152
153         Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
154             onapComponentSpecMock,
155             blueprintHelperServiceMock,
156             dmaapServiceMock,
157             createInputs(),
158             true);
159
160         assertTrue(streamSubscribes.isEmpty());
161     }
162
163     @Test
164     void whenSubscribesIsNullCreateStreamSubscribesShouldReturnEmptyMap() {
165         when(streamsMock.getPublishes()).thenReturn(null);
166         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
167
168         Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
169             onapComponentSpecMock,
170             blueprintHelperServiceMock,
171             dmaapServiceMock,
172             createInputs(),
173             true);
174
175         assertTrue(streamSubscribes.isEmpty());
176     }
177
178     @Test
179     void whenSubscribesIsNotEmptyDRCreateStreamSubscribesShouldReturnNonEmptyMap() {
180         when(streamsMock.getSubscribes()).thenReturn(createSubscribesArray());
181         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
182         when(blueprintHelperServiceMock.isDataRouterType(anyString())).thenReturn(true);
183
184         DmaapService dmaapService = new DmaapService();
185
186         Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
187             onapComponentSpecMock,
188             blueprintHelperServiceMock,
189             dmaapService,
190             createInputs(),
191             true);
192
193         Map<String, BaseStream> expectedMap = createExpectedMap("_feed");
194
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());
202         }
203     }
204
205     @Test
206     void whenSubscribesIsNotEmptyMRCreateStreamSubscribesShouldReturnNonEmptyMap() {
207         when(streamsMock.getSubscribes()).thenReturn(createSubscribesArray());
208         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
209         when(blueprintHelperServiceMock.isMessageRouterType(anyString())).thenReturn(true);
210
211         DmaapService dmaapService = new DmaapService();
212
213         Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
214             onapComponentSpecMock,
215             blueprintHelperServiceMock,
216             dmaapService,
217             createInputs(),
218             true);
219
220         Map<String, BaseStream> expectedMap = createExpectedMap("_topic");
221
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());
229         }
230     }
231
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 + ">>");
237
238         Dmaap dmaap2 = new Dmaap();
239         dmaap2.setType("t2");
240         dmaap2.setDmaap_info("<<k2" + suffix + ">>");
241
242         Dmaap dmaap3 = new Dmaap();
243         dmaap3.setType("t3");
244         dmaap3.setDmaap_info("<<k3" + suffix + ">>");
245
246         expectedMap.put("k1", dmaap1);
247         expectedMap.put("k2", dmaap2);
248         expectedMap.put("k3", dmaap3);
249         return expectedMap;
250     }
251
252     private Publishes[] createPublishesArray() {
253         Publishes pub1 = createPublishes("k1", "t1");
254         Publishes pub2 = createPublishes("k2", "t2");
255         Publishes pub3 = createPublishes("k3", "t3");
256
257         return new Publishes[]{pub1, pub2, pub3};
258     }
259
260     private Subscribes[] createSubscribesArray() {
261         Subscribes sub1 = createSubscribes("k1", "t1");
262         Subscribes sub2 = createSubscribes("k2", "t2");
263         Subscribes sub3 = createSubscribes("k3", "t3");
264
265         return new Subscribes[]{sub1, sub2, sub3};
266     }
267
268     private Publishes createPublishes(String key, String type) {
269         Publishes publishes = new Publishes();
270         publishes.setConfig_key(key);
271         publishes.setType(type);
272         return publishes;
273     }
274
275     private Subscribes createSubscribes(String key, String type) {
276         Subscribes subscribes = new Subscribes();
277         subscribes.setConfig_key(key);
278         subscribes.setType(type);
279         return subscribes;
280     }
281
282     private Map<String, LinkedHashMap<String, Object>> createInputs() {
283         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
284         map.put("key-1", "obj-1");
285
286         Map<String, LinkedHashMap<String, Object>> mapsMap = new HashMap<>();
287         mapsMap.put("inputs", map);
288         return mapsMap;
289     }
290 }