3d12eac13ddbafe180570894b57d7409b05943f8
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / test / java / org / onap / config / CliConfigurationImpTest.java
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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21
22 import java.util.Map;
23
24 import org.junit.Test;
25 import org.onap.config.api.ConfigurationManager;
26 import org.onap.config.impl.CliConfigurationImpl;
27 import org.onap.config.util.ConfigTestConstant;
28 import org.onap.config.util.TestImplementationConfiguration;
29
30 public class CliConfigurationImpTest {
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() {
39
40         // given
41         ConfigurationManager conf = new CliConfigurationImpl();
42         // when
43         Map outputMap = conf.generateMap(TENANT, NAMESPACE, ConfigTestConstant.ARTIFACT);
44         TestImplementationConfiguration testServiceImpl =
45             conf.populateMap(TENANT, NAMESPACE, IMPL_KEY, TestImplementationConfiguration.class)
46                 .get(SERVICE_CONF);
47
48         // then
49         validateCliMapConfig(outputMap);
50         assertTrue(testServiceImpl.isEnable());
51         assertEquals("org.junit.Test", testServiceImpl.getImplementationClass());
52     }
53
54     private void validateCliMapConfig(Map outputMap) {
55         assertEquals("appc",
56             outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_CONSUMER)));
57         assertEquals("6",
58             extract(outputMap, withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_MINLENGTH)));
59         assertEquals("true",
60             outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_ENCODED)));
61         assertEquals("14",
62             extract(outputMap, withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH)));
63         assertEquals("pdf", outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_EXT)));
64         assertEquals("Base64",
65             outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_ENC)));
66         assertEquals("a-zA-Z_0-9",
67             extract(outputMap, withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_UPPER)));
68         assertEquals("deleted",
69             outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_STATUS)));
70
71     }
72
73     private String withoutArtifactPrefix(String key) {
74         return key.replace(ConfigTestConstant.ARTIFACT + ".", "");
75     }
76
77     private 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 }