[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / common / openecomp-common-configuration-management / openecomp-configuration-management-test / src / test / java / org / openecomp / config / test / NodeSpecificCLITest.java
1 package org.openecomp.config.test;
2
3 import org.openecomp.config.Constants;
4 import org.openecomp.config.api.ConfigurationChangeListener;
5 import org.openecomp.config.api.ConfigurationManager;
6 import org.openecomp.config.util.ConfigTestConstant;
7 import org.openecomp.config.util.TestUtil;
8 import org.junit.AfterClass;
9 import org.junit.Assert;
10 import org.junit.Test;
11
12 import javax.management.JMX;
13 import javax.management.MBeanServerConnection;
14 import javax.management.ObjectName;
15 import java.io.File;
16 import java.io.FileOutputStream;
17 import java.io.OutputStream;
18 import java.lang.management.ManagementFactory;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Properties;
22
23 /**
24  * Created by sheetalm on 10/19/2016.
25  * Scenario 19
26  * Pre-requisite - set -Dnode.config.location=${"user.home"}/TestResources/ while running test
27  * Verify node specific override using CLI
28  */
29 public class NodeSpecificCLITest {
30
31     public final static String NAMESPACE = "NodeCLI";
32     private String updatedValue = "";
33
34     @Test
35     public void testCLIApi() throws Exception{
36         //Verify without fallback
37         Map<String, Object> input = new HashMap<>();
38         input.put("ImplClass", "org.openecomp.config.type.ConfigurationQuery");
39         input.put("namespace", NAMESPACE);
40         input.put("key", ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH);
41
42         MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer();
43         ObjectName mbeanName = new ObjectName(Constants.MBEAN_NAME);
44         ConfigurationManager conf = JMX.newMBeanProxy(mbsc, mbeanName, org.openecomp.config.api.ConfigurationManager.class, true);
45         String maxLength = conf.getConfigurationValue(input);
46
47         //Verify Property from Namespace configurations
48         Assert.assertEquals("30",maxLength);
49
50         //Add node specific configurations
51         Properties props = new Properties();
52         props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "50");
53         props.setProperty("_config.namespace",NAMESPACE);
54         File f = new File(TestUtil.jsonSchemaLoc+"config.properties");
55         OutputStream out = new FileOutputStream( f );
56         props.store(out, "Node Config Property");
57         out.close();
58
59         Thread.sleep(35000);
60
61         //Verify property from node specific configuration
62         input.put("nodeSpecific", true);
63         String nodeVal = conf.getConfigurationValue(input);
64         Assert.assertEquals("50", nodeVal);
65
66         //Add Change Listener
67         conf.addConfigurationChangeListener(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, new CLINodeListener());
68
69         //Update maxlength
70         input.put("ImplClass", "org.openecomp.config.type.ConfigurationUpdate");
71         input.put("nodeOverride", true);
72         input.put("nodeSpecific", false);
73         input.put("value", "60");
74         conf.updateConfigurationValue(input);
75
76         Thread.sleep(35000);
77
78         Assert.assertEquals("60",updatedValue);
79
80         //Fetch the updated nodespecific value
81         input.put("nodeOverride", false);
82         input.put("nodeSpecific", true);
83         input.put("ImplClass", "org.openecomp.config.type.ConfigurationQuery");
84         String updatedMaxLength = conf.getConfigurationValue(input);
85         Assert.assertEquals("60",updatedMaxLength);
86
87         //Verify maxlength on other nodes by deleting node specific configuration
88         if(f.exists()) {
89             boolean isDeleted = f.delete();
90         }
91
92         Thread.sleep(35000);
93
94         input.put("ImplClass", "org.openecomp.config.type.ConfigurationQuery");
95         input.put("nodeOverride", false);
96         input.put("nodeSpecific", false);
97         System.out.println("val on other node is::"+conf.getConfigurationValue(input));
98         Assert.assertEquals("30",conf.getConfigurationValue(input));
99     }
100
101     @AfterClass
102     public static void tearDown() throws Exception {
103         TestUtil.cleanUp();
104         File f = new File(TestUtil.jsonSchemaLoc+"config.properties");
105         if(f.exists()) {
106             boolean isDeleted = f.delete();
107         }
108     }
109
110     private class CLINodeListener implements ConfigurationChangeListener {
111         @Override
112         public void notify(String key, Object oldValue, Object newValue) {
113             System.out.println("received notification::oldValue=="+oldValue+" newValue=="+newValue);
114             updatedValue = newValue.toString();
115         }
116     }
117 }