summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoss Armstrong <joss.armstrong@ericsson.com>2018-12-19 14:15:04 +0000
committerTakamune Cho <takamune.cho@att.com>2018-12-19 17:46:34 +0000
commit8b1be00c0642f1f167cd3d136aa8215e40deb142 (patch)
treeb7a21c499f98d98a071cfe3d6eda8014b6fd57d4
parent3eb1c34bba8e1073467ac772adfb61d31fb25862 (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>
-rw-r--r--appc-oam/appc-oam-bundle/pom.xml1
-rw-r--r--appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/BundleHelper.java8
-rw-r--r--appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/StateHelper.java7
-rw-r--r--appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleFilterTest.java45
-rw-r--r--appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/BundleHelperTest.java94
-rw-r--r--appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/util/StateHelperTest.java20
6 files changed, 157 insertions, 18 deletions
diff --git a/appc-oam/appc-oam-bundle/pom.xml b/appc-oam/appc-oam-bundle/pom.xml
index edd909306..f2c1c6274 100644
--- a/appc-oam/appc-oam-bundle/pom.xml
+++ b/appc-oam/appc-oam-bundle/pom.xml
@@ -282,7 +282,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
-
</dependencies>
<version>1.5.0-SNAPSHOT</version>
diff --git a/appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/BundleHelper.java b/appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/BundleHelper.java
index b862ed3a7..7639c4075 100644
--- a/appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/BundleHelper.java
+++ b/appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/BundleHelper.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.
@@ -166,7 +168,7 @@ public class BundleHelper {
String[] bundlesToStop = readPropsFromPropListName(PROP_BUNDLE_TO_STOP);
String[] regExBundleNotStop = readPropsFromPropListName(PROP_BUNDLES_TO_NOT_STOP);
- BundleFilter bundleList = new BundleFilter(bundlesToStop, regExBundleNotStop, getBundleList());
+ BundleFilter bundleList = getBundleFilter(bundlesToStop, regExBundleNotStop, getBundleList());
logger.info(String.format("(%d) APPC bundles to Stop/Start: %s.", bundleList.getBundlesToStop().size(),
bundleList.getBundlesToStop().toString()));
@@ -220,6 +222,10 @@ public class BundleHelper {
}
}
+ protected BundleFilter getBundleFilter(String[] stopRegexes, String[] exceptRegexes, Bundle[] bundles) {
+ return new BundleFilter(stopRegexes, exceptRegexes, bundles);
+ }
+
/**
* Runnable to execute bundle operations: start or stop
*/
diff --git a/appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/StateHelper.java b/appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/StateHelper.java
index 0d24b5e8a..6ed50dbe6 100644
--- a/appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/StateHelper.java
+++ b/appc-oam/appc-oam-bundle/src/main/java/org/onap/appc/oam/util/StateHelper.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.
@@ -122,7 +124,7 @@ public class StateHelper {
* @return AppcOamStates
*/
public AppcOamStates getBundlesState() {
- BundleHelper bundleHelper = new BundleHelper(logger, configurationHelper, this);
+ BundleHelper bundleHelper = getBundleHelper(logger, configurationHelper);
Map<String, Bundle> lcmBundleMap = bundleHelper.getAppcLcmBundles();
if (lcmBundleMap == null || lcmBundleMap.isEmpty()) {
return AppcOamStates.Unknown;
@@ -140,4 +142,7 @@ public class StateHelper {
return AppcOamStates.getOamStateFromBundleState(currentState);
}
+ protected BundleHelper getBundleHelper(EELFLogger logger, ConfigurationHelper configurationHelper) {
+ return new BundleHelper(logger, configurationHelper, this);
+ }
}
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();