Added oparent to sdc main
[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 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.nosqldb.util;
18
19 import static org.hamcrest.core.StringContains.containsString;
20 import static org.junit.Assert.assertNotNull;
21
22 import java.io.Closeable;
23 import java.io.IOException;
24 import java.lang.reflect.Field;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29
30 /**
31  * @author EVITALIY
32  * @since 22 Oct 17
33  */
34 public class ConfigurationManagerTest {
35
36     @Rule
37     public ExpectedException expectedException = ExpectedException.none();
38
39     private static final String NON_EXISTENT = "unexistentfile";
40
41     @Before
42     public void resetInstance() throws NoSuchFieldException, IllegalAccessException {
43         Field field = ConfigurationManager.class.getDeclaredField("instance");
44         field.setAccessible(true);
45         field.set(null, null);
46     }
47
48     @Test
49     public void testGetInstanceSystemProperty() throws Throwable {
50
51         expectedException.expect(IOException.class);
52         expectedException.expectMessage(containsString(NON_EXISTENT));
53
54         try (ConfigurationSystemPropertyUpdater updater = new ConfigurationSystemPropertyUpdater(NON_EXISTENT)) {
55             ConfigurationManager.getInstance();
56         } catch (RuntimeException e) {
57             Throwable cause = e.getCause();
58             throw cause == null ? e : cause;
59         }
60     }
61
62     @Test()
63     public void testGetInstanceDefault() {
64
65         try (ConfigurationSystemPropertyUpdater property = new ConfigurationSystemPropertyUpdater()) {
66             ConfigurationManager manager = ConfigurationManager.getInstance();
67             assertNotNull(manager.getUsername());
68         }
69     }
70
71
72     private static class ConfigurationSystemPropertyUpdater implements Closeable {
73
74         private final String oldValue;
75
76         private ConfigurationSystemPropertyUpdater(String value) {
77             this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
78             System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, value);
79         }
80
81         private ConfigurationSystemPropertyUpdater() {
82             this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
83             System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
84         }
85
86         @Override
87         public void close() {
88
89             if (oldValue == null) {
90                 System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
91             } else {
92                 System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, oldValue);
93             }
94         }
95     }
96 }