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.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;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.LinkedHashMap;
37 import java.util.List;
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;
60 @RunWith(MockitoJUnitRunner.class)
61 public class PropertiesServiceTest {
63 private static final String PROPERTIES = "properties";
64 private static final String INPUTS = "inputs";
67 private PropertiesService propertiesService;
69 private AppConfigService appConfigService;
71 private ResourceConfigService resourceConfigService;
73 private StreamsService streamsService;
75 private BlueprintHelperService blueprintHelperService;
77 private Map<String, ?> resourceConfigResponse;
81 resourceConfigResponse = Map.of(INPUTS, new LinkedHashMap<>());
82 when(resourceConfigService.createResourceConfig(any(), eq(getSelf().getName())))
83 .thenReturn((Map<String, Object>) resourceConfigResponse);
87 public void shouldReturnInputsAndPropertiesForDmaap() {
89 Map<String, Object> response = propertiesService
90 .createDmaapProperties(new HashMap<>(), getOnapComponentSpec(), "");
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));
98 Properties actualProperties = (Properties) response.get(PROPERTIES);
99 assertNull(actualProperties.getStreams_subscribes());
100 assertNull(actualProperties.getStreams_publishes());
104 public void shouldReturnInputsAndPropertiesForOnap() {
106 when(appConfigService.createAppconfig(any(), eq(getOnapComponentSpec()), eq(false)))
107 .thenReturn((Map<String, Object>) resourceConfigResponse);
110 Map<String, Object> response = propertiesService
111 .createOnapProperties(new HashMap<>(), getOnapComponentSpec(), "");
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));
120 Properties actualProperties = (Properties) response.get(PROPERTIES);
121 assertNull(actualProperties.getStreams_subscribes());
122 assertNull(actualProperties.getStreams_publishes());
126 public void shouldSetStreamsMessageRoutesPublishesInProperties() {
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.A_TOPIC),
132 eq(publishes.getType()),
133 eq(publishes.getConfig_key()),
134 eq(publishes.getRoute()),
135 eq('p'))).thenReturn(getStreams());
138 Map<String, Object> response = propertiesService
139 .createDmaapProperties(new HashMap<>(), onapComponentSpec, "");
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());
151 public void shouldSetStreamsDataRoutesPublishesInProperties() {
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.A_FEED),
157 eq(publishes.getType()),
158 eq(publishes.getConfig_key()),
159 eq(publishes.getRoute()),
160 eq('p'))).thenReturn(getStreams());
163 Map<String, Object> response = propertiesService
164 .createDmaapProperties(new HashMap<>(), onapComponentSpec, "");
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());
176 public void shouldSetStreamsDataRoutesSubscribesInProperties() {
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.A_TOPIC),
182 eq(subscribes.getType()),
183 eq(subscribes.getConfig_key()),
184 eq(subscribes.getRoute()),
185 eq('s'))).thenReturn(getStreams());
188 Map<String, Object> response = propertiesService
189 .createDmaapProperties(new HashMap<>(), onapComponentSpec, "");
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());
201 public void shouldSetStreamsMessageRoutesSubscribesInProperties() {
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.A_FEED),
207 eq(subscribes.getType()),
208 eq(subscribes.getConfig_key()),
209 eq(subscribes.getRoute()),
210 eq('s'))).thenReturn(getStreams());
213 Map<String, Object> response = propertiesService
214 .createDmaapProperties(new HashMap<>(), onapComponentSpec, "");
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());
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;
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;
243 private HashMap<String, Object> getStreams() {
244 return new HashMap<>() {{
245 put("streams", getDmaapStreams());
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");
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");
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");
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"));
288 return expectedProperties;
291 private TlsInfo getTlsInfo() {
292 TlsInfo tlsInfo = new TlsInfo();
293 tlsInfo.setUseTls(getGetInput("use_tls"));
297 private GetInput getGetInput(String location_id) {
298 GetInput location = new GetInput();
299 location.setBpInputName(location_id);
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;
311 private Self getSelf() {
312 Self self = new Self();
313 self.setName("test.Name");
317 private Artifacts getTestUriArtifact() {
318 Artifacts artifacts = new Artifacts();
319 artifacts.setUri("test://testUri");
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()});
332 private Volumes getVolumes() {
333 return new Volumes();