Add missing parts and test to group management.

revert-70aa11f8
charithag 9 years ago
parent d1ee1fd578
commit 3fd141acdf

@ -19,15 +19,11 @@
package org.wso2.carbon.device.mgt.core.group.mgt.dao; package org.wso2.carbon.device.mgt.core.group.mgt.dao;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder; import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* This interface represents the key operations associated with persisting group related information. * This interface represents the key operations associated with persisting group related information.
@ -151,10 +147,10 @@ public interface GroupDAO {
* Get all devices of a given tenant and device group. * Get all devices of a given tenant and device group.
* *
* @param groupId of the group. * @param groupId of the group.
* @param tenantId of user's tenant.
* @param request for pagination. * @param request for pagination.
* @param tenantId of user's tenant.
* @return list of device in group * @return list of device in group
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
PaginationResult getDevices(int groupId, PaginationRequest request, int tenantId) throws GroupManagementDAOException; List<Device> getDevices(int groupId, PaginationRequest request, int tenantId) throws GroupManagementDAOException;
} }

@ -20,8 +20,8 @@ package org.wso2.carbon.device.mgt.core.group.mgt.dao;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder; import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder;
import java.sql.Connection; import java.sql.Connection;
@ -304,13 +304,79 @@ public class GroupDAOImpl implements GroupDAO {
@Override @Override
public List<Device> getDevices(int groupId, int tenantId) throws GroupManagementDAOException { public List<Device> getDevices(int groupId, int tenantId) throws GroupManagementDAOException {
return null; Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
try {
conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT gd.DEVICE_ID, " +
"gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " +
"FROM (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, " +
"d.DEVICE_TYPE_ID FROM DM_DEVICE d, DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ? " +
"AND d.ID = dgm.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " +
"WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while retrieving information of all " +
"registered devices", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return devices;
} }
@SuppressWarnings("JpaQueryApiInspection")
@Override @Override
public PaginationResult getDevices(int groupId, PaginationRequest request, int tenantId) public List<Device> getDevices(int groupId, PaginationRequest request, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
return null; Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> devices = null;
try {
conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT gd.DEVICE_ID, " +
"gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " +
"FROM (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, " +
"d.DEVICE_TYPE_ID FROM DM_DEVICE d, DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ? " +
"AND d.ID = dgm.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " +
"WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? " +
"LIMIT ?, ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
//noinspection JpaQueryApiInspection
stmt.setInt(4, request.getStartIndex());
stmt.setInt(5, request.getRowCount());
rs = stmt.executeQuery();
devices = new ArrayList<>();
while (rs.next()) {
Device device = DeviceManagementDAOUtil.loadDevice(rs);
devices.add(device);
}
} catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while retrieving information of all " +
"registered devices", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return devices;
} }
} }

@ -14,14 +14,12 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*/ */
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser; import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser;
@ -190,7 +188,7 @@ public interface GroupManagementProviderService {
* @return list of group devices * @return list of group devices
* @throws GroupManagementException * @throws GroupManagementException
*/ */
PaginationResult getDevices(int groupId, PaginationRequest request) throws GroupManagementException; List<Device> getDevices(int groupId, PaginationRequest request) throws GroupManagementException;
/** /**
* This method is used to retrieve the device count of a given group. * This method is used to retrieve the device count of a given group.

@ -14,7 +14,6 @@
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*
*/ */
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
@ -26,7 +25,6 @@ import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
@ -427,7 +425,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public PaginationResult getDevices(int groupId, PaginationRequest request) public List<Device> getDevices(int groupId, PaginationRequest request)
throws GroupManagementException { throws GroupManagementException {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {

@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
@ -39,19 +40,14 @@ import java.util.List;
public class GroupPersistTests extends BaseDeviceManagementTest { public class GroupPersistTests extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(GroupPersistTests.class); private static final Log log = LogFactory.getLog(GroupPersistTests.class);
GroupDAO groupDAO = GroupManagementDAOFactory.getGroupDAO();
int groupId = -1; int groupId = -1;
private GroupDAO groupDAO;
@BeforeClass @BeforeClass
@Override @Override
public void init() { public void init() throws Exception {
try {
initDataSource(); initDataSource();
} catch (Exception e) { groupDAO = GroupManagementDAOFactory.getGroupDAO();
String msg = "Error occurred while initializing data source.";
log.error(msg, e);
Assert.fail(msg, e);
}
} }
@Test @Test
@ -176,6 +172,68 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
} }
@Test(dependsOnMethods = {"updateGroupTest"}) @Test(dependsOnMethods = {"updateGroupTest"})
public void addDeviceToGroupTest() {
Device initialTestDevice = TestDataHolder.initialTestDevice;
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.addDevice(groupId, initialTestDevice.getId(), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
log.debug("Device added to group.");
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding device '" + initialTestDevice.getName() + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
try {
GroupManagementDAOFactory.openConnection();
List<Device> groupedDevices = groupDAO.getDevices(groupId, TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotEquals(groupedDevices.size(), 0, "No device found");
Assert.assertNotNull(groupedDevices.get(0), "Device is null");
Assert.assertEquals(groupedDevices.get(0).getId(), initialTestDevice.getId(), "Device ids not matched");
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while retrieving group details.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"addDeviceToGroupTest"})
public void removeDeviceFromGroupTest() {
Device initialTestDevice = TestDataHolder.initialTestDevice;
try {
GroupManagementDAOFactory.beginTransaction();
groupDAO.removeDevice(groupId, initialTestDevice.getId(), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction();
log.debug("Group added to database.");
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding device '" + initialTestDevice.getName() + "'.";
log.error(msg, e);
Assert.fail(msg, e);
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
Assert.fail(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Test(dependsOnMethods = {"removeDeviceFromGroupTest"})
public void deleteGroupTest() { public void deleteGroupTest() {
DeviceGroup group = getGroupById(groupId); DeviceGroup group = getGroupById(groupId);
int groupId = 0; int groupId = 0;
@ -201,4 +259,5 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
group = getGroupById(groupId); group = getGroupById(groupId);
Assert.assertNull(group, "Group not deleted"); Assert.assertNull(group, "Group not deleted");
} }
} }

@ -1,29 +1,31 @@
# #
# Copyright 2009 WSO2, Inc. (http://wso2.com) # Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # WSO2 Inc. licenses this file to you under the Apache License,
# you may not use this file except in compliance with the 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 # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing,
# distributed under the License is distributed on an "AS IS" BASIS, # software distributed under the License is distributed on an
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# See the License for the specific language governing permissions and # KIND, either express or implied. See the License for the
# limitations under the License. # specific language governing permissions and limitations
# under the License.
# #
# #
# This is the log4j configuration file used by WSO2 Carbon # This is the log4j configuration file used by WSO2 Carbon
# #
# IMPORTANT : Please do not remove or change the names of any # IMPORTANT : Please do not remove or change the names of any
# of the Appenders defined here. The layout pattern & log file # of the Appender defined here. The layout pattern & log file
# can be changed using the WSO2 Carbon Management Console, and those # can be changed using the WSO2 Carbon Management Console, and those
# settings will override the settings in this file. # settings will override the settings in this file.
# #
log4j.rootLogger=INFO, STD_OUT log4j.rootLogger=DEBUG, STD_OUT
# Redirect log messages to console # Redirect log messages to console
log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender

Loading…
Cancel
Save