Fix sonar issues
[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.Map;
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;
46
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 {
52
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";
57
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";
62
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";
67
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";
71
72     @Autowired
73     private AppConfigService appConfigService;
74
75     private OnapComponentSpec componentSpec;
76
77     @Before
78     public void setUp() {
79         componentSpec = Mockito.mock(OnapComponentSpec.class);
80     }
81
82     @Test
83     public void shouldCreateStringInputForStringParameter() {
84
85         mockParameters(PARAMETERS_TYPE_STRING, TEST_STRING_VALUE);
86         Map<String, Map<String, Object>> inputs = new HashMap<>();
87         
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);
91
92         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
93         assertEquals(STRING_INPUT_TYPE, createdParameters.get(TYPE));
94         assertEquals(TEST_STRING_VALUE, createdParameters.get(DEFAULT));
95     }
96
97     @Test
98     public void shouldCreateStringInputForUnknownParameter() {
99
100         mockParameters(UNKNOWN_TYPE, TEST_STRING_VALUE);
101         Map<String, Map<String, Object>> inputs = new HashMap<>();
102         
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);
106
107         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
108         assertEquals(STRING_INPUT_TYPE, createdParameters.get(TYPE));
109         assertEquals(TEST_STRING_VALUE, createdParameters.get(DEFAULT));
110     }
111
112     @Test
113     public void shouldCreateBooleanInputForBooleanParameter() {
114
115         mockParameters(PARAMETERS_TYPE_BOOLEAN, BOOLEAN_TEST_VALUE);
116         Map<String, Map<String, Object>> inputs = new HashMap<>();
117         
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);
121
122         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
123         assertEquals(BOOLEAN_INPUT_TYPE, createdParameters.get(TYPE));
124         assertEquals(BOOLEAN_TEST_VALUE, createdParameters.get(DEFAULT));
125     }
126
127     @Test
128     public void shouldCreateIntegerInputForIntegerParameter() {
129
130         mockParameters(PARAMETERS_TYPE_INTEGER, 123);
131         Map<String, Map<String, Object>> inputs = new HashMap<>();
132         
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);
136
137         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
138         assertEquals(INTEGER_INPUT_TYPE, createdParameters.get(TYPE));
139         assertEquals(123, createdParameters.get(DEFAULT));
140     }
141
142     @Test
143     public void shouldCreateIntegerInputForNumberParameter() {
144
145         mockParameters(PARAMETERS_TYPE_NUMBER, 123);
146         Map<String, Map<String, Object>> inputs = new HashMap<>();
147         
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);
151
152
153         assertAppConfigContainsParameterWithCorrectInputName(appConfig);
154         assertEquals(INTEGER_INPUT_TYPE, createdParameters.get(TYPE));
155         assertEquals(123, createdParameters.get(DEFAULT));
156     }
157
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);
162     }
163
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);
173     }
174 }