Merge branch 'master' into 'master'

Fix device group related issues

See merge request entgra/carbon-device-mgt!374
feature/appm-store/pbac
Charitha Goonetilleke 5 years ago
commit a2ceef5e84

@ -171,7 +171,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
for (T param : params) {
String groupName = (String) param;
subscribers.add(groupName);
devices.addAll(groupManagementProviderService.getAllDevicesOfGroup(groupName));
devices.addAll(groupManagementProviderService.getAllDevicesOfGroup(groupName, true));
}
}
@ -401,7 +401,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
for (T param : params) {
String groupName = (String) param;
subscribers.add(groupName);
devices.addAll(groupManagementProviderService.getAllDevicesOfGroup(groupName));
devices.addAll(groupManagementProviderService.getAllDevicesOfGroup(groupName, false));
}
} else {
String msg = "Found invalid subscription type " + subType+ " to install application release" ;

@ -212,29 +212,36 @@ public interface GroupManagementService {
message = "Internal Server Error. \n Server error occurred while fetching the groups list.",
response = ErrorResponse.class)
})
Response getGroups(@ApiParam(
name = "name",
value = "Name of the group.")
@QueryParam("name")
String name,
@ApiParam(
name = "owner",
value = "Owner of the group.")
@QueryParam("owner")
String owner,
@ApiParam(
name = "offset",
value = "The starting pagination index for the complete list of qualified items.",
defaultValue = "0")
@QueryParam("offset")
int offset,
@ApiParam(
name = "limit",
value = "Provide how many device details you require from the starting pagination " +
"index/offset.",
defaultValue = "5")
@QueryParam("limit")
int limit);
Response getGroups(
@ApiParam(
name = "name",
value = "Name of the group.")
@QueryParam("name")
String name,
@ApiParam(
name = "owner",
value = "Owner of the group.")
@QueryParam("owner")
String owner,
@ApiParam(
name = "offset",
value = "The starting pagination index for the complete list of qualified items.",
defaultValue = "0")
@QueryParam("offset")
int offset,
@ApiParam(
name = "limit",
value = "Provide how many device details you require from the starting pagination " +
"index/offset.",
defaultValue = "5")
@QueryParam("limit")
int limit,
@ApiParam(
name = "requireGroupProps",
value = "Request group properties to include in the response",
defaultValue = "false")
@QueryParam("requireGroupProps")
boolean requireGroupProps);
@Path("/count")
@GET
@ -394,11 +401,77 @@ public interface GroupManagementService {
message = "Internal Server Error. \n Server error occurred while fetching the group details.",
response = ErrorResponse.class)
})
Response getGroup(@ApiParam(
name = "groupId",
value = "The ID of the group.",
required = true)
@PathParam("groupId") int groupId);
Response getGroup(
@ApiParam(
name = "groupId",
value = "The ID of the group.",
required = true)
@PathParam("groupId") int groupId,
@ApiParam(
name = "requireGroupProps",
value = "Request group properties to include in the response",
defaultValue = "false")
@QueryParam("requireGroupProps")
boolean requireGroupProps);
@Path("/name/{groupName}")
@GET
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET,
value = "Getting Details of a Specific Device Group",
notes = "Get the details of a specific device group.",
tags = "Device Group Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:groups:groups-view")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the device group.",
response = DeviceGroup.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource has been modified the last time.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."),
@ApiResponse(
code = 404,
message = "Group not found.",
response = ErrorResponse.class),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the group details.",
response = ErrorResponse.class)
})
Response getGroup(
@ApiParam(
name = "groupName",
value = "Name of the group.",
required = true)
@PathParam("groupName") String groupName,
@ApiParam(
name = "requireGroupProps",
value = "Request group properties to include in the response",
defaultValue = "false")
@QueryParam("requireGroupProps")
boolean requireGroupProps);
@Path("/id/{groupId}")
@PUT
@ -739,11 +812,12 @@ public interface GroupManagementService {
message = "Internal Server Error. \n Server error occurred while fetching device count.",
response = ErrorResponse.class)
})
Response getDeviceCountOfGroup(@ApiParam(
name = "groupId",
value = "ID of the group.",
required = true)
@PathParam("groupId") int groupId);
Response getDeviceCountOfGroup(
@ApiParam(
name = "groupId",
value = "ID of the group.",
required = true)
@PathParam("groupId") int groupId);
@Path("/id/{groupId}/devices/add")
@POST
@ -966,6 +1040,12 @@ public interface GroupManagementService {
value = "The type of the device, such as android, ios, or windows.",
required = true)
@QueryParam("deviceType")
String deviceType);
String deviceType,
@ApiParam(
name = "requireGroupProps",
value = "Request group properties to include in the response",
defaultValue = "false")
@QueryParam("requireGroupProps")
boolean requireGroupProps);
}

@ -151,12 +151,17 @@ public interface GroupManagementAdminService {
defaultValue = "5")
@QueryParam("limit")
int limit,
@ApiParam(
name = "status",
value = "status of group to be retrieve.")
@QueryParam("status")
String status);
String status,
@ApiParam(
name = "requireGroupProps",
value = "Request group properties to include in the response",
defaultValue = "false")
@QueryParam("requireGroupProps")
boolean requireGroupProps);
@Path("/count")
@GET

