summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java
blob: 7a6343d15ce74648e9a0e6dbfd475c01f1354dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.openecomp.core.nosqldb.util;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Field;

import static org.testng.Assert.assertNotNull;

/**
 * @author EVITALIY
 * @since 22 Oct 17
 */
public class ConfigurationManagerTest {

    private static final String NON_EXISTENT = "unexistentfile";

    @BeforeMethod
    public void resetInstance() throws NoSuchFieldException, IllegalAccessException {
        Field field = ConfigurationManager.class.getDeclaredField("instance");
        field.setAccessible(true);
        field.set(null, null);
    }

    @Test(expectedExceptions = IOException.class,
            expectedExceptionsMessageRegExp = ".*" + NON_EXISTENT + ".*")
    public void testGetInstanceSystemProperty() throws Throwable {

        try (ConfigurationSystemPropertyUpdater updater = new ConfigurationSystemPropertyUpdater(NON_EXISTENT)) {
            ConfigurationManager.getInstance();
        } catch (RuntimeException e) {
            Throwable cause = e.getCause();
            throw cause == null ? e : cause;
        }
    }

    @Test()
    public void testGetInstanceDefault() throws Exception {

        try (ConfigurationSystemPropertyUpdater property = new ConfigurationSystemPropertyUpdater()) {
            ConfigurationManager manager = ConfigurationManager.getInstance();
            assertNotNull(manager.getUsername());
        }
    }


    private static class ConfigurationSystemPropertyUpdater implements Closeable {

        private final String oldValue;

        private ConfigurationSystemPropertyUpdater(String value) {
            this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
            System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, value);
        }

        private ConfigurationSystemPropertyUpdater() {
            this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
            System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
        }

        @Override
        public void close() throws IOException {

            if (oldValue == null) {
                System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
            } else {
                System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, oldValue);
            }
        }
    }
}