2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.oam.util;
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;
36 import static org.mockito.Mockito.mock;
38 public class ConfigurationHelperTest {
39 private ConfigurationHelper configurationHelper;
41 private Configuration mockConf;
42 private Configuration origConf;
45 public void setUp() throws Exception {
46 mockConf = mock(Configuration.class);
48 configurationHelper = new ConfigurationHelper(null);
50 // to avoid operation on logger fail, mock up the logger
51 EELFLogger fakeLogger = mock(EELFLogger.class);
52 Whitebox.setInternalState(configurationHelper, "logger", fakeLogger);
55 private void setMockConf() {
56 origConf = Whitebox.getInternalState(configurationHelper, "configuration");
57 Whitebox.setInternalState(configurationHelper, "configuration", mockConf);
60 private void resetOrigConfig() {
61 Whitebox.setInternalState(configurationHelper, "configuration", origConf);
66 public void getAppcName() throws Exception {
67 // test with existing properties file
68 Assert.assertEquals("Should return value(APPC).", "APPC", configurationHelper.getAppcName());
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());
82 public void isMetricEnabled() throws Exception {
86 Mockito.doReturn(false).when(mockConf).getBooleanProperty(
87 ConfigurationHelper.PROP_KEY_METRIC_STATE, false);
88 Assert.assertFalse("Should return false", configurationHelper.isMetricEnabled());
90 Mockito.doReturn(true).when(mockConf).getBooleanProperty(
91 ConfigurationHelper.PROP_KEY_METRIC_STATE, false);
92 Assert.assertTrue("Should return true", configurationHelper.isMetricEnabled());
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.*"));
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.*"));
110 public void testReadPropertyWithMockup() throws Exception {
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]);