revert-70aa11f8
manoj 10 years ago
commit ee6c067398

@ -20,17 +20,19 @@ package org.wso2.carbon.device.mgt.core.operation.mgt;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
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.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO;
import java.util.Iterator;
import java.util.List;
/**
@ -62,7 +64,11 @@ public class OperationManagerImpl implements OperationManager {
try {
OperationManagementDAOFactory.beginTransaction();
int operationId = this.lookupOperationDAO(operation).addOperation(operation);
operationMappingDAO.addOperationMapping(operationId, null);
for(Iterator<DeviceIdentifier> i = devices.iterator(); i.hasNext(); ) {
DeviceIdentifier deviceIdentifier = i.next();
org.wso2.carbon.device.mgt.core.dto.Device device = deviceDAO.getDevice(deviceIdentifier);
operationMappingDAO.addOperationMapping(operationId, device.getId());
}
OperationManagementDAOFactory.commitTransaction();
return true;
} catch (OperationManagementDAOException e) {
@ -72,6 +78,13 @@ public class OperationManagerImpl implements OperationManager {
log.warn("Error occurred while roll-backing the transaction", e1);
}
throw new OperationManagementException("Error occurred while adding operation", e);
} catch (DeviceManagementDAOException e) {
try {
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e1);
}
throw new OperationManagementException("Error occurred while adding operation", e);
}
}

@ -18,14 +18,10 @@
*/
package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import java.util.List;
public interface OperationMappingDAO {
void addOperationMapping(int operationId, List<Integer> deviceIds) throws OperationManagementDAOException;
void addOperationMapping(int operationId, Integer deviceIds) throws OperationManagementDAOException;
void removeOperationMapping(int operationId, List<Integer> deviceIds) throws OperationManagementDAOException;
void removeOperationMapping(int operationId, Integer deviceIds) throws OperationManagementDAOException;
}

