Fix unprocessed NPE
[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.HashMap;
23 import java.util.Map;
24
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.onap.config.api.ConfigurationManager;
28 import org.onap.config.impl.CliConfigurationImpl;
29 import org.onap.config.util.ConfigTestConstant;
30 import org.onap.config.util.TestImplementationConfiguration;
31
32 public class CliConfigurationImpTest {
33
34     private static final String NAMESPACE = "CLIConfiguration";
35     private static final String TENANT = "OPENECOMP";
36     private static final String IMPL_KEY = "CLIImpl";
37     private static final String SERVICE_CONF = "testService";
38
39     @Test
40     public void testGenerateAndPopulateMap() {
41
42         // given
43         ConfigurationManager conf = new CliConfigurationImpl();
44         // when
45         Map outputMap = conf.generateMap(TENANT, NAMESPACE, ConfigTestConstant.ARTIFACT);
46         TestImplementationConfiguration testServiceImpl =
47             conf.populateMap(TENANT, NAMESPACE, IMPL_KEY, TestImplementationConfiguration.class)
48                 .get(SERVICE_CONF);
49
50         // then
51         validateCliMapConfig(outputMap);
52         assertTrue(testServiceImpl.isEnable());
53         assertEquals("org.junit.Test", testServiceImpl.getImplementationClass());
54     }
55
56     @Test
57     public void listConfigurationNullQueryShouldntFailWithNPETest() {
58         ConfigurationManager conf = new CliConfigurationImpl();
59         Assert.assertEquals(0, conf.listConfiguration(null).size());
60     }
61
62     @Test
63     public void listConfigurationEmptyQueryTest() {
64         ConfigurationManager conf = new CliConfigurationImpl();
65         Assert.assertEquals(0, conf.listConfiguration(new HashMap<>()).size());
66     }
67
68     @Test
69     public void getConfigurationValueNullQueryShouldntFailWithNPETest() {
70         ConfigurationManager conf = new CliConfigurationImpl();
71         Assert.assertNull (conf.getConfigurationValue(null));
72     }
73
74     @Test
75     public void getConfigurationValueEmptyQueryTest() {
76         ConfigurationManager conf = new CliConfigurationImpl();
77         Assert.assertNull (conf.getConfigurationValue(new HashMap<>()));
78     }
79
80     private void validateCliMapConfig(Map outputMap) {
81         assertEquals("appc",
82             outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_CONSUMER)));
83         assertEquals("6",
84             extract(outputMap, withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_MINLENGTH)));
85         assertEquals("true",
86             outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_ENCODED)));
87         assertEquals("14",
88             extract(outputMap, withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH)));
89         assertEquals("pdf", outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_EXT)));
90         assertEquals("Base64",
91             outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_ENC)));
92         assertEquals("a-zA-Z_0-9",
93             extract(outputMap, withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_NAME_UPPER)));
94         assertEquals("deleted",
95             outputMap.get(withoutArtifactPrefix(ConfigTestConstant.ARTIFACT_STATUS)));
96
97     }
98
99     private String withoutArtifactPrefix(String key) {
100         return key.replace(ConfigTestConstant.ARTIFACT + ".", "");
101     }
102
103     private String extract(Map map, String keys) {
104
105         String[] keysList = keys.split("\\.");
106         Map recursive = (Map) map.get(keysList[0]);
107
108         for (int i = 1; i < keysList.length; i++) {
109             if (i == keysList.length - 1) {
110                 return (String) recursive.get(keysList[i]);
111             }
112             recursive = (Map) recursive.get(keysList[i]);
113         }
114         return null;
115     }
116 }