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