GroupManagementServiceImpl implements GroupManagementService { @Override public Response createGroup(DeviceGroup group) { String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (group == null) { return Response.status(Response.Status.BAD_REQUEST).build(); } group.setOwner(owner); try { DeviceMgtAPIUtils.getGroupManagementProviderService().createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); return Response.status(Response.Status.CREATED).build(); } catch (GroupManagementException e) { String msg = "Error occurred while adding new group."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (GroupAlreadyExistException e) { String msg = "Group already exists with name " + group.getName() + "."; log.warn(msg); return Response.status(Response.Status.CONFLICT).entity(msg).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 the behaviour of createGroup method under valid and invalid scenarios") public void testCreateGroup() throws GroupManagementException, GroupAlreadyExistException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext")) .toReturn(context); Response response = groupManagementService.createGroup(null); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), "createGroup request succeeded with the group equals to null"); Mockito.doNothing().when(groupManagementProviderService) .createGroup(Mockito.any(), Mockito.any(), Mockito.any()); response = groupManagementService.createGroup(new DeviceGroup()); Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), "createGroup request failed for a request with valid parameters"); Mockito.reset(groupManagementProviderService); Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService) .createGroup(Mockito.any(), Mockito.any(), Mockito.any()); response = groupManagementService.createGroup(new DeviceGroup()); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "createGroup request succeeded for a request with in-valid parameters"); Mockito.reset(groupManagementProviderService); Mockito.doThrow(new GroupAlreadyExistException()).when(groupManagementProviderService) .createGroup(Mockito.any(), Mockito.any(), Mockito.any()); response = groupManagementService.createGroup(new DeviceGroup()); Assert.assertEquals(response.getStatus(), Response.Status.CONFLICT.getStatusCode(), "createGroup request succeeded for a request with in-valid parameters"); }