From ff2b09c4c4f131afe53927f2858e15937d8125c4 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Thu, 12 Oct 2017 16:28:35 +0530 Subject: [PATCH] Adding more negative testcases for the operation management impl. --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 32 -- .../device/mgt/core/mock/MockConnection.java | 312 ++++++++++++++++++ .../device/mgt/core/mock/MockDataSource.java | 95 ++++++ .../device/mgt/core/mock/MockJDBCDriver.java | 69 ++++ .../device/mgt/core/mock/MockStatement.java | 250 ++++++++++++++ ...tionManagementNegativeDBOperationTest.java | 228 +++++++++++++ .../OperationManagementNoDBSchemaTests.java | 6 +- .../datasource/mock-data-source-config.xml | 33 ++ .../src/test/resources/testng.xml | 1 + 9 files changed, 992 insertions(+), 34 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockConnection.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockDataSource.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockJDBCDriver.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockStatement.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/mock-data-source-config.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index a575d18cf3..500e48b0ee 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -394,28 +394,6 @@ public class GenericOperationDAOImpl implements OperationDAO { List activities = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - -/* - String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" + - "op.OPERATION_CODE, op.TYPE as OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" + - "ops.RECEIVED_TIMESTAMP, ops.ID as OP_RES_ID, ops.OPERATION_RESPONSE,\n" + - "de.DEVICE_IDENTIFICATION, dt.NAME as DEVICE_TYPE\n" + - "FROM DM_ENROLMENT_OP_MAPPING AS opm\n" + - "LEFT JOIN DM_OPERATION AS op ON opm.OPERATION_ID = op.ID \n" + - "LEFT JOIN DM_ENROLMENT as en ON opm.ENROLMENT_ID = en.ID \n" + - "LEFT JOIN DM_DEVICE as de ON en.DEVICE_ID = de.ID \n" + - "LEFT JOIN DM_DEVICE_TYPE as dt ON dt.ID = de.DEVICE_TYPE_ID \n" + - "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE as ops ON \n" + - "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" + - "WHERE opm.UPDATED_TIMESTAMP > ? \n" + - "AND de.TENANT_ID = ? \n"; - - if (timestamp == 0) { - sql += "ORDER BY opm.OPERATION_ID LIMIT ? OFFSET ?;"; - } else { - sql += "ORDER BY opm.UPDATED_TIMESTAMP asc LIMIT ? OFFSET ?"; - } -*/ int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String sql = "SELECT " + " opr.ENROLMENT_ID, " + @@ -650,11 +628,6 @@ public class GenericOperationDAOImpl implements OperationDAO { operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); -// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { -// operation.setReceivedTimeStamp(""); -// } else { -// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); -// } if (rs.getLong("UPDATED_TIMESTAMP") == 0) { operation.setReceivedTimeStamp(""); } else { @@ -696,11 +669,6 @@ public class GenericOperationDAOImpl implements OperationDAO { operation.setId(rs.getInt("ID")); operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); -// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) { -// operation.setReceivedTimeStamp(""); -// } else { -// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); -// } if (rs.getLong("UPDATED_TIMESTAMP") == 0) { operation.setReceivedTimeStamp(""); } else { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockConnection.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockConnection.java new file mode 100644 index 0000000000..00b15f6a3a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockConnection.java @@ -0,0 +1,312 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. licenses this file to you 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. +* +*/ +package org.wso2.carbon.device.mgt.core.mock; + +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executor; + +/** + * This is mock class which provides mock database connection. + */ +public class MockConnection implements Connection { + @Override + public Statement createStatement() throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql) throws SQLException { + return null; + } + + @Override + public String nativeSQL(String sql) throws SQLException { + return null; + } + + @Override + public void setAutoCommit(boolean autoCommit) throws SQLException { + + } + + @Override + public boolean getAutoCommit() throws SQLException { + return false; + } + + @Override + public void commit() throws SQLException { + + } + + @Override + public void rollback() throws SQLException { + + } + + @Override + public void close() throws SQLException { + + } + + @Override + public boolean isClosed() throws SQLException { + return false; + } + + @Override + public DatabaseMetaData getMetaData() throws SQLException { + return null; + } + + @Override + public void setReadOnly(boolean readOnly) throws SQLException { + + } + + @Override + public boolean isReadOnly() throws SQLException { + return false; + } + + @Override + public void setCatalog(String catalog) throws SQLException { + + } + + @Override + public String getCatalog() throws SQLException { + return null; + } + + @Override + public void setTransactionIsolation(int level) throws SQLException { + + } + + @Override + public int getTransactionIsolation() throws SQLException { + return 0; + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return null; + } + + @Override + public void clearWarnings() throws SQLException { + + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { + return null; + } + + @Override + public Map> getTypeMap() throws SQLException { + return null; + } + + @Override + public void setTypeMap(Map> map) throws SQLException { + + } + + @Override + public void setHoldability(int holdability) throws SQLException { + + } + + @Override + public int getHoldability() throws SQLException { + return 0; + } + + @Override + public Savepoint setSavepoint() throws SQLException { + return null; + } + + @Override + public Savepoint setSavepoint(String name) throws SQLException { + return null; + } + + @Override + public void rollback(Savepoint savepoint) throws SQLException { + + } + + @Override + public void releaseSavepoint(Savepoint savepoint) throws SQLException { + + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { + return null; + } + + @Override + public Clob createClob() throws SQLException { + return null; + } + + @Override + public Blob createBlob() throws SQLException { + return null; + } + + @Override + public NClob createNClob() throws SQLException { + return null; + } + + @Override + public SQLXML createSQLXML() throws SQLException { + return null; + } + + @Override + public boolean isValid(int timeout) throws SQLException { + return false; + } + + @Override + public void setClientInfo(String name, String value) throws SQLClientInfoException { + + } + + @Override + public void setClientInfo(Properties properties) throws SQLClientInfoException { + + } + + @Override + public String getClientInfo(String name) throws SQLException { + return null; + } + + @Override + public Properties getClientInfo() throws SQLException { + return null; + } + + @Override + public Array createArrayOf(String typeName, Object[] elements) throws SQLException { + return null; + } + + @Override + public Struct createStruct(String typeName, Object[] attributes) throws SQLException { + return null; + } + + @Override + public void setSchema(String schema) throws SQLException { + + } + + @Override + public String getSchema() throws SQLException { + return null; + } + + @Override + public void abort(Executor executor) throws SQLException { + + } + + @Override + public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { + + } + + @Override + public int getNetworkTimeout() throws SQLException { + return 0; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockDataSource.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockDataSource.java new file mode 100644 index 0000000000..1e997f55c4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockDataSource.java @@ -0,0 +1,95 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. licenses this file to you 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. +* +*/ +package org.wso2.carbon.device.mgt.core.mock; + +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.logging.Logger; +import javax.sql.DataSource; + +public class MockDataSource implements DataSource { + private boolean throwException = false; + private Connection connection; + + @Override + public Connection getConnection() throws SQLException { + if (throwException) { + throw new SQLException("Cannot created test connection."); + } else { + if (connection != null) { + return connection; + } + return new MockConnection(); + } + } + + @Override + public Connection getConnection(String username, String password) throws SQLException { + return null; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } + + @Override + public PrintWriter getLogWriter() throws SQLException { + return null; + } + + @Override + public void setLogWriter(PrintWriter out) throws SQLException { + + } + + @Override + public void setLoginTimeout(int seconds) throws SQLException { + + } + + @Override + public int getLoginTimeout() throws SQLException { + return 0; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } + + public void setThrowException(boolean throwException) { + this.throwException = throwException; + } + + public void reset() { + this.throwException = false; + this.connection = null; + } + + private void setConnection(Connection connection) { + this.connection = connection; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockJDBCDriver.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockJDBCDriver.java new file mode 100644 index 0000000000..1ce52f5544 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockJDBCDriver.java @@ -0,0 +1,69 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. licenses this file to you 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. +* +*/ +package org.wso2.carbon.device.mgt.core.mock; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverPropertyInfo; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; +import java.util.logging.Logger; + +/** + * This is the mock JDBC driver which is used in the testcases to mock other database operations. + */ +public class MockJDBCDriver implements Driver { + + @Override + public Connection connect(String url, Properties info) throws SQLException { + return null; + } + + @Override + public boolean acceptsURL(String url) throws SQLException { + return true; + } + + @Override + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { + return new DriverPropertyInfo[0]; + } + + @Override + public int getMajorVersion() { + return 0; + } + + @Override + public int getMinorVersion() { + return 0; + } + + @Override + public boolean jdbcCompliant() { + return true; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockStatement.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockStatement.java new file mode 100644 index 0000000000..f717f67660 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/mock/MockStatement.java @@ -0,0 +1,250 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. licenses this file to you 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. +* +*/ +package org.wso2.carbon.device.mgt.core.mock; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.Statement; + +/** + * This is the mock statement for the testcases. + */ +public class MockStatement implements Statement { + + @Override + public ResultSet executeQuery(String sql) throws SQLException { + return null; + } + + @Override + public int executeUpdate(String sql) throws SQLException { + return 0; + } + + @Override + public void close() throws SQLException { + + } + + @Override + public int getMaxFieldSize() throws SQLException { + return 0; + } + + @Override + public void setMaxFieldSize(int max) throws SQLException { + + } + + @Override + public int getMaxRows() throws SQLException { + return 0; + } + + @Override + public void setMaxRows(int max) throws SQLException { + + } + + @Override + public void setEscapeProcessing(boolean enable) throws SQLException { + + } + + @Override + public int getQueryTimeout() throws SQLException { + return 0; + } + + @Override + public void setQueryTimeout(int seconds) throws SQLException { + + } + + @Override + public void cancel() throws SQLException { + + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return null; + } + + @Override + public void clearWarnings() throws SQLException { + + } + + @Override + public void setCursorName(String name) throws SQLException { + + } + + @Override + public boolean execute(String sql) throws SQLException { + return false; + } + + @Override + public ResultSet getResultSet() throws SQLException { + return null; + } + + @Override + public int getUpdateCount() throws SQLException { + return 0; + } + + @Override + public boolean getMoreResults() throws SQLException { + return false; + } + + @Override + public void setFetchDirection(int direction) throws SQLException { + + } + + @Override + public int getFetchDirection() throws SQLException { + return 0; + } + + @Override + public void setFetchSize(int rows) throws SQLException { + + } + + @Override + public int getFetchSize() throws SQLException { + return 0; + } + + @Override + public int getResultSetConcurrency() throws SQLException { + return 0; + } + + @Override + public int getResultSetType() throws SQLException { + return 0; + } + + @Override + public void addBatch(String sql) throws SQLException { + + } + + @Override + public void clearBatch() throws SQLException { + + } + + @Override + public int[] executeBatch() throws SQLException { + return new int[0]; + } + + @Override + public Connection getConnection() throws SQLException { + return null; + } + + @Override + public boolean getMoreResults(int current) throws SQLException { + return false; + } + + @Override + public ResultSet getGeneratedKeys() throws SQLException { + return null; + } + + @Override + public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { + return 0; + } + + @Override + public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { + return 0; + } + + @Override + public int executeUpdate(String sql, String[] columnNames) throws SQLException { + return 0; + } + + @Override + public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { + return false; + } + + @Override + public boolean execute(String sql, int[] columnIndexes) throws SQLException { + return false; + } + + @Override + public boolean execute(String sql, String[] columnNames) throws SQLException { + return false; + } + + @Override + public int getResultSetHoldability() throws SQLException { + return 0; + } + + @Override + public boolean isClosed() throws SQLException { + return false; + } + + @Override + public void setPoolable(boolean poolable) throws SQLException { + + } + + @Override + public boolean isPoolable() throws SQLException { + return false; + } + + @Override + public void closeOnCompletion() throws SQLException { + + } + + @Override + public boolean isCloseOnCompletion() throws SQLException { + return false; + } + + @Override + public T unwrap(Class iface) throws SQLException { + return null; + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return false; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java new file mode 100644 index 0000000000..981fbd5c3b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNegativeDBOperationTest.java @@ -0,0 +1,228 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. licenses this file to you 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. +* +*/ +package org.wso2.carbon.device.mgt.core.operation; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; +import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; +import org.wso2.carbon.device.mgt.core.common.TestDataHolder; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.core.mock.MockDataSource; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import javax.sql.DataSource; + +/** + * Negative testcases for {@link OperationManagerImpl} + * regarding the database connectivity. + */ +public class OperationManagementNegativeDBOperationTest extends BaseDeviceManagementTest { + + private static final String DEVICE_TYPE = "NEGDB_OP_TEST_TYPE"; + private static final String DEVICE_ID_PREFIX = "NEGDB_OP-TEST-DEVICE-ID-"; + private static final int NO_OF_DEVICES = 5; + private static final String ADMIN_USER = "admin"; + + private List deviceIds = new ArrayList<>(); + private OperationManager operationMgtService; + private MockDataSource dataSource; + + + @BeforeClass + @Override + public void init() throws Exception { + DataSource datasource = this.getDataSource(this. + readDataSourceConfig("src/test/resources/config/datasource/mock-data-source-config.xml")); + OperationManagementDAOFactory.init(datasource); + for (int i = 0; i < NO_OF_DEVICES; i++) { + deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); + } + List devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); + DeviceManagementProviderService deviceMgtService = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider(); + deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); + for (Device device : devices) { + deviceMgtService.enrollDevice(device); + } + List returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE); + for (Device device : returnedDevices) { + if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) { + throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); + } + } + NotificationStrategy notificationStrategy = new TestNotificationStrategy(); + this.operationMgtService = new OperationManagerImpl(DEVICE_TYPE, notificationStrategy); + this.setMockDataSource(); + } + + @Test(description = "Get operation by device id", expectedExceptions = OperationManagementException.class) + public void getOperations() throws OperationManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getOperations(this.deviceIds.get(0)); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get operations with paginated request", + expectedExceptions = OperationManagementException.class) + public void getOperationsPaginatedRequest() throws OperationManagementException { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID, true); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(ADMIN_USER); + PaginationRequest request = new PaginationRequest(1, 2); + request.setDeviceType(DEVICE_TYPE); + request.setOwner(ADMIN_USER); + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getOperations(this.deviceIds.get(0), request); + } finally { + PrivilegedCarbonContext.endTenantFlow(); + this.dataSource.reset(); + } + } + + @Test(description = "Get pending operations", expectedExceptions = OperationManagementException.class) + public void getPendingOperations() throws OperationManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getPendingOperations(this.deviceIds.get(0)); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get next pending operations", expectedExceptions = OperationManagementException.class) + public void getNextPendingOperations() throws OperationManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getNextPendingOperation(this.deviceIds.get(0)); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get operation by device and operation id", + expectedExceptions = OperationManagementException.class) + public void getOperationByDeviceAndOperationId() throws OperationManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getOperationByDeviceAndOperationId(this.deviceIds.get(0), 1); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get operation by device and status", + expectedExceptions = OperationManagementException.class) + public void getOperationByDeviceAndStatus() throws OperationManagementException, DeviceManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getOperationsByDeviceAndStatus(this.deviceIds.get(0), Operation.Status.PENDING); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get operation by operation id", + expectedExceptions = OperationManagementException.class) + public void getOperationByOperationId() throws OperationManagementException, DeviceManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getOperation(1); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get operation by activity id", + expectedExceptions = OperationManagementException.class) + public void getOperationByActivityId() throws OperationManagementException, DeviceManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getOperationByActivityId("ACTIVITY_1"); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get operation by activity id and device id", + expectedExceptions = OperationManagementException.class) + public void getOperationByActivityAndDeviceID() throws OperationManagementException, DeviceManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getOperationByActivityIdAndDevice("ACTIVITY_1", deviceIds.get(0)); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get activities updated after some timestamp", + expectedExceptions = OperationManagementException.class) + public void getActivitiesUpdatedAfter() throws OperationManagementException, DeviceManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getActivitiesUpdatedAfter(System.currentTimeMillis() / 1000, 10, 0); + } finally { + this.dataSource.reset(); + } + } + + @Test(description = "Get activities count updated after some timestamp", + expectedExceptions = OperationManagementException.class) + public void getActivityCountUpdatedAfter() throws OperationManagementException, DeviceManagementException { + this.dataSource.setThrowException(true); + try { + this.operationMgtService.getActivityCountUpdatedAfter(System.currentTimeMillis() / 1000); + } finally { + this.dataSource.reset(); + } + } + + private void setMockDataSource() throws NoSuchFieldException, IllegalAccessException { + Field datasource = OperationManagementDAOFactory.class.getDeclaredField("dataSource"); + datasource.setAccessible(true); + this.dataSource = new MockDataSource(); + datasource.set(datasource, this.dataSource); + } + + @AfterClass + public void resetDatabase() throws DeviceManagementException { + OperationManagementDAOFactory.init(this.getDataSource(this. + readDataSourceConfig("src/test/resources/config/datasource/data-source-config.xml"))); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java index f6d9aa5985..3292b4960e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementNoDBSchemaTests.java @@ -90,12 +90,14 @@ public class OperationManagementNoDBSchemaTests extends BaseDeviceManagementTest public void addCommandOperation() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { this.operationMgtService.addOperation( - OperationManagementTests.getOperation(new CommandOperation(), Operation.Type.COMMAND, COMMAND_OPERATON_CODE), + OperationManagementTests.getOperation(new CommandOperation(), + Operation.Type.COMMAND, COMMAND_OPERATON_CODE), this.deviceIds); } @Test(description = "Get operations", expectedExceptions = OperationManagementException.class) - public void getOperations() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + public void getOperations() throws DeviceManagementException, OperationManagementException, + InvalidDeviceException { for (DeviceIdentifier deviceIdentifier : deviceIds) { this.operationMgtService.getOperations(deviceIdentifier); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/mock-data-source-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/mock-data-source-config.xml new file mode 100644 index 0000000000..91d25ac9ad --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/mock-data-source-config.xml @@ -0,0 +1,33 @@ + + + + + jdbc:h2:mem:cdm-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true + org.wso2.carbon.device.mgt.core.mock.MockJDBCDriver + wso2carbon + wso2carbon + + + + + + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index 8ea8320887..8093861d4c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -41,6 +41,7 @@ +