re base code
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-test / src / test / java / org / onap / config / test / ResourceChangeNotificationTest.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.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 /**
18  * Scenario 7
19  * Test to Validate notification on changes to the underlying source
20  * Resource here is GeneratorsList.json ehich is created in test itself
21  */
22
23 public class ResourceChangeNotificationTest  {
24
25         String newValue = null;
26
27         public final static String NAMESPACE = "Notification";
28         
29         @Before
30         public void setUp() throws IOException {
31                 String data = "{name:\"SCM\"}";
32                 TestUtil.writeFile(data);
33         }
34
35         @Test
36         public void testNotification() throws IOException, InterruptedException {               
37                 Configuration config = ConfigurationManager.lookup();           
38                 config.addConfigurationChangeListener(NAMESPACE,ConfigTestConstant.ARTIFACT_JSON_SCHEMA, new MyListener());
39                 updateJsonInFile();
40                 Thread.sleep(35000);
41                 String newValue = config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_JSON_SCHEMA);
42
43                 Assert.assertEquals("{name:\"updated SCM\"}",newValue);
44                 
45                 Assert.assertEquals( "14",config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_MAXLENGTH ));
46                 
47                 Assert.assertEquals( "a-zA-Z", config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_NAME_UPPER ));
48                 
49                 String artifactConsumer = config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_CONSUMER );
50                 Assert.assertEquals(artifactConsumer,config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_CONSUMER_APPC ));
51
52                 List<String> expectedExtList = new ArrayList<String>();
53                 expectedExtList.add("pdf"); expectedExtList.add("zip"); expectedExtList.add("xml");
54                 List<String> extList = config.getAsStringValues(NAMESPACE, ConfigTestConstant.ARTIFACT_EXT);
55                 Assert.assertEquals(expectedExtList, extList);
56                 
57                 List<String> expectedEncList = new ArrayList<String>();
58                 expectedEncList.add("Base64"); expectedEncList.add("MD5"); 
59                 List<String> encList = config.getAsStringValues(NAMESPACE, ConfigTestConstant.ARTIFACT_ENC);
60                 Assert.assertEquals(expectedEncList, encList);          
61                 
62                 List<String> expectedLocList = new ArrayList<String>();
63                 expectedLocList.add("/opt/spool"); expectedLocList.add(System.getProperty("user.home")+"/asdc");
64                 List<String> locList = config.getAsStringValues(NAMESPACE, ConfigTestConstant.ARTIFACT_LOC);
65                 Assert.assertEquals(expectedLocList, locList);
66
67                 Assert.assertEquals("@"+System.getenv("Path")+"/myschema.json",config.getAsString(NAMESPACE, ConfigTestConstant.ARTIFACT_XML_SCHEMA));
68         }
69         
70         class MyListener implements ConfigurationChangeListener{
71                 @Override
72                 public void notify(String key, Object oldValue, Object newValue) {                      
73                         System.out.println("received notification::oldValue=="+oldValue+" newValue=="+newValue);                        
74                 }               
75         }
76         
77         private void updateJsonInFile() throws IOException{     
78                 String data = "{name:\"updated SCM\"}";
79                 TestUtil.writeFile(data);
80         }
81         
82         @After
83         public void tearDown() throws Exception {
84                 TestUtil.cleanUp();
85         }
86 }