diff options
author | 2018-03-13 12:36:39 -0400 | |
---|---|---|
committer | 2018-03-13 12:50:10 -0400 | |
commit | 8b514a087e96594dc982a95a866d654faccd6e0a (patch) | |
tree | 1d5e6bd285783699ea2e50e0f5526224856032d8 /ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system | |
parent | 5322b5e5d0f49f1ce7db8ebe28e7f28328180c9a (diff) |
Added Junits
Issue-ID: PORTAL-136
Includes JUNITS
Change-Id: Ie278f9d362e6c9fba0ecdb4a493d8f88a2768fc0
Signed-off-by: Kotta, Shireesha (sk434m) <sk434m@att.com>
Diffstat (limited to 'ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system')
5 files changed, 928 insertions, 0 deletions
diff --git a/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/DbUtilsTest.java b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/DbUtilsTest.java new file mode 100644 index 00000000..664e27c7 --- /dev/null +++ b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/DbUtilsTest.java @@ -0,0 +1,352 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software 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. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * 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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalsdk.analytics.system; +import static org.junit.Assert.*; + +import java.sql.CallableStatement; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +import javax.sql.DataSource; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalsdk.analytics.error.RaptorException; +import org.onap.portalsdk.analytics.error.ReportSQLException; +import org.onap.portalsdk.core.web.support.AppUtils; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({AppUtils.class, Globals.class}) +public class DbUtilsTest { + + @Mock + DataSource dataSource; + + @InjectMocks + DbUtils dbUtils = new DbUtils(); + + @Mock + Connection connection; + @Mock + CallableStatement stmt; + + @Mock + Statement statement; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @SuppressWarnings("static-access") + @Test + public void getConnectionTest() throws Exception + { + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + dbUtils.getConnection(); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test + public void getConnectionExceptionTest() throws Exception + { + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenThrow(SQLException.class); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + assertNull(dbUtils.getConnection()); + } + + @SuppressWarnings("static-access") + @Test + public void clearConnectionTest() throws Exception + { + PowerMockito.mockStatic(Globals.class); + IDbUtils iDbUtils= PowerMockito.mock(IDbUtils.class); + Mockito.when(Globals.getDbUtils()).thenReturn(iDbUtils); + Mockito.doNothing().when(iDbUtils).clearConnection(connection); + Globals.getDbUtils().clearConnection(connection); + Mockito.when(connection.isClosed()).thenReturn(false); + dbUtils.clearConnection(connection); + } + + @SuppressWarnings("static-access") + @Test + public void clearConnection1Test() throws Exception + { + dbUtils.clearConnection(null); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = Exception.class) + public void clearConnectionExceptionTest() throws Exception + { + PowerMockito.mockStatic(Globals.class); + IDbUtils iDbUtils= PowerMockito.mock(IDbUtils.class); + Mockito.when(Globals.getDbUtils()).thenThrow(Exception.class); + Mockito.doNothing().when(iDbUtils).clearConnection(connection); + Globals.getDbUtils().clearConnection(connection); + Mockito.when(connection.isClosed()).thenReturn(false); + dbUtils.clearConnection(connection); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = SQLException.class) + public void clearConnectionException2Test() throws Exception + { + PowerMockito.mockStatic(Globals.class); + IDbUtils iDbUtils= PowerMockito.mock(IDbUtils.class); + Mockito.when(Globals.getDbUtils()).thenThrow(SQLException.class); + Mockito.doNothing().when(iDbUtils).clearConnection(connection); + Globals.getDbUtils().clearConnection(connection); + Mockito.when(connection.isClosed()).thenReturn(false); + dbUtils.clearConnection(connection); + } + @SuppressWarnings("static-access") + @Test + public void startTransactionTest() throws Exception + { + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + Mockito.doNothing().when(connection).setAutoCommit(false); + dbUtils.startTransaction(); + } + + @SuppressWarnings("static-access") + @Test(expected = ReportSQLException.class) + public void startTransactionExceptionTest() throws Exception + { + Mockito.doNothing().when(connection).setAutoCommit(false); + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenThrow(SQLException.class); + Mockito.when(dataSource.getConnection()).thenReturn(null); + assertNull(dbUtils.startTransaction()); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = ReportSQLException.class) + public void startTransactionException1Test() throws Exception + { + Mockito.doNothing().when(connection).setAutoCommit(false); + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenThrow(Exception.class); + Mockito.when(dataSource.getConnection()).thenReturn(null); + assertNull(dbUtils.startTransaction()); + } + + @SuppressWarnings("static-access") + @Test + public void commitTransactionTest() throws Exception + { + Mockito.doNothing().when(connection).commit(); + dbUtils.commitTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test(expected = RaptorException.class) + public void commitTransactionExceptionTest() throws Exception { + Mockito.doThrow(SQLException.class).when(connection).commit(); + dbUtils.commitTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test(expected = Exception.class) + public void commitTransactionException2Test() throws Exception { + Mockito.doThrow(Exception.class).when(connection).commit(); + dbUtils.commitTransaction(connection); + } + +// @SuppressWarnings("static-access") +// @Test +// public void rollbackTransactionTest() throws Exception +// { +// Mockito.doNothing().when(connection).rollback(); +// dbUtils.rollbackTransaction(connection); +// } + + @SuppressWarnings("static-access") + @Test(expected = RaptorException.class) + public void rollbackTransactionExceptionTest() throws Exception { + Mockito.doThrow(SQLException.class).when(connection).rollback(); + dbUtils.rollbackTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test(expected = Exception.class) + public void rollbackTransactionException2Test() throws Exception { + Mockito.doThrow(Exception.class).when(connection).rollback(); + dbUtils.rollbackTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test + public void executeCallTest() throws Exception + { + Mockito.when(connection.isClosed()).thenReturn(true); + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + Mockito.when(connection.prepareCall(Matchers.anyString())).thenReturn(stmt); + Mockito.when(stmt.getString(1)).thenReturn("test"); + dbUtils.executeCall(connection ,"test", false); + } + + @SuppressWarnings("static-access") + @Test + public void executeCall1Test() throws Exception + { + Mockito.when(connection.isClosed()).thenReturn(true); + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + Mockito.when(connection.prepareCall(Matchers.anyString())).thenReturn(stmt); + Mockito.when(stmt.getString(1)).thenReturn("test"); + dbUtils.executeCall(connection ,"test", true); + } + + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = RaptorException.class) + public void executeCall2Test() throws Exception + { + Mockito.when(connection.isClosed()).thenReturn(true); + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + Mockito.when(connection.prepareCall(Matchers.anyString())).thenThrow(SQLException.class); + Mockito.when(stmt.getString(1)).thenReturn("test"); + dbUtils.executeCall(connection ,"test", true); + } + + @SuppressWarnings("static-access") + @Test + public void executeCall3Test() throws Exception + { + PowerMockito.mockStatic(AppUtils.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + Mockito.when(connection.isClosed()).thenReturn(true); + Mockito.when(connection.prepareCall(Matchers.anyString())).thenReturn(stmt); + Mockito.when(stmt.getString(1)).thenReturn("test"); + dbUtils.executeCall("test", true); + } + @SuppressWarnings("static-access") + @Test + public void executeUpdateTest() throws Exception + { + Mockito.when(connection.createStatement()).thenReturn(statement); + Mockito.when(statement.executeUpdate(Matchers.anyString())).thenReturn(1); + assertEquals(dbUtils.executeUpdate(connection, "test"),1); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = RaptorException.class ) + public void executeUpdate1Test() throws Exception + { + Mockito.when(connection.createStatement()).thenReturn(statement); + Mockito.when(statement.executeUpdate(Matchers.anyString())).thenThrow(SQLException.class); + dbUtils.executeUpdate(connection, "test"); + } + @SuppressWarnings("static-access") + @Test + public void executeUpdate2Test() throws Exception + { + PowerMockito.mockStatic(AppUtils.class); + PowerMockito.mockStatic(Globals.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + Mockito.when(connection.isClosed()).thenReturn(true); + Mockito.when(connection.prepareCall(Matchers.anyString())).thenReturn(stmt); + Mockito.when(connection.createStatement()).thenReturn(statement); + Mockito.when(statement.executeUpdate(Matchers.anyString())).thenReturn(1); + Mockito.when(stmt.getString(1)).thenReturn("test"); + Mockito.when(Globals.getDBType()).thenReturn("oracle"); + Mockito.doNothing().when(connection).commit(); + dbUtils.executeUpdate("test"); + } + + @SuppressWarnings("static-access") + @Test + public void executeUpdate3Test() throws Exception + { + PowerMockito.mockStatic(AppUtils.class); + PowerMockito.mockStatic(Globals.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + Mockito.when(connection.isClosed()).thenReturn(true); + Mockito.when(connection.prepareCall(Matchers.anyString())).thenReturn(stmt); + Mockito.when(connection.createStatement()).thenReturn(statement); + Mockito.when(statement.executeUpdate(Matchers.anyString())).thenReturn(1); + Mockito.when(stmt.getString(1)).thenReturn("test"); + Mockito.when(Globals.getDBType()).thenReturn("oracle1"); + Mockito.doNothing().when(connection).commit(); + dbUtils.executeUpdate("test"); + } + + + @SuppressWarnings("static-access") + @Test(expected = RaptorException.class) + public void executeUpdate4Test() throws Exception + { + PowerMockito.mockStatic(AppUtils.class); + PowerMockito.mockStatic(Globals.class); + Mockito.when(AppUtils.getDatasource()).thenReturn(dataSource); + Mockito.when(dataSource.getConnection()).thenReturn(connection); + Mockito.when(connection.isClosed()).thenReturn(true); + Mockito.when(connection.prepareCall(Matchers.anyString())).thenReturn(stmt); + Mockito.when(connection.createStatement()).thenReturn(statement); + Mockito.when(statement.executeUpdate(Matchers.anyString())).thenReturn(1); + Mockito.when(stmt.getString(1)).thenReturn("test"); + Mockito.when(Globals.getDBType()).thenReturn("oracle"); + Mockito.doThrow(SQLException.class).when(connection).commit(); + dbUtils.executeUpdate("test"); + } + +} diff --git a/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/RemDbUtilsTest.java b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/RemDbUtilsTest.java new file mode 100644 index 00000000..5bab7371 --- /dev/null +++ b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/RemDbUtilsTest.java @@ -0,0 +1,258 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software 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. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * 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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalsdk.analytics.system; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.onap.portalsdk.analytics.error.RaptorException; +import org.onap.portalsdk.analytics.error.RaptorRuntimeException; +import org.onap.portalsdk.analytics.error.ReportSQLException; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ Globals.class, Runtime.class }) +public class RemDbUtilsTest { + + @InjectMocks + RemDbUtils remDbUtils = new RemDbUtils(); + Connection connection = PowerMockito.mock(Connection.class); + + NullPointerException nullPointerException = new NullPointerException(); + + @SuppressWarnings("static-access") + @Test + public void getConnectionTest() throws Exception { + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.when(utils.getRemoteConnection("test")).thenReturn(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.getConnection("test"); + } + + @SuppressWarnings("static-access") + @Test + public void startTransactionTest() throws Exception { + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.when(utils.getRemoteConnection("test")).thenReturn(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.startTransaction("test"); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = RaptorRuntimeException.class) + public void startTransactionExceptionTest() throws Exception { + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.when(utils.getRemoteConnection("test")).thenThrow(SQLException.class); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.startTransaction("test"); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = RaptorException.class) + public void startTransactionException2Test() throws Exception { + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.when(utils.getRemoteConnection("test")).thenThrow(Exception.class); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.startTransaction("test"); + } + + @SuppressWarnings("static-access") + @Test + public void clearConnectionTest() throws Exception { + Mockito.when(connection.isClosed()).thenReturn(false); + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.doNothing().when(utils).clearConnection(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.clearConnection(connection); + } + + @SuppressWarnings("static-access") + @Test + public void clearConnection1Test() throws Exception { + Mockito.when(connection.isClosed()).thenReturn(true); + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.doNothing().when(utils).clearConnection(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.clearConnection(connection); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = ReportSQLException.class) + public void clearConnectionExceptionTest() throws Exception { + Mockito.when(connection.isClosed()).thenThrow(ReportSQLException.class); + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.doNothing().when(utils).clearConnection(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.clearConnection(connection); + } + + @SuppressWarnings("static-access") + @Test + public void commitTransactionTest() throws Exception { + Mockito.doNothing().when(connection).commit(); + remDbUtils.commitTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test(expected = RaptorException.class) + public void commitTransactionExceptionTest() throws Exception { + Mockito.doThrow(SQLException.class).when(connection).commit(); + remDbUtils.commitTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test(expected = Exception.class) + public void commitTransactionException2Test() throws Exception { + Mockito.doThrow(Exception.class).when(connection).commit(); + remDbUtils.commitTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test + public void rollbackTransactionTest() throws Exception { + Mockito.doNothing().when(connection).rollback(); + Mockito.when(connection.isClosed()).thenReturn(false); + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.doNothing().when(utils).clearConnection(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.rollbackTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test(expected = RaptorException.class) + public void rollbackTransactionExceptionTest() throws Exception { + Mockito.doThrow(Exception.class).when(connection).rollback(); + Mockito.when(connection.isClosed()).thenReturn(false); + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.doNothing().when(utils).clearConnection(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.rollbackTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test(expected = Exception.class) + public void rollbackTransactionException2Test() throws Exception { + Mockito.doThrow(SQLException.class).when(connection).rollback(); + Mockito.when(connection.isClosed()).thenReturn(false); + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.doNothing().when(utils).clearConnection(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + remDbUtils.rollbackTransaction(connection); + } + + @SuppressWarnings("static-access") + @Test + public void executeQueryTest() throws Exception { + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.when(utils.getRemoteConnection("test")).thenReturn(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + Statement stmt = PowerMockito.mock(Statement.class); + Mockito.when(connection.createStatement()).thenReturn(stmt); + ResultSet rs = PowerMockito.mock(ResultSet.class); + Mockito.when(stmt.executeQuery(Matchers.anyString())).thenReturn(rs); + + Mockito.when(connection.isClosed()).thenReturn(false); + PowerMockito.mockStatic(Globals.class); + Mockito.doNothing().when(utils).clearConnection(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + + ResultSetMetaData rsmd = PowerMockito.mock(ResultSetMetaData.class); + Mockito.when(rs.getMetaData()).thenReturn(rsmd); + PowerMockito.mockStatic(Runtime.class); + Runtime runtime = PowerMockito.mock(Runtime.class); + Mockito.when(Runtime.getRuntime()).thenReturn(runtime); + Mockito.when(rsmd.getColumnCount()).thenReturn(2); + Mockito.when(rsmd.getColumnLabel(Matchers.anyInt())).thenReturn("test"); + + remDbUtils.executeQuery("test", "test"); + } + + @SuppressWarnings({ "static-access", "unchecked" }) + @Test(expected = ReportSQLException.class) + public void executeQueryExceptionTest() throws Exception { + PowerMockito.mockStatic(Globals.class); + RDbUtils utils = PowerMockito.mock(RDbUtils.class); + Mockito.when(utils.getRemoteConnection("test")).thenReturn(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + Statement stmt = PowerMockito.mock(Statement.class); + Mockito.when(connection.createStatement()).thenThrow(SQLException.class); + ResultSet rs = PowerMockito.mock(ResultSet.class); + Mockito.when(stmt.executeQuery(Matchers.anyString())).thenReturn(rs); + + Mockito.when(connection.isClosed()).thenReturn(false); + PowerMockito.mockStatic(Globals.class); + Mockito.doNothing().when(utils).clearConnection(connection); + Mockito.when(Globals.getRDbUtils()).thenReturn(utils); + + ResultSetMetaData rsmd = PowerMockito.mock(ResultSetMetaData.class); + Mockito.when(rs.getMetaData()).thenReturn(rsmd); + PowerMockito.mockStatic(Runtime.class); + Runtime runtime = PowerMockito.mock(Runtime.class); + Mockito.when(Runtime.getRuntime()).thenReturn(runtime); + Mockito.when(rsmd.getColumnCount()).thenReturn(2); + Mockito.when(rsmd.getColumnLabel(Matchers.anyInt())).thenReturn("test"); + + remDbUtils.executeQuery("test", "test"); + } + +// @Test +// public void excecuteQueryTest2() throws Exception +// { +// remDbUtils.executeQuery(null, "test", Integer.MAX_VALUE,"test"); +// } +} diff --git a/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletControllerTest.java b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletControllerTest.java new file mode 100644 index 00000000..7b79c58c --- /dev/null +++ b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletControllerTest.java @@ -0,0 +1,103 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software 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. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * 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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalsdk.analytics.system.fusion.controller; + +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.portalsdk.analytics.util.XSSFilter; +import org.onap.portalsdk.analytics.xmlobj.MockitoTestSuite; +import org.onap.portalsdk.core.service.DataAccessService; +import org.owasp.esapi.ESAPI; +import org.owasp.esapi.Encoder; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ ESAPI.class, XSSFilter.class}) +public class FileServletControllerTest { + + @InjectMocks + FileServletController fileServletController = new FileServletController(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Mock + DataAccessService dataAccessService; + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + @Test + public void handleRequestInternalTest() throws Exception { + List<Object> objs = new ArrayList<>(); + byte [] byteVal = "test123".getBytes(); + objs.add(byteVal); + PowerMockito.mockStatic(ESAPI.class); + Encoder encoder = PowerMockito.mock(Encoder.class); + when(ESAPI.encoder()).thenReturn(encoder); + when(encoder.canonicalize(Matchers.anyString())).thenReturn("test"); + when(mockedRequest.getParameter(Matchers.anyString())).thenReturn("test"); + ServletOutputStream sos = mock(ServletOutputStream.class); + when(mockedResponse.getOutputStream()).thenReturn(sos); + when(fileServletController.getDataAccessService().executeNamedQuery(Matchers.anyString(), Matchers.anyMap(), Matchers.anyMap())).thenReturn(objs); + assertNull(fileServletController.handleRequestInternal(mockedRequest, mockedResponse)); + } +} + + diff --git a/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/web/RaptorControllerTest.java b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/web/RaptorControllerTest.java new file mode 100644 index 00000000..491554d8 --- /dev/null +++ b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/web/RaptorControllerTest.java @@ -0,0 +1,146 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software 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. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * 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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalsdk.analytics.system.fusion.web; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.portalsdk.analytics.controller.ActionMapping; +import org.onap.portalsdk.analytics.system.Globals; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalsdk.analytics.util.AppConstants; +import org.onap.portalsdk.analytics.xmlobj.MockitoTestSuite; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.onap.portalsdk.analytics.controller.Action; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(Globals.class) +public class RaptorControllerTest { + + @InjectMocks + RaptorController raptorController = new RaptorController(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + @Test + public void reportTest() { + assertEquals(raptorController.report(mockedRequest).getViewName(), "report"); + } + + @Test + public void reportDS1Test() { + assertEquals(raptorController.reportDS1(mockedRequest).getViewName(), "reportDS1"); + } + + @Test + public void reportEmbeddedTest() { + assertEquals(raptorController.reportEmbedded(mockedRequest).getViewName(), "report_embedded"); + } + + @Test + public void reportSampleTest() { + assertEquals(raptorController.reportSample(mockedRequest).getViewName(), "report_sample"); + } + + @Test(expected = java.lang.NullPointerException.class) + public void reportImportTest() throws Exception { + PowerMockito.mockStatic(Globals.class); + ServletContext servletContext = PowerMockito.mock(ServletContext.class); + Mockito.when(mockedRequest.getSession().getServletContext()).thenReturn(servletContext); + ActionMapping actionMapping = PowerMockito.mock(ActionMapping.class); + Mockito.when(Globals.getRaptorActionMapping()).thenReturn(actionMapping); + Mockito.when(actionMapping.getAction(Matchers.anyString())).thenReturn(null); + assertEquals(raptorController.reportImport(mockedRequest).getViewName(), "report_sample"); + } + + @Test + public void reportImport1Test() throws Exception { + PowerMockito.mockStatic(Globals.class); + ServletContext servletContext = PowerMockito.mock(ServletContext.class); + Mockito.when(mockedRequest.getSession().getServletContext()).thenReturn(servletContext); + ActionMapping actionMapping = PowerMockito.mock(ActionMapping.class); + Mockito.when(Globals.getRaptorActionMapping()).thenReturn(actionMapping); + Action action = PowerMockito.mock(Action.class); + + Mockito.when(actionMapping.getAction(Matchers.anyString())).thenReturn(action); + assertEquals(raptorController.reportImport(mockedRequest).getViewName(), "report_import"); + } + + @Test(expected = java.lang.NullPointerException.class) + public void reportWizardExceptionTest() throws Exception { + PowerMockito.mockStatic(Globals.class); + ServletContext servletContext = PowerMockito.mock(ServletContext.class); + Mockito.when(mockedRequest.getSession().getServletContext()).thenReturn(servletContext); + ActionMapping actionMapping = PowerMockito.mock(ActionMapping.class); + Mockito.when(Globals.getRaptorActionMapping()).thenReturn(actionMapping); + raptorController.reportWizard(mockedRequest, mockedResponse); + } + + @Test(expected = java.lang.NullPointerException.class) + public void reportWizardTest() throws Exception { + PowerMockito.mockStatic(Globals.class); + ServletContext servletContext = PowerMockito.mock(ServletContext.class); + Mockito.when(mockedRequest.getSession().getServletContext()).thenReturn(servletContext); + ActionMapping actionMapping = PowerMockito.mock(ActionMapping.class); + Mockito.when(Globals.getRaptorActionMapping()).thenReturn(actionMapping); + Action action = PowerMockito.mock(Action.class); + Mockito.when(actionMapping.getAction(Matchers.anyString())).thenReturn(action); + raptorController.reportWizard(mockedRequest, mockedResponse); + } + +} diff --git a/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/web/ReportsSearchListControllerTest.java b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/web/ReportsSearchListControllerTest.java new file mode 100644 index 00000000..ab3542cd --- /dev/null +++ b/ecomp-sdk/epsdk-analytics/src/test/java/org/onap/portalsdk/analytics/system/fusion/web/ReportsSearchListControllerTest.java @@ -0,0 +1,69 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software 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. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * 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============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalsdk.analytics.system.fusion.web; + +import static org.junit.Assert.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; +import org.onap.portalsdk.analytics.xmlobj.MockitoTestSuite; + +public class ReportsSearchListControllerTest { + @InjectMocks + ReportsSearchListController reportsSearchListController = new ReportsSearchListController(); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + + @Test + public void handleRequestInternalTest() { + assertNull(reportsSearchListController.handleRequestInternal(mockedRequest, mockedResponse)); + } + +} |