Rename packages from openecomp to onap.
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-test / src / test / java / org / onap / config / test / NotificationOnPropValTest.java
1 package org.onap.config.test;
2
3 import org.onap.config.api.Configuration;
4 import org.onap.config.api.ConfigurationChangeListener;
5 import org.onap.config.api.ConfigurationManager;
6 import org.onap.config.util.ConfigTestConstant;
7 import org.onap.config.util.TestUtil;
8 import org.junit.After;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.junit.Assert;
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  * Pre-requisite - set -Dconfig.location=${"user.home"}/TestResources/ while running test
21  * Scenario 14 - Verify Change Notifications for any change in the registered key
22  * Created by sheetalm on 10/14/2016.
23  */
24 public class NotificationOnPropValTest {
25
26     public final static String NAMESPACE = "NotificationOnPropVal";
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 testNotification() throws IOException, InterruptedException {
38         Configuration config = ConfigurationManager.lookup();
39
40         System.out.println(config.getAsString(NAMESPACE,ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
41
42         config.addConfigurationChangeListener(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, new PropValListener());
43
44         Properties props = new Properties();
45         props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, "20");
46         props.setProperty("_config.namespace",NAMESPACE);
47         props.setProperty("_config.mergeStrategy","override");
48         File f = new File(TestUtil.jsonSchemaLoc + "config.properties");
49         try (OutputStream out = new FileOutputStream(f)) {
50             props.store(out, "Override Config Property at Conventional Resource");
51         }
52
53         Thread.sleep(35000);
54
55         System.out.println(config.getAsString(NAMESPACE,ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
56
57         Assert.assertEquals("20" , updatedValue);
58     }
59
60      private class PropValListener implements ConfigurationChangeListener {
61         @Override
62         public void notify(String key, Object oldValue, Object newValue) {
63             System.out.println("received notification::oldValue=="+oldValue+" newValue=="+newValue);
64             updatedValue = newValue.toString();
65         }
66     }
67
68     @After
69     public void tearDown() throws Exception {
70         TestUtil.cleanUp();
71         File f = new File(TestUtil.jsonSchemaLoc+"config.properties");
72         if(f.exists()) {
73             f.delete();
74         }
75     }
76 }