@ -56,7 +56,7 @@ public class GroupManagementServiceImpl implements GroupManagementService {
"/permission/device-mgt/user/groups"};
@Override
public Response getGroups(String name, String owner, int offset, int limit) {
public Response getGroups(String name, String owner, int offset, int limit, boolean requireGroupProps) {
try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -64,7 +64,7 @@ public class GroupManagementServiceImpl implements GroupManagementService {
request.setGroupName(name);
request.setOwner(owner);
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroups(currentUser, request);
.getGroups(currentUser, request, requireGroupProps);
DeviceGroupList deviceGroupList = new DeviceGroupList();
if (deviceGroupsResult.getData() != null && deviceGroupsResult.getRecordsTotal() > 0) {
deviceGroupList.setList(deviceGroupsResult.getData());
@ -117,10 +117,27 @@ public class GroupManagementServiceImpl implements GroupManagementService {
}
@Override
public Response getGroup(int groupId) {
public Response getGroup(int groupId, boolean requireGroupProps) {
try {
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
DeviceGroup deviceGroup = service.getGroup(groupId);
DeviceGroup deviceGroup = service.getGroup(groupId, requireGroupProps);
if (deviceGroup != null) {
return Response.status(Response.Status.OK).entity(deviceGroup).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
} catch (GroupManagementException e) {
String error = "Error occurred while getting the group.";
log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
}
}
@Override
public Response getGroup(String groupName, boolean requireGroupProps) {
try {
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
DeviceGroup deviceGroup = service.getGroup(groupName, requireGroupProps);
if (deviceGroup != null) {
return Response.status(Response.Status.OK).entity(deviceGroup).build();
} else {
@ -272,7 +289,7 @@ public class GroupManagementServiceImpl implements GroupManagementService {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
deviceIdentifiers.add(deviceToGroupsAssignment.getDeviceIdentifier());
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
List<DeviceGroup> deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier());
List<DeviceGroup> deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier(), false);
for (DeviceGroup group : deviceGroups) {
Integer groupId = group.getGroupId();
if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) {
@ -295,10 +312,11 @@ public class GroupManagementServiceImpl implements GroupManagementService {
}
@Override
public Response getGroups(String deviceId, String deviceType) {
public Response getGroups(String deviceId, String deviceType, boolean requireGroupProps) {
try {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
List<DeviceGroup> deviceGroups = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroups(deviceIdentifier);
List<DeviceGroup> deviceGroups = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroups(deviceIdentifier, requireGroupProps);
return Response.status(Response.Status.OK).entity(deviceGroups).build();
} catch (GroupManagementException e) {
String msg = "Error occurred while getting groups of device.";

@ -43,7 +43,7 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
"/permission/device-mgt/user/groups"};
@Override
public Response getGroups(String name, String owner, int offset, int limit, String status) {
public Response getGroups(String name, String owner, int offset, int limit, String status, boolean requireGroupProps) {
try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
GroupPaginationRequest request = new GroupPaginationRequest(offset, limit);
@ -57,7 +57,7 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
request.setStatus(status.toUpperCase());
}
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroups(request);
.getGroups(request, requireGroupProps);
DeviceGroupList deviceGroupList = new DeviceGroupList();
if (deviceGroupsResult.getData() != null) {
deviceGroupList.setList(deviceGroupsResult.getData());

@ -84,8 +84,8 @@ public class GroupManagementServiceImplTest {
.toReturn(context);
PaginationResult paginationResult = new PaginationResult();
Mockito.doReturn(paginationResult).when(groupManagementProviderService)
.getGroups(Mockito.anyString(), Mockito.any(GroupPaginationRequest.class));
Response response = groupManagementService.getGroups("test", "admin", 0, 10);
.getGroups(Mockito.anyString(), Mockito.any(GroupPaginationRequest.class), Mockito.anyBoolean());
Response response = groupManagementService.getGroups("test", "admin", 0, 10, false);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"GetGroups request failed with valid parameters");
@ -95,8 +95,8 @@ public class GroupManagementServiceImplTest {
paginationResult.setData(deviceGroupList);
paginationResult.setRecordsTotal(1);
Mockito.doReturn(paginationResult).when(groupManagementProviderService)
.getGroups(Mockito.anyString(), Mockito.any(GroupPaginationRequest.class));
response = groupManagementService.getGroups("test", "admin", 0, 10);
.getGroups(Mockito.anyString(), Mockito.any(GroupPaginationRequest.class), Mockito.anyBoolean());
response = groupManagementService.getGroups("test", "admin", 0, 10, false);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"GetGroups request failed with valid parameters");
}
@ -110,8 +110,8 @@ public class GroupManagementServiceImplTest {
.toReturn(context);
Mockito.reset(groupManagementProviderService);
Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService)
.getGroups(Mockito.anyString(), Mockito.any(GroupPaginationRequest.class));
Response response = groupManagementService.getGroups("test", "admin", 0, 10);
.getGroups(Mockito.anyString(), Mockito.any(GroupPaginationRequest.class), Mockito.anyBoolean());
Response response = groupManagementService.getGroups("test", "admin", 0, 10, false);
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"GetGroups request succeeded with in-valid parameters");
}
@ -167,16 +167,16 @@ public class GroupManagementServiceImplTest {
public void testGetGroup() throws GroupManagementException {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService"))
.toReturn(groupManagementProviderService);
Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1);
Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2);
Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getGroup(3);
Response response = groupManagementService.getGroup(1);
Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1, false);
Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2, false);
Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getGroup(3, false);
Response response = groupManagementService.getGroup(1, false);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"getGroup request failed for a request with valid parameters");
response = groupManagementService.getGroup(2);
response = groupManagementService.getGroup(2, false);
Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(),
"getGroup request returned a group for a non-existing group");
response = groupManagementService.getGroup(3);
response = groupManagementService.getGroup(3, false);
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"getGroup request returned a group for a in-valid request");
}
@ -341,14 +341,14 @@ public class GroupManagementServiceImplTest {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService"))
.toReturn(groupManagementProviderService);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier("test", "android");
Mockito.doReturn(new ArrayList<DeviceGroup>()).when(groupManagementProviderService).getGroups(deviceIdentifier);
Response response = groupManagementService.getGroups("test", "android");
Mockito.doReturn(new ArrayList<DeviceGroup>()).when(groupManagementProviderService).getGroups(deviceIdentifier, true);
Response response = groupManagementService.getGroups("test", "android", false);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"getGroups request failed with valid parameters");
Mockito.reset(groupManagementProviderService);
Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService)
.getGroups(Mockito.any(DeviceIdentifier.class));
response = groupManagementService.getGroups("test", "android2");
.getGroups(Mockito.any(DeviceIdentifier.class), Mockito.anyBoolean());
response = groupManagementService.getGroups("test", "android2", false);
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"getGroups request succeeded with in-valid parameters");
}
@ -377,7 +377,7 @@ public class GroupManagementServiceImplTest {
deviceGroup.setOwner("test");
deviceGroups.add(deviceGroup);
Mockito.doReturn(deviceGroups).when(groupManagementProviderService)
.getGroups(Mockito.any(DeviceIdentifier.class));
.getGroups(Mockito.any(DeviceIdentifier.class), Mockito.anyBoolean());
Mockito.doNothing().when(groupManagementProviderService).addDevices(Mockito.anyInt(), Mockito.any());
Mockito.doNothing().when(groupManagementProviderService).removeDevice(Mockito.anyInt(), Mockito.any());
Response response = groupManagementService.updateDeviceAssigningToGroups(deviceToGroupsAssignment);
@ -389,7 +389,7 @@ public class GroupManagementServiceImplTest {
Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(),
"updateDeviceAssigningToGroups request succeeded with in-valid parameters");
Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService)
.getGroups(Mockito.any(DeviceIdentifier.class));
.getGroups(Mockito.any(DeviceIdentifier.class), Mockito.anyBoolean());
response = groupManagementService.updateDeviceAssigningToGroups(deviceToGroupsAssignment);
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"updateDeviceAssigningToGroups request succeeded with in-valid parameters");

