Add coverage for executionqueue helper package 78/77978/5
authorJoss Armstrong <joss.armstrong@ericsson.com>
Wed, 6 Feb 2019 18:14:04 +0000 (18:14 +0000)
committerPatrick Brady <patrick.brady@att.com>
Wed, 6 Feb 2019 22:48:13 +0000 (22:48 +0000)
New test file giving 100% coverage

Issue-ID: APPC-1396
Change-Id: I597a778e4c128f745335ad243c0fa8cc8b0b1e45
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/onap/appc/executionqueue/helper/Util.java
appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/test/java/org/onap/appc/executionqueue/helper/UtilTest.java [new file with mode: 0644]

index 878794b..b890014 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2019 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,7 +27,8 @@ package org.onap.appc.executionqueue.helper;
 
 import org.onap.appc.configuration.Configuration;
 import org.onap.appc.configuration.ConfigurationFactory;
-
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -36,6 +39,7 @@ public class Util {
     private int default_threadpool_size = 10;
     private String queue_size_key = "appc.dispatcher.executionqueue.backlog.size";
     private String threadpool_size_key = "appc.dispatcher.executionqueue.threadpool.size";
+    private EELFLogger logger = EELFManager.getInstance().getLogger(Util.class);
 
     private Configuration configuration;
 
@@ -54,7 +58,7 @@ public class Util {
         try {
             size = Integer.parseInt(sizeStr);
         } catch (NumberFormatException e) {
-
+            logger.error("Error parsing dispatcher execution queue backlog size", e);
         }
 
         return size;
@@ -67,7 +71,7 @@ public class Util {
         try {
             size = Integer.parseInt(sizeStr);
         } catch (NumberFormatException e) {
-
+            logger.error("Error parsing dispatcher execution queue threadpool size", e);
         }
 
         return size;
diff --git a/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/test/java/org/onap/appc/executionqueue/helper/UtilTest.java b/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/test/java/org/onap/appc/executionqueue/helper/UtilTest.java
new file mode 100644 (file)
index 0000000..fdd5e34
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2019 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.executionqueue.helper;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import java.util.concurrent.ThreadFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.onap.appc.configuration.Configuration;
+import org.onap.appc.configuration.ConfigurationFactory;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+import com.att.eelf.configuration.EELFLogger;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ConfigurationFactory.class)
+public class UtilTest {
+
+    private Configuration configuration;
+
+    @Before
+    public void setup() {
+        PowerMockito.mockStatic(ConfigurationFactory.class);
+         configuration = Mockito.mock(Configuration.class);
+        PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(configuration);
+    }
+
+    @Test
+    public void testInit() {
+        Util util = new Util();
+        util.init();
+        assertEquals(configuration, Whitebox.getInternalState(util, "configuration"));
+    }
+
+    @Test
+    public void testGetExecutionQueueSize() {
+        Mockito.when(configuration.getProperty("appc.dispatcher.executionqueue.backlog.size", String.valueOf(10))).thenReturn("1");
+        Util util = new Util();
+        Whitebox.setInternalState(util, "configuration", configuration);
+        assertEquals(1, util.getExecutionQueueSize());
+    }
+
+    @Test
+    public void testGetExecutionQueueSizeExceptionFlow() {
+        Mockito.when(configuration.getProperty("appc.dispatcher.executionqueue.backlog.size", String.valueOf(10))).thenReturn("Not A Number");
+        Util util = new Util();
+        Whitebox.setInternalState(util, "configuration", configuration);
+        EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
+        Whitebox.setInternalState(util, "logger", mockLogger);
+        util.getExecutionQueueSize();
+        Mockito.verify(mockLogger).error(Mockito.contains("Error parsing dispatcher execution queue backlog size"),
+                Mockito.any(NumberFormatException.class));
+    }
+
+    @Test
+    public void testGetThreadPoolSize() {
+        Mockito.when(configuration.getProperty("appc.dispatcher.executionqueue.threadpool.size", String.valueOf(10))).thenReturn("1");
+        Util util = new Util();
+        Whitebox.setInternalState(util, "configuration", configuration);
+        assertEquals(1, util.getThreadPoolSize());
+    }
+
+    @Test
+    public void testGetThreadPoolSizeExceptionFlow() {
+        Mockito.when(configuration.getProperty("appc.dispatcher.executionqueue.threadpool.size", String.valueOf(10))).thenReturn("Not A Number");
+        Util util = new Util();
+        Whitebox.setInternalState(util, "configuration", configuration);
+        EELFLogger mockLogger = Mockito.mock(EELFLogger.class);
+        Whitebox.setInternalState(util, "logger", mockLogger);
+        util.getThreadPoolSize();
+        Mockito.verify(mockLogger).error(Mockito.contains("Error parsing dispatcher execution queue threadpool size"),
+                Mockito.any(NumberFormatException.class));
+    }
+
+    @Test
+    public void testGetThreadFactory() {
+        Util util = new Util();
+        Whitebox.setInternalState(util, "configuration", configuration);
+        assertTrue(util.getThreadFactory(true, "prefix") instanceof ThreadFactory);
+        assertTrue(util.getThreadFactory(true, "prefix").newThread(new Thread()) instanceof Thread);
+    }
+}