Added unit-tests for mobile related dao layer

revert-dabc3590
harshanL 10 years ago
parent b0d905c0ef
commit 3b1e9ebfd5

@ -93,12 +93,12 @@ public interface MobileDeviceOperationMappingDAO {
throws MobileDeviceManagementDAOException; throws MobileDeviceManagementDAOException;
/** /**
* Retrieves all the of mobile device operation mappings relavent to the given mobile device. * Retrieves all the of mobile device operation mappings relevant to the given mobile device.
* *
* @return Device operation mapping object list. * @return Device operation mapping object list.
* @throws MobileDeviceManagementDAOException * @throws MobileDeviceManagementDAOException
*/ */
List<MobileDeviceOperationMapping> getAllMobileDeviceOperationNappingsOfDevice(String deviceId) List<MobileDeviceOperationMapping> getAllMobileDeviceOperationMappingsOfDevice(String deviceId)
throws MobileDeviceManagementDAOException; throws MobileDeviceManagementDAOException;
/** /**

@ -32,10 +32,10 @@ public interface MobileFeatureDAO {
* Add a new feature to feature table. * Add a new feature to feature table.
* *
* @param mobileFeature Feature object that holds data related to the feature to be inserted. * @param mobileFeature Feature object that holds data related to the feature to be inserted.
* @return The status of the operation. If the insert was successful or not. * @return The id of inserted feature.
* @throws MobileDeviceManagementDAOException * @throws MobileDeviceManagementDAOException
*/ */
boolean addMobileFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException; int addMobileFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException;
/** /**
* Update a feature in the feature table. * Update a feature in the feature table.

@ -38,7 +38,7 @@ public interface MobileFeaturePropertyDAO {
throws MobileDeviceManagementDAOException; throws MobileDeviceManagementDAOException;
/** /**
* Update a feature property in the feature property table. * Updates a feature property in the feature property table.
* *
* @param mobileFeatureProperty Feature property object that holds data has to be updated. * @param mobileFeatureProperty Feature property object that holds data has to be updated.
* @return The status of the operation. If the update was successful or not. * @return The status of the operation. If the update was successful or not.
@ -48,7 +48,7 @@ public interface MobileFeaturePropertyDAO {
throws MobileDeviceManagementDAOException; throws MobileDeviceManagementDAOException;
/** /**
* Delete a given feature property from feature property table. * Deletes a given feature property from feature property table.
* *
* @param property Property of the feature property to be deleted. * @param property Property of the feature property to be deleted.
* @return The status of the operation. If the operationId was successful or not. * @return The status of the operation. If the operationId was successful or not.
@ -57,22 +57,33 @@ public interface MobileFeaturePropertyDAO {
boolean deleteMobileFeatureProperty(String property) throws MobileDeviceManagementDAOException; boolean deleteMobileFeatureProperty(String property) throws MobileDeviceManagementDAOException;
/** /**
* Retrieve a given feature property from feature property table. * Deletes feature properties of a feature from feature property table.
*
* @param featureId Feature-id of the feature corresponding properties should be deleted.
* @return The status of the operation. If the operationId was successful or not.
* @throws MobileDeviceManagementDAOException
*/
boolean deleteMobileFeaturePropertiesOfFeature(Integer featureId)
throws MobileDeviceManagementDAOException;
/**
* Retrieves a given feature property from feature property table.
* *
* @param property Property of the feature property to be retrieved. * @param property Property of the feature property to be retrieved.
* @return Feature property object that holds data of the feature property represented by propertyId. * @return Feature property object that holds data of the feature property represented by propertyId.
* @throws MobileDeviceManagementDAOException * @throws MobileDeviceManagementDAOException
*/ */
MobileFeatureProperty getMobileFeatureProperty(String property) throws MobileDeviceManagementDAOException; MobileFeatureProperty getMobileFeatureProperty(String property)
throws MobileDeviceManagementDAOException;
/** /**
* Retrieve a list of feature property corresponds to a feature id . * Retrieves a list of feature property corresponds to a feature id .
* *
* @param featureId feature id of the feature property to be retrieved. * @param featureId feature id of the feature property to be retrieved.
* @return Feature property object that holds data of the feature property represented by propertyId. * @return Feature property object that holds data of the feature property represented by propertyId.
* @throws MobileDeviceManagementDAOException * @throws MobileDeviceManagementDAOException
*/ */
List<MobileFeatureProperty> getFeaturePropertyOfFeature(Integer featureId) List<MobileFeatureProperty> getFeaturePropertiesOfFeature(Integer featureId)
throws MobileDeviceManagementDAOException; throws MobileDeviceManagementDAOException;
} }

@ -53,11 +53,12 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT * FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; "SELECT MOBILE_DEVICE_ID, REG_ID, IMEI, IMSI, OS_VERSION,DEVICE_MODEL, VENDOR, " +
"LATITUDE, LONGITUDE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(selectDBQuery); stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, deviceId); stmt.setString(1, deviceId);
ResultSet resultSet = stmt.executeQuery(); ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) { if (resultSet.next()) {
mobileDevice = new MobileDevice(); mobileDevice = new MobileDevice();
mobileDevice.setMobileDeviceId(resultSet.getString(1)); mobileDevice.setMobileDeviceId(resultSet.getString(1));
mobileDevice.setRegId(resultSet.getString(2)); mobileDevice.setRegId(resultSet.getString(2));
@ -68,7 +69,6 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
mobileDevice.setVendor(resultSet.getString(7)); mobileDevice.setVendor(resultSet.getString(7));
mobileDevice.setLatitude(resultSet.getString(8)); mobileDevice.setLatitude(resultSet.getString(8));
mobileDevice.setLongitude(resultSet.getString(9)); mobileDevice.setLongitude(resultSet.getString(9));
break;
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while fetching mobile device '" + String msg = "Error occurred while fetching mobile device '" +
@ -104,7 +104,7 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
stmt.setString(8, mobileDevice.getLatitude()); stmt.setString(8, mobileDevice.getLatitude());
stmt.setString(9, mobileDevice.getLongitude()); stmt.setString(9, mobileDevice.getLongitude());
int rows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
if(rows>0){ if (rows > 0) {
status = true; status = true;
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -140,7 +140,7 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
stmt.setString(8, mobileDevice.getLongitude()); stmt.setString(8, mobileDevice.getLongitude());
stmt.setString(9, mobileDevice.getMobileDeviceId()); stmt.setString(9, mobileDevice.getMobileDeviceId());
int rows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
if(rows>0){ if (rows > 0) {
status = true; status = true;
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -164,9 +164,9 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
String deleteDBQuery = String deleteDBQuery =
"DELETE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?"; "DELETE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(deleteDBQuery); stmt = conn.prepareStatement(deleteDBQuery);
stmt.setString(1,deviceId); stmt.setString(1, deviceId);
int rows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
if(rows>0){ if (rows > 0) {
status = true; status = true;
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -184,11 +184,12 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
MobileDevice mobileDevice; MobileDevice mobileDevice;
List<MobileDevice> mobileDevices=new ArrayList<MobileDevice>(); List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT * FROM MBL_DEVICE"; "SELECT MOBILE_DEVICE_ID, REG_ID, IMEI, IMSI, OS_VERSION,DEVICE_MODEL, VENDOR," +
"LATITUDE, LONGITUDE FROM MBL_DEVICE";
stmt = conn.prepareStatement(selectDBQuery); stmt = conn.prepareStatement(selectDBQuery);
ResultSet resultSet = stmt.executeQuery(); ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {

@ -249,7 +249,7 @@ public class MobileDeviceOperationMappingDAOImpl implements MobileDeviceOperatio
} }
@Override @Override
public List<MobileDeviceOperationMapping> getAllMobileDeviceOperationNappingsOfDevice( public List<MobileDeviceOperationMapping> getAllMobileDeviceOperationMappingsOfDevice(
String deviceId) String deviceId)
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
Connection conn = null; Connection conn = null;

@ -46,8 +46,8 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
} }
@Override @Override
public boolean addMobileFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { public int addMobileFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException {
boolean status = false; int status = 0;
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
@ -62,7 +62,10 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
stmt.setString(4, mobileFeature.getDeviceType()); stmt.setString(4, mobileFeature.getDeviceType());
int rows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
if (rows > 0) { if (rows > 0) {
status = true; ResultSet rs = stmt.getGeneratedKeys();
if (rs != null && rs.next()) {
status = rs.getInt(1);
}
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding feature code - '" + String msg = "Error occurred while adding feature code - '" +
@ -167,17 +170,17 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT FEATURE_ID,DEVICE_TYPE, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE CODE = ?"; "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION, DEVICE_TYPE FROM MBL_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(selectDBQuery); stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, featureCode); stmt.setString(1, featureCode);
ResultSet resultSet = stmt.executeQuery(); ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
mobileFeature = new MobileFeature(); mobileFeature = new MobileFeature();
mobileFeature.setId(resultSet.getInt(1)); mobileFeature.setId(resultSet.getInt(1));
mobileFeature.setDeviceType(resultSet.getString(2)); mobileFeature.setCode(resultSet.getString(2));
mobileFeature.setCode(resultSet.getString(3)); mobileFeature.setName(resultSet.getString(3));
mobileFeature.setName(resultSet.getString(4)); mobileFeature.setDescription(resultSet.getString(4));
mobileFeature.setDescription(resultSet.getString(5)); mobileFeature.setDeviceType(resultSet.getString(5));
break; break;
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -200,17 +203,17 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT FEATURE_ID,DEVICE_TYPE, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE FEATURE_ID = ?"; "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION, DEVICE_TYPE FROM MBL_FEATURE WHERE FEATURE_ID = ?";
stmt = conn.prepareStatement(selectDBQuery); stmt = conn.prepareStatement(selectDBQuery);
stmt.setInt(1, featureID); stmt.setInt(1, featureID);
ResultSet resultSet = stmt.executeQuery(); ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
mobileFeature = new MobileFeature(); mobileFeature = new MobileFeature();
mobileFeature.setId(resultSet.getInt(1)); mobileFeature.setId(resultSet.getInt(1));
mobileFeature.setDeviceType(resultSet.getString(2)); mobileFeature.setCode(resultSet.getString(2));
mobileFeature.setCode(resultSet.getString(3)); mobileFeature.setName(resultSet.getString(3));
mobileFeature.setName(resultSet.getString(4)); mobileFeature.setDescription(resultSet.getString(4));
mobileFeature.setDescription(resultSet.getString(5)); mobileFeature.setDeviceType(resultSet.getString(5));
break; break;
} }
} catch (SQLException e) { } catch (SQLException e) {
@ -233,16 +236,16 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT FEATURE_ID,DEVICE_TYPE, CODE, NAME, DESCRIPTION FROM MBL_FEATURE"; "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION, DEVICE_TYPE FROM MBL_FEATURE";
stmt = conn.prepareStatement(selectDBQuery); stmt = conn.prepareStatement(selectDBQuery);
ResultSet resultSet = stmt.executeQuery(); ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
mobileFeature = new MobileFeature(); mobileFeature = new MobileFeature();
mobileFeature.setId(resultSet.getInt(1)); mobileFeature.setId(resultSet.getInt(1));
mobileFeature.setDeviceType(resultSet.getString(2)); mobileFeature.setCode(resultSet.getString(2));
mobileFeature.setCode(resultSet.getString(3)); mobileFeature.setName(resultSet.getString(3));
mobileFeature.setName(resultSet.getString(4)); mobileFeature.setDescription(resultSet.getString(4));
mobileFeature.setDescription(resultSet.getString(5)); mobileFeature.setDeviceType(resultSet.getString(5));
mobileFeatures.add(mobileFeature); mobileFeatures.add(mobileFeature);
} }
return mobileFeatures; return mobileFeatures;
@ -264,17 +267,17 @@ public class MobileFeatureDAOImpl implements MobileFeatureDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT FEATURE_ID, DEVICE_TYPE, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE DEVICE_TYPE = ?"; "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION, DEVICE_TYPE FROM MBL_FEATURE WHERE DEVICE_TYPE = ?";
stmt = conn.prepareStatement(selectDBQuery); stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, deviceType); stmt.setString(1, deviceType);
ResultSet resultSet = stmt.executeQuery(); ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
mobileFeature = new MobileFeature(); mobileFeature = new MobileFeature();
mobileFeature.setId(resultSet.getInt(1)); mobileFeature.setId(resultSet.getInt(1));
mobileFeature.setDeviceType(resultSet.getString(2)); mobileFeature.setCode(resultSet.getString(2));
mobileFeature.setCode(resultSet.getString(3)); mobileFeature.setName(resultSet.getString(3));
mobileFeature.setName(resultSet.getString(4)); mobileFeature.setDescription(resultSet.getString(4));
mobileFeature.setDescription(resultSet.getString(5)); mobileFeature.setDeviceType(resultSet.getString(5));
mobileFeatures.add(mobileFeature); mobileFeatures.add(mobileFeature);
} }
return mobileFeatures; return mobileFeatures;

@ -129,6 +129,33 @@ public class MobileFeaturePropertyDAOImpl implements MobileFeaturePropertyDAO {
return status; return status;
} }
@Override
public boolean deleteMobileFeaturePropertiesOfFeature(Integer featureId)
throws MobileDeviceManagementDAOException {
boolean status = false;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String deleteDBQuery =
"DELETE FROM MBL_FEATURE_PROPERTY WHERE FEATURE_ID = ?";
stmt = conn.prepareStatement(deleteDBQuery);
stmt.setInt(1, featureId);
int rows = stmt.executeUpdate();
if (rows > 0) {
status = true;
}
} catch (SQLException e) {
String msg = "Error occurred while deleting feature properties of feature - " +
featureId;
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return status;
}
@Override @Override
public MobileFeatureProperty getMobileFeatureProperty(String property) public MobileFeatureProperty getMobileFeatureProperty(String property)
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
@ -160,7 +187,7 @@ public class MobileFeaturePropertyDAOImpl implements MobileFeaturePropertyDAO {
} }
@Override @Override
public List<MobileFeatureProperty> getFeaturePropertyOfFeature(Integer featureId) public List<MobileFeatureProperty> getFeaturePropertiesOfFeature(Integer featureId)
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;

