re base code
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-test / src / test / java / org / onap / config / test / NotificationForNodeConfigTest.java
1 package org.onap.config.test;
2
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.onap.config.api.Configuration;
8 import org.onap.config.api.ConfigurationChangeListener;
9 import org.onap.config.api.ConfigurationManager;
10 import org.onap.config.util.ConfigTestConstant;
11 import org.onap.config.util.TestUtil;
12
13 import java.io.File;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.util.Properties;
18
19 /**
20  * Scenario 15
21  * Update and Verify Change Notifications for any change in the registered key for node specific configuration
22  * Pre-requisite - set -Dnode.config.location=${"user.home"}/TestResources/ while running test
23  * Created by sheetalm on 10/17/2016.
24  */
25 public class NotificationForNodeConfigTest {
26     public final static String NAMESPACE = "NotificationForNodeConfig";
27
28     private String updatedValue = null;
29
30     @Before
31     public void setUp() throws IOException {
32         String data = "{name:\"SCM\"}";
33         TestUtil.writeFile(data);
34     }
35
36     @Test
37     public void testNotificationForNode() throws IOException, InterruptedException {
38         Configuration config = ConfigurationManager.lookup();
39
40         System.out.println(config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
41
42         Properties props = new Properties();
43         props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "30");
44         props.setProperty("_config.namespace",NAMESPACE);
45         File f = new File(TestUtil.jsonSchemaLoc + "config.properties");
46         try (OutputStream out = new FileOutputStream(f)) {
47             props.store(out, "Node Config Property");
48         }
49
50         Thread.sleep(35000);
51
52         //Verify property from node specific configuration
53         Assert.assertEquals("30", config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
54
55         config.addConfigurationChangeListener(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, new NodePropValListener());
56
57         props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "80");
58         try (OutputStream out = new FileOutputStream(f)) {
59             props.store(out, "Updated Node Config Property");
60         }
61
62         Thread.sleep(35000);
63
64         //Verify change listenere is invoked when node specific configuration is changed.
65         Assert.assertEquals("80", updatedValue);
66
67     }
68
69     private class NodePropValListener implements ConfigurationChangeListener {
70         @Override
71         public void notify(String key, Object oldValue, Object newValue) {
72             System.out.println("received notification::oldValue=="+oldValue+" newValue=="+newValue);
73             updatedValue = newValue.toString();
74         }
75     }
76
77     @After
78     public void tearDown() throws Exception {
79         TestUtil.cleanUp();
80         File f = new File(TestUtil.jsonSchemaLoc+"config.properties");
81         if(f.exists()) {
82             f.delete();
83         }
84     }
85 }