@ -79,7 +79,7 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori
}
}
return true;
} catch (GroupManagementException | UserStoreException e) {
} catch (GroupManagementException e) {
throw new DeviceAccessAuthorizationException("Unable to authorize the access to device : " +
deviceIdentifier.getId() + " for the user : " +
username, e);
@ -152,7 +152,7 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori
} else {
deviceAuthorizationResult.addUnauthorizedDevice(deviceIdentifier);
}
} catch (GroupManagementException | UserStoreException e) {
} catch (GroupManagementException e) {
throw new DeviceAccessAuthorizationException("Unable to authorize the access to device : " +
deviceIdentifier.getId() + " for the user : " +
username, e);
@ -192,13 +192,13 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori
}
private boolean isAuthorizedViaGroup(String username, DeviceIdentifier deviceIdentifier, String groupPermission)
throws GroupManagementException, UserStoreException {
throws GroupManagementException {
List<DeviceGroup> authorizedGroups =
DeviceManagementDataHolder.getInstance().getGroupManagementProviderService()
.getGroups(username, groupPermission);
.getGroups(username, groupPermission, false);
List<DeviceGroup> groupsWithDevice =
DeviceManagementDataHolder.getInstance().getGroupManagementProviderService()
.getGroups(deviceIdentifier);
.getGroups(deviceIdentifier, false);
for (DeviceGroup group : authorizedGroups) {
Iterator<DeviceGroup> groupsWithDeviceIterator = groupsWithDevice.iterator();
while (groupsWithDeviceIterator.hasNext()) {

@ -216,7 +216,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
try {
Connection conn = GroupManagementDAOFactory.getConnection();
stmt = conn.prepareStatement(
"DELETE GROUP_PROPERTIES WHERE GROUP_ID = ? AND TENANT_ID = ?");
"DELETE FROM GROUP_PROPERTIES WHERE GROUP_ID = ? AND TENANT_ID = ?");
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
stmt.executeUpdate();

@ -2789,7 +2789,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (log.isDebugEnabled()) {
log.debug("Create default group with name '" + groupName + "'");
}
DeviceGroup defaultGroup = service.getGroup(groupName);
DeviceGroup defaultGroup = service.getGroup(groupName, false);
if (defaultGroup == null) {
defaultGroup = new DeviceGroup(groupName);
// Setting system level user (wso2.system.user) as the owner
@ -2808,7 +2808,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
log.error(msg, e);
throw new GroupManagementException(msg, e);
}
return service.getGroup(groupName);
return service.getGroup(groupName, false);
} else {
return defaultGroup;
}

@ -69,55 +69,63 @@ public interface GroupManagementProviderService {
* Get the device group provided the device group id.
*
* @param groupId of the group.
* @param requireGroupProps to include group properties
* @return group with details.
* @throws GroupManagementException
*/
DeviceGroup getGroup(int groupId) throws GroupManagementException;
DeviceGroup getGroup(int groupId, boolean requireGroupProps) throws GroupManagementException;
/**
* Get the device group provided the device group name.
*
* @param groupName of the group.
* @param requireGroupProps to include group properties
* @return group with details.
* @throws GroupManagementException
*/
DeviceGroup getGroup(String groupName) throws GroupManagementException;
DeviceGroup getGroup(String groupName, boolean requireGroupProps) throws GroupManagementException;
/**
* Get all device groups in tenant.
*
* @param requireGroupProps to include group properties
* @return list of groups.
* @throws GroupManagementException
*/
List<DeviceGroup> getGroups() throws GroupManagementException;
List<DeviceGroup> getGroups(boolean requireGroupProps) throws GroupManagementException;
/**
* Get all device groups for user.
*
* @param username of the user.
* @param requireGroupProps to include group properties
* @return list of groups
* @throws GroupManagementException
*/
List<DeviceGroup> getGroups(String username) throws GroupManagementException;
List<DeviceGroup> getGroups(String username, boolean requireGroupProps) throws GroupManagementException;
/**
* Get device groups with pagination.
*
* @param paginationRequest to filter results
* @param requireGroupProps to include group properties
* @return list of groups.
* @throws GroupManagementException
*/
PaginationResult getGroups(GroupPaginationRequest paginationRequest) throws GroupManagementException;
PaginationResult getGroups(GroupPaginationRequest paginationRequest, boolean requireGroupProps)
throws GroupManagementException;
/**
* Get device groups belongs to specified user with pagination.
*
* @param username of the user.
* @param paginationRequest to filter results
* @param requireGroupProps to include group properties
* @return list of groups.
* @throws GroupManagementException
*/
PaginationResult getGroups(String username, GroupPaginationRequest paginationRequest) throws GroupManagementException;
PaginationResult getGroups(String username, GroupPaginationRequest paginationRequest, boolean requireGroupProps)
throws GroupManagementException;
/**
* Get all device group count in tenant
@ -169,12 +177,22 @@ public interface GroupManagementProviderService {
* @param groupId of the group
* @param startIndex for pagination.
* @param rowCount for pagination.
* @param requireDeviceProps to include device properties.
* @return list of devices in group.
* @throws GroupManagementException
*/
List<Device> getDevices(int groupId, int startIndex, int rowCount, boolean requireDeviceProps) throws GroupManagementException;
List<Device> getDevices(int groupId, int startIndex, int rowCount, boolean requireDeviceProps)
throws GroupManagementException;
List<Device> getAllDevicesOfGroup(String groupName) throws GroupManagementException;
/**
* Get all devices in device group as paginated result.
*
* @param groupName of the group.
* @param requireDeviceProps to include device properties.
* @return list of devices in group.
* @throws GroupManagementException
*/
List<Device> getAllDevicesOfGroup(String groupName, boolean requireDeviceProps) throws GroupManagementException;
/**
@ -203,8 +221,8 @@ public interface GroupManagementProviderService {
* @param deviceIdentifiers of devices.
* @throws GroupManagementException
*/
void removeDevice(int groupId, List<DeviceIdentifier> deviceIdentifiers) throws GroupManagementException,
DeviceNotFoundException;
void removeDevice(int groupId, List<DeviceIdentifier> deviceIdentifiers)
throws GroupManagementException, DeviceNotFoundException;
/**
* Get device groups of user with permission.
*
@ -213,7 +231,8 @@ public interface GroupManagementProviderService {
* @return group list with specified permissions.
* @throws GroupManagementException
*/
List<DeviceGroup> getGroups(String username, String permission) throws GroupManagementException;
List<DeviceGroup> getGroups(String username, String permission, boolean requireGroupProps)
throws GroupManagementException;
/**
* Get groups which contains particular device.
@ -222,7 +241,7 @@ public interface GroupManagementProviderService {
* @return groups contain the device.
* @throws GroupManagementException
*/
List<DeviceGroup> getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException;
List<DeviceGroup> getGroups(DeviceIdentifier deviceIdentifier, boolean requireGroupProps) throws GroupManagementException;
/**
* Checks for the default group existence and create group based on device ownership.

@ -174,14 +174,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (log.isDebugEnabled()) {
log.debug("Delete group: " + groupId);
}
DeviceGroup deviceGroup = getGroup(groupId);
DeviceGroup deviceGroup = getGroup(groupId, false);
if (deviceGroup == null) {
return false;
}
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
GroupManagementDAOFactory.beginTransaction();
this.groupDAO.deleteGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
this.groupDAO.deleteAllGroupProperties(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
this.groupDAO.deleteGroup(groupId, tenantId);
this.groupDAO.deleteAllGroupProperties(groupId, tenantId);
GroupManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) {
log.debug("DeviceGroup " + deviceGroup.getName() + " removed.");
@ -209,18 +210,19 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc}
*/
@Override
public DeviceGroup getGroup(int groupId) throws GroupManagementException {
public DeviceGroup getGroup(int groupId, boolean requireGroupProps) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get group by id: " + groupId);
}
DeviceGroup deviceGroup;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
GroupManagementDAOFactory.openConnection();
deviceGroup = this.groupDAO.getGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
if(deviceGroup != null && deviceGroup.getGroupId() > 0) {
deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(),
CarbonContext.getThreadLocalCarbonContext().getTenantId()));
deviceGroup = this.groupDAO.getGroup(groupId, tenantId);
if (requireGroupProps) {
populateGroupProperties(deviceGroup, tenantId);
}
return deviceGroup;
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while obtaining group '" + groupId + "'";
log.error(msg, e);
@ -236,14 +238,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} finally {
GroupManagementDAOFactory.closeConnection();
}
return deviceGroup;
}
/**
* {@inheritDoc}
*/
@Override
public DeviceGroup getGroup(String groupName) throws GroupManagementException {
public DeviceGroup getGroup(String groupName, boolean requireGroupProps) throws GroupManagementException {
if (groupName == null) {
String msg = "Received empty groupName for getGroup";
log.error(msg);
@ -253,13 +254,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
log.debug("Get group by name '" + groupName + "'");
}
DeviceGroup deviceGroup;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
GroupManagementDAOFactory.openConnection();
deviceGroup = this.groupDAO.getGroup(groupName, CarbonContext.getThreadLocalCarbonContext().getTenantId());
if(deviceGroup != null && deviceGroup.getGroupId() > 0) {
deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(),
CarbonContext.getThreadLocalCarbonContext().getTenantId()));
deviceGroup = this.groupDAO.getGroup(groupName, tenantId);
if (requireGroupProps) {
populateGroupProperties(deviceGroup, tenantId);
}
return deviceGroup;
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while obtaining group with name: '" + groupName + "'";
log.error(msg, e);
@ -275,11 +277,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} finally {
GroupManagementDAOFactory.closeConnection();
}
return deviceGroup;
}
@Override
public List<DeviceGroup> getGroups() throws GroupManagementException {
public List<DeviceGroup> getGroups(boolean requireGroupProps) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get groups");
}
@ -288,11 +289,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection();
deviceGroups = this.groupDAO.getGroups(tenantId);
if(deviceGroups != null && !deviceGroups.isEmpty()) {
for (DeviceGroup group : deviceGroups) {
group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId));
if (requireGroupProps) {
if (deviceGroups != null && !deviceGroups.isEmpty()) {
for (DeviceGroup group : deviceGroups) {
populateGroupProperties(group, tenantId);
}
}
}
return deviceGroups;
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
@ -308,11 +312,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} finally {
GroupManagementDAOFactory.closeConnection();
}
return deviceGroups;
}
@Override
public PaginationResult getGroups(GroupPaginationRequest request) throws GroupManagementException {
public PaginationResult getGroups(GroupPaginationRequest request, boolean requireGroupProps)
throws GroupManagementException {
if (request == null) {
String msg = "Received incomplete data for getGroup";
log.error(msg);
@ -327,9 +331,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection();
deviceGroups = this.groupDAO.getGroups(request, tenantId);
if(deviceGroups != null && !deviceGroups.isEmpty()) {
for (DeviceGroup group : deviceGroups) {
group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId));
if (requireGroupProps) {
if (deviceGroups != null && !deviceGroups.isEmpty()) {
for (DeviceGroup group : deviceGroups) {
populateGroupProperties(group, tenantId);
}
}
}
} catch (GroupManagementDAOException e) {
@ -354,7 +360,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
@Override
public List<DeviceGroup> getGroups(String username) throws GroupManagementException {
public List<DeviceGroup> getGroups(String username, boolean requireGroupProps) throws GroupManagementException {
if (username == null || username.isEmpty()) {
String msg = "Received null user name for getGroups";
log.error(msg);
@ -364,6 +370,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
log.debug("Get groups of owner '" + username + "'");
}
Map<Integer, DeviceGroup> groups = new HashMap<>();
List<DeviceGroup> mergedGroups = new ArrayList<>();
UserStoreManager userStoreManager;
try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -377,11 +384,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
deviceGroups = this.groupDAO.getGroups(roleList, tenantId);
for (DeviceGroup deviceGroup : deviceGroups) {
if(deviceGroup != null && deviceGroup.getGroupId() > 0) {
deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(), tenantId));
}
groups.put(deviceGroup.getGroupId(), deviceGroup);
}
if (requireGroupProps) {
for (DeviceGroup deviceGroup : groups.values()) {
populateGroupProperties(deviceGroup, tenantId);
mergedGroups.add(deviceGroup);
}
} else {
mergedGroups.addAll(groups.values());
}
} catch (UserStoreException | SQLException | GroupManagementDAOException e) {
String msg = "Error occurred while retrieving all groups accessible to user.";
log.error(msg, e);
@ -393,7 +405,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} finally {
GroupManagementDAOFactory.closeConnection();
}
return new ArrayList<>(groups.values());
return mergedGroups;
}
private List<Integer> getGroupIds(String username) throws GroupManagementException {
@ -425,7 +437,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
@Override
public PaginationResult getGroups(String currentUser, GroupPaginationRequest request)
public PaginationResult getGroups(String currentUser, GroupPaginationRequest request, boolean requireGroupProps)
throws GroupManagementException {
if (currentUser == null || request == null) {
String msg = "Received incomplete date for getGroups";
@ -442,9 +454,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection();
allMatchingGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId);
if(allMatchingGroups != null && !allMatchingGroups.isEmpty()) {
for (DeviceGroup group : allMatchingGroups) {
group.setGroupProperties(this.groupDAO.getAllGroupProperties(group.getGroupId(), tenantId));
if (requireGroupProps) {
if (allMatchingGroups != null && !allMatchingGroups.isEmpty()) {
for (DeviceGroup group : allMatchingGroups) {
populateGroupProperties(group, tenantId);
}
}
}
} catch (GroupManagementDAOException | SQLException e) {
@ -485,6 +499,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.closeConnection();
}
}
@Override
public int getGroupCountByStatus(String status) throws GroupManagementException {
if (log.isDebugEnabled()) {
@ -497,7 +512,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return groupDAO.getGroupCount(tenantId, status);
} catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving all groups in tenant " + tenantId
+" by status : " + status;
+ " by status : " + status;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
@ -554,12 +569,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
count = groupDAO.getOwnGroupsCount(username, tenantId);
count += groupDAO.getGroupsCount(roleList, tenantId);
return count;
} catch (UserStoreException | GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving group count of user '" + username + "'";
} catch (UserStoreException e) {
String msg = "Error occurred while retrieving role list of user '" + username + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroupCount for username '" + username + "'";
} catch (SQLException e) {
String msg = "Error occurred while opening db connection to get group count.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while retrieving group count of user '" + username + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
@ -572,7 +591,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/
@Override
public void manageGroupSharing(int groupId, List<String> newRoles)
throws GroupManagementException, RoleDoesNotExistException {
throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Manage group sharing for group: " + groupId);
}
@ -665,8 +684,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
rowCount = DeviceManagerUtil.validateDeviceListPageSize(rowCount);
GroupManagementDAOFactory.openConnection();
devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId);
if(requireDeviceProps) {
if (requireDeviceProps) {
DeviceManagementDAOFactory.openConnection();
for (Device device : devices) {
Device retrievedDevice = deviceDAO.getDeviceProps(device.getDeviceIdentifier(), tenantId);
@ -685,15 +703,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
if(requireDeviceProps){
if (requireDeviceProps) {
DeviceManagementDAOFactory.closeConnection();
}
}
return devices;
}
public List<Device> getAllDevicesOfGroup(String groupName) throws GroupManagementException {
@Override
public List<Device> getAllDevicesOfGroup(String groupName, boolean requireDeviceProps)
throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Group devices of group: " + groupName);
}
@ -702,6 +721,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
try {
GroupManagementDAOFactory.openConnection();
devices = this.groupDAO.getAllDevicesOfGroup(groupName, tenantId);
if (requireDeviceProps) {
DeviceManagementDAOFactory.openConnection();
for (Device device : devices) {
Device retrievedDevice = deviceDAO.getDeviceProps(device.getDeviceIdentifier(), tenantId);
if (retrievedDevice != null && !retrievedDevice.getProperties().isEmpty()) {
device.setProperties(retrievedDevice.getProperties());
}
}
}
} catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while getting devices in group.";
log.error(msg, e);
@ -712,6 +740,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
if (requireDeviceProps) {
DeviceManagementDAOFactory.closeConnection();
}
}
return devices;
}
@ -745,7 +776,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/
@Override
public void addDevices(int groupId, List<DeviceIdentifier> deviceIdentifiers)
throws GroupManagementException, DeviceNotFoundException {
throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Group devices to the group: " + groupId);
}
@ -791,7 +822,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/
@Override
public void removeDevice(int groupId, List<DeviceIdentifier> deviceIdentifiers)
throws GroupManagementException, DeviceNotFoundException {
throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Remove devices from the group: " + groupId);
}
@ -834,11 +865,12 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc}
*/
@Override
public List<DeviceGroup> getGroups(String username, String permission) throws GroupManagementException {
public List<DeviceGroup> getGroups(String username, String permission, boolean requireGroupProps)
throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get groups of user '" + username + "'");
}
List<DeviceGroup> deviceGroups = getGroups(username);
List<DeviceGroup> deviceGroups = getGroups(username, false);
Map<Integer, DeviceGroup> permittedDeviceGroups = new HashMap<>();
UserRealm userRealm;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -849,6 +881,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
for (String roleName : roles) {
if (userRealm.getAuthorizationManager().
isRoleAuthorized(roleName, permission, CarbonConstants.UI_PERMISSION_ACTION)) {
if (requireGroupProps) {
populateGroupProperties(deviceGroup, tenantId);
}
permittedDeviceGroups.put(deviceGroup.getGroupId(), deviceGroup);
}
}
@ -866,7 +901,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
@Override
public List<DeviceGroup> getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException {
public List<DeviceGroup> getGroups(DeviceIdentifier deviceIdentifier, boolean requireGroupProps)
throws GroupManagementException {
if (deviceIdentifier == null) {
String msg = "Received empty device identifier for getGroups";
log.error(msg);
@ -875,12 +911,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (log.isDebugEnabled()) {
log.debug("Get groups of device " + deviceIdentifier.getId());
}
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementProviderService managementProviderService = new DeviceManagementProviderServiceImpl();
try {
Device device = managementProviderService.getDevice(deviceIdentifier, false);
GroupManagementDAOFactory.openConnection();
return groupDAO.getGroups(device.getId(),
PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
List<DeviceGroup> deviceGroups = groupDAO.getGroups(device.getId(), tenantId);
if (requireGroupProps) {
if (deviceGroups != null && !deviceGroups.isEmpty()) {
for (DeviceGroup group : deviceGroups) {
populateGroupProperties(group, tenantId);
}
}
}
return deviceGroups;
} catch (DeviceManagementException | GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving device groups.";
log.error(msg, e);
@ -902,7 +946,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (log.isDebugEnabled()) {
log.debug("Create default group " + groupName);
}
DeviceGroup defaultGroup = this.getGroup(groupName);
DeviceGroup defaultGroup = this.getGroup(groupName, false);
if (defaultGroup == null) {
defaultGroup = new DeviceGroup(groupName);
defaultGroup.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE);
@ -921,7 +965,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
log.error(msg, e);
throw new GroupManagementException(msg, e);
}
return this.getGroup(groupName);
return this.getGroup(groupName, false);
} else {
return defaultGroup;
}
@ -940,25 +984,39 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
getDevice(deviceIdentifier, false);
if (device == null) {
throw new GroupManagementException("Device not found for id '" + deviceIdentifier.getId() +
"' type '" + deviceIdentifier.getType() + "'");
"' type '" + deviceIdentifier.getType() + "'");
}
} catch (DeviceManagementException e) {
throw new GroupManagementException("Device management exception occurred when retrieving device. " +
e.getMessage(), e);
e.getMessage(), e);
}
try{
try {
GroupManagementDAOFactory.openConnection();
return this.groupDAO.isDeviceMappedToGroup(groupId, device.getId(), tenantId);
} catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred when checking device, group mapping between device id '" +
deviceIdentifier.getId() + "' and group id '" + groupId + "'", e);
deviceIdentifier.getId() + "' and group id '" + groupId + "'", e);
} catch (SQLException e) {
throw new GroupManagementException("Error occurred when opening db connection to check device, group " +
"mapping between device id '" + deviceIdentifier.getId() +
"' and group id '" + groupId + "'", e);
"mapping between device id '" + deviceIdentifier.getId() +
"' and group id '" + groupId + "'", e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
/**
* This is populating group properties in to the group pass as an argument. Call should be manage the transactions.
*
* @param deviceGroup which needs to populate with properties
* @param tenantId of the caller
* @throws GroupManagementDAOException on DAO exceptions
*/
private void populateGroupProperties(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException {
if (deviceGroup != null && deviceGroup.getGroupId() > 0) {
deviceGroup.setGroupProperties(this.groupDAO.getAllGroupProperties(deviceGroup.getGroupId(),
tenantId));
}
}
}

@ -167,7 +167,7 @@ public class DeviceAccessAuthorizationServiceTest {
GroupManagementProviderService groupManagementProviderService = DeviceManagementDataHolder.getInstance()
.getGroupManagementProviderService();
groupManagementProviderService.createDefaultGroup(DEFAULT_GROUP);
int groupId = groupManagementProviderService.getGroup(DEFAULT_GROUP).getGroupId();
int groupId = groupManagementProviderService.getGroup(DEFAULT_GROUP, false).getGroupId();
//Sharing group with admin and non admin roles
groupManagementProviderService.manageGroupSharing(groupId, new ArrayList<>(Arrays.asList(ADMIN_ROLE,
NON_ADMIN_ROLE)));

@ -68,28 +68,28 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage
+ "negative conditions", expectedExceptions = {GroupManagementException.class},
expectedExceptionsMessageRegExp = "Error occurred while obtaining group.*")
public void testGetGroup() throws GroupManagementException {
groupManagementProviderService.getGroup(1);
groupManagementProviderService.getGroup(1, false);
}
@Test(description = "This method tests the getGroup method of the GroupManagementProviderService under "
+ "negative conditions", expectedExceptions = {GroupManagementException.class},
expectedExceptionsMessageRegExp = "Error occurred while obtaining group with name.*")
public void testGetGroupWithName() throws GroupManagementException {
groupManagementProviderService.getGroup("1");
groupManagementProviderService.getGroup("1", false);
}
@Test(description = "This method tests the getGroups method of the GroupManagementProviderService under negative "
+ "conditions", expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp =
"Error occurred while retrieving all groups in tenant.*")
public void testGetGroups() throws GroupManagementException {
groupManagementProviderService.getGroups();
groupManagementProviderService.getGroups(true);
}
@Test(description = "This method tests the getGroups method of the GroupManagementProviderService under negative "
+ "conditions", expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp =
"Error occurred while retrieving all groups accessible to user.*")
public void testGetGroupsWithUserName() throws GroupManagementException {
groupManagementProviderService.getGroups("test");
groupManagementProviderService.getGroups("test", false);
}
@Test(description = "This method tests the getGroupCount method under negative circumstances", expectedExceptions
@ -111,14 +111,14 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage
+ "circumstances", expectedExceptions = {GroupManagementException.class},
expectedExceptionsMessageRegExp = "Error occurred while retrieving all groups in tenant")
public void testGetGroupsWithPaginationRequest() throws GroupManagementException {
groupManagementProviderService.getGroups(TestUtils.createPaginationRequest());
groupManagementProviderService.getGroups(TestUtils.createPaginationRequest(), false);
}
@Test(description = "This method tests the getGroups method with pagination request and username under negative "
+ "circumstances", expectedExceptions = {GroupManagementException.class},
expectedExceptionsMessageRegExp = "Error occurred while retrieving all groups accessible to user.")
public void testGetGroupsWithPaginationRequestAndUserName() throws GroupManagementException {
groupManagementProviderService.getGroups("test", TestUtils.createPaginationRequest());
groupManagementProviderService.getGroups("test", TestUtils.createPaginationRequest(), false);
}
@Test(description = "This method tests the get roles method under negative circumstances",
@ -152,6 +152,6 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage
expectedExceptionsMessageRegExp = "Received empty device identifier for getGroups",
expectedExceptions = {GroupManagementException.class})
public void testGetGroupsWithDeviceIdentifier() throws GroupManagementException {
groupManagementProviderService.getGroups((DeviceIdentifier) null);
groupManagementProviderService.getGroups((DeviceIdentifier) null, false);
}
}

