Initial OpenECOMP MSO commit
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / openecomp / mso / bpmn / core / PropertyConfigurationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.core;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.nio.file.StandardCopyOption;
28 import java.util.Map;
29
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 import org.openecomp.mso.bpmn.test.PropertyConfigurationSetup;
35
36 public class PropertyConfigurationTest {
37         @Before
38         public void beforeTest() throws IOException {
39                 Map<String, String> defaultProperties = PropertyConfigurationSetup.createBpmnProperties();
40                 defaultProperties.put("testValue", "testKey");
41                 PropertyConfigurationSetup.init(defaultProperties);
42         }
43         
44         @Test
45         public void testPropertyFileWatcher() throws InterruptedException, IOException {
46                 Assert.assertEquals(true, PropertyConfiguration.getInstance().isFileWatcherRunning());
47         }
48         
49         @Test
50         public void testPropertyLoading() throws IOException, InterruptedException {
51                 PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
52                 Map<String,String> props = propertyConfiguration.getProperties(PropertyConfiguration.MSO_BPMN_PROPERTIES);
53                 Assert.assertNotNull(props);
54                 Assert.assertEquals("testValue", props.get("testKey"));
55         }
56         
57         @Test
58         public void testPropertyReload() throws IOException, InterruptedException {
59                 PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
60                 Map<String,String> properties = propertyConfiguration.getProperties(PropertyConfiguration.MSO_BPMN_PROPERTIES);
61                 Assert.assertNotNull(properties);
62                 Assert.assertEquals("testValue", properties.get("testKey"));
63
64                 Map<String, String> newProperties = PropertyConfigurationSetup.createBpmnProperties();
65                 newProperties.put("newKey", "newValue");
66                 PropertyConfigurationSetup.addProperties(newProperties, 10000);
67
68                 // Reload and check for the new value
69                 properties = propertyConfiguration.getProperties(PropertyConfiguration.MSO_BPMN_PROPERTIES);
70                 Assert.assertNotNull(properties);
71                 Assert.assertEquals("newValue", properties.get("newKey"));
72         }
73         
74         @Test(expected=IllegalArgumentException.class)
75         public void testPropertyFileDoesNotExists_NotIntheList() throws IOException {
76                 PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
77                 propertyConfiguration.getProperties("badfile.properties");
78                 Assert.fail("Expected IllegalArgumentException");
79         }
80         
81         @Test(expected=java.lang.UnsupportedOperationException.class)
82         public void testPropertyModificationException() throws IOException {
83                 PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
84                 Map<String,String> props = propertyConfiguration.getProperties(PropertyConfiguration.MSO_BPMN_PROPERTIES);
85                 Assert.assertNotNull(props);
86                 Assert.assertEquals("testValue", props.get("testKey"));
87                 props.put("newKey", "newvalue");
88         }
89         
90         @Test
91         public void testNotAllowedPropertyReloading() throws IOException {
92                 Path msoConfigPath = Paths.get(System.getProperty("mso.config.path"));
93                 Path backupPropFilePath = msoConfigPath.resolve("backup-" + PropertyConfiguration.MSO_BPMN_PROPERTIES);
94
95                 try {
96                         // Create a new file... a backup file
97                         Files.createFile(backupPropFilePath);
98
99                         // Load properties
100                         PropertyConfiguration propertyConfiguration = PropertyConfiguration.getInstance();
101                         Map<String,String> props = propertyConfiguration.getProperties(PropertyConfiguration.MSO_BPMN_PROPERTIES);
102                         Assert.assertNotNull(props);
103                         Assert.assertEquals("testValue", props.get("testKey"));
104
105                         // Update the backup file
106                         Path bpmnPropertiesSourcePath = Paths.get("src", "test", "resources", "mso.bpmn.properties");
107                         Files.copy(bpmnPropertiesSourcePath, backupPropFilePath, StandardCopyOption.REPLACE_EXISTING);
108
109                         // Cache size should remain the same
110                         Assert.assertEquals(1, PropertyConfiguration.getInstance().cacheSize());
111                 } finally {
112                         backupPropFilePath.toFile().delete();
113                 }
114         }
115 }