Merge pull request #1053 from sinthuja/origin-wso2-master

Adding more negative testcase for operation management
revert-70aa11f8
Megala Uthayakumar 7 years ago committed by GitHub
commit e7319c5ac8

@ -17,7 +17,8 @@
~ under the License. ~ under the License.
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>apimgt-extensions</artifactId> <artifactId>apimgt-extensions</artifactId>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>

@ -394,28 +394,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
List<Activity> activities = new ArrayList<>(); List<Activity> activities = new ArrayList<>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); 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(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" opr.ENROLMENT_ID, " + " opr.ENROLMENT_ID, " +
@ -650,11 +628,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); 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) { if (rs.getLong("UPDATED_TIMESTAMP") == 0) {
operation.setReceivedTimeStamp(""); operation.setReceivedTimeStamp("");
} else { } else {
@ -696,11 +669,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
operation.setId(rs.getInt("ID")); operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); 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) { if (rs.getLong("UPDATED_TIMESTAMP") == 0) {
operation.setReceivedTimeStamp(""); operation.setReceivedTimeStamp("");
} else { } else {

@ -21,10 +21,7 @@ package org.wso2.carbon.device.mgt.core.common;
import org.apache.axis2.AxisFault; import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory; import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.PoolProperties; import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeSuite;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -59,14 +56,11 @@ import javax.xml.bind.Unmarshaller;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
public abstract class BaseDeviceManagementTest { public abstract class BaseDeviceManagementTest {
private DataSource dataSource; private DataSource dataSource;
private static final Log log = LogFactory.getLog(BaseDeviceManagementTest.class);
@BeforeSuite @BeforeSuite
public void setupDataSource() throws Exception { public void setupDataSource() throws Exception {
@ -77,7 +71,8 @@ public abstract class BaseDeviceManagementTest {
} }
protected void initDataSource() throws Exception { protected void initDataSource() throws Exception {
this.dataSource = this.getDataSource(this.readDataSourceConfig()); this.dataSource = this.getDataSource(this.
readDataSourceConfig("src/test/resources/config/datasource/data-source-config.xml"));
DeviceManagementDAOFactory.init(dataSource); DeviceManagementDAOFactory.init(dataSource);
GroupManagementDAOFactory.init(dataSource); GroupManagementDAOFactory.init(dataSource);
OperationManagementDAOFactory.init(dataSource); OperationManagementDAOFactory.init(dataSource);
@ -116,7 +111,7 @@ public abstract class BaseDeviceManagementTest {
@BeforeClass @BeforeClass
public abstract void init() throws Exception; public abstract void init() throws Exception;
private DataSource getDataSource(DataSourceConfig config) { protected DataSource getDataSource(DataSourceConfig config) {
PoolProperties properties = new PoolProperties(); PoolProperties properties = new PoolProperties();
properties.setUrl(config.getUrl()); properties.setUrl(config.getUrl());
properties.setDriverClassName(config.getDriverClassName()); properties.setDriverClassName(config.getDriverClassName());
@ -151,9 +146,9 @@ public abstract class BaseDeviceManagementTest {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
} }
private DataSourceConfig readDataSourceConfig() throws DeviceManagementException { protected DataSourceConfig readDataSourceConfig(String configLocation) throws DeviceManagementException {
try { try {
File file = new File("src/test/resources/config/datasource/data-source-config.xml"); File file = new File(configLocation);
Document doc = DeviceManagerUtil.convertToDocument(file); Document doc = DeviceManagerUtil.convertToDocument(file);
JAXBContext testDBContext = JAXBContext.newInstance(DataSourceConfig.class); JAXBContext testDBContext = JAXBContext.newInstance(DataSourceConfig.class);
Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); Unmarshaller unmarshaller = testDBContext.createUnmarshaller();

@ -0,0 +1,316 @@
/*
* 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<String, Class<?>> getTypeMap() throws SQLException {
return null;
}
@Override
public void setTypeMap(Map<String, Class<?>> 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> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}

@ -0,0 +1,99 @@
/*
* 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;
/**
* This is the mock data source implementation that will be used in the test cases.
*
*/
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> T unwrap(Class<T> 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;
}
}

@ -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;
}
}

@ -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> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}

@ -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<DeviceIdentifier> 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<Device> 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<Device> 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")));
}
}

@ -0,0 +1,196 @@
/*
* 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.InvalidDeviceException;
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.DeviceManagementConstants;
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.operation.mgt.CommandOperation;
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.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
/**
* This is the testcase which covers the methods from {@link OperationManager}
*/
public class OperationManagementNoDBSchemaTests extends BaseDeviceManagementTest {
private static final String DEVICE_TYPE = "NEG_OP_TEST_TYPE";
private static final String DEVICE_ID_PREFIX = "NEG_OP-TEST-DEVICE-ID-";
private static final String COMMAND_OPERATON_CODE = "COMMAND-TEST";
private static final int NO_OF_DEVICES = 5;
private static final String ADMIN_USER = "admin";
private List<DeviceIdentifier> deviceIds = new ArrayList<>();
private OperationManager operationMgtService;
@BeforeClass
public void init() throws Exception {
DataSource datasource = this.getDataSource(this.
readDataSourceConfig("src/test/resources/config/datasource/no-table-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<Device> 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<Device> 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);
}
@Test(description = "add operation", expectedExceptions = OperationManagementException.class)
public void addCommandOperation() throws DeviceManagementException, OperationManagementException,
InvalidDeviceException {
this.operationMgtService.addOperation(
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 {
for (DeviceIdentifier deviceIdentifier : deviceIds) {
this.operationMgtService.getOperations(deviceIdentifier);
}
}
@Test(description = "Get Pending operations", expectedExceptions = OperationManagementException.class)
public void getPendingOperations() throws DeviceManagementException, OperationManagementException,
InvalidDeviceException {
for (DeviceIdentifier deviceIdentifier : deviceIds) {
this.operationMgtService.getPendingOperations(deviceIdentifier);
}
}
@Test(description = "Get paginated request", expectedExceptions = OperationManagementException.class)
public void getPaginatedRequestAsAdmin() 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);
for (DeviceIdentifier deviceIdentifier : deviceIds) {
this.operationMgtService.getOperations(deviceIdentifier, request);
}
PrivilegedCarbonContext.endTenantFlow();
}
@Test(description = "Update operation", expectedExceptions = OperationManagementException.class)
public void updateOperation() throws OperationManagementException {
DeviceIdentifier deviceIdentifier = this.deviceIds.get(0);
Operation operation = OperationManagementTests.getOperation(new CommandOperation(), Operation.Type.COMMAND,
COMMAND_OPERATON_CODE);
operation.setStatus(Operation.Status.COMPLETED);
operation.setOperationResponse("The operation is successfully completed");
this.operationMgtService.updateOperation(deviceIdentifier, operation);
}
@Test(description = "Get next pending operation", expectedExceptions = OperationManagementException.class)
public void getNextPendingOperation() throws OperationManagementException {
DeviceIdentifier deviceIdentifier = this.deviceIds.get(0);
this.operationMgtService.getNextPendingOperation(deviceIdentifier);
}
@Test(description = "get operation by device and operation id",
expectedExceptions = OperationManagementException.class)
public void getOperationByDeviceAndOperationId() throws OperationManagementException {
DeviceIdentifier deviceIdentifier = this.deviceIds.get(0);
this.operationMgtService.getOperationByDeviceAndOperationId(deviceIdentifier, 1);
}
@Test(description = "Get operation by device and status",
expectedExceptions = OperationManagementException.class)
public void getOperationsByDeviceAndStatus() throws OperationManagementException, DeviceManagementException {
DeviceIdentifier deviceIdentifier = this.deviceIds.get(0);
this.operationMgtService.getOperationsByDeviceAndStatus(deviceIdentifier,
Operation.Status.PENDING);
}
@Test(description = "Get operation by operation id", expectedExceptions = OperationManagementException.class)
public void getOperation() throws OperationManagementException, DeviceManagementException {
this.operationMgtService.getOperation(1);
}
@Test(description = "Get operation activity", expectedExceptions = OperationManagementException.class)
public void getOperationActivity() throws OperationManagementException {
this.operationMgtService.getOperationByActivityId
(DeviceManagementConstants.OperationAttributes.ACTIVITY + "1");
}
@Test(description = "Get operation by activity id and device",
expectedExceptions = OperationManagementException.class)
public void getOperationByActivityIdAndDevice() throws OperationManagementException {
this.operationMgtService.getOperationByActivityIdAndDevice(
DeviceManagementConstants.OperationAttributes.ACTIVITY + "1", this.deviceIds.get(0));
}
@Test(description = "Get activities updated after some time",
expectedExceptions = OperationManagementException.class)
public void getOperationUpdatedAfterWithLimitAndOffset() throws OperationManagementException {
this.operationMgtService.getActivitiesUpdatedAfter(System.currentTimeMillis() / 1000, 10, 0);
}
@Test(description = "Get activity count updated after",
expectedExceptions = OperationManagementException.class)
public void getActivityCountUpdatedAfter() throws OperationManagementException {
this.operationMgtService.getActivityCountUpdatedAfter(System.currentTimeMillis() / 1000);
}
@AfterClass
public void resetDatabase() throws DeviceManagementException {
OperationManagementDAOFactory.init(this.getDataSource(this.
readDataSourceConfig("src/test/resources/config/datasource/data-source-config.xml")));
}
}

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
~ 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.
-->
<DataSourceConfig>
<Url>jdbc:h2:mem:cdm-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true</Url>
<DriverClassName>org.wso2.carbon.device.mgt.core.mock.MockJDBCDriver</DriverClassName>
<User>wso2carbon</User>
<Password>wso2carbon</Password>
<!-- For MySql -->
<!--<Url>jdbc:mysql://localhost:3306/WSO2CDM</Url>-->
<!--<DriverClassName>com.mysql.jdbc.Driver</DriverClassName>-->
<!--<User>root</User>-->
<!--<Password></Password>-->
</DataSourceConfig>

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
~ 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.
-->
<DataSourceConfig>
<Url>jdbc:h2:mem:nodb-test-db;DB_CLOSE_ON_EXIT=FALSE;MVCC=true</Url>
<DriverClassName>org.h2.Driver</DriverClassName>
<User>wso2carbon</User>
<Password>wso2carbon</Password>
<!-- For MySql -->
<!--<Url>jdbc:mysql://localhost:3306/WSO2CDM</Url>-->
<!--<DriverClassName>com.mysql.jdbc.Driver</DriverClassName>-->
<!--<User>root</User>-->
<!--<Password></Password>-->
</DataSourceConfig>

@ -1,4 +1,3 @@
<!-- <!--
~ Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. ~ Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~ ~
@ -41,6 +40,8 @@
<class name="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServiceTest"/> <class name="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServiceTest"/>
<class name="org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceTest"/> <class name="org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceTest"/>
<class name="org.wso2.carbon.device.mgt.core.operation.OperationManagementTests"/> <class name="org.wso2.carbon.device.mgt.core.operation.OperationManagementTests"/>
<class name="org.wso2.carbon.device.mgt.core.operation.OperationManagementNoDBSchemaTests"/>
<class name="org.wso2.carbon.device.mgt.core.operation.OperationManagementNegativeDBOperationTest"/>
<class name="org.wso2.carbon.device.mgt.core.geo.service.GeoLocationProviderServiceTest"/> <class name="org.wso2.carbon.device.mgt.core.geo.service.GeoLocationProviderServiceTest"/>
<class name="org.wso2.carbon.device.mgt.core.operation.ScheduledTaskOperationTests"/> <class name="org.wso2.carbon.device.mgt.core.operation.ScheduledTaskOperationTests"/>
<class name="org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceTest"/> <class name="org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceTest"/>

Loading…
Cancel
Save