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;
30 import java.util.LinkedHashMap;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mockito;
36 import org.onap.blueprintgenerator.model.common.Appconfig;
37 import org.onap.blueprintgenerator.model.common.GetInput;
38 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
39 import org.onap.blueprintgenerator.model.componentspec.common.Parameters;
40 import org.onap.blueprintgenerator.service.InfoService;
41 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
42 import org.onap.blueprintgenerator.service.common.kafka.KafkaStreamService;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration(classes = {AppConfigService.class, BlueprintHelperService.class, DmaapService.class,
50 InfoService.class, StreamService.class, KafkaStreamService.class},
51 initializers = ConfigFileApplicationContextInitializer.class)
52 public class AppConfigServiceTest {
54 private static final String TEST_PARAMETER_NAME = "testParameter";
55 private static final String INPUTS = "inputs";
56 private static final String TYPE = "type";
57 private static final String DEFAULT = "default";
59 private static final String PARAMETERS_TYPE_STRING = "string";
60 private static final String PARAMETERS_TYPE_INTEGER = "integer";
61 private static final String PARAMETERS_TYPE_BOOLEAN = "boolean";
62 private static final String PARAMETERS_TYPE_NUMBER = "number";
64 private static final String STRING_INPUT_TYPE = "string";
65 private static final String INTEGER_INPUT_TYPE = "integer";
66 private static final String BOOLEAN_INPUT_TYPE = "boolean";
67 private static final String UNKNOWN_TYPE = "test_unknown_type";
69 private static final boolean BOOLEAN_TEST_VALUE = true;
70 private static final String TEST_STRING_VALUE = "testValue";
71 private static final String APP_CONFIG = "appconfig";
74 private AppConfigService appConfigService;
76 private OnapComponentSpec componentSpec;
80 componentSpec = Mockito.mock(OnapComponentSpec.class);
84 public void shouldCreateStringInputForStringParameter() {
86 mockParameters(PARAMETERS_TYPE_STRING, TEST_STRING_VALUE);
87 Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
89 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
90 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
91 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
93 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
94 assertEquals(STRING_INPUT_TYPE, createdParameters.get(TYPE));
95 assertEquals(TEST_STRING_VALUE, createdParameters.get(DEFAULT));
99 public void shouldCreateStringInputForUnknownParameter() {
101 mockParameters(UNKNOWN_TYPE, TEST_STRING_VALUE);
102 Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
104 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
105 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
106 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
108 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
109 assertEquals(STRING_INPUT_TYPE, createdParameters.get(TYPE));
110 assertEquals(TEST_STRING_VALUE, createdParameters.get(DEFAULT));
114 public void shouldCreateBooleanInputForBooleanParameter() {
116 mockParameters(PARAMETERS_TYPE_BOOLEAN, BOOLEAN_TEST_VALUE);
117 Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
119 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
120 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
121 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
123 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
124 assertEquals(BOOLEAN_INPUT_TYPE, createdParameters.get(TYPE));
125 assertEquals(BOOLEAN_TEST_VALUE, createdParameters.get(DEFAULT));
129 public void shouldCreateIntegerInputForIntegerParameter() {
131 mockParameters(PARAMETERS_TYPE_INTEGER, 123);
132 Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
134 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
135 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
136 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
138 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
139 assertEquals(INTEGER_INPUT_TYPE, createdParameters.get(TYPE));
140 assertEquals(123, createdParameters.get(DEFAULT));
144 public void shouldCreateIntegerInputForNumberParameter() {
146 mockParameters(PARAMETERS_TYPE_NUMBER, 123);
147 Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
149 Map<String, Object> appConfig = appConfigService.createAppconfig(inputs, componentSpec, false);
150 Map<String, Object> createdInputs = (Map<String, Object>) appConfig.get(INPUTS);
151 Map<String, Object> createdParameters = (Map<String, Object>) createdInputs.get(TEST_PARAMETER_NAME);
154 assertAppConfigContainsParameterWithCorrectInputName(appConfig);
155 assertEquals(INTEGER_INPUT_TYPE, createdParameters.get(TYPE));
156 assertEquals(123, createdParameters.get(DEFAULT));
159 private void assertAppConfigContainsParameterWithCorrectInputName(Map<String, Object> appConfig) {
160 Appconfig appConfigModel = (Appconfig) appConfig.get(APP_CONFIG);
161 Object bpInputName = ((GetInput) appConfigModel.getParams().get(TEST_PARAMETER_NAME)).getBpInputName();
162 assertEquals(bpInputName, TEST_PARAMETER_NAME);
165 private void mockParameters(String type, Object value) {
166 Parameters testParameter = new Parameters();
167 testParameter.setName(TEST_PARAMETER_NAME);
168 testParameter.setType(type);
169 testParameter.setSourced_at_deployment(true);
170 testParameter.setValue(value);
171 Parameters[] parametersArray = new Parameters[1];
172 parametersArray[0] = testParameter;
173 when(componentSpec.getParameters()).thenReturn(parametersArray);