Refactor AppConfigService with tests
[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.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;
44
45 class StreamServiceTest {
46
47     private StreamService streamService;
48
49     OnapComponentSpec onapComponentSpecMock;
50     BlueprintHelperService blueprintHelperServiceMock;
51     DmaapService dmaapServiceMock;
52
53     Streams streamsMock;
54
55     @BeforeEach
56     public void setup() {
57         streamService = new StreamService();
58         onapComponentSpecMock = mock(OnapComponentSpec.class);
59         blueprintHelperServiceMock = mock(BlueprintHelperService.class);
60         dmaapServiceMock = mock(DmaapService.class);
61
62         streamsMock = mock(Streams.class);
63     }
64
65     @Test
66     void whenStreamsIsNullCreateStreamPublishesShouldReturnEmptyMap() {
67         when(onapComponentSpecMock.getStreams()).thenReturn(null);
68
69         Map<String, Dmaap> streamPublishes = streamService.createStreamPublishes(
70             onapComponentSpecMock,
71             blueprintHelperServiceMock,
72             dmaapServiceMock,
73             createInputs(),
74             true);
75
76         assertTrue(streamPublishes.isEmpty());
77     }
78
79     @Test
80     void whenPublishesIsNullCreateStreamPublishesShouldReturnEmptyMap() {
81         when(streamsMock.getPublishes()).thenReturn(null);
82         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
83
84         Map<String, Dmaap> streamPublishes = streamService.createStreamPublishes(
85             onapComponentSpecMock,
86             blueprintHelperServiceMock,
87             dmaapServiceMock,
88             createInputs(),
89             true);
90
91         assertTrue(streamPublishes.isEmpty());
92     }
93
94     @Test
95     void whenPublishesIsNotEmptyDRCreateStreamPublishesShouldReturnNonEmptyMap() {
96         when(streamsMock.getPublishes()).thenReturn(createPublishesArray());
97         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
98         when(blueprintHelperServiceMock.isDataRouterType(anyString())).thenReturn(true);
99
100         DmaapService dmaapService = new DmaapService();
101
102         Map<String, Dmaap> streamPublishes = streamService.createStreamPublishes(
103             onapComponentSpecMock,
104             blueprintHelperServiceMock,
105             dmaapService,
106             createInputs(),
107             true);
108
109         Map<String, Dmaap> expectedMap = createExpectedMap("_feed");
110
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()));
117         }
118     }
119
120     @Test
121     void whenPublishesIsNotEmptyMRCreateStreamPublishesShouldReturnNonEmptyMap() {
122         when(streamsMock.getPublishes()).thenReturn(createPublishesArray());
123         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
124         when(blueprintHelperServiceMock.isMessageRouterType(anyString())).thenReturn(true);
125
126         DmaapService dmaapService = new DmaapService();
127
128         Map<String, Dmaap> streamPublishes = streamService.createStreamPublishes(
129             onapComponentSpecMock,
130             blueprintHelperServiceMock,
131             dmaapService,
132             createInputs(),
133             true);
134
135         Map<String, Dmaap> expectedMap = createExpectedMap("_topic");
136
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()));
143         }
144     }
145
146     @Test
147     void whenStreamsIsNullCreateStreamSubscribesShouldReturnEmptyMap() {
148         when(onapComponentSpecMock.getStreams()).thenReturn(null);
149
150         Map<String, Dmaap> streamSubscribes = streamService.createStreamSubscribes(
151             onapComponentSpecMock,
152             blueprintHelperServiceMock,
153             dmaapServiceMock,
154             createInputs(),
155             true);
156
157         assertTrue(streamSubscribes.isEmpty());
158     }
159
160     @Test
161     void whenSubscribesIsNullCreateStreamSubscribesShouldReturnEmptyMap() {
162         when(streamsMock.getPublishes()).thenReturn(null);
163         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
164
165         Map<String, Dmaap> streamSubscribes = streamService.createStreamSubscribes(
166             onapComponentSpecMock,
167             blueprintHelperServiceMock,
168             dmaapServiceMock,
169             createInputs(),
170             true);
171
172         assertTrue(streamSubscribes.isEmpty());
173     }
174
175     @Test
176     void whenSubscribesIsNotEmptyDRCreateStreamSubscribesShouldReturnNonEmptyMap() {
177         when(streamsMock.getSubscribes()).thenReturn(createSubscribesArray());
178         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
179         when(blueprintHelperServiceMock.isDataRouterType(anyString())).thenReturn(true);
180
181         DmaapService dmaapService = new DmaapService();
182
183         Map<String, Dmaap> streamSubscribes = streamService.createStreamSubscribes(
184             onapComponentSpecMock,
185             blueprintHelperServiceMock,
186             dmaapService,
187             createInputs(),
188             true);
189
190         Map<String, Dmaap> expectedMap = createExpectedMap("_feed");
191
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()));
198         }
199     }
200
201     @Test
202     void whenSubscribesIsNotEmptyMRCreateStreamSubscribesShouldReturnNonEmptyMap() {
203         when(streamsMock.getSubscribes()).thenReturn(createSubscribesArray());
204         when(onapComponentSpecMock.getStreams()).thenReturn(streamsMock);
205         when(blueprintHelperServiceMock.isMessageRouterType(anyString())).thenReturn(true);
206
207         DmaapService dmaapService = new DmaapService();
208
209         Map<String, Dmaap> streamSubscribes = streamService.createStreamSubscribes(
210             onapComponentSpecMock,
211             blueprintHelperServiceMock,
212             dmaapService,
213             createInputs(),
214             true);
215
216         Map<String, Dmaap> expectedMap = createExpectedMap("_topic");
217
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()));
224         }
225     }
226
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 + ">>");
232
233         Dmaap dmaap2 = new Dmaap();
234         dmaap2.setType("t2");
235         dmaap2.setDmaap_info("<<k2" + suffix + ">>");
236
237         Dmaap dmaap3 = new Dmaap();
238         dmaap3.setType("t3");
239         dmaap3.setDmaap_info("<<k3" + suffix + ">>");
240
241         expectedMap.put("k1", dmaap1);
242         expectedMap.put("k2", dmaap2);
243         expectedMap.put("k3", dmaap3);
244         return expectedMap;
245     }
246
247     private Publishes[] createPublishesArray() {
248         Publishes pub1 = createPublishes("k1", "t1");
249         Publishes pub2 = createPublishes("k2", "t2");
250         Publishes pub3 = createPublishes("k3", "t3");
251
252         return new Publishes[]{pub1, pub2, pub3};
253     }
254
255     private Subscribes[] createSubscribesArray() {
256         Subscribes sub1 = createSubscribes("k1", "t1");
257         Subscribes sub2 = createSubscribes("k2", "t2");
258         Subscribes sub3 = createSubscribes("k3", "t3");
259
260         return new Subscribes[]{sub1, sub2, sub3};
261     }
262
263     private Publishes createPublishes(String key, String type){
264         Publishes publishes = new Publishes();
265         publishes.setConfig_key(key);
266         publishes.setType(type);
267         return publishes;
268     }
269
270     private Subscribes createSubscribes(String key, String type){
271         Subscribes subscribes = new Subscribes();
272         subscribes.setConfig_key(key);
273         subscribes.setType(type);
274         return subscribes;
275     }
276
277     private Map<String, LinkedHashMap<String, Object>> createInputs(){
278         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
279         map.put("key-1", "obj-1");
280
281         Map<String, LinkedHashMap<String, Object>> mapsMap = new HashMap<>();
282         mapsMap.put("inputs", map);
283         return mapsMap;
284     }
285 }