@ -36,6 +36,7 @@ import java.sql.SQLException;
*/ */
public class MobileOperationDAOImpl implements MobileOperationDAO { public class MobileOperationDAOImpl implements MobileOperationDAO {
public static final String COLUMN_OPERATION_ID = "OPERATION_ID";
private DataSource dataSource; private DataSource dataSource;
private static final Log log = LogFactory.getLog(MobileOperationDAOImpl.class); private static final Log log = LogFactory.getLog(MobileOperationDAOImpl.class);
@ -53,7 +54,7 @@ public class MobileOperationDAOImpl implements MobileOperationDAO {
conn = this.getConnection(); conn = this.getConnection();
String createDBQuery = String createDBQuery =
"INSERT INTO MBL_OPERATION(FEATURE_CODE, CREATED_DATE) VALUES ( ?, ?)"; "INSERT INTO MBL_OPERATION(FEATURE_CODE, CREATED_DATE) VALUES ( ?, ?)";
stmt = conn.prepareStatement(createDBQuery, new String[] { "OPERATION_ID" }); stmt = conn.prepareStatement(createDBQuery, new String[] { COLUMN_OPERATION_ID });
stmt.setString(1, operation.getFeatureCode()); stmt.setString(1, operation.getFeatureCode());
stmt.setLong(2, operation.getCreatedDate()); stmt.setLong(2, operation.getCreatedDate());
int rows = stmt.executeUpdate(); int rows = stmt.executeUpdate();
@ -93,8 +94,9 @@ public class MobileOperationDAOImpl implements MobileOperationDAO {
status = true; status = true;
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while updating the MBL_OPERATION table entry with operation id - '" + String msg =
operation.getOperationId() + "'"; "Error occurred while updating the MBL_OPERATION table entry with operation id - '" +
operation.getOperationId() + "'";
log.error(msg, e); log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e); throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {

@ -82,7 +82,7 @@ public class AndroidMobileOperationManager extends AbstractMobileOperationManage
MobileOperation mobileOperation = null; MobileOperation mobileOperation = null;
try { try {
mobileDeviceOperationMappings = MobileDeviceManagementDAOFactory.getMobileDeviceOperationDAO() mobileDeviceOperationMappings = MobileDeviceManagementDAOFactory.getMobileDeviceOperationDAO()
.getAllMobileDeviceOperationNappingsOfDevice( .getAllMobileDeviceOperationMappingsOfDevice(
deviceIdentifier deviceIdentifier
.getId()); .getId());
if (mobileDeviceOperationMappings.size() > 0) { if (mobileDeviceOperationMappings.size() > 0) {
@ -172,7 +172,7 @@ public class AndroidMobileOperationManager extends AbstractMobileOperationManage
feature.setDescription(mobileFeature.getDescription()); feature.setDescription(mobileFeature.getDescription());
List<Feature.MetadataEntry> metadataEntries = new ArrayList<Feature.MetadataEntry>(); List<Feature.MetadataEntry> metadataEntries = new ArrayList<Feature.MetadataEntry>();
List<MobileFeatureProperty> properties = List<MobileFeatureProperty> properties =
featurePropertyDAO.getFeaturePropertyOfFeature(mobileFeature.getId()); featurePropertyDAO.getFeaturePropertiesOfFeature(mobileFeature.getId());
for (MobileFeatureProperty property : properties) { for (MobileFeatureProperty property : properties) {
Feature.MetadataEntry metaEntry = new Feature.MetadataEntry(); Feature.MetadataEntry metaEntry = new Feature.MetadataEntry();
metaEntry.setId(property.getFeatureID()); metaEntry.setId(property.getFeatureID());

@ -34,89 +34,105 @@ import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory; import javax.xml.validation.SchemaFactory;
import java.io.File; import java.io.File;
/**
* Class for holding unit-tests related to MobileDeviceManagementConfig class.
*/
public class MobileDeviceManagementConfigTests { public class MobileDeviceManagementConfigTests {
private static final Log log = LogFactory.getLog(MobileDeviceManagementConfigTests.class); private static final Log log = LogFactory.getLog(MobileDeviceManagementConfigTests.class);
private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_MGT_REPOSITORY = private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_MGT_REPOSITORY =
"./src/test/resources/config/malformed-mobile-config-no-mgt-repo.xml"; "./src/test/resources/config/malformed-mobile-config-no-mgt-repo.xml";
private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_DS_CONFIG = private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_DS_CONFIG =
"./src/test/resources/config/malformed-mobile-config-no-ds-config.xml"; "./src/test/resources/config/malformed-mobile-config-no-ds-config.xml";
private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_JNDI_CONFIG = private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_JNDI_CONFIG =
"./src/test/resources/config/malformed-mobile-config-no-jndi-config.xml"; "./src/test/resources/config/malformed-mobile-config-no-jndi-config.xml";
private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_APIS_CONFIG = private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_APIS_CONFIG =
"./src/test/resources/config/malformed-mobile-config-no-apis-config.xml"; "./src/test/resources/config/malformed-mobile-config-no-apis-config.xml";
private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_API_CONFIG = private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_API_CONFIG =
"./src/test/resources/config/malformed-mobile-config-no-api-config.xml"; "./src/test/resources/config/malformed-mobile-config-no-api-config.xml";
private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_API_PUBLISHER_CONFIG = private static final String MALFORMED_TEST_CONFIG_LOCATION_NO_API_PUBLISHER_CONFIG =
"./src/test/resources/config/malformed-mobile-config-no-api-publisher-config.xml"; "./src/test/resources/config/malformed-mobile-config-no-api-publisher-config.xml";
private static final String TEST_CONFIG_SCHEMA_LOCATION = private static final String TEST_CONFIG_SCHEMA_LOCATION =
"./src/test/resources/config/schema/MobileDeviceManagementConfigSchema.xsd"; "./src/test/resources/config/schema/MobileDeviceManagementConfigSchema.xsd";
private Schema schema; private Schema schema;
@BeforeClass @BeforeClass
private void initSchema() { private void initSchema() {
File deviceManagementSchemaConfig = new File(MobileDeviceManagementConfigTests.TEST_CONFIG_SCHEMA_LOCATION); File deviceManagementSchemaConfig =
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); new File(MobileDeviceManagementConfigTests.TEST_CONFIG_SCHEMA_LOCATION);
try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schema = factory.newSchema(deviceManagementSchemaConfig); try {
} catch (SAXException e) { schema = factory.newSchema(deviceManagementSchemaConfig);
Assert.fail("Invalid schema found", e); } catch (SAXException e) {
} Assert.fail("Invalid schema found", e);
} }
}
@Test()
public void testMandateManagementRepositoryElement() { @Test()
File malformedConfig = public void testMandateManagementRepositoryElement() {
new File(MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_MGT_REPOSITORY); File malformedConfig =
this.validateMalformedConfig(malformedConfig); new File(
} MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_MGT_REPOSITORY);
this.validateMalformedConfig(malformedConfig);
@Test }
public void testMandateDataSourceConfigurationElement() {
File malformedConfig = new File(MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_DS_CONFIG); @Test
this.validateMalformedConfig(malformedConfig); public void testMandateDataSourceConfigurationElement() {
} File malformedConfig = new File(
MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_DS_CONFIG);
@Test this.validateMalformedConfig(malformedConfig);
public void testMandateJndiLookupDefinitionElement() { }
File malformedConfig = new File(MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_JNDI_CONFIG);
this.validateMalformedConfig(malformedConfig); @Test
} public void testMandateJndiLookupDefinitionElement() {
File malformedConfig = new File(
@Test MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_JNDI_CONFIG);
public void testMandateAPIPublisherElement() { this.validateMalformedConfig(malformedConfig);
File malformedConfig = new File(MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_API_PUBLISHER_CONFIG); }
this.validateMalformedConfig(malformedConfig);
} @Test
public void testMandateAPIPublisherElement() {
@Test File malformedConfig = new File(
public void testMandateAPIsElement() { MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_API_PUBLISHER_CONFIG);
File malformedConfig = new File(MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_APIS_CONFIG); this.validateMalformedConfig(malformedConfig);
this.validateMalformedConfig(malformedConfig); }
}
@Test
@Test public void testMandateAPIsElement() {
public void testMandateAPIElement() { File malformedConfig = new File(
File malformedConfig = new File(MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_API_CONFIG); MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_APIS_CONFIG);
this.validateMalformedConfig(malformedConfig); this.validateMalformedConfig(malformedConfig);
} }
private void validateMalformedConfig(File malformedConfig) { @Test
try { public void testMandateAPIElement() {
JAXBContext ctx = JAXBContext.newInstance(MobileDeviceManagementConfig.class); File malformedConfig = new File(
Unmarshaller um = ctx.createUnmarshaller(); MobileDeviceManagementConfigTests.MALFORMED_TEST_CONFIG_LOCATION_NO_API_CONFIG);
um.setSchema(this.getSchema()); this.validateMalformedConfig(malformedConfig);
um.unmarshal(malformedConfig); }
Assert.assertTrue(false);
} catch (JAXBException e) { /**
log.error("Error occurred while unmarsharlling mobile device management config", e); *
Assert.assertTrue(true); * Validates a given malformed-configuration file.
} *
} */
private void validateMalformedConfig(File malformedConfig) {
private Schema getSchema() { try {
return schema; JAXBContext ctx = JAXBContext.newInstance(MobileDeviceManagementConfig.class);
} Unmarshaller um = ctx.createUnmarshaller();
um.setSchema(this.getSchema());
um.unmarshal(malformedConfig);
Assert.assertTrue(false);
} catch (JAXBException e) {
log.error("Error occurred while unmarsharlling mobile device management config", e);
Assert.assertTrue(true);
}
}
private Schema getSchema() {
return schema;
}
} }

@ -1,57 +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.mobile.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestUtils {
private static final Log log = LogFactory.getLog(TestUtils.class);
public static void cleanupResources(Connection conn, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Error occurred while closing result set", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing prepared statement", e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
log.warn("Error occurred while closing database connection", e);
}
}
}
}

@ -18,5 +18,237 @@
package org.wso2.carbon.device.mgt.mobile.impl.dao; package org.wso2.carbon.device.mgt.mobile.impl.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileDeviceDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDatabaseUtils;
import java.sql.*;
import java.util.List;
public class MobileDeviceDAOTestSuite { public class MobileDeviceDAOTestSuite {
private static final Log log = LogFactory.getLog(MobileDeviceDAOTestSuite.class);
public static final String TEST_MOBILE_DEVICE_ID = "ABCD";
public static final String TEST_MOBILE_IMEI = "2412421412";
public static final String TEST_MOBILE_IMSI = "325235235";
public static final String TEST_MOBILE_MODEL = "S5";
public static final String TEST_MOBILE_VENDOR = "samsung";
public static final String TEST_MOBILE_UPDATED_VENDOR = "sony";
public static final String TEST_MOBILE_REG_ID = "2414";
public static final String TEST_MOBILE_OS_VERSION = "5.0.0";
public static final String TEST_MOBILE_LATITUDE = "6.93N";
public static final String TEST_MOBILE_LONGITUDE = "80.60E";
private TestDBConfiguration testDBConfiguration;
private MobileDeviceDAOImpl mblDeviceDAO;
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
DBTypes dbType = DBTypes.valueOf(dbTypeStr);
testDBConfiguration = MobileDatabaseUtils.getTestDBConfiguration(dbType);
switch (dbType) {
case H2:
MobileDatabaseUtils.createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
testDataSource.setPoolProperties(properties);
mblDeviceDAO = new MobileDeviceDAOImpl(testDataSource);
default:
}
}
@Test
public void addMobileDeviceTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileDevice mobileDevice = new MobileDevice();
MobileDevice testMblDevice = new MobileDevice();
mobileDevice.setMobileDeviceId(TEST_MOBILE_DEVICE_ID);
mobileDevice.setImei(TEST_MOBILE_IMEI);
mobileDevice.setImsi(TEST_MOBILE_IMSI);
mobileDevice.setModel(TEST_MOBILE_MODEL);
mobileDevice.setVendor(TEST_MOBILE_VENDOR);
mobileDevice.setRegId(TEST_MOBILE_REG_ID);
mobileDevice.setOsVersion(TEST_MOBILE_OS_VERSION);
mobileDevice.setLatitude(TEST_MOBILE_LATITUDE);
mobileDevice.setLongitude(TEST_MOBILE_LONGITUDE);
boolean added = mblDeviceDAO.addMobileDevice(mobileDevice);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT MOBILE_DEVICE_ID, REG_ID, IMEI, IMSI, OS_VERSION,DEVICE_MODEL, VENDOR, " +
"LATITUDE, LONGITUDE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setString(1, TEST_MOBILE_DEVICE_ID);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
testMblDevice.setMobileDeviceId(resultSet.getString(1));
testMblDevice.setRegId(resultSet.getString(2));
testMblDevice.setImei(resultSet.getString(3));
testMblDevice.setImsi(resultSet.getString(4));
testMblDevice.setOsVersion(resultSet.getString(5));
testMblDevice.setModel(resultSet.getString(6));
testMblDevice.setVendor(resultSet.getString(7));
testMblDevice.setLatitude(resultSet.getString(8));
testMblDevice.setLongitude(resultSet.getString(9));
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Device data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(added, "MobileDevice has added");
Assert.assertEquals(TEST_MOBILE_DEVICE_ID, testMblDevice.getMobileDeviceId(),
"MobileDevice id has persisted ");
Assert.assertEquals(TEST_MOBILE_IMEI, testMblDevice.getImei(),
"MobileDevice IMEI has persisted ");
Assert.assertEquals(TEST_MOBILE_IMSI, testMblDevice.getImsi(),
"MobileDevice IMSI has persisted ");
Assert.assertEquals(TEST_MOBILE_LATITUDE, testMblDevice.getLatitude(),
"MobileDevice latitude has persisted ");
Assert.assertEquals(TEST_MOBILE_LONGITUDE, testMblDevice.getLongitude(),
"MobileDevice longitude has persisted ");
Assert.assertEquals(TEST_MOBILE_MODEL, testMblDevice.getModel(),
"MobileDevice model has persisted ");
Assert.assertEquals(TEST_MOBILE_OS_VERSION, testMblDevice.getOsVersion(),
"MobileDevice os-version has persisted ");
Assert.assertEquals(TEST_MOBILE_REG_ID, testMblDevice.getRegId(),
"MobileDevice reg-id has persisted ");
Assert.assertEquals(TEST_MOBILE_VENDOR, testMblDevice.getVendor(),
"MobileDevice vendor has persisted ");
}
@Test(dependsOnMethods = { "addMobileDeviceTest" })
public void getMobileDeviceTest()
throws MobileDeviceManagementDAOException {
MobileDevice testMblDevice = mblDeviceDAO.getMobileDevice(TEST_MOBILE_DEVICE_ID);
Assert.assertEquals(TEST_MOBILE_DEVICE_ID, testMblDevice.getMobileDeviceId(),
"MobileDevice id has persisted ");
Assert.assertEquals(TEST_MOBILE_IMEI, testMblDevice.getImei(),
"MobileDevice IMEI has persisted ");
Assert.assertEquals(TEST_MOBILE_IMSI, testMblDevice.getImsi(),
"MobileDevice IMSI has persisted ");
Assert.assertEquals(TEST_MOBILE_LATITUDE, testMblDevice.getLatitude(),
"MobileDevice latitude has persisted ");
Assert.assertEquals(TEST_MOBILE_LONGITUDE, testMblDevice.getLongitude(),
"MobileDevice longitude has persisted ");
Assert.assertEquals(TEST_MOBILE_MODEL, testMblDevice.getModel(),
"MobileDevice model has persisted ");
Assert.assertEquals(TEST_MOBILE_OS_VERSION, testMblDevice.getOsVersion(),
"MobileDevice os-version has persisted ");
Assert.assertEquals(TEST_MOBILE_REG_ID, testMblDevice.getRegId(),
"MobileDevice reg-id has persisted ");
Assert.assertEquals(TEST_MOBILE_VENDOR, testMblDevice.getVendor(),
"MobileDevice vendor has persisted ");
}
@Test(dependsOnMethods = { "addMobileDeviceTest" })
public void getAllMobileDevicesTest()
throws MobileDeviceManagementDAOException {
List<MobileDevice> mblDevices = mblDeviceDAO.getAllMobileDevices();
Assert.assertNotNull(mblDevices, "MobileDevice list is not null");
Assert.assertTrue(mblDevices.size() == 1, "MobileDevice list has 1 MobileDevice");
}
@Test(dependsOnMethods = { "addMobileDeviceTest", "getMobileDeviceTest",
"getAllMobileDevicesTest" })
public void updateMobileDeviceTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileDevice mobileDevice = new MobileDevice();
MobileDevice testMblDevice = new MobileDevice();
mobileDevice.setMobileDeviceId(TEST_MOBILE_DEVICE_ID);
mobileDevice.setImei(TEST_MOBILE_IMEI);
mobileDevice.setImsi(TEST_MOBILE_IMSI);
mobileDevice.setModel(TEST_MOBILE_MODEL);
mobileDevice.setVendor(TEST_MOBILE_UPDATED_VENDOR);
mobileDevice.setRegId(TEST_MOBILE_REG_ID);
mobileDevice.setOsVersion(TEST_MOBILE_OS_VERSION);
mobileDevice.setLatitude(TEST_MOBILE_LATITUDE);
mobileDevice.setLongitude(TEST_MOBILE_LONGITUDE);
boolean updated = mblDeviceDAO.updateMobileDevice(mobileDevice);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT MOBILE_DEVICE_ID, REG_ID, IMEI, IMSI, OS_VERSION,DEVICE_MODEL, VENDOR, " +
"LATITUDE, LONGITUDE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setString(1, TEST_MOBILE_DEVICE_ID);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
testMblDevice.setMobileDeviceId(resultSet.getString(1));
testMblDevice.setRegId(resultSet.getString(2));
testMblDevice.setImei(resultSet.getString(3));
testMblDevice.setImsi(resultSet.getString(4));
testMblDevice.setOsVersion(resultSet.getString(5));
testMblDevice.setModel(resultSet.getString(6));
testMblDevice.setVendor(resultSet.getString(7));
testMblDevice.setLatitude(resultSet.getString(8));
testMblDevice.setLongitude(resultSet.getString(9));
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Device data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(updated, "MobileDevice has updated ");
Assert.assertEquals(TEST_MOBILE_UPDATED_VENDOR, testMblDevice.getVendor(),
"MobileDevice vendor has updated ");
}
@Test(dependsOnMethods = { "addMobileDeviceTest", "getMobileDeviceTest",
"getAllMobileDevicesTest", "updateMobileDeviceTest" })
public void deleteMobileDeviceTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean deleted = mblDeviceDAO.deleteMobileDevice(TEST_MOBILE_DEVICE_ID);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT MOBILE_DEVICE_ID, REG_ID, IMEI, IMSI, OS_VERSION,DEVICE_MODEL, VENDOR, " +
"LATITUDE, LONGITUDE FROM MBL_DEVICE WHERE MOBILE_DEVICE_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setString(1, TEST_MOBILE_DEVICE_ID);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
deleted = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Device data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(deleted, "MobileDevice has deleted ");
}
} }

@ -1,22 +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.mobile.impl.dao;
public class MobileDeviceOperationDAOTestSuite {
}

@ -0,0 +1,429 @@
/*
* 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.mobile.impl.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileDeviceDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileDeviceOperationMappingDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileOperationDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDeviceOperationMapping;
import org.wso2.carbon.device.mgt.mobile.dto.MobileOperation;
import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDatabaseUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MobileDeviceOperationMappingDAOTestSuite {
private static final Log log =
LogFactory.getLog(MobileDeviceOperationMappingDAOTestSuite.class);
public static final String TEST_MOBILE_DEVICE_ID = "ABCD";
public static final String TEST_MOBILE_IMEI = "2412421412";
public static final String TEST_MOBILE_IMSI = "325235235";
public static final String TEST_MOBILE_MODEL = "S5";
public static final String TEST_MOBILE_VENDOR = "samsung";
public static final String TEST_MOBILE_REG_ID = "2414";
public static final String TEST_MOBILE_OS_VERSION = "5.0.0";
public static final String TEST_MOBILE_LATITUDE = "6.93N";
public static final String TEST_MOBILE_LONGITUDE = "80.60E";
public static final String TEST_MBL_OPR_FEATURE_CODE1 = "LOCK";
public static final String TEST_MBL_OPR_FEATURE_CODE2 = "WIPE";
public static final long TEST_MBL_OPR_CREATED_DATE = new java.util.Date().getTime();
private TestDBConfiguration testDBConfiguration;
private MobileDeviceDAOImpl mblDeviceDAO;
private MobileOperationDAOImpl mblOperationDAO;
private MobileDeviceOperationMappingDAOImpl mblDeviceOperationMappingDAO;
private int mblOperationId1;
private int mblOperationId2;
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
DBTypes dbType = DBTypes.valueOf(dbTypeStr);
testDBConfiguration = MobileDatabaseUtils.getTestDBConfiguration(dbType);
switch (dbType) {
case H2:
MobileDatabaseUtils.createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
testDataSource.setPoolProperties(properties);
mblDeviceDAO = new MobileDeviceDAOImpl(testDataSource);
mblOperationDAO = new MobileOperationDAOImpl(testDataSource);
mblDeviceOperationMappingDAO =
new MobileDeviceOperationMappingDAOImpl(testDataSource);
default:
}
}
@Test
public void addMobileDeviceOperationMappingTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
List<MobileDeviceOperationMapping> mblOperations =
new ArrayList<MobileDeviceOperationMapping>();
MobileDeviceOperationMapping mblDvOperationMapping =
new MobileDeviceOperationMapping();
//Add a new Device to the database
MobileDevice mobileDevice = new MobileDevice();
mobileDevice.setMobileDeviceId(TEST_MOBILE_DEVICE_ID);
mobileDevice.setImei(TEST_MOBILE_IMEI);
mobileDevice.setImsi(TEST_MOBILE_IMSI);
mobileDevice.setModel(TEST_MOBILE_MODEL);
mobileDevice.setVendor(TEST_MOBILE_VENDOR);
mobileDevice.setRegId(TEST_MOBILE_REG_ID);
mobileDevice.setOsVersion(TEST_MOBILE_OS_VERSION);
mobileDevice.setLatitude(TEST_MOBILE_LATITUDE);
mobileDevice.setLongitude(TEST_MOBILE_LONGITUDE);
mblDeviceDAO.addMobileDevice(mobileDevice);
//Add an Operation to the db
MobileOperation mblOperation = new MobileOperation();
mblOperation.setFeatureCode(TEST_MBL_OPR_FEATURE_CODE1);
mblOperation.setCreatedDate(TEST_MBL_OPR_CREATED_DATE);
mblOperationId1 = mblOperationDAO.addMobileOperation(mblOperation);
//Add a new Operation 2 to the db
mblOperation.setFeatureCode(TEST_MBL_OPR_FEATURE_CODE2);
mblOperation.setCreatedDate(TEST_MBL_OPR_CREATED_DATE);
mblOperationId2 = mblOperationDAO.addMobileOperation(mblOperation);
//Add a device-operation mapping 1 to the table
mblDvOperationMapping.setDeviceId(TEST_MOBILE_DEVICE_ID);
mblDvOperationMapping.setOperationId(mblOperationId1);
mblDvOperationMapping.setStatus(MobileDeviceOperationMapping.Status.NEW);
boolean status1 =
mblDeviceOperationMappingDAO.addMobileDeviceOperationMapping(mblDvOperationMapping);
//Add a device-operation mapping 2 to the table
mblDvOperationMapping.setDeviceId(TEST_MOBILE_DEVICE_ID);
mblDvOperationMapping.setOperationId(mblOperationId2);
mblDvOperationMapping.setStatus(MobileDeviceOperationMapping.Status.NEW);
boolean status2 =
mblDeviceOperationMappingDAO.addMobileDeviceOperationMapping(mblDvOperationMapping);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT DEVICE_ID, OPERATION_ID, SENT_DATE, RECEIVED_DATE, STATUS FROM " +
"MBL_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setString(1, TEST_MOBILE_DEVICE_ID);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
mblDvOperationMapping = new MobileDeviceOperationMapping();
mblDvOperationMapping.setDeviceId(resultSet.getString(1));
mblDvOperationMapping.setOperationId(resultSet.getInt(2));
mblDvOperationMapping.setSentDate(resultSet.getLong(3));
mblDvOperationMapping.setReceivedDate(resultSet.getLong(4));
mblDvOperationMapping.setStatus(resultSet.getString(5));
mblOperations.add(mblDvOperationMapping);
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Operation Mappings data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status1, "MobileOperationMapping1 has added ");
Assert.assertTrue(status2, "MobileOperationMapping2 has added ");
Assert.assertTrue(mblOperations.size() == 2, "MobileOperationMappings have retrieved ");
for (MobileDeviceOperationMapping mapping : mblOperations) {
Assert.assertEquals(TEST_MOBILE_DEVICE_ID, mapping.getDeviceId(),
"MobileOperationMapping device id has persisted ");
Assert.assertEquals(MobileDeviceOperationMapping.Status.NEW, mapping.getStatus(),
"MobileOperationMapping status has persisted ");
Assert.assertTrue(mapping.getOperationId() > 0,
"MobileOperationMapping operation-id has persisted ");
Assert.assertTrue(mapping.getSentDate() == 0,
"MobileOperationMapping sent-date has fetched ");
Assert.assertTrue(mapping.getReceivedDate() == 0,
"MobileOperationMapping received-date has fetched ");
}
}
@Test(dependsOnMethods = { "addMobileDeviceOperationMappingTest" })
public void getMobileDeviceOperationMappingTest() throws MobileDeviceManagementDAOException {
MobileDeviceOperationMapping mblOperationMapping =
mblDeviceOperationMappingDAO.getMobileDeviceOperationMapping(
TEST_MOBILE_DEVICE_ID, mblOperationId1);
Assert.assertNotNull(mblOperationMapping, "MobileOperationMapping 1 has fetched ");
Assert.assertEquals(TEST_MOBILE_DEVICE_ID, mblOperationMapping.getDeviceId(),
"MobileOperationMapping device id has fetched ");
Assert.assertEquals(mblOperationId1, mblOperationMapping.getOperationId(),
"MobileOperationMapping device id has fetched ");
Assert.assertEquals(MobileDeviceOperationMapping.Status.NEW,
mblOperationMapping.getStatus(),
"MobileOperationMapping status has fetched ");
Assert.assertTrue(mblOperationMapping.getSentDate() == 0,
"MobileOperationMapping sent-date has fetched ");
Assert.assertTrue(mblOperationMapping.getReceivedDate() == 0,
"MobileOperationMapping received-date has fetched ");
}
@Test(dependsOnMethods = { "addMobileDeviceOperationMappingTest" })
public void getAllMobileDeviceOperationMappingsOfDeviceTest()
throws MobileDeviceManagementDAOException {
List<MobileDeviceOperationMapping> mblOperationMappings =
mblDeviceOperationMappingDAO.getAllMobileDeviceOperationMappingsOfDevice(
TEST_MOBILE_DEVICE_ID);
Assert.assertNotNull(mblOperationMappings, "MobileOperationMappings have fetched ");
Assert.assertTrue(mblOperationMappings.size() == 2,
"All MobileOperationMappings have fetched ");
for (MobileDeviceOperationMapping mblOperationMapping : mblOperationMappings) {
Assert.assertEquals(TEST_MOBILE_DEVICE_ID, mblOperationMapping.getDeviceId(),
"MobileOperationMapping device id has fetched ");
Assert.assertTrue(mblOperationMapping.getOperationId() > 0,
"MobileOperationMapping operation-id has fetched ");
Assert.assertEquals(MobileDeviceOperationMapping.Status.NEW,
mblOperationMapping.getStatus(),
"MobileOperationMapping status has fetched ");
Assert.assertTrue(mblOperationMapping.getSentDate() == 0,
"MobileOperationMapping sent-date has fetched ");
Assert.assertTrue(mblOperationMapping.getReceivedDate() == 0,
"MobileOperationMapping received-date has fetched ");
}
}
@Test(dependsOnMethods = { "addMobileDeviceOperationMappingTest",
"getAllMobileDeviceOperationMappingsOfDeviceTest" })
public void updateMobileDeviceOperationMappingToInProgressTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileDeviceOperationMapping mblOperationMapping = null;
//Update device-operation to In-Progress state
boolean status =
mblDeviceOperationMappingDAO.updateMobileDeviceOperationMappingToInProgress(
TEST_MOBILE_DEVICE_ID, mblOperationId1);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT DEVICE_ID, OPERATION_ID, SENT_DATE, STATUS FROM " +
"MBL_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setString(1, TEST_MOBILE_DEVICE_ID);
preparedStatement.setInt(2, mblOperationId1);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
mblOperationMapping = new MobileDeviceOperationMapping();
mblOperationMapping.setDeviceId(resultSet.getString(1));
mblOperationMapping.setOperationId(resultSet.getInt(2));
mblOperationMapping.setSentDate(resultSet.getLong(3));
mblOperationMapping.setStatus(resultSet.getString(4));
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Operation Mappings data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileOperationMapping 1 has updated ");
Assert.assertNotNull(mblOperationMapping, "MobileOperationMappings have fetched ");
Assert.assertEquals(MobileDeviceOperationMapping.Status.INPROGRESS,
mblOperationMapping.getStatus(),
"MobileOperationMapping status has updated ");
Assert.assertTrue(mblOperationMapping.getSentDate() > 0,
"MobileOperationMapping sent-date has updated ");
}
@Test(dependsOnMethods = { "addMobileDeviceOperationMappingTest",
"getAllMobileDeviceOperationMappingsOfDeviceTest" })
public void updateMobileDeviceOperationMappingToCompletedTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileDeviceOperationMapping mblOperationMapping = null;
//Update device-operation to Completed state
boolean status =
mblDeviceOperationMappingDAO.updateMobileDeviceOperationMappingToCompleted(
TEST_MOBILE_DEVICE_ID, mblOperationId1);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT DEVICE_ID, OPERATION_ID, RECEIVED_DATE, STATUS FROM " +
"MBL_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setString(1, TEST_MOBILE_DEVICE_ID);
preparedStatement.setInt(2, mblOperationId1);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
mblOperationMapping = new MobileDeviceOperationMapping();
mblOperationMapping.setDeviceId(resultSet.getString(1));
mblOperationMapping.setOperationId(resultSet.getInt(2));
mblOperationMapping.setReceivedDate(resultSet.getLong(3));
mblOperationMapping.setStatus(resultSet.getString(4));
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Operation Mappings data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileOperationMapping 1 has updated ");
Assert.assertNotNull(mblOperationMapping, "MobileOperationMappings have fetched ");
Assert.assertEquals(MobileDeviceOperationMapping.Status.COMPLETED,
mblOperationMapping.getStatus(),
"MobileOperationMapping status has updated ");
Assert.assertTrue(mblOperationMapping.getReceivedDate() > 0,
"MobileOperationMapping received-date has updated ");
}
@Test(dependsOnMethods = { "addMobileDeviceOperationMappingTest",
"getAllMobileDeviceOperationMappingsOfDeviceTest",
"updateMobileDeviceOperationMappingToInProgressTest",
"updateMobileDeviceOperationMappingToCompletedTest" })
public void updateMobileDeviceOperationMappingTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileDeviceOperationMapping mblOperationMapping = new MobileDeviceOperationMapping();
long currentTime = new java.util.Date().getTime();
//Update device-operation mapping 1
mblOperationMapping.setDeviceId(TEST_MOBILE_DEVICE_ID);
mblOperationMapping.setOperationId(mblOperationId1);
mblOperationMapping.setStatus(MobileDeviceOperationMapping.Status.INPROGRESS);
mblOperationMapping.setSentDate(currentTime);
mblOperationMapping.setReceivedDate(currentTime);
boolean status =
mblDeviceOperationMappingDAO.updateMobileDeviceOperationMapping(
mblOperationMapping);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT DEVICE_ID, OPERATION_ID, RECEIVED_DATE, SENT_DATE, STATUS FROM " +
"MBL_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setString(1, TEST_MOBILE_DEVICE_ID);
preparedStatement.setInt(2, mblOperationId1);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
mblOperationMapping = new MobileDeviceOperationMapping();
mblOperationMapping.setDeviceId(resultSet.getString(1));
mblOperationMapping.setOperationId(resultSet.getInt(2));
mblOperationMapping.setReceivedDate(resultSet.getLong(3));
mblOperationMapping.setSentDate(resultSet.getLong(4));
mblOperationMapping.setStatus(resultSet.getString(5));
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Operation Mappings data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileOperationMapping 1 has updated ");
Assert.assertNotNull(mblOperationMapping, "MobileOperationMappings have fetched ");
Assert.assertEquals(MobileDeviceOperationMapping.Status.INPROGRESS,
mblOperationMapping.getStatus(),
"MobileOperationMapping status has updated ");
Assert.assertTrue(mblOperationMapping.getReceivedDate() == currentTime,
"MobileOperationMapping received-date has updated ");
Assert.assertTrue(mblOperationMapping.getSentDate() == currentTime,
"MobileOperationMapping sent-date has updated ");
}
@Test(dependsOnMethods = { "addMobileDeviceOperationMappingTest",
"getAllMobileDeviceOperationMappingsOfDeviceTest",
"updateMobileDeviceOperationMappingToInProgressTest" })
public void getAllPendingOperationMappingsOfMobileDeviceTest()
throws MobileDeviceManagementDAOException {
List<MobileDeviceOperationMapping> mblOperationMappings =
mblDeviceOperationMappingDAO.getAllPendingOperationMappingsOfMobileDevice(
TEST_MOBILE_DEVICE_ID);
Assert.assertNotNull(mblOperationMappings, "Pending MobileOperationMappings have fetched ");
Assert.assertTrue(mblOperationMappings.size() == 1,
"All MobileOperationMappings have fetched ");
for (MobileDeviceOperationMapping mblOperationMapping : mblOperationMappings) {
Assert.assertEquals(TEST_MOBILE_DEVICE_ID, mblOperationMapping.getDeviceId(),
"MobileOperationMapping device id has fetched ");
Assert.assertTrue(mblOperationMapping.getOperationId() == mblOperationId2,
"MobileOperationMapping operation-id has fetched ");
Assert.assertEquals(MobileDeviceOperationMapping.Status.NEW,
mblOperationMapping.getStatus(),
"MobileOperationMapping status has fetched ");
Assert.assertTrue(mblOperationMapping.getSentDate() == 0,
"MobileOperationMapping sent-date has fetched ");
Assert.assertTrue(mblOperationMapping.getReceivedDate() == 0,
"MobileOperationMapping received-date has fetched ");
}
}
@Test(dependsOnMethods = { "addMobileDeviceOperationMappingTest",
"getAllMobileDeviceOperationMappingsOfDeviceTest",
"updateMobileDeviceOperationMappingToInProgressTest",
"updateMobileDeviceOperationMappingToCompletedTest",
"updateMobileDeviceOperationMappingTest" })
public void deleteMobileDeviceOperationMappingTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean status =
mblDeviceOperationMappingDAO.deleteMobileDeviceOperationMapping(
TEST_MOBILE_DEVICE_ID, mblOperationId1);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT DEVICE_ID, OPERATION_ID, RECEIVED_DATE, SENT_DATE, STATUS FROM " +
"MBL_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setString(1, TEST_MOBILE_DEVICE_ID);
preparedStatement.setInt(2, mblOperationId1);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileFeatureProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileDeviceOperationMapping 1 has deleted ");
}
}

@ -20,272 +20,238 @@ package org.wso2.carbon.device.mgt.mobile.impl.dao;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties; import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters; import org.testng.annotations.Parameters;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileFeatureDAOImpl; import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileFeatureDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature; import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
import org.wso2.carbon.device.mgt.mobile.impl.TestUtils;
import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes; import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration; import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfigurations; import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDatabaseUtils;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
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.*; import java.sql.*;
import java.util.Iterator;
import java.util.List; import java.util.List;
/**
* Class for holding the units tests related to MobileFeatureDAO class.
*/
public class MobileFeatureDAOTestSuite { public class MobileFeatureDAOTestSuite {
private static final Log log = LogFactory.getLog(MobileFeatureDAOTestSuite.class); private static final Log log = LogFactory.getLog(MobileFeatureDAOTestSuite.class);
public static final String MBL_FEATURE_NAME = "Camera"; public static final String MBL_FEATURE_NAME = "Camera";
private static final String MBL_FEATURE_CODE = "500A"; private static final String MBL_FEATURE_CODE = "500A";
public static final String MBL_FEATURE_DESCRIPTION = "Camera enable or disable"; public static final String MBL_FEATURE_DESCRIPTION = "Camera enable or disable";
public static final String MBL_FEATURE_UPDATED_CODE = "501B"; public static final String MBL_FEATURE_DEVICE_TYPE = "Android";
private TestDBConfiguration testDBConfiguration; public static final String MBL_FEATURE_UPDATED_CODE = "501B";
private Connection conn = null; private TestDBConfiguration testDBConfiguration;
private Statement stmt = null; private MobileFeatureDAOImpl mblFeatureDAO;
private MobileFeatureDAOImpl mblFeatureDAO; private int mblFeatureId;
private int mblFeatureId;
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
DBTypes dbType = DBTypes.valueOf(dbTypeStr);
testDBConfiguration = getTestDBConfiguration(dbType);
switch (dbType) {
case H2:
createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
mblFeatureDAO = new MobileFeatureDAOImpl(testDataSource);
default:
}
}
private TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws
MobileDeviceManagementDAOException,
DeviceManagementException {
File deviceMgtConfig = new File("src/test/resources/testdbconfig.xml");
Document doc = null;
testDBConfiguration = null;
TestDBConfigurations testDBConfigurations = null;
doc = MobileDeviceManagementUtil.convertToDocument(deviceMgtConfig);
JAXBContext testDBContext = null;
try {
testDBContext = JAXBContext.newInstance(TestDBConfigurations.class);
Unmarshaller unmarshaller = testDBContext.createUnmarshaller();
testDBConfigurations = (TestDBConfigurations) unmarshaller.unmarshal(doc);
} catch (JAXBException e) {
throw new MobileDeviceManagementDAOException("Error parsing test db configurations", e);
}
Iterator<TestDBConfiguration> itrDBConfigs =
testDBConfigurations.getDbTypesList().iterator();
while (itrDBConfigs.hasNext()) {
testDBConfiguration = itrDBConfigs.next();
if (testDBConfiguration.getType().equals(dbType.toString())) {
break;
}
}
return testDBConfiguration; @BeforeClass
} @Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
private void createH2DB(TestDBConfiguration testDBConf) throws Exception { DBTypes dbType = DBTypes.valueOf(dbTypeStr);
try { testDBConfiguration = MobileDatabaseUtils.getTestDBConfiguration(dbType);
Class.forName(testDBConf.getDriverClassName());
conn = DriverManager.getConnection(testDBConf.getConnectionURL());
stmt = conn.createStatement();
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'");
} finally {
stmt.close();
conn.close();
TestUtils.cleanupResources(conn, stmt, null);
}
}
@Test switch (dbType) {
public void addMobileFeatureTest() case H2:
throws MobileDeviceManagementDAOException { MobileDatabaseUtils.createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
testDataSource.setPoolProperties(properties);
mblFeatureDAO = new MobileFeatureDAOImpl(testDataSource);
default:
}
}
MobileFeature mobileFeature = new MobileFeature(); @Test
MobileFeature testMblFeature = new MobileFeature(); public void addMobileFeatureTest()
mobileFeature.setCode(MBL_FEATURE_CODE); throws MobileDeviceManagementDAOException {
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
boolean added = mblFeatureDAO.addMobileFeature(mobileFeature);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
stmt = conn.createStatement();
ResultSet resultSet = stmt
.executeQuery(
"SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE CODE = '500A'");
while (resultSet.next()) {
testMblFeature.setId(resultSet.getInt(1));
testMblFeature.setCode(resultSet.getString(2));
testMblFeature.setName(resultSet.getString(3));
testMblFeature.setDescription(resultSet.getString(4));
}
conn.close();
} catch (SQLException e) {
log.error("Error in retrieving Mobile Feature data ", e);
throw new MobileDeviceManagementDAOException("Error in retrieving Mobile Feature data ",
e);
} finally {
TestUtils.cleanupResources(conn, stmt, null);
}
mblFeatureId = testMblFeature.getId();
Assert.assertTrue(added, "MobileFeature is added");
Assert.assertEquals(MBL_FEATURE_CODE, testMblFeature.getCode(),
"MobileFeature code has persisted successfully");
Assert.assertEquals(MBL_FEATURE_NAME, testMblFeature.getName(),
"MobileFeature name has persisted successfully");
Assert.assertEquals(MBL_FEATURE_DESCRIPTION, testMblFeature.getDescription(),
"MobileFeature description has persisted successfully");
}
@Test(dependsOnMethods = {"addMobileFeatureTest"}) Connection conn = null;
public void getMobileFeatureByCodeTest() PreparedStatement preparedStatement = null;
throws MobileDeviceManagementDAOException { MobileFeature mobileFeature = new MobileFeature();
MobileFeature testMblFeature = new MobileFeature();
mobileFeature.setCode(MBL_FEATURE_CODE);
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setDeviceType(MBL_FEATURE_DEVICE_TYPE);
int id = mblFeatureDAO.addMobileFeature(mobileFeature);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT FEATURE_ID, CODE, NAME, DESCRIPTION, DEVICE_TYPE FROM MBL_FEATURE WHERE CODE = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, MBL_FEATURE_CODE);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
testMblFeature.setId(resultSet.getInt(1));
testMblFeature.setCode(resultSet.getString(2));
testMblFeature.setName(resultSet.getString(3));
testMblFeature.setDescription(resultSet.getString(4));
testMblFeature.setDeviceType(resultSet.getString(5));
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Feature data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
mblFeatureId = testMblFeature.getId();
Assert.assertTrue(id > 0, "MobileFeature has added ");
Assert.assertEquals(MBL_FEATURE_CODE, testMblFeature.getCode(),
"MobileFeature code has persisted ");
Assert.assertEquals(MBL_FEATURE_NAME, testMblFeature.getName(),
"MobileFeature name has persisted ");
Assert.assertEquals(MBL_FEATURE_DESCRIPTION, testMblFeature.getDescription(),
"MobileFeature description has persisted ");
Assert.assertEquals(MBL_FEATURE_DEVICE_TYPE, testMblFeature.getDeviceType(),
"MobileFeature device-type has persisted ");
}
MobileFeature mobileFeature = mblFeatureDAO.getMobileFeatureByCode(MBL_FEATURE_CODE); @Test(dependsOnMethods = { "addMobileFeatureTest" })
Assert.assertEquals(MBL_FEATURE_CODE, mobileFeature.getCode(), public void getMobileFeatureByCodeTest()
"MobileFeature code has retrieved successfully"); throws MobileDeviceManagementDAOException {
Assert.assertEquals(MBL_FEATURE_NAME, mobileFeature.getName(),
"MobileFeature name has retrieved successfully");
Assert.assertEquals(MBL_FEATURE_DESCRIPTION, mobileFeature.getDescription(),
"MobileFeature description has retrieved successfully");
}
@Test(dependsOnMethods = {"addMobileFeatureTest"}) MobileFeature mobileFeature = mblFeatureDAO.getMobileFeatureByCode(MBL_FEATURE_CODE);
public void deleteMobileFeatureByCodeTest() Assert.assertEquals(MBL_FEATURE_CODE, mobileFeature.getCode(),
throws MobileDeviceManagementDAOException { "MobileFeature code has retrieved ");
boolean status = mblFeatureDAO.deleteMobileFeatureByCode(MBL_FEATURE_CODE); Assert.assertEquals(MBL_FEATURE_NAME, mobileFeature.getName(),
try { "MobileFeature name has retrieved ");
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL()); Assert.assertEquals(MBL_FEATURE_DESCRIPTION, mobileFeature.getDescription(),
stmt = conn.createStatement(); "MobileFeature description has retrieved ");
ResultSet resultSet = stmt }
.executeQuery(
"SELECT FEATURE_ID, CODE FROM MBL_FEATURE WHERE CODE = '500A'");
while (resultSet.next()) {
status = false;
}
conn.close();
} catch (SQLException e) {
log.error("Error in deleting Mobile Feature data ", e);
throw new MobileDeviceManagementDAOException("Error in deleting Mobile Feature data ",
e);
} finally {
TestUtils.cleanupResources(conn, stmt, null);
}
Assert.assertTrue(status, "MobileFeature has deleted successfully");
}
@Test(dependsOnMethods = {"addMobileFeatureTest"}) @Test(dependsOnMethods = { "addMobileFeatureTest" })
public void getMobileFeatureByIdTest() public void getMobileFeatureByIdTest()
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
MobileFeature mobileFeature = mblFeatureDAO.getMobileFeatureById(mblFeatureId); MobileFeature mobileFeature = mblFeatureDAO.getMobileFeatureById(mblFeatureId);
Assert.assertEquals(MBL_FEATURE_CODE, mobileFeature.getCode(), Assert.assertEquals(MBL_FEATURE_CODE, mobileFeature.getCode(),
"MobileFeature code has retrieved successfully"); "MobileFeature code has retrieved ");
Assert.assertEquals(MBL_FEATURE_NAME, mobileFeature.getName(), Assert.assertEquals(MBL_FEATURE_NAME, mobileFeature.getName(),
"MobileFeature name has retrieved successfully"); "MobileFeature name has retrieved ");
Assert.assertEquals(MBL_FEATURE_DESCRIPTION, mobileFeature.getDescription(), Assert.assertEquals(MBL_FEATURE_DESCRIPTION, mobileFeature.getDescription(),
"MobileFeature description has retrieved successfully"); "MobileFeature description has retrieved ");
} }
@Test(dependsOnMethods = {"addMobileFeatureTest"}) @Test(dependsOnMethods = { "addMobileFeatureTest" })
public void getAllMobileFeaturesTest() public void getAllMobileFeaturesTest()
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
List<MobileFeature> mobileFeatures = mblFeatureDAO.getAllMobileFeatures(); List<MobileFeature> mobileFeatures = mblFeatureDAO.getAllMobileFeatures();
Assert.assertNotNull(mobileFeatures, "MobileFeature list is not null"); Assert.assertNotNull(mobileFeatures, "MobileFeature list is not null");
Assert.assertTrue(mobileFeatures.size() > 0, "MobileFeature list has 1 MobileFeature"); Assert.assertTrue(mobileFeatures.size() > 0, "MobileFeature list has 1 MobileFeature");
} }
@Test(dependsOnMethods = {"addMobileFeatureTest"}) @Test(dependsOnMethods = { "addMobileFeatureTest", "getMobileFeatureByCodeTest",
public void deleteMobileFeatureByIdTest() "getMobileFeatureByIdTest", "getAllMobileFeaturesTest" })
throws MobileDeviceManagementDAOException { public void updateMobileFeatureTest()
Connection conn = null; throws MobileDeviceManagementDAOException {
Statement stmt = null;
boolean status = mblFeatureDAO.deleteMobileFeatureById(mblFeatureId); Connection conn = null;
try { PreparedStatement stmt = null;
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
ResultSet resultSet = stmt
.executeQuery(
"SELECT FEATURE_ID, CODE FROM MBL_FEATURE WHERE FEATURE_ID = " +
mblFeatureId);
while (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
log.error("Error in deleting Mobile Feature data ", e);
throw new MobileDeviceManagementDAOException("Error in deleting Mobile Feature data ",
e);
} finally {
TestUtils.cleanupResources(conn, stmt, null);
}
Assert.assertTrue(status, "MobileFeature has deleted successfully");
}
@Test(dependsOnMethods = {"deleteMobileFeatureByCodeTest", "addMobileFeatureTest"}) MobileFeature mobileFeature = new MobileFeature();
public void updateMobileFeatureTest() MobileFeature testMblFeature = new MobileFeature();
throws MobileDeviceManagementDAOException { mobileFeature.setCode(MBL_FEATURE_UPDATED_CODE);
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setId(mblFeatureId);
boolean updated = mblFeatureDAO.updateMobileFeature(mobileFeature);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(query);
stmt.setString(1, MBL_FEATURE_UPDATED_CODE);
ResultSet resultSet = stmt.executeQuery();
if (resultSet.next()) {
testMblFeature.setId(resultSet.getInt(1));
testMblFeature.setCode(resultSet.getString(2));
testMblFeature.setName(resultSet.getString(3));
testMblFeature.setDescription(resultSet.getString(4));
}
} catch (SQLException e) {
String msg = "Error in updating Mobile Feature data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, stmt, null);
}
Assert.assertTrue(updated, "MobileFeature has updated");
Assert.assertEquals(MBL_FEATURE_UPDATED_CODE, testMblFeature.getCode(),
"MobileFeature data has updated ");
}
Connection conn = null; @Test(dependsOnMethods = { "addMobileFeatureTest", "getMobileFeatureByCodeTest",
Statement stmt = null; "getMobileFeatureByIdTest", "getAllMobileFeaturesTest",
"updateMobileFeatureTest" })
public void deleteMobileFeatureByIdTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
MobileFeature mobileFeature = new MobileFeature(); boolean status = mblFeatureDAO.deleteMobileFeatureById(mblFeatureId);
MobileFeature testMblFeature = new MobileFeature(); try {
mobileFeature.setCode(MBL_FEATURE_UPDATED_CODE); conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION); String query = "SELECT FEATURE_ID, CODE FROM MBL_FEATURE WHERE FEATURE_ID = ?";
mobileFeature.setName(MBL_FEATURE_NAME); stmt = conn.prepareStatement(query);
mobileFeature.setId(mblFeatureId); stmt.setInt(1, mblFeatureId);
boolean updated = mblFeatureDAO.updateMobileFeature(mobileFeature); ResultSet resultSet = stmt.executeQuery();
try { if (resultSet.next()) {
conn = DriverManager.getConnection(testDBConfiguration.getDriverClassName()); status = false;
stmt = conn.createStatement(); }
ResultSet resultSet = stmt } catch (SQLException e) {
.executeQuery( String msg = "Error in deleting Mobile Feature data ";
"SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE CODE = '" + log.error(msg, e);
MBL_FEATURE_UPDATED_CODE + "'"); throw new MobileDeviceManagementDAOException(msg, e);
while (resultSet.next()) { } finally {
testMblFeature.setId(resultSet.getInt(1)); MobileDatabaseUtils.cleanupResources(conn, stmt, null);
testMblFeature.setCode(resultSet.getString(2)); }
testMblFeature.setName(resultSet.getString(3)); Assert.assertTrue(status, "MobileFeature has deleted ");
testMblFeature.setDescription(resultSet.getString(4)); }
}
} catch (SQLException e) {
log.error("Error in updating Mobile Feature data ", e);
throw new MobileDeviceManagementDAOException("Error in updating Mobile Feature data ",
e);
} finally {
TestUtils.cleanupResources(conn, stmt, null);
}
Assert.assertTrue(updated, "MobileFeature has updated");
Assert.assertEquals(MBL_FEATURE_UPDATED_CODE, testMblFeature.getCode(),
"MobileFeature data has updated successfully");
}
@Test(dependsOnMethods = { "addMobileFeatureTest", "getMobileFeatureByCodeTest",
"getMobileFeatureByIdTest", "getAllMobileFeaturesTest",
"updateMobileFeatureTest", "deleteMobileFeatureByIdTest" })
public void deleteMobileFeatureByCodeTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileFeature mobileFeature = new MobileFeature();
mobileFeature.setCode(MBL_FEATURE_CODE);
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setDeviceType(MBL_FEATURE_DEVICE_TYPE);
mblFeatureDAO.addMobileFeature(mobileFeature);
boolean status = mblFeatureDAO.deleteMobileFeatureByCode(MBL_FEATURE_CODE);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query = "SELECT FEATURE_ID, CODE FROM MBL_FEATURE WHERE CODE = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, MBL_FEATURE_CODE);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in deleting Mobile Feature data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileFeature has deleted ");
}
} }

