ActivityProviderServiceImpl implements ActivityInfoProviderService { @GET @Override @Path("/ids") public Response getActivities(@QueryParam("ids") ActivityIdList activityIdList) { List<String> idList; idList = activityIdList.getIdList(); if (idList == null || idList.isEmpty()) { String msg = "Activity Ids shouldn't be empty"; log.error(msg); return Response.status(400).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } Response validationFailedResponse = validateAdminUser(); if (validationFailedResponse == null) { List<Activity> activities; ActivityList activityList = new ActivityList(); DeviceManagementProviderService dmService; try { for (String id : idList) { RequestValidationUtil.validateActivityId(id); } dmService = DeviceMgtAPIUtils.getDeviceManagementService(); activities = dmService.getOperationByActivityIds(idList); if (!activities.isEmpty()) { activityList.setList(activities); int count = activities.size(); if (log.isDebugEnabled()) { log.debug("Number of activities : " + count); } activityList.setCount(count); return Response.ok().entity(activityList).build(); } else { String msg = "No activity found with the given IDs."; log.error(msg); return Response.status(404).entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } catch (OperationManagementException e) { String msg = "ErrorResponse occurred while fetching the activity list for the supplied ids."; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } else { return validationFailedResponse; } }  @GET @Override @Path("/{id}") Response getActivity(@PathParam("id")
                                @Size(max = 45) String id,
                                @HeaderParam("If-Modified-Since") String ifModifiedSince); @GET @Override @Path("/ids") Response getActivities(@QueryParam("ids") ActivityIdList activityIdList); @GET @Override @Path("/{id}/{devicetype}/{deviceid}") Response getActivityByDevice(@PathParam("id")
                                        @Size(max = 45) String id,
                                        @PathParam("devicetype")
                                        @Size(max = 45) String devicetype,
                                        @PathParam("deviceid")
                                        @Size(max = 45) String deviceid,
                                        @HeaderParam("If-Modified-Since") String ifModifiedSince); @GET @Override @Path("/type/{operationCode}") Response getActivities(@PathParam("operationCode") String operationCode,
                                  @QueryParam("offset") int offset, @QueryParam("limit") int limit); @GET @Override Response getActivities(@QueryParam("since") String since,  @QueryParam("initiatedBy")String initiatedBy,
                                  @QueryParam("offset") int offset, @QueryParam("limit") int limit,
                                  @HeaderParam("If-Modified-Since") String ifModifiedSince);  }
@Test(description = "This method tests getting details of list of given activity IDs") public void testGetActivitiesWithActivityIdList() throws OperationManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Mockito.when(this.deviceManagementProviderService.getOperationByActivityIds(idList)).thenReturn( activities); Response response = this.activityInfoProviderService.getActivities(activityList); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }
@Test(description = "This method tests trying to get details activity IDs when call with empty list") public void testGetActivitiesWithEmptyActivityIdList() throws OperationManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); Response response = this.activityInfoProviderService.getActivities(activityListEmpty); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }
@Test(description = "This method tests trying to get details of a list activity IDs which does not exists") public void testGetActivitiesWithNonExistingActivityIdList() throws OperationManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Mockito.when(this.deviceManagementProviderService.getOperationByActivityIds(idList1)).thenReturn( activities1); Response response = this.activityInfoProviderService.getActivities(activityList); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }
@Test(description = "This method tests getting details of an activity for a given device") public void testGetActivities() throws OperationManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(true); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Mockito.when( this.deviceManagementProviderService.getFilteredActivities(OPERATION_CODE, OFFSET, LIMIT)) .thenReturn(activities); Response response = this.activityInfoProviderService.getActivities(OPERATION_CODE, OFFSET, LIMIT); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }
@Test(description = "This method tests getting details of an activity for a given device") public void testGetActivitiesForInvalidUser() throws OperationManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(false); PowerMockito.stub(PowerMockito.method(RequestValidationUtil.class, "validateActivityId")); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Mockito.when( this.deviceManagementProviderService.getFilteredActivities(OPERATION_CODE, OFFSET, LIMIT)) .thenReturn(activities); Response response = this.activityInfoProviderService.getActivities(OPERATION_CODE, OFFSET, LIMIT); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }