Second part of onap rename
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / openecomp / appc / oam / util / ConfigurationHelperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.oam.util;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import org.apache.commons.lang3.ArrayUtils;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.appc.configuration.Configuration;
34 import org.powermock.reflect.Whitebox;
35
36 import static org.mockito.Mockito.mock;
37
38 public class ConfigurationHelperTest {
39     private ConfigurationHelper configurationHelper;
40
41     private Configuration mockConf;
42     private Configuration origConf;
43
44     @Before
45     public void setUp() throws Exception {
46         mockConf = mock(Configuration.class);
47
48         configurationHelper = new ConfigurationHelper(null);
49
50         // to avoid operation on logger fail, mock up the logger
51         EELFLogger fakeLogger = mock(EELFLogger.class);
52         Whitebox.setInternalState(configurationHelper, "logger", fakeLogger);
53     }
54
55     private void setMockConf() {
56         origConf = Whitebox.getInternalState(configurationHelper, "configuration");
57         Whitebox.setInternalState(configurationHelper, "configuration", mockConf);
58     }
59
60     private void resetOrigConfig() {
61         Whitebox.setInternalState(configurationHelper, "configuration", origConf);
62         origConf = null;
63     }
64
65     @Test
66     public void getAppcName() throws Exception {
67         // test with existing properties file
68         Assert.assertEquals("Should return value(APPC).", "APPC", configurationHelper.getAppcName());
69
70         // test with mockup
71         setMockConf();
72
73         String propValue = "testing";
74         Mockito.doReturn(propValue).when(mockConf).getProperty(ConfigurationHelper.PROP_KEY_APPC_NAME);
75         Assert.assertEquals(String.format("Should return value(%s).", propValue), propValue,
76                 configurationHelper.getAppcName());
77
78         resetOrigConfig();
79     }
80
81     @Test
82     public void isMetricEnabled() throws Exception {
83         // test with mockup
84         setMockConf();
85
86         Mockito.doReturn(false).when(mockConf).getBooleanProperty(
87                 ConfigurationHelper.PROP_KEY_METRIC_STATE, false);
88         Assert.assertFalse("Should return false", configurationHelper.isMetricEnabled());
89
90         Mockito.doReturn(true).when(mockConf).getBooleanProperty(
91                 ConfigurationHelper.PROP_KEY_METRIC_STATE, false);
92         Assert.assertTrue("Should return true", configurationHelper.isMetricEnabled());
93     }
94
95     @Test
96     public void testReadPropertyNotStop() throws Exception {
97         String[] str = configurationHelper.readProperty("appc.OAM.AppcBundlesToNotStop");
98         Assert.assertTrue(str.length > 0);
99         Assert.assertTrue(str[0].equals(".*appc.oam.*"));
100     }
101
102     @Test
103     public void testReadPropertyStop() throws Exception {
104         String[] str = configurationHelper.readProperty("appc.OAM.AppcBundlesToStop");
105         Assert.assertTrue(str.length > 0);
106         Assert.assertTrue(str[0].equals(".*appc.*"));
107     }
108
109     @Test
110     public void testReadPropertyWithMockup() throws Exception {
111         setMockConf();
112
113         String propKey = "testing";
114         // Property does not exist
115         Mockito.doReturn(null).when(mockConf).getProperty(propKey);
116         String[] propResult = configurationHelper.readProperty(propKey);
117         Assert.assertArrayEquals("PropertyResult should be empty string array",
118                 ArrayUtils.EMPTY_STRING_ARRAY, propResult);
119         // Property has one entry
120         String propValue = "1234";
121         Mockito.doReturn(propValue).when(mockConf).getProperty(propKey);
122         propResult = configurationHelper.readProperty(propKey);
123         Assert.assertTrue("PropertyResult should have only one element", propResult.length == 1);
124         Assert.assertEquals("PropertyResult should martch propertyValue", propValue, propResult[0]);
125
126         resetOrigConfig();
127     }
128 }