@ -86,20 +86,20 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test(dependsOnMethods = ("createGroup"))
public void updateGroup() throws GroupManagementException, GroupNotExistException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName());
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName(), false);
deviceGroup.setName(deviceGroup.getName() + "_UPDATED");
groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId());
}
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class})
public void getGroupNull() throws GroupManagementException, GroupNotExistException {
groupManagementProviderService.getGroup(null);
groupManagementProviderService.getGroup(null, false);
}
// Rename again to use in different place.
@Test(dependsOnMethods = ("updateGroup"))
public void updateGroupSecondTime() throws GroupManagementException, GroupNotExistException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName() + "_UPDATED");
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup1().getName() + "_UPDATED", true);
deviceGroup.setName(TestUtils.createDeviceGroup1().getName());
groupManagementProviderService.updateGroup(deviceGroup, deviceGroup.getGroupId());
}
@ -111,14 +111,14 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class, GroupNotExistException.class})
public void updateGroupErrorNotExist() throws GroupManagementException, GroupNotExistException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup2().getName());
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup2().getName(), false);
deviceGroup.setName(deviceGroup.getName() + "_UPDATED");
groupManagementProviderService.updateGroup(deviceGroup, 6);
}
@Test(dependsOnMethods = ("createGroup"))
public void deleteGroup() throws GroupManagementException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup4().getName());
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup4().getName(), false);
Assert.assertTrue(groupManagementProviderService.deleteGroup(deviceGroup.getGroupId()));
}
@ -131,47 +131,47 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test(dependsOnMethods = ("createGroup"))
public void getGroup() throws GroupManagementException {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup3().getName());
Assert.assertNotNull(groupManagementProviderService.getGroup(deviceGroup.getGroupId()));
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup3().getName(), false);
Assert.assertNotNull(groupManagementProviderService.getGroup(deviceGroup.getGroupId(), false));
}
@Test(dependsOnMethods = ("createGroup"))
public void getGroupByName() throws GroupManagementException {
Assert.assertNotNull(groupManagementProviderService.getGroup(TestUtils.createDeviceGroup3().getName()));
Assert.assertNotNull(groupManagementProviderService.getGroup(TestUtils.createDeviceGroup3().getName(), false));
}
@Test(dependsOnMethods = ("createGroup"))
public void getGroups() throws GroupManagementException {
List<DeviceGroup> deviceGroups = groupManagementProviderService.getGroups();
List<DeviceGroup> deviceGroups = groupManagementProviderService.getGroups(true);
Assert.assertNotNull(deviceGroups);
}
@Test(dependsOnMethods = ("createGroup"))
public void getGroupsByUsername() throws GroupManagementException {
List<DeviceGroup> deviceGroups = groupManagementProviderService.getGroups("admin");
List<DeviceGroup> deviceGroups = groupManagementProviderService.getGroups("admin", true);
Assert.assertNotNull(deviceGroups);
}
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class})
public void getGroupsByUsernameError() throws GroupManagementException {
groupManagementProviderService.getGroups((String) null);
groupManagementProviderService.getGroups((String) null, false);
}
@Test(dependsOnMethods = ("createGroup"))
public void getGroupsByPagination() throws GroupManagementException {
PaginationResult result = groupManagementProviderService.getGroups(TestUtils.createPaginationRequest());
PaginationResult result = groupManagementProviderService.getGroups(TestUtils.createPaginationRequest(), true);
Assert.assertNotNull(result);
}
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class})
public void getGroupsByPaginationError() throws GroupManagementException {
groupManagementProviderService.getGroups((GroupPaginationRequest) null);
groupManagementProviderService.getGroups((GroupPaginationRequest) null, true);
}
@Test(dependsOnMethods = ("createGroup"))
public void getGroupsByUsernameAndPagination()
throws GroupManagementException {
PaginationResult result = groupManagementProviderService.getGroups("admin", TestUtils.createPaginationRequest());
PaginationResult result = groupManagementProviderService.getGroups("admin", TestUtils.createPaginationRequest(), false);
Assert.assertNotNull(result);
}
@ -179,7 +179,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test(dependsOnMethods = ("createGroup"), expectedExceptions = {GroupManagementException.class})
public void getGroupsByUsernameAndPaginationError()
throws GroupManagementException {
groupManagementProviderService.getGroups(null, TestUtils.createPaginationRequest());
groupManagementProviderService.getGroups(null, TestUtils.createPaginationRequest(), true);
}
@Test(dependsOnMethods = ("createGroup"))
@ -214,7 +214,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
userStoreManager.addRole("TEST_ROLE_3", null, permissions);
groupManagementProviderService.manageGroupSharing(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup1().getName()).getGroupId(), newRoles);
TestUtils.createDeviceGroup1().getName(), false).getGroupId(), newRoles);
}
@Test(dependsOnMethods = ("createGroup"))
@ -244,25 +244,25 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
DeviceConfigurationManager.getInstance().getDeviceManagementConfig().setDeviceCacheConfiguration(configuration);
List<DeviceIdentifier> list = TestUtils.getDeviceIdentifiersList();
groupManagementProviderService.addDevices(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup1().getName()).getGroupId(), list);
TestUtils.createDeviceGroup1().getName(), false).getGroupId(), list);
groupManagementProviderService.addDevices(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup2().getName()).getGroupId(), list);
TestUtils.createDeviceGroup2().getName(), false).getGroupId(), list);
groupManagementProviderService.addDevices(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup3().getName()).getGroupId(), list);
TestUtils.createDeviceGroup3().getName(), false).getGroupId(), list);
}
@Test(dependsOnMethods = ("addDevices"))
public void removeDevice() throws GroupManagementException, DeviceNotFoundException {
List<DeviceIdentifier> list = TestUtils.getDeviceIdentifiersList();
groupManagementProviderService.removeDevice(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup2().getName()).getGroupId(), list);
TestUtils.createDeviceGroup2().getName(), false).getGroupId(), list);
groupManagementProviderService.removeDevice(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup3().getName()).getGroupId(), list);
TestUtils.createDeviceGroup3().getName(), false).getGroupId(), list);
}
@Test(dependsOnMethods = ("createGroup"))
public void getGroupsByUsernameAndPermissions() throws GroupManagementException {
List<DeviceGroup> groups = groupManagementProviderService.getGroups("admin", "/permission/device-mgt/admin/groups");
List<DeviceGroup> groups = groupManagementProviderService.getGroups("admin", "/permission/device-mgt/admin/groups", true);
Assert.assertNotNull(groups);
}
@ -271,7 +271,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
DeviceIdentifier identifier = new DeviceIdentifier();
identifier.setId("12345");
identifier.setType(TestDataHolder.TEST_DEVICE_TYPE);
List<DeviceGroup> groups = groupManagementProviderService.getGroups(identifier);
List<DeviceGroup> groups = groupManagementProviderService.getGroups(identifier, true);
Assert.assertNotNull(groups);
}
@ -290,7 +290,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
List<DeviceIdentifier> list = TestUtils.getDeviceIdentifiersList();
boolean isMapped = groupManagementProviderService
.isDeviceMappedToGroup(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup1().getName()).getGroupId(), list.get(0));
TestUtils.createDeviceGroup1().getName(), true).getGroupId(), list.get(0));
Assert.assertEquals(isMapped, true);
}
@ -306,7 +306,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest
@Test(dependsOnMethods = {"createGroup", "updateGroupSecondTime"}, expectedExceptions = {GroupManagementException.class})
public void checkNullDeviceBelongsToGroup() throws GroupManagementException {
groupManagementProviderService.isDeviceMappedToGroup(groupManagementProviderService.getGroup(
TestUtils.createDeviceGroup1().getName()).getGroupId(), null);
TestUtils.createDeviceGroup1().getName(), true).getGroupId(), null);
}
}

