GroupManagementServiceImpl implements GroupManagementService { @Override public Response getGroups(String name, String owner, int offset, int limit) { try { RequestValidationUtil.validatePaginationParameters(offset, limit); String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); GroupPaginationRequest request = new GroupPaginationRequest(offset, limit); request.setGroupName(name); request.setOwner(owner); PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService() .getGroups(currentUser, request); DeviceGroupList deviceGroupList = new DeviceGroupList(); if (deviceGroupsResult.getData() != null && deviceGroupsResult.getRecordsTotal() > 0) { deviceGroupList.setList(deviceGroupsResult.getData()); deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal()); } else { deviceGroupList.setList(new ArrayList<>()); deviceGroupList.setCount(0); } return Response.status(Response.Status.OK).entity(deviceGroupList).build(); } catch (GroupManagementException e) { String error = "Error occurred while getting the groups."; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).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 getGroups with device id and device type under different conditions") public void testGetGroupsWithDeviceId() throws GroupManagementException { 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"); 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"); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "getGroups request succeeded with in-valid parameters"); }
@Test(description = "This method tests the behaviour of getGroups under valid conditions") public void testGetGroups() throws GroupManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext")) .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); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetGroups request failed with valid parameters"); Mockito.reset(groupManagementProviderService); List<DeviceGroup> deviceGroupList = new ArrayList<>(); deviceGroupList.add(new DeviceGroup("test")); 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); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetGroups request failed with valid parameters"); }
@Test(description = "This method tests the behaviour of getGroups method under negative circumstances", dependsOnMethods = {"testGetGroups"}) public void testGetGroupUnderNegativeConditions() throws GroupManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext")) .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); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "GetGroups request succeeded with in-valid parameters"); }