ActivityProviderServiceImpl implements ActivityInfoProviderService { @GET @Override @Path("/{id}") public Response getActivity(@PathParam("id") @Size(max = 45) String id, @HeaderParam("If-Modified-Since") String ifModifiedSince) { Activity activity; DeviceManagementProviderService dmService; Response response = validateAdminUser(); if (response == null) { try { RequestValidationUtil.validateActivityId(id); dmService = DeviceMgtAPIUtils.getDeviceManagementService(); activity = dmService.getOperationByActivityId(id); if (activity == null) { return Response.status(404).entity( new ErrorResponse.ErrorResponseBuilder().setMessage("No activity can be " + "found upon the provided activity id '" + id + "'").build()).build(); } return Response.status(Response.Status.OK).entity(activity).build(); } catch (OperationManagementException e) { String msg = "ErrorResponse occurred while fetching the activity for the supplied id."; log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } else { return response; } }  @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 an activity with an admin user with an existing" + " activity Id") public void testGetActivitiesWithValidAdminUserWithValidId() 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.getOperationByActivityId(TEST_ACTIVITY_ID)) .thenReturn(activity); Response response = this.activityInfoProviderService.getActivity(TEST_ACTIVITY_ID, IF_MODIFIED_SINCE); 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 with an invalid admin user") public void testGetActivitiesWithInvalidAdminUserWithValidId() throws OperationManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "isAdmin")).toReturn(false); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Mockito.when(this.deviceManagementProviderService.getOperationByActivityId(TEST_ACTIVITY_ID)) .thenReturn(activity); Response response = this.activityInfoProviderService.getActivity(TEST_ACTIVITY_ID, IF_MODIFIED_SINCE); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }
@Test(description = "This method tests getting details of an activity which does not exists") public void testGetActivitiesWithNonExistingActivityID() 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.getOperationByActivityId(TEST_ACTIVITY_ID)) .thenReturn(null); Response response = this.activityInfoProviderService.getActivity(TEST_ACTIVITY_ID, IF_MODIFIED_SINCE); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }
@Test(description = "This method tests the getActivity method under negative conditions.") public void testGetActivitiesWithOperationManagementException() 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.getOperationByActivityId(Mockito.any())).thenThrow( new OperationManagementException()); Response response = this.activityInfoProviderService.getActivity(TEST_ACTIVITY_ID, IF_MODIFIED_SINCE); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); }