Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-nosqldb-lib / openecomp-nosqldb-core / src / test / java / org / openecomp / core / nosqldb / util / ConfigurationManagerTest.java
1 package org.openecomp.core.nosqldb.util;
2
3 import org.testng.annotations.BeforeMethod;
4 import org.testng.annotations.Test;
5
6 import java.io.Closeable;
7 import java.io.IOException;
8 import java.lang.reflect.Field;
9
10 import static org.testng.Assert.assertNotNull;
11
12 /**
13  * @author EVITALIY
14  * @since 22 Oct 17
15  */
16 public class ConfigurationManagerTest {
17
18     private static final String NON_EXISTENT = "unexistentfile";
19
20     @BeforeMethod
21     public void resetInstance() throws NoSuchFieldException, IllegalAccessException {
22         Field field = ConfigurationManager.class.getDeclaredField("instance");
23         field.setAccessible(true);
24         field.set(null, null);
25     }
26
27     @Test(expectedExceptions = IOException.class,
28             expectedExceptionsMessageRegExp = ".*" + NON_EXISTENT + ".*")
29     public void testGetInstanceSystemProperty() throws Throwable {
30
31         try (ConfigurationSystemPropertyUpdater updater = new ConfigurationSystemPropertyUpdater(NON_EXISTENT)) {
32             ConfigurationManager.getInstance();
33         } catch (RuntimeException e) {
34             Throwable cause = e.getCause();
35             throw cause == null ? e : cause;
36         }
37     }
38
39     @Test()
40     public void testGetInstanceDefault() throws Exception {
41
42         try (ConfigurationSystemPropertyUpdater property = new ConfigurationSystemPropertyUpdater()) {
43             ConfigurationManager manager = ConfigurationManager.getInstance();
44             assertNotNull(manager.getUsername());
45         }
46     }
47
48
49     private static class ConfigurationSystemPropertyUpdater implements Closeable {
50
51         private final String oldValue;
52
53         private ConfigurationSystemPropertyUpdater(String value) {
54             this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
55             System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, value);
56         }
57
58         private ConfigurationSystemPropertyUpdater() {
59             this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
60             System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
61         }
62
63         @Override
64         public void close() throws IOException {
65
66             if (oldValue == null) {
67                 System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
68             } else {
69                 System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, oldValue);
70             }
71         }
72     }
73 }