From c65b43dacca651f1aea70d3145edb6f2c4daea3a Mon Sep 17 00:00:00 2001 From: Lathishbabu Ganesan Date: Mon, 18 Feb 2019 11:05:01 -0500 Subject: Added test case for DbService util Increased the coverage from 22% to 82% Issue-ID: APPC-1448 Change-Id: I478367d6f5aac4a453b6d711a81c0ee5cefcdd36 Signed-off-by: Lathishbabu Ganesan --- .../encryptiontool/wrapper/TestDbServiceUtil.java | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 appc-config/appc-encryption-tool/provider/src/test/java/org/onap/appc/encryptiontool/wrapper/TestDbServiceUtil.java (limited to 'appc-config/appc-encryption-tool') diff --git a/appc-config/appc-encryption-tool/provider/src/test/java/org/onap/appc/encryptiontool/wrapper/TestDbServiceUtil.java b/appc-config/appc-encryption-tool/provider/src/test/java/org/onap/appc/encryptiontool/wrapper/TestDbServiceUtil.java new file mode 100644 index 000000000..530a57542 --- /dev/null +++ b/appc-config/appc-encryption-tool/provider/src/test/java/org/onap/appc/encryptiontool/wrapper/TestDbServiceUtil.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Ericsson. All rights reserved. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.encryptiontool.wrapper; + +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import javax.sql.rowset.CachedRowSet; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.ccsdk.sli.core.dblib.DBResourceManager; +import org.powermock.reflect.Whitebox; + + +public class TestDbServiceUtil { + + private DBResourceManager jdbcDataSource; + private CachedRowSet cachedRowSet; + private List argList = new ArrayList<>(); + + @Before + public void setUp() { + jdbcDataSource = Mockito.mock(DBResourceManager.class); + cachedRowSet = Mockito.mock(CachedRowSet.class); + } + + @Test + public void testUpdateDB() throws SQLException { + Whitebox.setInternalState(DbServiceUtil.class, "jdbcDataSource", jdbcDataSource); + when(jdbcDataSource.writeData(eq("update tableName set where "), anyObject(), eq(Constants.SCHEMA_SDNCTL))) + .thenReturn(true); + assertTrue(DbServiceUtil.updateDB("tableName", argList, "", "")); + } + + @Test + public void testGetData() throws SQLException { + Whitebox.setInternalState(DbServiceUtil.class, "jdbcDataSource", jdbcDataSource); + when(jdbcDataSource.getData(eq("select from tableName where "), anyObject(), eq(Constants.SCHEMA_SDNCTL))) + .thenReturn(cachedRowSet); + assertSame(cachedRowSet, DbServiceUtil.getData("tableName", argList, Constants.SCHEMA_SDNCTL, "", "")); + } + + @Test + public void testDeleteData() throws SQLException { + Whitebox.setInternalState(DbServiceUtil.class, "jdbcDataSource", jdbcDataSource); + when(jdbcDataSource.writeData(eq("delete from tableName"), anyObject(), eq(Constants.SCHEMA_SDNCTL))) + .thenReturn(true); + assertTrue(DbServiceUtil.deleteData("tableName", argList)); + } + + @Test + public void testInsertDB() throws SQLException { + Whitebox.setInternalState(DbServiceUtil.class, "jdbcDataSource", jdbcDataSource); + when(jdbcDataSource.writeData(eq("INSERT INTO tableName ( ) VALUES ()"), anyObject(), + eq(Constants.SCHEMA_SDNCTL))).thenReturn(true); + assertTrue(DbServiceUtil.insertDB("tableName", argList, "", "")); + } +} -- cgit 1.2.3-korg