40807155b6b2dae448aa63d27e4109545533acbc
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / test / java / org / onap / config / test / NodeSpecificCliTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
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.test;
18
19 import static org.junit.Assert.assertTrue;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.OutputStream;
24 import java.lang.management.ManagementFactory;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Properties;
28 import javax.management.JMX;
29 import javax.management.MBeanServerConnection;
30 import javax.management.ObjectName;
31 import org.junit.AfterClass;
32 import org.junit.Assert;
33 import org.junit.Test;
34 import org.onap.config.Constants;
35 import org.onap.config.api.ConfigurationManager;
36 import org.onap.config.util.ConfigTestConstant;
37 import org.onap.config.util.TestUtil;
38
39 /**
40  * Created by sheetalm on 10/19/2016.
41  * Scenario 19
42  * Pre-requisite - set -Dnode.config.location=${"user.home"}/TestResources/ while running test
43  * Verify node specific override using CLI
44  */
45 public class NodeSpecificCliTest {
46
47     private static final String NAMESPACE = "NodeCLI";
48
49     @AfterClass
50     public static void tearDown() throws Exception {
51         TestUtil.cleanUp();
52         File f = new File(TestUtil.jsonSchemaLoc + "config.properties");
53         if (f.exists()) {
54             assertTrue(f.delete());
55         }
56     }
57
58     @Test
59     public void testCliApi() throws Exception {
60         //Verify without fallback
61         Map<String, Object> input = new HashMap<>();
62         input.put("ImplClass", "org.onap.config.type.ConfigurationQuery");
63         input.put("namespace", NAMESPACE);
64         input.put("key", ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH);
65
66         MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer();
67         ObjectName mbeanName = new ObjectName(Constants.MBEAN_NAME);
68         ConfigurationManager conf = JMX.newMBeanProxy(mbsc, mbeanName, ConfigurationManager.class, true);
69         String maxLength = conf.getConfigurationValue(input);
70
71         //Verify Property from Namespace configurations
72         Assert.assertEquals("30", maxLength);
73
74         //Add node specific configurations
75         Properties props = new Properties();
76         props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "50");
77         props.setProperty("_config.namespace", NAMESPACE);
78         File f = new File(TestUtil.jsonSchemaLoc + "config.properties");
79         try (OutputStream out = new FileOutputStream(f)) {
80             props.store(out, "Node Config Property");
81         }
82
83         //Verify property from node specific configuration
84         input.put("nodeSpecific", true);
85         String nodeVal = conf.getConfigurationValue(input);
86         Assert.assertEquals("30", nodeVal);
87
88         if (f.exists()) {
89             assertTrue(f.delete());
90         }
91     }
92 }