@ -18,5 +18,216 @@
package org.wso2.carbon.device.mgt.mobile.impl.dao; package org.wso2.carbon.device.mgt.mobile.impl.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileFeatureDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileFeaturePropertyDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeatureProperty;
import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDatabaseUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MobileFeaturePropertyDAOTestSuite { public class MobileFeaturePropertyDAOTestSuite {
private static final Log log = LogFactory.getLog(MobileFeaturePropertyDAOTestSuite.class);
public static final String MBL_FEATURE_NAME = "WIFI";
private static final String MBL_FEATURE_CODE = "500A";
public static final String MBL_FEATURE_DESCRIPTION = "Wifi config";
public static final String MBL_FEATURE_DEVICE_TYPE = "Android";
public static final String MBL_FEATURE_PROP_1 = "SSID";
public static final String MBL_FEATURE_PROP_2 = "PASSWORD";
private TestDBConfiguration testDBConfiguration;
private MobileFeatureDAOImpl mblFeatureDAO;
private MobileFeaturePropertyDAOImpl mobileFeaturePropertyDAO;
private int mblFeatureId;
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
DBTypes dbType = DBTypes.valueOf(dbTypeStr);
testDBConfiguration = MobileDatabaseUtils.getTestDBConfiguration(dbType);
switch (dbType) {
case H2:
MobileDatabaseUtils.createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
testDataSource.setPoolProperties(properties);
mblFeatureDAO = new MobileFeatureDAOImpl(testDataSource);
mobileFeaturePropertyDAO = new MobileFeaturePropertyDAOImpl(testDataSource);
default:
}
}
@Test
public void addMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
List<MobileFeatureProperty> propertyList = new ArrayList<MobileFeatureProperty>();
//Add a new MobileFeature to the database
MobileFeature mobileFeature = new MobileFeature();
mobileFeature.setCode(MBL_FEATURE_CODE);
mobileFeature.setDescription(MBL_FEATURE_DESCRIPTION);
mobileFeature.setName(MBL_FEATURE_NAME);
mobileFeature.setDeviceType(MBL_FEATURE_DEVICE_TYPE);
mblFeatureId = mblFeatureDAO.addMobileFeature(mobileFeature);
//Add 1st property to the feature
MobileFeatureProperty mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(mblFeatureId);
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_1);
boolean status1 = mobileFeaturePropertyDAO.addMobileFeatureProperty(mobileFeatureProperty);
//Add 2nd property to the feature
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_2);
boolean status2 = mobileFeaturePropertyDAO.addMobileFeatureProperty(mobileFeatureProperty);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT FEATURE_ID, PROPERTY FROM MBL_FEATURE_PROPERTY WHERE FEATURE_ID = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, mblFeatureId);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(resultSet.getInt(1));
mobileFeatureProperty.setProperty(resultSet.getString(2));
propertyList.add(mobileFeatureProperty);
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Feature data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status1, "MobileFeatureProperty1 has added ");
Assert.assertTrue(status2, "MobileFeatureProperty2 has added ");
Assert.assertTrue(propertyList.size() == 2, "MobileFeatureProperties have retrieved ");
for (MobileFeatureProperty mblFeatureProperty : propertyList) {
Assert.assertNotNull(mblFeatureProperty.getProperty(),
"MobileFeatureProperty property has persisted ");
Assert.assertNotNull(mblFeatureProperty.getFeatureID(),
"MobileFeatureProperty feature-id has persisted ");
}
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest" })
public void getMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
MobileFeatureProperty mobileFeatureProperty =
mobileFeaturePropertyDAO.getMobileFeatureProperty(MBL_FEATURE_PROP_1);
Assert.assertNotNull(mobileFeatureProperty, "MobileFeatureProperty has retrieved ");
Assert.assertEquals(MBL_FEATURE_PROP_1, mobileFeatureProperty.getProperty(),
"MobileFeatureProperty property has retrieved ");
Assert.assertTrue(mblFeatureId == mobileFeatureProperty.getFeatureID(),
"MobileFeatureProperty featureId has retrieved ");
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest" })
public void getFeaturePropertyOfFeatureTest()
throws MobileDeviceManagementDAOException {
List<MobileFeatureProperty> mobileFeatureProperties =
mobileFeaturePropertyDAO.getFeaturePropertiesOfFeature(mblFeatureId);
Assert.assertNotNull(mobileFeatureProperties, "MobileFeatureProperty list has retrieved ");
Assert.assertTrue(mobileFeatureProperties.size() == 2,
"MobileFeatureProperties have fetched ");
for (MobileFeatureProperty mblFeatureProperty : mobileFeatureProperties) {
Assert.assertNotNull(mblFeatureProperty.getProperty(),
"MobileFeatureProperty property has fetched ");
Assert.assertNotNull(mblFeatureProperty.getFeatureID(),
"MobileFeatureProperty feature-id has fetched ");
}
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest" }, expectedExceptions = MobileDeviceManagementDAOException.class)
public void updateFeaturePropertyTest() throws MobileDeviceManagementDAOException {
//Update 1st property to a non-exist feature
MobileFeatureProperty mobileFeatureProperty = new MobileFeatureProperty();
mobileFeatureProperty.setFeatureID(2);
mobileFeatureProperty.setProperty(MBL_FEATURE_PROP_1);
mobileFeaturePropertyDAO.updateMobileFeatureProperty(mobileFeatureProperty);
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest" })
public void deleteMobileFeaturePropertyTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean status =
mobileFeaturePropertyDAO.deleteMobileFeatureProperty(MBL_FEATURE_PROP_2);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT PROPERTY, FEATURE_ID FROM MBL_FEATURE_PROPERTY WHERE PROPERTY = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setString(1, MBL_FEATURE_PROP_2);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileFeatureProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileFeatureProperty has deleted ");
}
@Test(dependsOnMethods = { "addMobileFeaturePropertyTest", "getMobileFeaturePropertyTest",
"getFeaturePropertyOfFeatureTest" , "updateFeaturePropertyTest",
"deleteMobileFeaturePropertyTest"})
public void deleteMobileFeaturePropertiesOfFeatureTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean status =
mobileFeaturePropertyDAO.deleteMobileFeaturePropertiesOfFeature(mblFeatureId);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String query =
"SELECT PROPERTY, FEATURE_ID FROM MBL_FEATURE_PROPERTY WHERE FEATURE_ID = ?";
preparedStatement = conn.prepareStatement(query);
preparedStatement.setInt(1, mblFeatureId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileFeatureProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileFeatureProperties has deleted ");
}
} }