@ -26,18 +26,17 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
public class OperationMappingDAOImpl implements OperationMappingDAO {
@Override
public void addOperationMapping(int operationId, List<Integer> deviceIds) throws OperationManagementDAOException {
public void addOperationMapping(int operationId, Integer deviceId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "INSERT INTO DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID) VALUES(?, ?)";
String sql = "INSERT INTO DM_DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID) VALUES(?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, 0);
stmt.setInt(1, deviceId);
stmt.setInt(2, operationId);
stmt.executeUpdate();
} catch (SQLException e) {
@ -49,11 +48,11 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
@Override
public void removeOperationMapping(int operationId,
List<Integer> deviceIds) throws OperationManagementDAOException {
Integer deviceIds) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "DELETE FROM DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?";
String sql = "DELETE FROM DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, 0);
stmt.setInt(2, operationId);

@ -18,51 +18,86 @@
*/
package org.wso2.carbon.device.mgt.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.common.DBTypes;
import org.wso2.carbon.device.mgt.core.common.TestDBConfiguration;
import org.wso2.carbon.device.mgt.core.common.TestDBConfigurations;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import javax.sql.DataSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class DeviceManagementBaseTest {
private DataSource dataSource;
private static final Log log = LogFactory.getLog(DeviceManagementBaseTest.class);
@BeforeClass(alwaysRun = true)
@Parameters("dbType")
public void setupDatabase(String dbTypeName) throws Exception {
DBTypes type = DBTypes.valueOf(dbTypeName);
TestDBConfiguration config = getTestDBConfiguration(type);
switch (type) {
case H2:
PoolProperties properties = new PoolProperties();
properties.setUrl(config.getConnectionUrl());
properties.setDriverClassName(config.getDriverClass());
properties.setUsername(config.getUserName());
properties.setPassword(config.getPwd());
dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties);
this.initSQLScript();
default:
}
}
private TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws DeviceManagementDAOException,
DeviceManagementException {
File dbConfig = new File("src/test/resources/testdbconfig.xml");
Document doc = DeviceManagerUtil.convertToDocument(dbConfig);
TestDBConfigurations dbConfigs;
JAXBContext testDBContext;
public void init() {
this.initDataSource();
try {
this.initDeviceManagementDatabaseSchema();
} catch (SQLException e) {
Assert.fail("Error occurred while initializing database schema", e);
testDBContext = JAXBContext.newInstance(TestDBConfigurations.class);
Unmarshaller unmarshaller = testDBContext.createUnmarshaller();
dbConfigs = (TestDBConfigurations) unmarshaller.unmarshal(doc);
for (TestDBConfiguration config : dbConfigs.getDbTypesList()) {
if (config.getDbType().equals(dbType.toString())) {
return config;
}
}
return null;
} catch (JAXBException e) {
throw new DeviceManagementDAOException("Error parsing test db configurations", e);
}
}
private void initDeviceManagementDatabaseSchema() throws SQLException {
private void initSQLScript() throws Exception {
Connection conn = null;
Statement stmt = null;
try {
if (dataSource == null) {
Assert.fail("Device management datasource is not initialized peroperly");
}
conn = dataSource.getConnection();
conn = this.getDataSource().getConnection();
stmt = conn.createStatement();
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'");
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'");
} catch(Exception e){
log.error(e);
throw e;
}finally {
TestUtils.cleanupResources(conn, stmt, null);
}
}
private void initDataSource() {
PoolProperties properties = new PoolProperties();
properties.setUrl("jdbc:h2:mem:MDM_DB;DB_CLOSE_DELAY=-1");
properties.setDriverClassName("org.h2.Driver");
properties.setUsername("wso2carbon");
properties.setPassword("wso2carbon");
this.dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties);
}
protected DataSource getDataSource() {
return dataSource;
}

@ -18,14 +18,17 @@
*/
package org.wso2.carbon.device.mgt.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
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.core.dto.Device;
import org.wso2.carbon.device.mgt.core.operation.mgt.*;
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 java.util.ArrayList;
@ -34,33 +37,44 @@ import java.util.List;
public class DeviceOperationManagementTests extends DeviceManagementBaseTest {
private OperationManager operationManager;
private static final Log log = LogFactory.getLog(DeviceOperationManagementTests.class);
@BeforeClass(alwaysRun = true)
public void init() {
super.init();
this.initOperationManager();
public void init() throws Exception{
OperationManagementDAOFactory.init(this.getDataSource());
this.initOperationManager();
this.setupData();
}
private void setupData() throws Exception {
String deviceSql = "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, " +
"OWNERSHIP, STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) " +
"VALUES ('Galaxy Tab', 'Samsung', 1425467382, 1425467382, 'BYOD', 'ACTIVE', 1, " +
"'4892813d-0b18-4a02-b7b1-61775257400e', 'admin@wso2.com', '-1234');";
String typeSql = "Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android');";
this.getDataSource().getConnection().createStatement().execute(typeSql);
this.getDataSource().getConnection().createStatement().execute(deviceSql);
}
public void initOperationManager() {
private void initOperationManager() {
this.operationManager = new OperationManagerImpl();
}
@Test
public void testAddOperation() throws Exception {
CommandOperation op = new CommandOperation();
op.setEnabled(true);
op.setType(Operation.Type.COMMAND);
List<DeviceIdentifier> deviceIds = new ArrayList<DeviceIdentifier>();
DeviceIdentifier deviceId = new DeviceIdentifier();
deviceId.setId("Test");
deviceId.setType("Android");
deviceId.setId("4892813d-0b18-4a02-b7b1-61775257400e");
deviceId.setType("android");
deviceIds.add(deviceId);
try {
operationManager.addOperation(op, deviceIds);
boolean isAdded = operationManager.addOperation(op, deviceIds);
Assert.assertTrue(isAdded);
} catch (OperationManagementException e) {
e.printStackTrace();
throw new Exception(e);
@ -69,7 +83,14 @@ public class DeviceOperationManagementTests extends DeviceManagementBaseTest {
public void testGetOperations() {
try {
operationManager.getOperations(null);
//TODO:- operationManager.getOperations is not implemented
DeviceIdentifier deviceId = new DeviceIdentifier();
deviceId.setId("4892813d-0b18-4a02-b7b1-61775257400e");
deviceId.setType("android");
List<Operation> operations = operationManager.getOperations(deviceId);
Assert.assertNotNull(operations);
boolean notEmpty = operations.size() > 0;
Assert.assertTrue(notEmpty);
} catch (OperationManagementException e) {
e.printStackTrace();
}

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
@ -50,6 +51,13 @@ public class DeviceManagementDAOTests {
private DataSource dataSource;
private static final Log log = LogFactory.getLog(DeviceManagementDAOTests.class);
@AfterClass
public void deleteData() throws Exception{
Connection connection = dataSource.getConnection();
connection.createStatement().execute("DELETE FROM DM_DEVICE");
connection.createStatement().execute("DELETE FROM DM_DEVICE_TYPE");
}
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {

@ -1,13 +1,11 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE
(
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL,
NAME VARCHAR(300) NULL DEFAULT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE IF NOT EXISTS DM_DEVICE
(
ID INT auto_increment NOT NULL,
CREATE TABLE IF NOT EXISTS DM_DEVICE (
ID INTEGER auto_increment NOT NULL,
DESCRIPTION TEXT NULL DEFAULT NULL,
NAME VARCHAR(100) NULL DEFAULT NULL,
DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL,
@ -22,3 +20,38 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE
CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID )
REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_OPERATION (
ID INTEGER AUTO_INCREMENT NOT NULL,
TYPE VARCHAR(50) NOT NULL,
CREATED_TIMESTAMP TIMESTAMP NOT NULL,
RECEIVED_TIMESTAMP TIMESTAMP NULL,
STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID)
);
CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION (
OPERATION_ID INTEGER NOT NULL,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION (
OPERATION_ID INTEGER NOT NULL,
ENABLED INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);

@ -1,58 +0,0 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL,
NAME VARCHAR(300) NULL DEFAULT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE IF NOT EXISTS DM_DEVICE (
ID INTEGER auto_increment NOT NULL,
DESCRIPTION TEXT NULL DEFAULT NULL,
NAME VARCHAR(100) NULL DEFAULT NULL,
DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL,
DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL,
OWNERSHIP VARCHAR(45) NULL DEFAULT NULL,
STATUS VARCHAR(15) NULL DEFAULT NULL,
DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL,
OWNER VARCHAR(45) NULL DEFAULT NULL,
TENANT_ID INTEGER DEFAULT 0,
PRIMARY KEY (ID),
CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID )
REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_OPERATION (
ID INTEGER AUTO_INCREMENT NOT NULL,
TYPE VARCHAR(50) NOT NULL,
CREATED_TIMESTAMP TIMESTAMP NOT NULL,
RECEIVED_TIMESTAMP TIMESTAMP NULL,
STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID)
)
CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION (
OPERATION_ID INTEGER NOT NULL,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION (
OPERATION_ID INTEGER NOT NULL,
ENABLED INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)

@ -28,7 +28,7 @@
<class name="org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOTests"/>
<class name="org.wso2.carbon.device.mgt.core.DeviceManagementRepositoryTests"/>
<class name="org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests"/>
<!--<class name="org.wso2.carbon.device.mgt.core.DeviceOperationManagementTests"/>-->
<class name="org.wso2.carbon.device.mgt.core.DeviceOperationManagementTests"/>
</classes>
</test>
</suite>

@ -1,13 +1,11 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE
(
ID INT(11) auto_increment NOT NULL,
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL,
NAME VARCHAR(300) NULL DEFAULT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE IF NOT EXISTS DM_DEVICE
(
ID INT auto_increment NOT NULL,
CREATE TABLE IF NOT EXISTS DM_DEVICE (
ID INTEGER auto_increment NOT NULL,
DESCRIPTION TEXT NULL DEFAULT NULL,
NAME VARCHAR(100) NULL DEFAULT NULL,
DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL,
@ -22,5 +20,41 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE
CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID )
REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_OPERATION (
ID INTEGER AUTO_INCREMENT NOT NULL,
TYPE VARCHAR(50) NOT NULL,
CREATED_TIMESTAMP TIMESTAMP NOT NULL,
RECEIVED_TIMESTAMP TIMESTAMP NULL,
STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID)
);
CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION (
OPERATION_ID INTEGER NOT NULL,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION (
OPERATION_ID INTEGER NOT NULL,
ENABLED INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
-- TO:DO - Remove this INSERT sql statement.
Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android');

Loading…
Cancel
Save