forked from community/device-mgt-core
Merge branch 'master' of https://github.com/wso1/carbon-device-mgt
commit
524ce44036
@ -1,14 +1,7 @@
|
|||||||
package org.wso2.carbon.device.mgt.core.api.mgt;
|
package org.wso2.carbon.device.mgt.core.api.mgt;
|
||||||
|
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
||||||
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
|
||||||
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
|
|
||||||
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
|
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface ApplicationManagementProviderService extends ApplicationManager {
|
public interface ApplicationManagementProviderService extends ApplicationManager {
|
||||||
|
|
||||||
void updateApplicationListInstallInDevice(DeviceIdentifier deviceIdentifier, List<Application> applications)
|
|
||||||
throws ApplicationManagementException;
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ApplicationDAO {
|
||||||
|
|
||||||
|
int addApplication(Application application, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
List<Integer> addApplications(List<Application> applications, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
|
||||||
|
|
||||||
|
public interface EnrolmentDAO {
|
||||||
|
|
||||||
|
int addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
int updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
|
||||||
|
int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
boolean setStatus(int deviceId, String currentOwner, Status status,
|
||||||
|
int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
EnrolmentInfo getEnrolment(int deviceId, String currentUser, int tenantId) throws DeviceManagementDAOException;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,186 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao.impl;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO;
|
||||||
|
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.dao.util.DeviceManagementDAOUtil;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ApplicationDAOImpl implements ApplicationDAO {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(ApplicationDAOImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int addApplication(Application application, int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
int applicationId = -1;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PACKAGE_NAME, PLATFORM, CATEGORY, " +
|
||||||
|
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||||
|
Statement.RETURN_GENERATED_KEYS);
|
||||||
|
stmt.setString(1, application.getName());
|
||||||
|
stmt.setString(2, application.getPackageName());
|
||||||
|
stmt.setString(3, application.getPlatform());
|
||||||
|
stmt.setString(4, application.getCategory());
|
||||||
|
stmt.setString(5, application.getVersion());
|
||||||
|
stmt.setString(6, application.getType());
|
||||||
|
stmt.setString(7, application.getLocationUrl());
|
||||||
|
stmt.setString(8, application.getImageUrl());
|
||||||
|
stmt.setInt(9, tenantId);
|
||||||
|
stmt.execute();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
applicationId = rs.getInt(1);
|
||||||
|
}
|
||||||
|
return applicationId;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while adding application '" +
|
||||||
|
application.getName() + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Integer> addApplications(List<Application> applications,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs;
|
||||||
|
List<Integer> applicationIds = new ArrayList<Integer>();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PACKAGE_NAME, PLATFORM, CATEGORY, " +
|
||||||
|
"VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||||
|
for (Application application : applications) {
|
||||||
|
stmt.setString(1, application.getName());
|
||||||
|
stmt.setString(2, application.getPackageName());
|
||||||
|
stmt.setString(3, application.getPlatform());
|
||||||
|
stmt.setString(4, application.getCategory());
|
||||||
|
stmt.setString(5, application.getVersion());
|
||||||
|
stmt.setString(6, application.getType());
|
||||||
|
stmt.setString(7, application.getLocationUrl());
|
||||||
|
stmt.setString(8, application.getImageUrl());
|
||||||
|
stmt.setInt(9, tenantId);
|
||||||
|
stmt.addBatch();
|
||||||
|
}
|
||||||
|
stmt.executeBatch();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
while (rs.next()) {
|
||||||
|
applicationIds.add(rs.getInt(1));
|
||||||
|
}
|
||||||
|
return applicationIds;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn = null;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
int applicationId = -1;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE NAME = ? AND TENANT_ID = ?");
|
||||||
|
stmt.setString(1, applicationName);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
stmt.execute();
|
||||||
|
conn.commit();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
applicationId = rs.getInt(1);
|
||||||
|
}
|
||||||
|
return applicationId;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
try {
|
||||||
|
conn.rollback();
|
||||||
|
} catch (SQLException e1) {
|
||||||
|
log.warn("Error occurred while roll-backing the transaction", e);
|
||||||
|
}
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while removing application '" +
|
||||||
|
applicationName + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
Application application = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
stmt = conn.prepareStatement("SELECT ID, NAME, PACKAGE_NAME, CATEGORY, PLATFORM, TYPE, VERSION, IMAGE_URL, " +
|
||||||
|
"LOCATION_URL FROM DM_APPLICATION WHERE PACKAGE_NAME = ? AND TENANT_ID = ?");
|
||||||
|
stmt.setString(1, identifier);
|
||||||
|
stmt.setInt(2, tenantId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
application = this.loadApplication(rs);
|
||||||
|
}
|
||||||
|
return application;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving application application '" +
|
||||||
|
identifier + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws DeviceManagementDAOException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Application loadApplication(ResultSet rs) throws SQLException {
|
||||||
|
Application application = new Application();
|
||||||
|
application.setId(rs.getInt("ID"));
|
||||||
|
application.setName(rs.getString("NAME"));
|
||||||
|
application.setPackageName(rs.getString("PACKAGE_NAME"));
|
||||||
|
application.setCategory(rs.getString("CATEGORY"));
|
||||||
|
application.setType(rs.getString("TYPE"));
|
||||||
|
application.setVersion(rs.getString("VERSION"));
|
||||||
|
application.setImageUrl(rs.getString("IMAGE_URL"));
|
||||||
|
application.setLocationUrl(rs.getString("LOCATION_URL"));
|
||||||
|
application.setPlatform(rs.getString("PLATFORM"));
|
||||||
|
return application;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,124 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.ApplicationMappingDAO;
|
||||||
|
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.dao.util.DeviceManagementDAOUtil;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ApplicationMappingDAOImpl implements ApplicationMappingDAO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int addApplicationMapping(int deviceId, int applicationId,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
int mappingId = -1;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
|
||||||
|
"TENANT_ID) VALUES (?, ?, ?)";
|
||||||
|
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setInt(2, applicationId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.execute();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
mappingId = rs.getInt(1);
|
||||||
|
}
|
||||||
|
return mappingId;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Integer> addApplicationMappings(int deviceId, List<Integer> applicationIds,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
List<Integer> mappingIds = new ArrayList<Integer>();
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " +
|
||||||
|
"TENANT_ID) VALUES (?, ?, ?)";
|
||||||
|
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
|
||||||
|
for (int applicationId : applicationIds) {
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setInt(2, applicationId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.addBatch();
|
||||||
|
}
|
||||||
|
stmt.executeBatch();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
while (rs.next()) {
|
||||||
|
mappingIds.add(rs.getInt(1));
|
||||||
|
}
|
||||||
|
return mappingIds;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int removeApplicationMapping(int deviceId, int applicationId,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
ResultSet rs;
|
||||||
|
int mappingId = -1;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " +
|
||||||
|
"APPLICATION_ID = ? AND TENANT_ID = ?";
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setInt(2, applicationId);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.execute();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
mappingId = rs.getInt(1);
|
||||||
|
}
|
||||||
|
return mappingId;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws DeviceManagementDAOException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015, 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.dao.impl;
|
|
||||||
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
|
||||||
import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO;
|
|
||||||
|
|
||||||
public class EnrollmentDAOImpl implements EnrollmentDAO {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean addEnrollment() throws DeviceManagementDAOException {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean updateEnrollment() throws DeviceManagementDAOException {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean removeEnrollment() throws DeviceManagementDAOException {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean setStatus(int deviceId, String currentOwner, String status) throws DeviceManagementDAOException {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getStatus() throws DeviceManagementDAOException {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,216 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao.impl;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
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.dao.EnrolmentDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class EnrolmentDAOImpl implements EnrolmentDAO {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
int enrolmentId = -1;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " +
|
||||||
|
"DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)";
|
||||||
|
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setString(2, enrolmentInfo.getOwner());
|
||||||
|
stmt.setString(3, enrolmentInfo.getOwnership().toString());
|
||||||
|
stmt.setString(4, enrolmentInfo.getStatus().toString());
|
||||||
|
stmt.setTimestamp(5, new Timestamp(new Date().getTime()));
|
||||||
|
stmt.setTimestamp(6, new Timestamp(new Date().getTime()));
|
||||||
|
stmt.setInt(7, tenantId);
|
||||||
|
stmt.execute();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
enrolmentId = rs.getInt(1);
|
||||||
|
}
|
||||||
|
return enrolmentId;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
int enrolmentId = -1;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " +
|
||||||
|
"DATE_OF_ENROLMENT = ?, DATE_OF_LAST_UPDATE = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
stmt.setString(1, enrolmentInfo.getOwnership().toString());
|
||||||
|
stmt.setString(2, enrolmentInfo.getStatus().toString());
|
||||||
|
stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment()));
|
||||||
|
stmt.setTimestamp(4, new Timestamp(enrolmentInfo.getDateOfLastUpdate()));
|
||||||
|
stmt.setInt(5, deviceId);
|
||||||
|
stmt.setString(6, enrolmentInfo.getOwner());
|
||||||
|
stmt.setInt(7, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
enrolmentId = rs.getInt(1);
|
||||||
|
}
|
||||||
|
return enrolmentId;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int removeEnrollment(int deviceId, String currentOwner,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
int enrolmentId = -1;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "DELETE DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setString(2, currentOwner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
|
||||||
|
rs = stmt.getGeneratedKeys();
|
||||||
|
if (rs.next()) {
|
||||||
|
enrolmentId = rs.getInt(1);
|
||||||
|
}
|
||||||
|
return enrolmentId;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setStatus(int deviceId, String currentOwner, EnrolmentInfo.Status status,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setString(1, status.toString());
|
||||||
|
stmt.setInt(2, deviceId);
|
||||||
|
stmt.setString(3, currentOwner);
|
||||||
|
stmt.setInt(4, tenantId);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, null);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
EnrolmentInfo.Status status = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(2, deviceId);
|
||||||
|
stmt.setString(3, currentOwner);
|
||||||
|
stmt.setInt(4, tenantId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
if (rs.next()) {
|
||||||
|
status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnrolmentInfo getEnrolment(int deviceId, String currentOwner,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
Connection conn;
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
EnrolmentInfo enrolmentInfo = null;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, " +
|
||||||
|
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
|
||||||
|
stmt = conn.prepareStatement(sql);
|
||||||
|
stmt.setInt(1, deviceId);
|
||||||
|
stmt.setString(2, currentOwner);
|
||||||
|
stmt.setInt(3, tenantId);
|
||||||
|
rs = stmt.executeQuery();
|
||||||
|
if (rs.next()) {
|
||||||
|
enrolmentInfo = this.loadEnrolment(rs);
|
||||||
|
}
|
||||||
|
return enrolmentInfo;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
|
||||||
|
"information of user '" + currentOwner + "' upon device '" + deviceId + "'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection getConnection() throws DeviceManagementDAOException {
|
||||||
|
return DeviceManagementDAOFactory.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
|
||||||
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||||
|
enrolmentInfo.setOwner(rs.getString("OWNER"));
|
||||||
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
|
||||||
|
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
|
||||||
|
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
|
||||||
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
|
||||||
|
return enrolmentInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,105 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015, 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;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
|
||||||
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.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;
|
|
||||||
|
|
||||||
try {
|
|
||||||
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 initSQLScript() throws Exception {
|
|
||||||
Connection conn = null;
|
|
||||||
Statement stmt = null;
|
|
||||||
try {
|
|
||||||
conn = this.getDataSource().getConnection();
|
|
||||||
stmt = conn.createStatement();
|
|
||||||
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'");
|
|
||||||
} catch(Exception e){
|
|
||||||
log.error(e);
|
|
||||||
throw e;
|
|
||||||
}finally {
|
|
||||||
TestUtils.cleanupResources(conn, stmt, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected DataSource getDataSource() {
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,104 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015, 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;
|
|
||||||
|
|
||||||
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.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.DeviceManagementProviderServiceImpl;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
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() 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initOperationManager() {
|
|
||||||
this.operationManager = new OperationManagerImpl();
|
|
||||||
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(new DeviceManagementProviderServiceImpl());
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Test
|
|
||||||
// public void testAddOperation() throws Exception {
|
|
||||||
// CommandOperation op = new CommandOperation();
|
|
||||||
// op.setEnabled(true);
|
|
||||||
// op.setType(Operation.Type.COMMAND);
|
|
||||||
// op.setCode("OPCODE1");
|
|
||||||
//
|
|
||||||
// List<DeviceIdentifier> deviceIds = new ArrayList<DeviceIdentifier>();
|
|
||||||
// DeviceIdentifier deviceId = new DeviceIdentifier();
|
|
||||||
// deviceId.setId("4892813d-0b18-4a02-b7b1-61775257400e");
|
|
||||||
// deviceId.setType("android");
|
|
||||||
// deviceIds.add(deviceId);
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// boolean isAdded = operationManager.addOperation(op, deviceIds);
|
|
||||||
// Assert.assertTrue(isAdded);
|
|
||||||
// } catch (OperationManagementException e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
// throw new Exception(e);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void testGetOperations() {
|
|
||||||
try {
|
|
||||||
//TODO:- operationManager.getOperations is not implemented
|
|
||||||
DeviceIdentifier deviceId = new DeviceIdentifier();
|
|
||||||
deviceId.setId("4892813d-0b18-4a02-b7b1-61775257400e");
|
|
||||||
deviceId.setType("android");
|
|
||||||
List<? extends Operation> operations = operationManager.getOperations(deviceId);
|
|
||||||
Assert.assertNotNull(operations);
|
|
||||||
boolean notEmpty = operations.size() > 0;
|
|
||||||
Assert.assertTrue(notEmpty);
|
|
||||||
} catch (OperationManagementException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014, 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.common;
|
|
||||||
|
|
||||||
|
|
||||||
public enum DBTypes {
|
|
||||||
Oracle("Oracle"),H2("H2"),MySql("MySql");
|
|
||||||
|
|
||||||
String dbName ;
|
|
||||||
DBTypes(String dbStrName) {
|
|
||||||
dbName = dbStrName;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.common;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "DataSourceConfig")
|
||||||
|
public class DataSourceConfig {
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
private String driverClassName;
|
||||||
|
private String user;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@Override public String toString() {
|
||||||
|
return "DataSourceConfig[" +
|
||||||
|
" Url ='" + url + '\'' +
|
||||||
|
", DriverClassName ='" + driverClassName + '\'' +
|
||||||
|
", UserName ='" + user + '\'' +
|
||||||
|
", Password ='" + password + '\'' +
|
||||||
|
"]";
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Url", nillable = false)
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "DriverClassName", nillable = false)
|
||||||
|
public String getDriverClassName() {
|
||||||
|
return driverClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDriverClassName(String driverClassName) {
|
||||||
|
this.driverClassName = driverClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "User", nillable = false)
|
||||||
|
public String getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(String user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Password", nillable = false)
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,90 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014, 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.common;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
|
|
||||||
@XmlRootElement(name = "DBType")
|
|
||||||
public class TestDBConfiguration {
|
|
||||||
|
|
||||||
private String connectionUrl;
|
|
||||||
private String driverClass;
|
|
||||||
private String userName;
|
|
||||||
private String pwd;
|
|
||||||
|
|
||||||
@Override public String toString() {
|
|
||||||
return "TestDBConfiguration{" +
|
|
||||||
"connectionUrl='" + connectionUrl + '\'' +
|
|
||||||
", driverClass='" + driverClass + '\'' +
|
|
||||||
", userName='" + userName + '\'' +
|
|
||||||
", pwd='" + pwd + '\'' +
|
|
||||||
", dbType='" + dbType + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
|
|
||||||
private String dbType;
|
|
||||||
|
|
||||||
@XmlElement(name = "connectionurl", nillable = false)
|
|
||||||
public String getConnectionUrl() {
|
|
||||||
return connectionUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setConnectionUrl(String connectionUrl) {
|
|
||||||
this.connectionUrl = connectionUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "driverclass", nillable = false)
|
|
||||||
public String getDriverClass() {
|
|
||||||
return driverClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDriverClass(String driverClass) {
|
|
||||||
this.driverClass = driverClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "userName", nillable = false)
|
|
||||||
public String getUserName() {
|
|
||||||
return userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
|
||||||
this.userName = userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "pwd", nillable = false)
|
|
||||||
public String getPwd() {
|
|
||||||
return pwd;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPwd(String pwd) {
|
|
||||||
this.pwd = pwd;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlAttribute(name = "typeName")
|
|
||||||
public String getDbType() {
|
|
||||||
return dbType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDbType(String dbType) {
|
|
||||||
this.dbType = dbType;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014, 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.common;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@XmlRootElement(name = "DeviceMgtTestDBConfigurations")
|
|
||||||
public class TestDBConfigurations {
|
|
||||||
|
|
||||||
private List<TestDBConfiguration> dbTypesList;
|
|
||||||
|
|
||||||
@XmlElement(name = "DBType")
|
|
||||||
public List<TestDBConfiguration> getDbTypesList() {
|
|
||||||
return dbTypesList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDbTypesList(List<TestDBConfiguration> dbTypesList) {
|
|
||||||
this.dbTypesList = dbTypesList;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao;
|
||||||
|
|
||||||
|
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.app.mgt.Application;
|
||||||
|
|
||||||
|
public class ApplicationPersistenceDAOTests extends BaseDeviceManagementDAOTest {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(ApplicationPersistenceDAOTests.class);
|
||||||
|
private ApplicationDAO applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddApplication() {
|
||||||
|
/* Initializing source application bean to be tested */
|
||||||
|
Application source = new Application();
|
||||||
|
source.setName("SimpleCalculator");
|
||||||
|
source.setCategory("TestCategory");
|
||||||
|
source.setPackageName("com.simple.calculator");
|
||||||
|
source.setType("TestType");
|
||||||
|
source.setVersion("1.0.0");
|
||||||
|
source.setImageUrl("http://test.org/image/");
|
||||||
|
source.setLocationUrl("http://test.org/location/");
|
||||||
|
|
||||||
|
/* Adding dummy application to the application store */
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.openConnection();
|
||||||
|
applicationDAO.addApplication(source, -1234);
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
log.error("Error occurred while adding application '" + source.getName() + "'", e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
log.warn("Error occurred while closing the connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Retrieving the application by its name */
|
||||||
|
Application target = null;
|
||||||
|
try {
|
||||||
|
target = this.getApplication(source.getPackageName(), -1234);
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
String msg = "Error occurred while retrieving application info";
|
||||||
|
log.error(msg, e);
|
||||||
|
Assert.fail(msg, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(target, source, "Application added is not as same as what's retrieved");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Application getApplication(String packageName, int tenantId) throws DeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.openConnection();
|
||||||
|
return applicationDAO.getApplication(packageName, tenantId);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
log.warn("Error occurred while closing connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
@Override
|
||||||
|
public void init() throws Exception {
|
||||||
|
this.initDatSource();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,170 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao;
|
||||||
|
|
||||||
|
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.AfterSuite;
|
||||||
|
import org.testng.annotations.BeforeClass;
|
||||||
|
import org.testng.annotations.BeforeSuite;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.TestUtils;
|
||||||
|
import org.wso2.carbon.device.mgt.core.common.DataSourceConfig;
|
||||||
|
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.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public abstract class BaseDeviceManagementDAOTest {
|
||||||
|
|
||||||
|
private DataSource dataSource;
|
||||||
|
private static final Log log = LogFactory.getLog(BaseDeviceManagementDAOTest.class);
|
||||||
|
|
||||||
|
@BeforeSuite
|
||||||
|
public void setupDataSource() throws Exception {
|
||||||
|
this.initDatSource();
|
||||||
|
this.initSQLScript();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initDatSource() throws Exception {
|
||||||
|
this.dataSource = this.getDataSource(this.readDataSourceConfig());
|
||||||
|
DeviceManagementDAOFactory.init(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public abstract void init() throws Exception;
|
||||||
|
|
||||||
|
private DataSource getDataSource(DataSourceConfig config) {
|
||||||
|
PoolProperties properties = new PoolProperties();
|
||||||
|
properties.setUrl(config.getUrl());
|
||||||
|
properties.setDriverClassName(config.getDriverClassName());
|
||||||
|
properties.setUsername(config.getUser());
|
||||||
|
properties.setPassword(config.getPassword());
|
||||||
|
return new org.apache.tomcat.jdbc.pool.DataSource(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DataSourceConfig readDataSourceConfig() throws DeviceManagementException {
|
||||||
|
try {
|
||||||
|
File file = new File("src/test/resources/config/datasource/data-source-config.xml");
|
||||||
|
Document doc = DeviceManagerUtil.convertToDocument(file);
|
||||||
|
JAXBContext testDBContext = JAXBContext.newInstance(DataSourceConfig.class);
|
||||||
|
Unmarshaller unmarshaller = testDBContext.createUnmarshaller();
|
||||||
|
return (DataSourceConfig) unmarshaller.unmarshal(doc);
|
||||||
|
} catch (JAXBException e) {
|
||||||
|
throw new DeviceManagementException("Error occurred while reading data source configuration", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initSQLScript() throws Exception {
|
||||||
|
Connection conn = null;
|
||||||
|
Statement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getDataSource().getConnection();
|
||||||
|
stmt = conn.createStatement();
|
||||||
|
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'");
|
||||||
|
} finally {
|
||||||
|
TestUtils.cleanupResources(conn, stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterSuite
|
||||||
|
public void deleteData() {
|
||||||
|
Connection conn = null;
|
||||||
|
try {
|
||||||
|
conn = getDataSource().getConnection();
|
||||||
|
conn.setAutoCommit(false);
|
||||||
|
|
||||||
|
this.cleanupEnrolmentData(conn);
|
||||||
|
this.cleanupDeviceData(conn);
|
||||||
|
this.cleanupDeviceTypeData(conn);
|
||||||
|
|
||||||
|
conn.commit();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
try {
|
||||||
|
if (conn != null) {
|
||||||
|
conn.rollback();
|
||||||
|
}
|
||||||
|
} catch (SQLException e1) {
|
||||||
|
log.error("Error occurred while roll-backing the transaction", e);
|
||||||
|
}
|
||||||
|
String msg = "Error occurred while cleaning up temporary data generated during test execution";
|
||||||
|
log.error(msg, e);
|
||||||
|
Assert.fail(msg, e);
|
||||||
|
} finally {
|
||||||
|
if (conn != null) {
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
log.warn("Error occurred while closing the connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanupEnrolmentData(Connection conn) throws SQLException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
stmt = conn.prepareStatement("DELETE FROM DM_ENROLMENT");
|
||||||
|
stmt.execute();
|
||||||
|
} finally {
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanupDeviceData(Connection conn) throws SQLException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
stmt = conn.prepareStatement("DELETE FROM DM_DEVICE");
|
||||||
|
stmt.execute();
|
||||||
|
} finally {
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanupDeviceTypeData(Connection conn) throws SQLException {
|
||||||
|
PreparedStatement stmt = null;
|
||||||
|
try {
|
||||||
|
stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_TYPE");
|
||||||
|
stmt.execute();
|
||||||
|
} finally {
|
||||||
|
if (stmt != null) {
|
||||||
|
stmt.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataSource getDataSource() {
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.dao;
|
||||||
|
|
||||||
|
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.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class EnrolmentPersistenceDAOTests extends BaseDeviceManagementDAOTest {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(EnrolmentPersistenceDAOTests.class);
|
||||||
|
private EnrolmentDAO enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddEnrolment() {
|
||||||
|
int deviceId = 1234;
|
||||||
|
String owner = "admin";
|
||||||
|
|
||||||
|
/* Initializing source enrolment configuration bean to be tested */
|
||||||
|
EnrolmentInfo source =
|
||||||
|
new EnrolmentInfo(null, owner, EnrolmentInfo.OwnerShip.BYOD,
|
||||||
|
EnrolmentInfo.Status.CREATED);
|
||||||
|
|
||||||
|
/* Adding dummy enrolment configuration to the device management metadata store */
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.openConnection();
|
||||||
|
enrolmentDAO.addEnrollment(deviceId, source, -1234);
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
log.error("Error occurred while adding enrollment", e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
log.warn("Error occurred while closing the connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Retrieving the enrolment associated with the given deviceId and owner */
|
||||||
|
EnrolmentInfo target = null;
|
||||||
|
try {
|
||||||
|
target = this.getEnrolmentConfig(deviceId, owner, -1234);
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
String msg = "Error occurred while retrieving application info";
|
||||||
|
log.error(msg, e);
|
||||||
|
Assert.fail(msg, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(target, source, "Enrolment configuration added is not as same as what's retrieved");
|
||||||
|
}
|
||||||
|
|
||||||
|
private EnrolmentInfo getEnrolmentConfig(int deviceId, String currentOwner,
|
||||||
|
int tenantId) throws DeviceManagementDAOException {
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.openConnection();
|
||||||
|
return enrolmentDAO.getEnrolment(deviceId, currentOwner, tenantId);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
log.warn("Error occurred while closing connection", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
@Override
|
||||||
|
public void init() throws Exception {
|
||||||
|
this.initDatSource();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015, 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.policy.mgt.core;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
||||||
|
import org.testng.annotations.BeforeClass;
|
||||||
|
import org.testng.annotations.BeforeSuite;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
|
||||||
|
import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig;
|
||||||
|
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
|
||||||
|
|
||||||
|
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.Statement;
|
||||||
|
|
||||||
|
public abstract class BasePolicyManagementDAOTest {
|
||||||
|
|
||||||
|
private DataSource dataSource;
|
||||||
|
private static final Log log = LogFactory.getLog(BasePolicyManagementDAOTest.class);
|
||||||
|
|
||||||
|
@BeforeSuite
|
||||||
|
public void setupDataSource() throws Exception {
|
||||||
|
this.initDatSource();
|
||||||
|
this.initSQLScript();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initDatSource() throws Exception {
|
||||||
|
this.dataSource = this.getDataSource(this.readDataSourceConfig());
|
||||||
|
DeviceManagementDAOFactory.init(dataSource);
|
||||||
|
PolicyManagementDAOFactory.init(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public abstract void init() throws Exception;
|
||||||
|
|
||||||
|
private DataSource getDataSource(DataSourceConfig config) {
|
||||||
|
PoolProperties properties = new PoolProperties();
|
||||||
|
properties.setUrl(config.getUrl());
|
||||||
|
properties.setDriverClassName(config.getDriverClassName());
|
||||||
|
properties.setUsername(config.getUser());
|
||||||
|
properties.setPassword(config.getPassword());
|
||||||
|
return new org.apache.tomcat.jdbc.pool.DataSource(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DataSourceConfig readDataSourceConfig() throws PolicyManagementException {
|
||||||
|
try {
|
||||||
|
File file = new File("src/test/resources/config/datasource/data-source-config.xml");
|
||||||
|
Document doc = PolicyManagerUtil.convertToDocument(file);
|
||||||
|
JAXBContext testDBContext = JAXBContext.newInstance(DataSourceConfig.class);
|
||||||
|
Unmarshaller unmarshaller = testDBContext.createUnmarshaller();
|
||||||
|
return (DataSourceConfig) unmarshaller.unmarshal(doc);
|
||||||
|
} catch (JAXBException e) {
|
||||||
|
throw new PolicyManagementException("Error occurred while reading data source configuration", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initSQLScript() throws Exception {
|
||||||
|
Connection conn = null;
|
||||||
|
Statement stmt = null;
|
||||||
|
try {
|
||||||
|
conn = this.getDataSource().getConnection();
|
||||||
|
stmt = conn.createStatement();
|
||||||
|
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'");
|
||||||
|
} finally {
|
||||||
|
TestUtils.cleanupResources(conn, stmt, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataSource getDataSource() {
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,29 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014, 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.policy.mgt.core.common;
|
|
||||||
|
|
||||||
|
|
||||||
public enum DBTypes {
|
|
||||||
Oracle("Oracle"),H2("H2"),MySql("MySql");
|
|
||||||
|
|
||||||
String dbName ;
|
|
||||||
DBTypes(String dbStrName) {
|
|
||||||
dbName = dbStrName;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2014, 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.policy.mgt.core.common;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "DataSourceConfig")
|
||||||
|
public class DataSourceConfig {
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
private String driverClassName;
|
||||||
|
private String user;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@Override public String toString() {
|
||||||
|
return "DataSourceConfig[" +
|
||||||
|
" Url ='" + url + '\'' +
|
||||||
|
", DriverClassName ='" + driverClassName + '\'' +
|
||||||
|
", UserName ='" + user + '\'' +
|
||||||
|
", Password ='" + password + '\'' +
|
||||||
|
"]";
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Url", nillable = false)
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "DriverClassName", nillable = false)
|
||||||
|
public String getDriverClassName() {
|
||||||
|
return driverClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDriverClassName(String driverClassName) {
|
||||||
|
this.driverClassName = driverClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "User", nillable = false)
|
||||||
|
public String getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(String user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Password", nillable = false)
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,90 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014, 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.policy.mgt.core.common;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
|
|
||||||
@XmlRootElement(name = "DBType")
|
|
||||||
public class TestDBConfiguration {
|
|
||||||
|
|
||||||
private String connectionUrl;
|
|
||||||
private String driverClass;
|
|
||||||
private String userName;
|
|
||||||
private String pwd;
|
|
||||||
|
|
||||||
@Override public String toString() {
|
|
||||||
return "TestDBConfiguration{" +
|
|
||||||
"connectionUrl='" + connectionUrl + '\'' +
|
|
||||||
", driverClass='" + driverClass + '\'' +
|
|
||||||
", userName='" + userName + '\'' +
|
|
||||||
", pwd='" + pwd + '\'' +
|
|
||||||
", dbType='" + dbType + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
|
|
||||||
private String dbType;
|
|
||||||
|
|
||||||
@XmlElement(name = "connectionurl", nillable = false)
|
|
||||||
public String getConnectionUrl() {
|
|
||||||
return connectionUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setConnectionUrl(String connectionUrl) {
|
|
||||||
this.connectionUrl = connectionUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "driverclass", nillable = false)
|
|
||||||
public String getDriverClass() {
|
|
||||||
return driverClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDriverClass(String driverClass) {
|
|
||||||
this.driverClass = driverClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "userName", nillable = false)
|
|
||||||
public String getUserName() {
|
|
||||||
return userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
|
||||||
this.userName = userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlElement(name = "pwd", nillable = false)
|
|
||||||
public String getPwd() {
|
|
||||||
return pwd;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPwd(String pwd) {
|
|
||||||
this.pwd = pwd;
|
|
||||||
}
|
|
||||||
|
|
||||||
@XmlAttribute(name = "typeName")
|
|
||||||
public String getDbType() {
|
|
||||||
return dbType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDbType(String dbType) {
|
|
||||||
this.dbType = dbType;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014, 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.policy.mgt.core.common;
|
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@XmlRootElement(name = "DeviceMgtTestDBConfigurations")
|
|
||||||
public class TestDBConfigurations {
|
|
||||||
|
|
||||||
private List<TestDBConfiguration> dbTypesList;
|
|
||||||
|
|
||||||
@XmlElement(name = "DBType")
|
|
||||||
public List<TestDBConfiguration> getDbTypesList() {
|
|
||||||
return dbTypesList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDbTypesList(List<TestDBConfiguration> dbTypesList) {
|
|
||||||
this.dbTypesList = dbTypesList;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in new issue