@ -18,5 +18,166 @@
package org.wso2.carbon.device.mgt.mobile.impl.dao; package org.wso2.carbon.device.mgt.mobile.impl.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileOperationDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dto.MobileOperation;
import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDatabaseUtils;
import java.sql.*;
public class MobileOperationDAOTestSuite { public class MobileOperationDAOTestSuite {
private static final Log log = LogFactory.getLog(MobileOperationDAOTestSuite.class);
public static final String TEST_MBL_OPR_FEATURE_CODE = "LOCK";
public static final String TEST_MBL_OPR_UPDATED_FEATURE_CODE = "MUTE";
public static final long TEST_MBL_OPR_CREATED_DATE = new java.util.Date().getTime();
private TestDBConfiguration testDBConfiguration;
private MobileOperationDAOImpl mblOperationDAO;
private int mblOperationId;
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
DBTypes dbType = DBTypes.valueOf(dbTypeStr);
testDBConfiguration = MobileDatabaseUtils.getTestDBConfiguration(dbType);
switch (dbType) {
case H2:
MobileDatabaseUtils.createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
testDataSource.setPoolProperties(properties);
mblOperationDAO = new MobileOperationDAOImpl(testDataSource);
default:
}
}
@Test
public void addMobileOperationTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileOperation mblOperation = new MobileOperation();
MobileOperation testMblOperation = new MobileOperation();
mblOperation.setFeatureCode(TEST_MBL_OPR_FEATURE_CODE);
mblOperation.setCreatedDate(TEST_MBL_OPR_CREATED_DATE);
mblOperationId = mblOperationDAO.addMobileOperation(mblOperation);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT OPERATION_ID, FEATURE_CODE, CREATED_DATE FROM MBL_OPERATION WHERE OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setInt(1, mblOperationId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
testMblOperation.setOperationId(resultSet.getInt(1));
testMblOperation.setFeatureCode(resultSet.getString(2));
testMblOperation.setCreatedDate(resultSet.getLong(3));
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Operation data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(mblOperationId > 0 , "MobileOperation has added ");
Assert.assertEquals(TEST_MBL_OPR_FEATURE_CODE, testMblOperation.getFeatureCode(),
"MobileOperation feature code has persisted ");
Assert.assertEquals(TEST_MBL_OPR_CREATED_DATE, testMblOperation.getCreatedDate(),
"MobileOperation created-date has persisted ");
}
@Test(dependsOnMethods = { "addMobileOperationTest" })
public void getMobileOperationTest()
throws MobileDeviceManagementDAOException {
MobileOperation mobileOperation = mblOperationDAO.getMobileOperation(mblOperationId);
Assert.assertEquals(TEST_MBL_OPR_CREATED_DATE, mobileOperation.getCreatedDate(),
"MobileOperation created-date has retrieved ");
Assert.assertEquals(TEST_MBL_OPR_FEATURE_CODE, mobileOperation.getFeatureCode(),
"MobileOperation feature-code has retrieved ");
}
@Test(dependsOnMethods = { "addMobileOperationTest" , "getMobileOperationTest"})
public void updateMobileOperationTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
long updatedDate = new java.util.Date().getTime();
MobileOperation mblOperation = new MobileOperation();
MobileOperation testMblOperation = new MobileOperation();
mblOperation.setFeatureCode(TEST_MBL_OPR_UPDATED_FEATURE_CODE);
mblOperation.setCreatedDate(updatedDate);
mblOperation.setOperationId(mblOperationId);
boolean status = mblOperationDAO.updateMobileOperation(mblOperation);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT OPERATION_ID, FEATURE_CODE, CREATED_DATE FROM MBL_OPERATION WHERE OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setInt(1, mblOperationId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
testMblOperation.setOperationId(resultSet.getInt(1));
testMblOperation.setFeatureCode(resultSet.getString(2));
testMblOperation.setCreatedDate(resultSet.getLong(3));
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Operation data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status , "MobileOperation has updated ");
Assert.assertEquals(TEST_MBL_OPR_UPDATED_FEATURE_CODE, testMblOperation.getFeatureCode(),
"MobileOperation feature code has updated ");
Assert.assertEquals(updatedDate, testMblOperation.getCreatedDate(),
"MobileOperation created-date has updated ");
}
@Test(dependsOnMethods = { "addMobileOperationTest" , "getMobileOperationTest",
"updateMobileOperationTest" })
public void deleteMobileDeviceTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean deleted = mblOperationDAO.deleteMobileOperation(mblOperationId);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT OPERATION_ID, FEATURE_CODE, CREATED_DATE FROM MBL_OPERATION WHERE OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setInt(1, mblOperationId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
deleted = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Operation data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(deleted, "MobileOperation has deleted ");
}
} }

