diff options
author | 2018-12-19 14:15:04 +0000 | |
---|---|---|
committer | 2018-12-19 17:46:34 +0000 | |
commit | 8b1be00c0642f1f167cd3d136aa8215e40deb142 (patch) | |
tree | b7a21c499f98d98a071cfe3d6eda8014b6fd57d4 /appc-oam/appc-oam-bundle/src/test | |
parent | 3eb1c34bba8e1073467ac772adfb61d31fb25862 (diff) |
Fix for test coverage in oam.utils package
Removed 'PrepareForTest' annotations which were preventing test
coverage of classes in appc.oam.utils package.
Added unit tests for untested code.
Increased line coverage of the package from 49% to 83%.
Issue-ID: APPC-1287
Change-Id: I893de5281f8c46e92269be895ef304bbf87c88b9
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
Diffstat (limited to 'appc-oam/appc-oam-bundle/src/test')
3 files changed, 144 insertions, 15 deletions
diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleFilterTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleFilterTest.java new file mode 100644 index 000000000..f987ececf --- /dev/null +++ b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleFilterTest.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Ericsson + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.oam.util; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.mockito.Mockito; +import org.osgi.framework.Bundle; + +public class BundleFilterTest { + + @Test + public void testBundleFilter() { + Bundle mockBundle1 = Mockito.mock(Bundle.class); + Bundle mockBundle2 = Mockito.mock(Bundle.class); + Mockito.doReturn("Bundle-regex1").when(mockBundle1).getSymbolicName(); + Mockito.doReturn("Bundle-regex2").when(mockBundle2).getSymbolicName(); + Bundle[] bundles = {mockBundle1, mockBundle2}; + String[] stopRegex = {"Bundle-regex1"}; + String[] exceptRegex = {"NOT_MATCHING_REGEX"}; + BundleFilter mockBundleFilter = Mockito.spy(new BundleFilter(stopRegex, exceptRegex, bundles)); + assertEquals(1, mockBundleFilter.getBundlesToStop().size()); + assertEquals(1, mockBundleFilter.getBundlesToNotStop().size()); + } + +} diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleHelperTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleHelperTest.java index 4d6b2f8ec..24df0440e 100644 --- a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleHelperTest.java +++ b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleHelperTest.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2018 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,16 +25,23 @@ package org.onap.appc.oam.util; +import static org.hamcrest.CoreMatchers.isA; + import com.att.eelf.configuration.EELFLogger; +import com.google.common.collect.ImmutableMap; import org.apache.commons.lang.ArrayUtils; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.appc.configuration.Configuration; import org.onap.appc.exceptions.APPCException; import org.onap.appc.oam.AppcOam; +import org.onap.appc.oam.processor.BaseCommon; +import org.onap.appc.oam.util.BundleHelper.BundleTask; import org.onap.appc.statemachine.impl.readers.AppcOamStates; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -47,20 +56,28 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; - +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.spy; -@SuppressWarnings("ResultOfMethodCallIgnored") @RunWith(PowerMockRunner.class) -@PrepareForTest({BundleHelper.class, FrameworkUtil.class}) +@PrepareForTest(FrameworkUtil.class) public class BundleHelperTest { private BundleHelper bundleHelper; private AsyncTaskHelper mockTaskHelper = mock(AsyncTaskHelper.class); + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + @Before public void setUp() throws Exception { bundleHelper = spy(new BundleHelper(null, null, null)); @@ -72,7 +89,7 @@ public class BundleHelperTest { @Test public void testBundleOperations() throws Exception { - // spy mocked bundle for calls to method statr or stop. + // spy mocked bundle for calls to method start or stop. // Note: the time of method calls are accumulated in this test method. Bundle mockBundle = spy(Mockito.mock(Bundle.class)); Map<String, Bundle> mapFromGetAppcLcmBundles = new HashMap<>(); @@ -172,8 +189,75 @@ public class BundleHelperTest { Mockito.doReturn(propValue3).when(fakeConf).getProperty("4321"); propResult = bundleHelper.readPropsFromPropListName(propKey); Assert.assertTrue("PropertyResult should have two elements", propResult.length == 2); - List propResultList = Arrays.asList(propResult); + List<String> propResultList = Arrays.asList(propResult); Assert.assertTrue("PropertyResult should have propertyValue2", propResultList.contains(propValue2)); Assert.assertTrue("PropertyResult should have propertyValue2", propResultList.contains(propValue3)); } + + @Test + public void testIsTaskAllDone() throws InterruptedException, ExecutionException { + assertTrue(bundleHelper.isAllTaskDone(getMapForTests(true))); + } + + @Test + public void testIsTaskAllDoneNotDone() throws InterruptedException, ExecutionException { + assertFalse(bundleHelper.isAllTaskDone(getMapForTests(false))); + } + + @Test + public void testCancelUnfinished() throws InterruptedException, ExecutionException { + Map<String, Future<?>> map = getMapForTests(false); + bundleHelper.cancelUnfinished(map); + Mockito.verify(map.get("TEST_KEY"), Mockito.times(1)).cancel(true); + } + + @Test + public void testGetFailedMetrics() throws InterruptedException, ExecutionException { + Map<String, Future<?>> map = getMapForTests(false); + FutureTask<String> mockFutureTask = (FutureTask<String>) map.get("TEST_KEY"); + Mockito.doReturn(Mockito.mock(BundleHelper.BundleTask.class)).when(mockFutureTask).get(); + assertEquals(0, bundleHelper.getFailedMetrics(map)); + } + + @Test + public void testGetFailedMetricsExceptionFlow() throws InterruptedException, ExecutionException { + Map<String, Future<?>> map = getMapForTests(false); + FutureTask<String> mockFutureTask = (FutureTask<String>) map.get("TEST_KEY"); + Mockito.doThrow(new ExecutionException("TestExecutionException", new Throwable())).when(mockFutureTask).get(); + expectedEx.expect(RuntimeException.class); + expectedEx.expectCause(isA(ExecutionException.class)); + bundleHelper.getFailedMetrics(map); + } + + @Test + public void testGetAppcLcmBundles() { + Mockito.doReturn(null).when(bundleHelper).readPropsFromPropListName(Mockito.anyString()); + mockStatic(FrameworkUtil.class); + Bundle myBundle = mock(Bundle.class); + PowerMockito.when(FrameworkUtil.getBundle(any())).thenReturn(myBundle); + BundleFilter mockBundleFilter = Mockito.mock(BundleFilter.class); + Mockito.doReturn(mockBundleFilter).when(bundleHelper).getBundleFilter(null, null, null); + assertTrue(bundleHelper.getAppcLcmBundles().isEmpty()); + } + + @Test + public void testBundleTask() throws Exception { + AppcOam.RPC mockRpc = AppcOam.RPC.maintenance_mode; + Bundle mockBundle = Mockito.mock(Bundle.class); + BaseCommon mockBaseCommon = Mockito.mock(BaseCommon.class); + assertTrue(bundleHelper. new BundleTask(mockRpc, mockBundle, mockBaseCommon) instanceof BundleHelper.BundleTask); + } + + private Map<String, Future<?>> getMapForTests(boolean isDone) throws InterruptedException, ExecutionException { + Callable<String> callable = new Callable<String>() { + @Override + public String call() throws Exception { + return "CALLED"; + } + }; + FutureTask<String> futureTask = Mockito.spy(new FutureTask<String>(callable)); + Mockito.doReturn(isDone).when(futureTask).isDone(); + Map<String, Future<?>> map = ImmutableMap.of("TEST_KEY", futureTask); + return map; + } } diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/StateHelperTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/StateHelperTest.java index 8cbb9bb4c..c2cf8e7a0 100644 --- a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/StateHelperTest.java +++ b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/StateHelperTest.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2018 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +35,6 @@ import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.AppcState; import org.onap.appc.statemachine.impl.readers.AppcOamStates; import org.osgi.framework.Bundle; import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; @@ -44,7 +45,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @RunWith(PowerMockRunner.class) -@PrepareForTest({StateHelper.class}) public class StateHelperTest { private StateHelper stateHelper; @@ -63,7 +63,7 @@ public class StateHelperTest { stateHelper.setState(appcOamStates); Assert.assertEquals("Should have the new value", appcOamStates, Whitebox.getInternalState(stateHelper, "appcOamCurrentState")); - // reest to default value + // reset to default value stateHelper.setState(AppcOamStates.Unknown); } @@ -90,7 +90,7 @@ public class StateHelperTest { @Test public void testGetCurrentOamState() throws Exception { AppcOamStates mockResult = AppcOamStates.Started; - // mock getBundlesState, as we are testin it separately + // mock getBundlesState, as we are testing it separately PowerMockito.doReturn(mockResult).when(stateHelper, "getBundlesState"); Whitebox.setInternalState(stateHelper, "appcOamCurrentState", AppcOamStates.Unknown); @@ -142,23 +142,23 @@ public class StateHelperTest { @Test public void testGetBundlesState() throws Exception { - BundleHelper mockBundlerHelper = mock(BundleHelper.class); - PowerMockito.whenNew(BundleHelper.class).withAnyArguments().thenReturn(mockBundlerHelper); - + BundleHelper mockBundleHelper = mock(BundleHelper.class); + Mockito.doReturn(mockBundleHelper).when(stateHelper).getBundleHelper(Mockito.any(EELFLogger.class), Mockito.any(ConfigurationHelper.class)); + // test null bundle map - Mockito.when(mockBundlerHelper.getAppcLcmBundles()).thenReturn(null); + Mockito.when(mockBundleHelper.getAppcLcmBundles()).thenReturn(null); Assert.assertEquals("Should return unknown state", AppcOamStates.Unknown, stateHelper.getBundlesState()); // tet empty bundle map Map<String, Bundle> bundleMap = new HashMap<>(); - Mockito.when(mockBundlerHelper.getAppcLcmBundles()).thenReturn(bundleMap); + Mockito.when(mockBundleHelper.getAppcLcmBundles()).thenReturn(bundleMap); Assert.assertEquals("Should return unknown state", AppcOamStates.Unknown, stateHelper.getBundlesState()); Bundle mockBundle1 = mock(Bundle.class); Bundle mockBundle2 = mock(Bundle.class); bundleMap.put("1", mockBundle1); bundleMap.put("2", mockBundle2); - Mockito.when(mockBundlerHelper.getAppcLcmBundles()).thenReturn(bundleMap); + Mockito.when(mockBundleHelper.getAppcLcmBundles()).thenReturn(bundleMap); // test bundles have differnt states Mockito.doReturn(Bundle.RESOLVED).when(mockBundle1).getState(); |