@ -98,7 +98,7 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
pipDevice.setDeviceIdentifier(deviceIdentifier);
pipDevice.setUserId(device.getEnrolmentInfo().getOwner());
pipDevice.setOwnershipType(device.getEnrolmentInfo().getOwnership().toString());
pipDevice.setDeviceGroups(groupManagementProviderService.getGroups(pipDevice.getDeviceIdentifier()));
pipDevice.setDeviceGroups(groupManagementProviderService.getGroups(pipDevice.getDeviceIdentifier(), false));
} else {
throw new PolicyManagementException("Device details cannot be null.");

@ -1160,7 +1160,7 @@ public class PolicyManagerImpl implements PolicyManager {
private List<DeviceGroupWrapper> getDeviceGroupNames(List<DeviceGroupWrapper> groupWrappers) throws GroupManagementException {
GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl();
for (DeviceGroupWrapper wrapper : groupWrappers) {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(wrapper.getId());
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(wrapper.getId(), false);
wrapper.setName(deviceGroup.getName());
wrapper.setOwner(deviceGroup.getOwner());
}

@ -247,7 +247,7 @@ public abstract class BasePolicyManagementDAOTest {
List<DeviceIdentifier> groupDevices = new ArrayList<>();
groupDevices.add(deviceIdentifier);
try {
DeviceGroup group = groupMgtService.getGroup(groupName);
DeviceGroup group = groupMgtService.getGroup(groupName, false);
groupMgtService.addDevices(group.getGroupId(), groupDevices);
} catch (DeviceNotFoundException | GroupManagementException e) {
String msg = "Failed to add device " + deviceIdentifier.getId() + " to group " + groupName;

@ -94,7 +94,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest {
operationManager = new OperationManagerImpl(DEVICE_TYPE_A, deviceManagementService);
enrollDevice(DEVICE1, DEVICE_TYPE_A);
createDeviceGroup(GROUP1);
DeviceGroup group1 = groupMgtService.getGroup(GROUP1);
DeviceGroup group1 = groupMgtService.getGroup(GROUP1, false);
addDeviceToGroup(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), GROUP1);
Profile profile = new Profile();

@ -85,7 +85,7 @@ public class FeatureManagerImplTest extends BasePolicyManagementDAOTest {
enrollDevice(DEVICE4, DEVICE_TYPE_D);
createDeviceGroup(GROUP4);
DeviceGroup group4 = groupMgtService.getGroup(GROUP4);
DeviceGroup group4 = groupMgtService.getGroup(GROUP4, false);
addDeviceToGroup(new DeviceIdentifier(DEVICE4, DEVICE_TYPE_D), GROUP4);
Profile profile = new Profile();

@ -97,7 +97,7 @@ public class MonitoringManagerImplTest extends BasePolicyManagementDAOTest{
enrollDevice(DEVICE5, DEVICE_TYPE_E);
createDeviceGroup(GROUP5);
addDeviceToGroup(new DeviceIdentifier(DEVICE5, DEVICE_TYPE_E), GROUP5);
DeviceGroup group5 = groupMgtService.getGroup(GROUP5);
DeviceGroup group5 = groupMgtService.getGroup(GROUP5, false);
device5 = deviceMgtService.getAllDevices().get(0);

@ -77,7 +77,7 @@ public class ProfileManagerImplTest extends BasePolicyManagementDAOTest {
operationManager = new OperationManagerImpl(DEVICE_TYPE_C, deviceManagementService);
enrollDevice(DEVICE3, DEVICE_TYPE_C);
createDeviceGroup(GROUP3);
DeviceGroup group1 = groupMgtService.getGroup(GROUP3);
DeviceGroup group1 = groupMgtService.getGroup(GROUP3, false);
addDeviceToGroup(new DeviceIdentifier(DEVICE3, DEVICE_TYPE_C), GROUP3);
}

Loading…
Cancel
Save