@ -18,5 +18,215 @@
package org.wso2.carbon.device.mgt.mobile.impl.dao; package org.wso2.carbon.device.mgt.mobile.impl.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileOperationDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dao.impl.MobileOperationPropertyDAOImpl;
import org.wso2.carbon.device.mgt.mobile.dto.MobileOperation;
import org.wso2.carbon.device.mgt.mobile.dto.MobileOperationProperty;
import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration;
import org.wso2.carbon.device.mgt.mobile.impl.dao.util.MobileDatabaseUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MobileOperationPropertyDAOTestSuite { public class MobileOperationPropertyDAOTestSuite {
private static final Log log = LogFactory.getLog(MobileOperationPropertyDAOTestSuite.class);
public static final String TEST_MBL_OPR_FEATURE_CODE = "LOCK";
public static final long TEST_MBL_OPR_CREATED_DATE = new java.util.Date().getTime();
public static final String TEST_MBL_OPR_PROPERTY_SSID = "SSID";
public static final String TEST_MBL_OPR_PROPERTY_SSID_VALUE = "wso2";
public static final String TEST_MBL_OPR_PROPERTY_PWD = "PASSWORD";
public static final String TEST_MBL_OPR_PROPERTY_PWD_VALUE = "wso2";
public static final String TEST_MBL_OPR_PROPERTY_PWD_UPDATED_VALUE = "wso2mdm";
private int mblOperationId;
private TestDBConfiguration testDBConfiguration;
private MobileOperationPropertyDAOImpl mobileOperationPropertyDAO;
private MobileOperationDAOImpl mblOperationDAO;
@BeforeClass
@Parameters("dbType")
public void setUpDB(String dbTypeStr) throws Exception {
DBTypes dbType = DBTypes.valueOf(dbTypeStr);
testDBConfiguration = MobileDatabaseUtils.getTestDBConfiguration(dbType);
switch (dbType) {
case H2:
MobileDatabaseUtils.createH2DB(testDBConfiguration);
DataSource testDataSource = new org.apache.tomcat.jdbc.pool.DataSource();
PoolProperties properties = new PoolProperties();
properties.setUrl(testDBConfiguration.getConnectionURL());
properties.setDriverClassName(testDBConfiguration.getDriverClassName());
properties.setUsername(testDBConfiguration.getUsername());
properties.setPassword(testDBConfiguration.getPassword());
testDataSource.setPoolProperties(properties);
mobileOperationPropertyDAO = new MobileOperationPropertyDAOImpl(testDataSource);
mblOperationDAO = new MobileOperationDAOImpl(testDataSource);
default:
}
}
@Test
public void addMobileOperationPropertyTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileOperation mblOperation = new MobileOperation();
MobileOperationProperty operationProperty = new MobileOperationProperty();
List<MobileOperationProperty> properties = new ArrayList<MobileOperationProperty>();
//Add a new Operation to the database
MobileOperation testMblOperation = new MobileOperation();
mblOperation.setFeatureCode(TEST_MBL_OPR_FEATURE_CODE);
mblOperation.setCreatedDate(TEST_MBL_OPR_CREATED_DATE);
mblOperationId = mblOperationDAO.addMobileOperation(mblOperation);
//Add property1
operationProperty.setOperationId(mblOperationId);
operationProperty.setProperty(TEST_MBL_OPR_PROPERTY_SSID);
operationProperty.setValue(TEST_MBL_OPR_PROPERTY_SSID_VALUE);
boolean status1 = mobileOperationPropertyDAO.addMobileOperationProperty(operationProperty);
//add property2
operationProperty = new MobileOperationProperty();
operationProperty.setOperationId(mblOperationId);
operationProperty.setProperty(TEST_MBL_OPR_PROPERTY_PWD);
operationProperty.setValue(TEST_MBL_OPR_PROPERTY_PWD_VALUE);
boolean status2 = mobileOperationPropertyDAO.addMobileOperationProperty(operationProperty);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT OPERATION_ID, PROPERTY, VALUE FROM MBL_OPERATION_PROPERTY WHERE OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setInt(1, mblOperationId);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
operationProperty = new MobileOperationProperty();
operationProperty.setOperationId(resultSet.getInt(1));
operationProperty.setProperty(resultSet.getString(2));
operationProperty.setValue(resultSet.getString(3));
properties.add(operationProperty);
}
} catch (SQLException e) {
String msg = "Error in retrieving Mobile Operation Property data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status1, "MobileOperationProperty1 has added ");
Assert.assertTrue(status2, "MobileOperationProperty2 has added ");
Assert.assertTrue(properties.size() == 2, "MobileOperationProperties have retrieved ");
for (MobileOperationProperty mobileOperationProperty : properties) {
Assert.assertNotNull(mobileOperationProperty.getProperty(),
"MobileOperationProperty property has persisted ");
Assert.assertNotNull(mobileOperationProperty.getValue(),
"MobileOperationProperty value has persisted ");
}
}
@Test(dependsOnMethods = { "addMobileOperationPropertyTest" })
public void getMobileOperationPropertyTest()
throws MobileDeviceManagementDAOException {
MobileOperationProperty mobileOperationProperty = mobileOperationPropertyDAO
.getMobileOperationProperty(mblOperationId, TEST_MBL_OPR_PROPERTY_PWD);
Assert.assertEquals(mblOperationId, mobileOperationProperty.getOperationId(),
"MobileOperationProperty operation-id has retrieved ");
Assert.assertEquals(TEST_MBL_OPR_PROPERTY_PWD, mobileOperationProperty.getProperty(),
"MobileOperationProperty property has retrieved ");
Assert.assertEquals(TEST_MBL_OPR_PROPERTY_PWD_VALUE, mobileOperationProperty.getValue(),
"MobileOperationProperty property-value has retrieved ");
}
@Test(dependsOnMethods = { "addMobileOperationPropertyTest" })
public void getAllMobileOperationPropertiesOfOperationTest()
throws MobileDeviceManagementDAOException {
List<MobileOperationProperty> mobileOperationProperties = mobileOperationPropertyDAO
.getAllMobileOperationPropertiesOfOperation(mblOperationId);
Assert.assertTrue(mobileOperationProperties.size() == 2,
"MobileOperationProperties of operation has retrieved");
}
@Test(dependsOnMethods = { "addMobileOperationPropertyTest", "getMobileOperationPropertyTest",
"getAllMobileOperationPropertiesOfOperationTest" })
public void updateMobileOperationPropertyTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
MobileOperationProperty mblOperationProperty = new MobileOperationProperty();
MobileOperationProperty testMblOperationProperty = new MobileOperationProperty();
mblOperationProperty.setOperationId(mblOperationId);
mblOperationProperty.setProperty(TEST_MBL_OPR_PROPERTY_PWD);
mblOperationProperty.setValue(TEST_MBL_OPR_PROPERTY_PWD_UPDATED_VALUE);
boolean status =
mobileOperationPropertyDAO.updateMobileOperationProperty(mblOperationProperty);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT OPERATION_ID, PROPERTY, VALUE FROM MBL_OPERATION_PROPERTY WHERE" +
" OPERATION_ID = ? AND PROPERTY = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setInt(1, mblOperationId);
preparedStatement.setString(2, TEST_MBL_OPR_PROPERTY_PWD);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
testMblOperationProperty.setOperationId(resultSet.getInt(1));
testMblOperationProperty.setProperty(resultSet.getString(2));
testMblOperationProperty.setValue(resultSet.getString(3));
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileOperationProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileOperationProperty has updated ");
Assert.assertEquals(TEST_MBL_OPR_PROPERTY_PWD_UPDATED_VALUE,
testMblOperationProperty.getValue(),
"MobileOperationProperty value has updated ");
}
@Test(dependsOnMethods = { "addMobileOperationPropertyTest", "getMobileOperationPropertyTest",
"getAllMobileOperationPropertiesOfOperationTest",
"updateMobileOperationPropertyTest" })
public void deleteMobileOperationPropertiesOfOperationTest()
throws MobileDeviceManagementDAOException {
Connection conn = null;
PreparedStatement preparedStatement = null;
boolean status =
mobileOperationPropertyDAO.deleteMobileOperationProperties(mblOperationId);
try {
conn = DriverManager.getConnection(testDBConfiguration.getConnectionURL());
String selectDBQuery =
"SELECT OPERATION_ID, PROPERTY, VALUE FROM MBL_OPERATION_PROPERTY WHERE" +
" OPERATION_ID = ?";
preparedStatement = conn.prepareStatement(selectDBQuery);
preparedStatement.setInt(1, mblOperationId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
status = false;
}
} catch (SQLException e) {
String msg = "Error in retrieving MobileOperationProperty data ";
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally {
MobileDatabaseUtils.cleanupResources(conn, preparedStatement, null);
}
Assert.assertTrue(status, "MobileOperationProperty has deleted ");
}
} }

