Rename packages from openecomp to onap.
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-test / src / test / java / org / onap / config / test / UnregisterNotificationTest.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.Assert;
10 import org.junit.Before;
11 import org.junit.Test;
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  * Created by sheetalm on 10/19/2016.
22  * Scenario 24
23  * Unregister notification and verify listener
24  */
25 public class UnregisterNotificationTest {
26     public final static String NAMESPACE = "UnregisterNotification";
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         PropertyListener propListener = new PropertyListener();
43         config.addConfigurationChangeListener(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH,propListener);
44
45         updateValue("20");
46
47         Thread.sleep(35000);
48
49         System.out.println(config.getAsString(NAMESPACE,ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
50
51         //Verify listener is invoked and updated value to 20
52         Assert.assertEquals("20" , updatedValue);
53
54         config.removeConfigurationChangeListener(NAMESPACE,ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH,propListener);
55
56         updateValue("22");
57
58         Thread.sleep(35000);
59
60         //When listener is unregistered updating value does not invoke any listener and  value from listener should remain unchanged
61         Assert.assertEquals("20" , updatedValue);
62
63         //Verify value is updated even if listener is unregistered
64         Assert.assertEquals("22" , config.getAsString(NAMESPACE,ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
65     }
66
67     private void updateValue(String newValue) throws IOException {
68         Properties props = new Properties();
69         props.setProperty(ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH, newValue);
70         props.setProperty("_config.namespace",NAMESPACE);
71         props.setProperty("_config.mergeStrategy","override");
72         File f = new File(TestUtil.jsonSchemaLoc+"config.properties");
73         try (OutputStream out = new FileOutputStream(f)) {
74             props.store(out, "Override Config Property at Conventional Resource");
75         }
76     }
77
78     private class PropertyListener implements ConfigurationChangeListener {
79         @Override
80         public void notify(String key, Object oldValue, Object newValue) {
81             System.out.println("received notification::oldValue=="+oldValue+" newValue=="+newValue);
82             updatedValue = newValue.toString();
83         }
84     }
85
86     @After
87     public void tearDown() throws Exception {
88         TestUtil.cleanUp();
89         File f = new File(TestUtil.jsonSchemaLoc+"config.properties");
90         if(f.exists()) {
91             f.delete();
92         }
93     }
94 }