a598315fad4d984d1532cebb9a2336b009436739
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / test / java / org / onap / blueprintgenerator / service / common / PropertiesServiceTest.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.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.Map;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.blueprintgenerator.constants.Constants;
46 import org.onap.blueprintgenerator.model.common.GetInput;
47 import org.onap.blueprintgenerator.model.common.Properties;
48 import org.onap.blueprintgenerator.model.componentspec.OnapAuxilary;
49 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
50 import org.onap.blueprintgenerator.model.componentspec.common.Artifacts;
51 import org.onap.blueprintgenerator.model.componentspec.common.Publishes;
52 import org.onap.blueprintgenerator.model.componentspec.common.Self;
53 import org.onap.blueprintgenerator.model.componentspec.common.Streams;
54 import org.onap.blueprintgenerator.model.componentspec.common.Subscribes;
55 import org.onap.blueprintgenerator.model.componentspec.common.Volumes;
56 import org.onap.blueprintgenerator.model.dmaap.TlsInfo;
57 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
58 import org.onap.blueprintgenerator.service.dmaap.StreamsService;
59
60 @RunWith(MockitoJUnitRunner.class)
61 public class PropertiesServiceTest {
62
63     private static final String PROPERTIES = "properties";
64     private static final String INPUTS = "inputs";
65
66     @InjectMocks
67     private PropertiesService propertiesService;
68     @Mock
69     private AppConfigService appConfigService;
70     @Mock
71     private ResourceConfigService resourceConfigService;
72     @Mock
73     private StreamsService streamsService;
74     @Mock
75     private BlueprintHelperService blueprintHelperService;
76
77     private Map<String, ?> resourceConfigResponse;
78
79     @Before
80     public void setup() {
81         resourceConfigResponse = Map.of(INPUTS, new LinkedHashMap<>());
82         when(resourceConfigService.createResourceConfig(any(), eq(getSelf().getName())))
83             .thenReturn((Map<String, Object>) resourceConfigResponse);
84     }
85
86     @Test
87     public void shouldReturnInputsAndPropertiesForDmaap() {
88         //when
89         Map<String, Object> response = propertiesService
90             .createDmaapProperties(new HashMap<>(), getOnapComponentSpec(), "");
91
92         //then
93         assertTrue(response.get(PROPERTIES) instanceof Properties);
94         assertNotNull(response.get(INPUTS));
95         assertEquals(resourceConfigResponse.get(INPUTS), response.get(INPUTS));
96         assertEquals(getProperties("tag_version", false), response.get(PROPERTIES));
97
98         Properties actualProperties = (Properties) response.get(PROPERTIES);
99         assertNull(actualProperties.getStreams_subscribes());
100         assertNull(actualProperties.getStreams_publishes());
101     }
102
103     @Test
104     public void shouldReturnInputsAndPropertiesForOnap() {
105         //given
106         when(appConfigService.createAppconfig(any(), eq(getOnapComponentSpec()), eq(false)))
107             .thenReturn((Map<String, Object>) resourceConfigResponse);
108
109         //when
110         Map<String, Object> response = propertiesService
111             .createOnapProperties(new HashMap<>(), getOnapComponentSpec(), "");
112
113         //then
114         assertNotNull(response.get(PROPERTIES));
115         assertTrue(response.get(PROPERTIES) instanceof Properties);
116         assertNotNull(response.get(INPUTS));
117         assertEquals(resourceConfigResponse.get(INPUTS), response.get(INPUTS));
118         assertEquals(getProperties("image", true), response.get(PROPERTIES));
119
120         Properties actualProperties = (Properties) response.get(PROPERTIES);
121         assertNull(actualProperties.getStreams_subscribes());
122         assertNull(actualProperties.getStreams_publishes());
123     }
124
125     @Test
126     public void shouldSetStreamsMessageRoutesPublishesInProperties() {
127         //given
128         OnapComponentSpec onapComponentSpec = getOnapComponentSpecWithStreamsPublishes();
129         Publishes publishes = onapComponentSpec.getStreams().getPublishes()[0];
130         when(blueprintHelperService.isMessageRouterType(eq(publishes.getType()))).thenReturn(true);
131         when(streamsService.createStreams(any(), eq(publishes.getConfig_key() + Constants._TOPIC),
132             eq(publishes.getType()),
133             eq(publishes.getConfig_key()),
134             eq(publishes.getRoute()),
135             eq('p'))).thenReturn(getStreams());
136
137         //when
138         Map<String, Object> response = propertiesService
139             .createDmaapProperties(new HashMap<>(), onapComponentSpec, "");
140
141         //then
142         assertNotNull(response.get(PROPERTIES));
143         assertTrue(response.get(PROPERTIES) instanceof Properties);
144         Properties actualProperties = (Properties) response.get(PROPERTIES);
145         assertNull(actualProperties.getStreams_subscribes());
146         assertNotNull(actualProperties.getStreams_publishes());
147         assertEquals(List.of(getDmaapStreams()), actualProperties.getStreams_publishes());
148     }
149
150     @Test
151     public void shouldSetStreamsDataRoutesPublishesInProperties() {
152         //given
153         OnapComponentSpec onapComponentSpec = getOnapComponentSpecWithStreamsPublishes();
154         Publishes publishes = onapComponentSpec.getStreams().getPublishes()[0];
155         when(blueprintHelperService.isDataRouterType(eq(publishes.getType()))).thenReturn(true);
156         when(streamsService.createStreams(any(), eq(publishes.getConfig_key() + Constants._FEED),
157             eq(publishes.getType()),
158             eq(publishes.getConfig_key()),
159             eq(publishes.getRoute()),
160             eq('p'))).thenReturn(getStreams());
161
162         //when
163         Map<String, Object> response = propertiesService
164             .createDmaapProperties(new HashMap<>(), onapComponentSpec, "");
165
166         //then
167         assertNotNull(response.get(PROPERTIES));
168         assertTrue(response.get(PROPERTIES) instanceof Properties);
169         Properties actualProperties = (Properties) response.get(PROPERTIES);
170         assertNull(actualProperties.getStreams_subscribes());
171         assertNotNull(actualProperties.getStreams_publishes());
172         assertEquals(List.of(getDmaapStreams()), actualProperties.getStreams_publishes());
173     }
174
175     @Test
176     public void shouldSetStreamsDataRoutesSubscribesInProperties() {
177         //given
178         OnapComponentSpec onapComponentSpec = getOnapComponentSpecWithStreamsSubscribes();
179         Subscribes subscribes = onapComponentSpec.getStreams().getSubscribes()[0];
180         when(blueprintHelperService.isMessageRouterType(eq(subscribes.getType()))).thenReturn(true);
181         when(streamsService.createStreams(any(), eq(subscribes.getConfig_key() + Constants._TOPIC),
182             eq(subscribes.getType()),
183             eq(subscribes.getConfig_key()),
184             eq(subscribes.getRoute()),
185             eq('s'))).thenReturn(getStreams());
186
187         //when
188         Map<String, Object> response = propertiesService
189             .createDmaapProperties(new HashMap<>(), onapComponentSpec, "");
190
191         //then
192         assertNotNull(response.get(PROPERTIES));
193         assertTrue(response.get(PROPERTIES) instanceof Properties);
194         Properties actualProperties = (Properties) response.get(PROPERTIES);
195         assertNull(actualProperties.getStreams_publishes());
196         assertNotNull(actualProperties.getStreams_subscribes());
197         assertEquals(List.of(getDmaapStreams()), actualProperties.getStreams_subscribes());
198     }
199
200     @Test
201     public void shouldSetStreamsMessageRoutesSubscribesInProperties() {
202         //given
203         OnapComponentSpec onapComponentSpec = getOnapComponentSpecWithStreamsSubscribes();
204         Subscribes subscribes = onapComponentSpec.getStreams().getSubscribes()[0];
205         when(blueprintHelperService.isDataRouterType(eq(subscribes.getType()))).thenReturn(true);
206         when(streamsService.createStreams(any(), eq(subscribes.getConfig_key() + Constants._FEED),
207             eq(subscribes.getType()),
208             eq(subscribes.getConfig_key()),
209             eq(subscribes.getRoute()),
210             eq('s'))).thenReturn(getStreams());
211
212         //when
213         Map<String, Object> response = propertiesService
214             .createDmaapProperties(new HashMap<>(), onapComponentSpec, "");
215
216         //then
217         assertNotNull(response.get(PROPERTIES));
218         assertTrue(response.get(PROPERTIES) instanceof Properties);
219         Properties actualProperties = (Properties) response.get(PROPERTIES);
220         assertNull(actualProperties.getStreams_publishes());
221         assertNotNull(actualProperties.getStreams_subscribes());
222         assertEquals(List.of(getDmaapStreams()), actualProperties.getStreams_subscribes());
223     }
224
225     private OnapComponentSpec getOnapComponentSpecWithStreamsSubscribes() {
226         OnapComponentSpec onapComponentSpec = getOnapComponentSpec();
227         Streams streams = new Streams();
228         Subscribes subscribes = getSubscribes();
229         streams.setSubscribes(new Subscribes[]{subscribes});
230         onapComponentSpec.setStreams(streams);
231         return onapComponentSpec;
232     }
233
234     private OnapComponentSpec getOnapComponentSpecWithStreamsPublishes() {
235         OnapComponentSpec onapComponentSpec = getOnapComponentSpec();
236         Streams streams = new Streams();
237         Publishes publishes1 = getPublishes();
238         streams.setPublishes(new Publishes[]{publishes1});
239         onapComponentSpec.setStreams(streams);
240         return onapComponentSpec;
241     }
242
243     private HashMap<String, Object> getStreams() {
244         return new HashMap<>() {{
245             put("streams", getDmaapStreams());
246         }};
247     }
248
249     private org.onap.blueprintgenerator.model.dmaap.Streams getDmaapStreams() {
250         org.onap.blueprintgenerator.model.dmaap.Streams dmaapStreams = new org.onap.blueprintgenerator.model.dmaap.Streams();
251         dmaapStreams.setName("testDmaapStreams");
252         return dmaapStreams;
253     }
254
255     private Publishes getPublishes() {
256         Publishes publishes = new Publishes();
257         publishes.setConfig_key("test_config_key");
258         publishes.setFormat("test_format");
259         publishes.setRoute("test_route");
260         publishes.setType("test_type");
261         publishes.setVersion("test_version");
262         return publishes;
263     }
264
265     private Subscribes getSubscribes() {
266         Subscribes subscribes = new Subscribes();
267         subscribes.setConfig_key("test_config_key_s");
268         subscribes.setFormat("test_format_s");
269         subscribes.setRoute("test_route_s");
270         subscribes.setType("test_type_s");
271         subscribes.setVersion("test_version_s");
272         return subscribes;
273     }
274
275     private Properties getProperties(String image, boolean alwaysPullImage) {
276         Properties expectedProperties = new Properties();
277         expectedProperties.setDocker_config(getOnapAuxilary());
278         expectedProperties.setImage(getGetInput(image));
279         expectedProperties.setLocation_id(getGetInput("location_id"));
280         expectedProperties.setService_component_type("test-Name");
281         expectedProperties.setService_component_name_override(getGetInput("service_component_name_override"));
282         expectedProperties.setLog_info(Collections.emptyMap());
283         expectedProperties.setReplicas(getGetInput("replicas"));
284         expectedProperties.setTls_info(getTlsInfo());
285         if (alwaysPullImage) {
286             expectedProperties.setAlways_pull_image(getGetInput("always_pull_image"));
287         }
288         return expectedProperties;
289     }
290
291     private TlsInfo getTlsInfo() {
292         TlsInfo tlsInfo = new TlsInfo();
293         tlsInfo.setUseTls(getGetInput("use_tls"));
294         return tlsInfo;
295     }
296
297     private GetInput getGetInput(String location_id) {
298         GetInput location = new GetInput();
299         location.setBpInputName(location_id);
300         return location;
301     }
302
303     private OnapComponentSpec getOnapComponentSpec() {
304         OnapComponentSpec onapComponentSpec = new OnapComponentSpec();
305         onapComponentSpec.setArtifacts(new Artifacts[]{getTestUriArtifact()});
306         onapComponentSpec.setAuxilary(getOnapAuxilary());
307         onapComponentSpec.setSelf(getSelf());
308         return onapComponentSpec;
309     }
310
311     private Self getSelf() {
312         Self self = new Self();
313         self.setName("test.Name");
314         return self;
315     }
316
317     private Artifacts getTestUriArtifact() {
318         Artifacts artifacts = new Artifacts();
319         artifacts.setUri("test://testUri");
320         return artifacts;
321     }
322
323     private OnapAuxilary getOnapAuxilary() {
324         OnapAuxilary onapAuxilary = new OnapAuxilary();
325         onapAuxilary.setLog_info(Map.of());
326         onapAuxilary.setTls_info(Map.of());
327         onapAuxilary.setPorts(List.of("testPorts"));
328         onapAuxilary.setVolumes(new Volumes[]{getVolumes()});
329         return onapAuxilary;
330     }
331
332     private Volumes getVolumes() {
333         return new Volumes();
334     }
335
336 }