@ -0,0 +1,111 @@
/*
* 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.mobile.impl.dao.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration;
import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfigurations;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.sql.*;
/**
* This class provides the utility methods needed for DAO related test executions.
*/
public class MobileDatabaseUtils {
private static final Log log = LogFactory.getLog(MobileDatabaseUtils.class);
public static final String TEST_RESOURCES_DB_CONFIG_FILE =
"src/test/resources/testdbconfig.xml";
public static void cleanupResources(Connection conn, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Error occurred while closing result set", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing prepared statement", e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
log.warn("Error occurred while closing database connection", e);
}
}
}
public static TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws
MobileDeviceManagementDAOException,
DeviceManagementException {
File deviceMgtConfig = new File(TEST_RESOURCES_DB_CONFIG_FILE);
Document doc;
TestDBConfiguration testDBConfiguration = null;
TestDBConfigurations testDBConfigurations;
doc = MobileDeviceManagementUtil.convertToDocument(deviceMgtConfig);
JAXBContext testDBContext;
try {
testDBContext = JAXBContext.newInstance(TestDBConfigurations.class);
Unmarshaller unmarshaller = testDBContext.createUnmarshaller();
testDBConfigurations = (TestDBConfigurations) unmarshaller.unmarshal(doc);
} catch (JAXBException e) {
throw new MobileDeviceManagementDAOException("Error parsing test db configurations", e);
}
for (TestDBConfiguration testDBConfiguration1 : testDBConfigurations.getDbTypesList()) {
testDBConfiguration = testDBConfiguration1;
if (testDBConfiguration.getType().equals(dbType.toString())) {
break;
}
}
return testDBConfiguration;
}
public static void createH2DB(TestDBConfiguration testDBConf) throws Exception {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(testDBConf.getDriverClassName());
conn = DriverManager.getConnection(testDBConf.getConnectionURL());
stmt = conn.createStatement();
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'");
} finally {
cleanupResources(conn, stmt, null);
}
}
}

