1 package org.openecomp.config.test;
3 import org.openecomp.config.api.Configuration;
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.After;
9 import org.junit.Assert;
10 import org.junit.Before;
11 import org.junit.Test;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.util.Properties;
20 * Pre-requisite - set -Dconfig.location=${"user.home"}/TestResources/ while running test
21 * Created by sheetalm on 10/19/2016.
23 * Unregister notification and verify listener
25 public class UnregisterNotificationTest {
26 public final static String NAMESPACE = "UnregisterNotification";
28 public String updatedValue = null;
31 public void setUp() throws IOException {
32 String data = "{name:\"SCM\"}";
33 TestUtil.writeFile(data);
37 public void testNotification() throws IOException, InterruptedException {
38 Configuration config = ConfigurationManager.lookup();
40 System.out.println(config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
42 PropertyListener propListener = new PropertyListener();
43 config.addConfigurationChangeListener(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH,propListener);
49 System.out.println(config.getAsString(NAMESPACE,ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
51 //Verify listener is invoked and updated value to 20
52 Assert.assertEquals("20" , updatedValue);
54 config.removeConfigurationChangeListener(NAMESPACE,ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH,propListener);
60 //When listener is unregistered updating value does not invoke any listener and value from listener should remain unchanged
61 Assert.assertEquals("20" , updatedValue);
63 //Verify value is updated even if listener is unregistered
64 Assert.assertEquals("22" , config.getAsString(NAMESPACE,ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH));
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 OutputStream out = new FileOutputStream( f );
74 props.store(out, "Override Config Property at Conventional Resource");
78 private class PropertyListener implements ConfigurationChangeListener {
80 public void notify(String key, Object oldValue, Object newValue) {
81 System.out.println("received notification::oldValue=="+oldValue+" newValue=="+newValue);
82 updatedValue = newValue.toString();
87 public void tearDown() throws Exception {
89 File f = new File(TestUtil.jsonSchemaLoc+"config.properties");
91 boolean isDeleted = f.delete();