61f93534765705e082d1f24b5af348c43eeefc55
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / test / java / org / onap / blueprintgenerator / service / common / AppConfigServiceTest.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.mockito.Mockito.when;
28
29 import java.util.HashMap;
30 import java.util.LinkedHashMap;
31 import java.util.Map;
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;
47
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 {
53
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";
58
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";
63
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";
68
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";
72
73     @Autowired
74     private AppConfigService appConfigService;
75
76     private OnapComponentSpec componentSpec;
77
78     @Before
79     public void setUp() {
80         componentSpec = Mockito.mock(OnapComponentSpec.class);
81     }
82
83     @Test
84     public void shouldCreateStringInputForStringParameter() {
85
86         mockParameters(PARAMETERS_TYPE_STRING, TEST_STRING_VALUE);
87         Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
88         
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);
92
93         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
94         assertEquals(STRING_INPUT_TYPE, createdParameters.get(TYPE));
95         assertEquals(TEST_STRING_VALUE, createdParameters.get(DEFAULT));
96     }
97
98     @Test
99     public void shouldCreateStringInputForUnknownParameter() {
100
101         mockParameters(UNKNOWN_TYPE, TEST_STRING_VALUE);
102         Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
103         
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);
107
108         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
109         assertEquals(STRING_INPUT_TYPE, createdParameters.get(TYPE));
110         assertEquals(TEST_STRING_VALUE, createdParameters.get(DEFAULT));
111     }
112
113     @Test
114     public void shouldCreateBooleanInputForBooleanParameter() {
115
116         mockParameters(PARAMETERS_TYPE_BOOLEAN, BOOLEAN_TEST_VALUE);
117         Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
118         
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);
122
123         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
124         assertEquals(BOOLEAN_INPUT_TYPE, createdParameters.get(TYPE));
125         assertEquals(BOOLEAN_TEST_VALUE, createdParameters.get(DEFAULT));
126     }
127
128     @Test
129     public void shouldCreateIntegerInputForIntegerParameter() {
130
131         mockParameters(PARAMETERS_TYPE_INTEGER, 123);
132         Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
133         
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);
137
138         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
139         assertEquals(INTEGER_INPUT_TYPE, createdParameters.get(TYPE));
140         assertEquals(123, createdParameters.get(DEFAULT));
141     }
142
143     @Test
144     public void shouldCreateIntegerInputForNumberParameter() {
145
146         mockParameters(PARAMETERS_TYPE_NUMBER, 123);
147         Map<String, LinkedHashMap<String, Object>> inputs = new HashMap<>();
148         
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);
152
153
154         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
155         assertEquals(INTEGER_INPUT_TYPE, createdParameters.get(TYPE));
156         assertEquals(123, createdParameters.get(DEFAULT));
157     }
158
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);
163     }
164
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);
174     }
175 }