0d460a2bb1eeffa4ef5cdd1461024c1ac8d7e4e4
[sdc.git] /
1 /*
2  * Copyright (C) 2019 Samsung. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.config;
18
19 import org.junit.Test;
20 import org.onap.config.api.ConfigurationManager;
21 import org.onap.config.impl.CliConfigurationImpl;
22 import org.onap.config.util.ConfigTestConstant;
23 import org.onap.config.util.TestImplementationConfiguration;
24
25 import java.util.Map;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29
30 public class CliConfigurtationImpTest {
31
32     private static final String NAMESPACE = "CLIConfiguration";
33     private static final String TENANT = "OPENECOMP";
34     private static final String IMPL_KEY = "CLIImpl";
35     private static final String SERVICE_CONF = "testService";
36
37     @Test
38     public void testGenerateAndPopulateMap() throws Exception {
39
40         // given
41         ConfigurationManager conf = new CliConfigurationImpl();
42         // when
43         Map outputMap = conf.generateMap(TENANT,  NAMESPACE, ConfigTestConstant.ARTIFACT);
44         TestImplementationConfiguration testServiceImpl = conf.populateMap(TENANT, NAMESPACE, IMPL_KEY,
45                 TestImplementationConfiguration.class).get(SERVICE_CONF);
46
47         // then
48         validateCliMapConfig(outputMap);
49         assertTrue(testServiceImpl.isEnable());
50         assertEquals("org.junit.Test", testServiceImpl.getImplementationClass());
51     }
52
53     private void validateCliMapConfig(Map outputMap){
54         assertEquals("appc", outputMap.get(
55                 withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_CONSUMER)));
56         assertEquals("6", extract(outputMap,
57                 withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_MINLENGTH)));
58         assertEquals("true", outputMap.get(
59                 withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_ENCODED)));
60         assertEquals("14", extract(outputMap,
61                 withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH)));
62         assertEquals("pdf", outputMap.get(
63                 withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_EXT)));
64         assertEquals("Base64", outputMap.get(
65                 withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_ENC)));
66         assertEquals("a-zA-Z_0-9", extract(outputMap,
67                 withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_UPPER)));
68         assertEquals("deleted", outputMap.get(
69                 withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_STATUS)));
70
71     }
72
73     public String withoutArtifactPrefix(String key){
74         return key.replace(ConfigTestConstant.ARTIFACT + ".", "");
75     }
76
77     public String extract(Map map, String keys) {
78
79         String[] keysList = keys.split("\\.");
80         Map recursive = (Map) map.get(keysList[0]);
81
82         for (int i = 1; i < keysList.length; i++) {
83             if (i == keysList.length - 1) {
84                 return  (String) recursive.get(keysList[i]);
85             }
86             recursive = (Map) recursive.get(keysList[i]);
87         }
88         return null;
89     }
90
91
92 }