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.mockito.Mockito.when;
29 import java.util.HashMap;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mockito;
35 import org.onap.blueprintgenerator.model.common.Appconfig;
36 import org.onap.blueprintgenerator.model.common.GetInput;
37 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
38 import org.onap.blueprintgenerator.model.componentspec.common.Parameters;
39 import org.onap.blueprintgenerator.service.InfoService;
40 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
41 import org.onap.blueprintgenerator.service.common.kafka.KafkaStreamService;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 @RunWith(SpringJUnit4ClassRunner.class)
48 @ContextConfiguration(classes = {AppConfigService.class, BlueprintHelperService.class, DmaapService.class,
49 InfoService.class, StreamService.class, KafkaStreamService.class},
50 initializers = ConfigFileApplicationContextInitializer.class)
51 public class AppConfigServiceTest {
53 private static final String TEST_PARAMETER_NAME = "testParameter";
54 private static final String INPUTS = "inputs";
55 private static final String TYPE = "type";
56 private static final String DEFAULT = "default";
58 private static final String PARAMETERS_TYPE_STRING = "string";
59 private static final String PARAMETERS_TYPE_INTEGER = "integer";
60 private static final String PARAMETERS_TYPE_BOOLEAN = "boolean";
61 private static final String PARAMETERS_TYPE_NUMBER = "number";
63 private static final String STRING_INPUT_TYPE = "string";
64 private static final String INTEGER_INPUT_TYPE = "integer";
65 private static final String BOOLEAN_INPUT_TYPE = "boolean";
66 private static final String UNKNOWN_TYPE = "test_unknown_type";
68 private static final boolean BOOLEAN_TEST_VALUE = true;
69 private static final String TEST_STRING_VALUE = "testValue";
70 private static final String APP_CONFIG = "appconfig";
73 private AppConfigService appConfigService;
75 private OnapComponentSpec componentSpec;
79 componentSpec = Mockito.mock(OnapComponentSpec.class);
83 public void shouldCreateStringInputForStringParameter() {
85 mockParameters(PARAMETERS_TYPE_STRING, TEST_STRING_VALUE);
86 Map<String, Map<String, Object>> inputs = new HashMap<>();
88 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
89 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
90 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
92 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
93 assertEquals(STRING_INPUT_TYPE, createdParameters.get(TYPE));
94 assertEquals(TEST_STRING_VALUE, createdParameters.get(DEFAULT));
98 public void shouldCreateStringInputForUnknownParameter() {
100 mockParameters(UNKNOWN_TYPE, TEST_STRING_VALUE);
101 Map<String, Map<String, Object>> inputs = new HashMap<>();
103 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
104 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
105 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
107 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
108 assertEquals(STRING_INPUT_TYPE, createdParameters.get(TYPE));
109 assertEquals(TEST_STRING_VALUE, createdParameters.get(DEFAULT));
113 public void shouldCreateBooleanInputForBooleanParameter() {
115 mockParameters(PARAMETERS_TYPE_BOOLEAN, BOOLEAN_TEST_VALUE);
116 Map<String, Map<String, Object>> inputs = new HashMap<>();
118 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
119 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
120 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
122 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
123 assertEquals(BOOLEAN_INPUT_TYPE, createdParameters.get(TYPE));
124 assertEquals(BOOLEAN_TEST_VALUE, createdParameters.get(DEFAULT));
128 public void shouldCreateIntegerInputForIntegerParameter() {
130 mockParameters(PARAMETERS_TYPE_INTEGER, 123);
131 Map<String, Map<String, Object>> inputs = new HashMap<>();
133 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
134 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
135 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
137 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
138 assertEquals(INTEGER_INPUT_TYPE, createdParameters.get(TYPE));
139 assertEquals(123, createdParameters.get(DEFAULT));
143 public void shouldCreateIntegerInputForNumberParameter() {
145 mockParameters(PARAMETERS_TYPE_NUMBER, 123);
146 Map<String, Map<String, Object>> inputs = new HashMap<>();
148 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
149 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
150 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
153 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
154 assertEquals(INTEGER_INPUT_TYPE, createdParameters.get(TYPE));
155 assertEquals(123, createdParameters.get(DEFAULT));
158 private void assertAppConfigContainsParameterWithCorrectInputName(Map<String, Object> appConfig) {
159 Appconfig appConfigModel = (Appconfig) appConfig.get(APP_CONFIG);
160 Object bpInputName = ((GetInput) appConfigModel.getParams().get(TEST_PARAMETER_NAME)).getBpInputName();
161 assertEquals(bpInputName, TEST_PARAMETER_NAME);
164 private void mockParameters(String type, Object value) {
165 Parameters testParameter = new Parameters();
166 testParameter.setName(TEST_PARAMETER_NAME);
167 testParameter.setType(type);
168 testParameter.setSourced_at_deployment(true);
169 testParameter.setValue(value);
170 Parameters[] parametersArray = new Parameters[1];
171 parametersArray[0] = testParameter;
172 when(componentSpec.getParameters()).thenReturn(parametersArray);