GroupManagementServiceImpl implements GroupManagementService { @Override public Response updateDeviceAssigningToGroups(DeviceToGroupsAssignment deviceToGroupsAssignment) { try { List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); deviceIdentifiers.add(deviceToGroupsAssignment.getDeviceIdentifier()); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); List<DeviceGroup> deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier()); for (DeviceGroup group : deviceGroups) { Integer groupId = group.getGroupId(); if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) { deviceToGroupsAssignment.getDeviceGroupIds().remove(groupId); } else if (!CarbonConstants.REGISTRY_SYSTEM_USERNAME.equals(group.getOwner())) { DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers); } } for (int groupId : deviceToGroupsAssignment.getDeviceGroupIds()) { DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); } return Response.status(Response.Status.OK).build(); } catch (GroupManagementException e) { String msg = "Error occurred while assigning device to groups."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (DeviceNotFoundException e) { return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); } }  @Override Response getGroups(String name, String owner, int offset, int limit); @Override Response getGroupCount(); @Override Response createGroup(DeviceGroup group); @Override Response getGroup(int groupId); @Override Response updateGroup(int groupId, DeviceGroup deviceGroup); @Override Response deleteGroup(int groupId); @Override Response manageGroupSharing(int groupId, List<String> userRoles); @Override Response getRolesOfGroup(int groupId); @Override Response getDevicesOfGroup(int groupId, int offset, int limit); @Override Response getDeviceCountOfGroup(int groupId); @Override Response addDevicesToGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers); @Override Response removeDevicesFromGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers); @Override Response updateDeviceAssigningToGroups(DeviceToGroupsAssignment deviceToGroupsAssignment); @Override Response getGroups(String deviceId, String deviceType);  }
@Test(description = "This method tests updateDeviceAssigningToGroups under different conditions.") public void testUpdateDeviceAssigningToGroups() throws GroupManagementException, DeviceNotFoundException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); Mockito.reset(groupManagementProviderService); DeviceToGroupsAssignment deviceToGroupsAssignment = new DeviceToGroupsAssignment(); List<Integer> groupIds = new ArrayList<>(); groupIds.add(1); groupIds.add(2); deviceToGroupsAssignment.setDeviceGroupIds(groupIds); deviceToGroupsAssignment.setDeviceIdentifier(new DeviceIdentifier("test", "android")); List<DeviceGroup> deviceGroups = new ArrayList<>(); DeviceGroup deviceGroup = new DeviceGroup(); deviceGroup.setGroupId(1); deviceGroups.add(deviceGroup); deviceGroup = new DeviceGroup(); deviceGroup.setGroupId(3); deviceGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME); deviceGroups.add(deviceGroup); deviceGroup = new DeviceGroup(); deviceGroup.setGroupId(4); deviceGroup.setOwner("test"); deviceGroups.add(deviceGroup); Mockito.doReturn(deviceGroups).when(groupManagementProviderService) .getGroups(Mockito.any(DeviceIdentifier.class)); Mockito.doNothing().when(groupManagementProviderService).addDevices(Mockito.anyInt(), Mockito.any()); Mockito.doNothing().when(groupManagementProviderService).removeDevice(Mockito.anyInt(), Mockito.any()); Response response = groupManagementService.updateDeviceAssigningToGroups(deviceToGroupsAssignment); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "updateDeviceAssigningToGroups request failed with valid parameters"); Mockito.doThrow(new DeviceNotFoundException()).when(groupManagementProviderService) .removeDevice(Mockito.anyInt(), Mockito.any()); response = groupManagementService.updateDeviceAssigningToGroups(deviceToGroupsAssignment); 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)); response = groupManagementService.updateDeviceAssigningToGroups(deviceToGroupsAssignment); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "updateDeviceAssigningToGroups request succeeded with in-valid parameters"); }