@ -20,9 +20,10 @@ CREATE TABLE IF NOT EXISTS `MBL_DEVICE` (
-- ----------------------------------------------------- -- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `MBL_FEATURE` ( CREATE TABLE IF NOT EXISTS `MBL_FEATURE` (
`FEATURE_ID` INT NOT NULL AUTO_INCREMENT , `FEATURE_ID` INT NOT NULL AUTO_INCREMENT ,
`CODE` VARCHAR(45) NOT NULL , `CODE` VARCHAR(45) NOT NULL,
`NAME` VARCHAR(100) NULL , `NAME` VARCHAR(100) NULL ,
`DESCRIPTION` VARCHAR(200) NULL , `DESCRIPTION` VARCHAR(200) NULL ,
`DEVICE_TYPE` VARCHAR(50) NULL ,
PRIMARY KEY (`FEATURE_ID`) ); PRIMARY KEY (`FEATURE_ID`) );
-- ----------------------------------------------------- -- -----------------------------------------------------
@ -74,7 +75,7 @@ CREATE TABLE IF NOT EXISTS `MBL_OPERATION_PROPERTY` (
-- ----------------------------------------------------- -- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `MBL_FEATURE_PROPERTY` ( CREATE TABLE IF NOT EXISTS `MBL_FEATURE_PROPERTY` (
`PROPERTY` VARCHAR(45) NOT NULL , `PROPERTY` VARCHAR(45) NOT NULL ,
`FEATURE_ID` VARCHAR(45) NOT NULL , `FEATURE_ID` INT NOT NULL ,
PRIMARY KEY (`PROPERTY`) , PRIMARY KEY (`PROPERTY`) ,
CONSTRAINT `fk_MBL_FEATURE_PROPERTY_MBL_FEATURE1` CONSTRAINT `fk_MBL_FEATURE_PROPERTY_MBL_FEATURE1`
FOREIGN KEY (`FEATURE_ID` ) FOREIGN KEY (`FEATURE_ID` )

@ -25,11 +25,16 @@
</classes> </classes>
</test> </test>
<!--test name="DAO Unit Tests" preserve-order="true"> <test name="DAO Unit Tests" preserve-order="true">
<parameter name="dbType" value="H2"/> <parameter name="dbType" value="H2"/>
<classes> <classes>
<class name="org.wso2.carbon.device.mgt.mobile.impl.dao.MobileFeatureDAOTestSuite"/> <class name="org.wso2.carbon.device.mgt.mobile.impl.dao.MobileFeatureDAOTestSuite"/>
<class name="org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceDAOTestSuite"/>
<class name="org.wso2.carbon.device.mgt.mobile.impl.dao.MobileOperationDAOTestSuite"/>
<class name="org.wso2.carbon.device.mgt.mobile.impl.dao.MobileOperationPropertyDAOTestSuite"/>
<class name="org.wso2.carbon.device.mgt.mobile.impl.dao.MobileFeaturePropertyDAOTestSuite"/>
<class name="org.wso2.carbon.device.mgt.mobile.impl.dao.MobileDeviceOperationMappingDAOTestSuite"/>
</classes> </classes>
</test--> </test>
</suite> </suite>
Loading…
Cancel
Save