2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.oam.util;
26 import com.att.eelf.configuration.EELFLogger;
27 import org.apache.commons.lang.ArrayUtils;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.onap.appc.configuration.Configuration;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.appc.oam.AppcOam;
36 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
37 import org.osgi.framework.Bundle;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.api.support.membermodification.MemberMatcher;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44 import org.powermock.reflect.Whitebox;
46 import java.util.Arrays;
47 import java.util.HashMap;
48 import java.util.List;
51 import static org.mockito.Matchers.any;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.times;
54 import static org.powermock.api.mockito.PowerMockito.mockStatic;
55 import static org.powermock.api.mockito.PowerMockito.spy;
57 @SuppressWarnings("ResultOfMethodCallIgnored")
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({BundleHelper.class, FrameworkUtil.class})
60 public class BundleHelperTest {
61 private BundleHelper bundleHelper;
62 private AsyncTaskHelper mockTaskHelper = mock(AsyncTaskHelper.class);
65 public void setUp() throws Exception {
66 bundleHelper = spy(new BundleHelper(null, null, null));
68 // to avoid operation on logger fail, mock up the logger
69 EELFLogger mockLogger = mock(EELFLogger.class);
70 Whitebox.setInternalState(bundleHelper, "logger", mockLogger);
74 public void testBundleOperations() throws Exception {
75 // spy mocked bundle for calls to method statr or stop.
76 // Note: the time of method calls are accumulated in this test method.
77 Bundle mockBundle = spy(Mockito.mock(Bundle.class));
78 Map<String, Bundle> mapFromGetAppcLcmBundles = new HashMap<>();
79 mapFromGetAppcLcmBundles.put("BundleString", mockBundle);
81 PowerMockito.doReturn(mapFromGetAppcLcmBundles).when(bundleHelper, MemberMatcher.method(
82 BundleHelper.class, "getAppcLcmBundles")).withNoArguments();
84 StateHelper mockStateHelper = mock(StateHelper.class);
85 Whitebox.setInternalState(bundleHelper, "stateHelper", mockStateHelper);
87 AppcOamStates appcOamStates = AppcOamStates.Stopped;
88 Mockito.doReturn(appcOamStates).when(mockStateHelper).getState();
91 Mockito.doReturn(true).when(mockStateHelper).isSameState(appcOamStates);
92 boolean result = bundleHelper.bundleOperations(AppcOam.RPC.start, new HashMap<>(), mockTaskHelper,null);
93 Assert.assertTrue("Should be completed", result);
94 Mockito.verify(mockTaskHelper, times(1)).submitBaseSubCallable(any());
97 Mockito.doReturn(false).when(mockStateHelper).isSameState(appcOamStates);
98 result = bundleHelper.bundleOperations(AppcOam.RPC.start, new HashMap<>(), mockTaskHelper,null);
99 Assert.assertFalse("Should be abort", result);
100 Mockito.verify(mockTaskHelper, times(1)).submitBaseSubCallable(any());
103 result = bundleHelper.bundleOperations(AppcOam.RPC.stop, new HashMap<>(), mockTaskHelper,null);
104 Assert.assertTrue("Should be completed", result);
105 Mockito.verify(mockTaskHelper, times(2)).submitBaseSubCallable(any());
108 @Test(expected = APPCException.class)
109 public void testBundleOperationsRpcException() throws Exception {
110 bundleHelper.bundleOperations(AppcOam.RPC.maintenance_mode, new HashMap<>(), mockTaskHelper,null);
114 public void testGetBundleList() throws Exception {
115 mockStatic(FrameworkUtil.class);
116 Bundle myBundle = mock(Bundle.class);
117 PowerMockito.when(FrameworkUtil.getBundle(any())).thenReturn(myBundle);
119 // test bundle context is null
120 Mockito.when(myBundle.getBundleContext()).thenReturn(null);
121 Assert.assertTrue("Should return null", bundleHelper.getBundleList() == null);
123 BundleContext myBundleContext = mock(BundleContext.class);
124 Mockito.when(myBundle.getBundleContext()).thenReturn(myBundleContext);
126 // test bundle list is empty
127 Bundle[] bundleArray = {};
128 Mockito.when(myBundleContext.getBundles()).thenReturn(bundleArray);
129 Bundle[] results = bundleHelper.getBundleList();
130 Assert.assertTrue("Should not be null", results != null);
131 Assert.assertTrue("Should not have any element", results.length == 0);
133 // test bundle list has at one bundle
134 bundleArray = new Bundle[] { myBundle };
135 Mockito.when(myBundleContext.getBundles()).thenReturn(bundleArray);
136 results = bundleHelper.getBundleList();
137 Assert.assertTrue("Should not be null", results != null);
138 Assert.assertTrue("Should have one element", results.length == 1);
139 Assert.assertEquals("Should be the mock bundle", myBundle, results[0]);
143 public void testReadPropsFromPropListName() throws Exception {
144 // mock configuarion helper
145 ConfigurationHelper configurationHelper = new ConfigurationHelper(null);
146 EELFLogger fakeLogger = mock(EELFLogger.class);
147 Whitebox.setInternalState(configurationHelper, "logger", fakeLogger);
148 Configuration fakeConf = mock(Configuration.class);
149 Whitebox.setInternalState(configurationHelper, "configuration", fakeConf);
151 Whitebox.setInternalState(bundleHelper, "configurationHelper", configurationHelper);
153 String propKey = "testing";
154 // Property does not exist
155 Mockito.doReturn(null).when(fakeConf).getProperty(propKey);
156 String[] propResult = bundleHelper.readPropsFromPropListName(propKey);
157 Assert.assertArrayEquals("PropertyResult should be empty string array",
158 ArrayUtils.EMPTY_STRING_ARRAY, propResult);
159 // Property has one entry
160 String propValue1 = "1234";
161 String propValue2 = "5678";
162 Mockito.doReturn(propValue1).when(fakeConf).getProperty(propKey);
163 Mockito.doReturn(propValue2).when(fakeConf).getProperty(propValue1);
164 propResult = bundleHelper.readPropsFromPropListName(propKey);
165 Assert.assertTrue("PropertyResult should have only one element", propResult.length == 1);
166 Assert.assertEquals("PropertyResult should martch propertyValue", propValue2, propResult[0]);
167 // Property has two entries
168 propValue1 = "1234\n,4321";
169 String propValue3 = "8765";
170 Mockito.doReturn(propValue1).when(fakeConf).getProperty(propKey);
171 Mockito.doReturn(propValue2).when(fakeConf).getProperty(propValue1);
172 Mockito.doReturn(propValue3).when(fakeConf).getProperty("4321");
173 propResult = bundleHelper.readPropsFromPropListName(propKey);
174 Assert.assertTrue("PropertyResult should have two elements", propResult.length == 2);
175 List propResultList = Arrays.asList(propResult);
176 Assert.assertTrue("PropertyResult should have propertyValue2", propResultList.contains(propValue2));
177 Assert.assertTrue("PropertyResult should have propertyValue2", propResultList.contains(propValue3));