restMethod.setName(methodName); restMethod.setHttpMethod(HttpMethod.valueOf(methodType)); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setResponseStrategy(RestResponseStrategy.RANDOM); restMethod.setMockResponses(new ArrayList<RestMockResponse>());
public static RestMethod generateRestMethod(){ final RestMethod restMethod = new RestMethod(); restMethod.setId("REST METHOD"); restMethod.setName("REST method name"); restMethod.setForwardedEndpoint("Forward endpoint"); restMethod.setDefaultBody("Default body"); restMethod.setCurrentResponseSequenceIndex(1); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setHttpMethod(HttpMethod.GET); restMethod.setMockResponses(new ArrayList<RestMockResponse>()); restMethod.setResponseStrategy(RestResponseStrategy.RANDOM); return restMethod; } }
restMethod.setStatus(restMethodStatus);
restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setResponseStrategy(RestResponseStrategy.RANDOM); restMethod.setHttpMethod(httpMethod);
restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setResponseStrategy(RestResponseStrategy.RANDOM); restMethod.setHttpMethod(httpMethod);
restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); restMethod.setForwardedEndpoint(forwardAddress);
@Test public void testEcho(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(XML_REQUEST_BODY); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestMethod restMethod = getMockedRestMethod(); restMethod.setStatus(RestMethodStatus.ECHO); final IdentifyRestMethodOutput identifyRestMethodOutput = IdentifyRestMethodOutput.builder() .restProjectId(PROJECT_ID) .restApplicationId(APPLICATION_ID) .restResourceId(RESOURCE_ID) .restMethodId(METHOD_ID) .restMethod(restMethod) .pathParameters(PATH_PARAMETERS) .build(); when(serviceProcessor.process(any(IdentifyRestMethodInput.class))).thenReturn(identifyRestMethodOutput); when(httpServletRequest.getRequestURI()).thenReturn(CONTEXT + SLASH + MOCK + SLASH + REST + SLASH + PROJECT + SLASH + PROJECT_ID + SLASH + APPLICATION + SLASH + APPLICATION_ID + "/method/test"); final ResponseEntity responseEntity = restServiceController.getMethod(PROJECT_ID, APPLICATION_ID, httpServletRequest, httpServletResponse); Assert.assertEquals(XML_REQUEST_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(APPLICATION_JSON, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); }
/** * The process message is responsible for processing an incoming serviceTask and generate * a response based on the incoming serviceTask input * @param serviceTask The serviceTask that will be processed by the service * @return A result based on the processed incoming serviceTask * @see ServiceTask * @see ServiceResult */ @Override public ServiceResult<UpdateRestResourcesStatusOutput> process(final ServiceTask<UpdateRestResourcesStatusInput> serviceTask) { final UpdateRestResourcesStatusInput input = serviceTask.getInput(); final List<RestResource> resources = this.resourceRepository.findWithApplicationId(input.getRestApplicationId()); for(RestResource restResource : resources){ final List<RestMethod> methods = this.methodRepository.findWithResourceId(restResource.getId()); for(RestMethod restMethod : methods){ restMethod.setStatus(input.getRestMethodStatus()); this.methodRepository.update(restMethod.getId(), restMethod); } } return createServiceResult(UpdateRestResourcesStatusOutput.builder().build()); } }
/** * The process message is responsible for processing an incoming serviceTask and generate * a response based on the incoming serviceTask input * @param serviceTask The serviceTask that will be processed by the service * @return A result based on the processed incoming serviceTask * @see ServiceTask * @see ServiceResult */ @Override public ServiceResult<UpdateRestApplicationsStatusOutput> process(final ServiceTask<UpdateRestApplicationsStatusInput> serviceTask) { final UpdateRestApplicationsStatusInput input = serviceTask.getInput(); final List<RestResource> resources = this.resourceRepository.findWithApplicationId(input.getRestApplicationId()); for(RestResource restResource : resources){ final List<RestMethod> methods = this.methodRepository.findWithResourceId(restResource.getId()); for(RestMethod restMethod : methods){ restMethod.setStatus(input.getRestMethodStatus()); this.methodRepository.update(restMethod.getId(), restMethod); } } return createServiceResult(UpdateRestApplicationsStatusOutput.builder().build()); } }
/** * The method provides the functionality to forward a request to another endpoint. The response * will be recorded and can later be used as a mocked response. The REST method status will be updated * to mocked. * @param restRequest The incoming request * @param projectId The id of the project that the incoming request belongs to * @param applicationId The id of the application that the incoming request belongs to * @param resourceId The id of the resource that the incoming request belongs to * @param restMethod The REST method which the incoming request belongs to * @return The response received from the external endpoint */ protected RestResponse forwardRequestAndRecordResponseOnce(final RestRequest restRequest, final String projectId, final String applicationId, final String resourceId, final RestMethod restMethod, final Map<String, String> pathParameters){ final RestResponse response = forwardRequestAndRecordResponse(restRequest, projectId, applicationId, resourceId, restMethod, pathParameters); restMethod.setStatus(RestMethodStatus.MOCKED); serviceProcessor.process(UpdateRestMethodInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .restResourceId(resourceId) .restMethodId(restMethod.getId()) .restMethod(restMethod) .build()); return response; }
/** * The process message is responsible for processing an incoming serviceTask and generate * a response based on the incoming serviceTask input * @param serviceTask The serviceTask that will be processed by the service * @return A result based on the processed incoming serviceTask * @see ServiceTask * @see ServiceResult */ @Override public ServiceResult<CreateRestMethodOutput> process(final ServiceTask<CreateRestMethodInput> serviceTask) { final CreateRestMethodInput input = serviceTask.getInput(); final RestMethod restMethod = input.getMethod(); restMethod.setResourceId(input.getResourceId()); if(restMethod.getStatus() == null){ restMethod.setStatus(RestMethodStatus.MOCKED); } if(restMethod.getResponseStrategy() == null){ restMethod.setResponseStrategy(RestResponseStrategy.RANDOM); } final RestMethod createdRestMethod = this.methodRepository.save(restMethod); return createServiceResult(CreateRestMethodOutput.builder() .createdRestMethod(createdRestMethod) .build()); } }
restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); restMethod.setSimulateNetworkDelay(false); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setMockResponses(Arrays.asList(restMockResponse));
/** * The process message is responsible for processing an incoming serviceTask and generate * a response based on the incoming serviceTask input * @param serviceTask The serviceTask that will be processed by the service * @return A result based on the processed incoming serviceTask * @see ServiceTask * @see ServiceResult */ @Override public ServiceResult<UpdateRestMethodOutput> process(final ServiceTask<UpdateRestMethodInput> serviceTask) { final UpdateRestMethodInput input = serviceTask.getInput(); final RestMethod updatedRestMethod = input.getRestMethod(); final RestMethod existing = this.methodRepository.findOne(input.getRestMethodId()); existing.setName(updatedRestMethod.getName()); existing.setHttpMethod(updatedRestMethod.getHttpMethod()); existing.setResponseStrategy(updatedRestMethod.getResponseStrategy()); existing.setStatus(updatedRestMethod.getStatus()); existing.setForwardedEndpoint(updatedRestMethod.getForwardedEndpoint()); existing.setNetworkDelay(updatedRestMethod.getNetworkDelay()); existing.setSimulateNetworkDelay(updatedRestMethod.getSimulateNetworkDelay()); existing.setDefaultMockResponseId(updatedRestMethod.getDefaultMockResponseId()); this.methodRepository.update(input.getRestMethodId(), existing); return createServiceResult(UpdateRestMethodOutput.builder() .restMethod(updatedRestMethod) .build()); } }
restMethod.setStatus(methodV1.getStatus()); restMethod.setSimulateNetworkDelay(methodV1.getSimulateNetworkDelay()); restMethod.setNetworkDelay(methodV1.getNetworkDelay());
restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); restMethod.setSimulateNetworkDelay(false); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setMockResponses(Arrays.asList(restMockResponse1, restMockResponse2)); restMethod.setDefaultResponseName("Mocked response 2");
@Test public void testProcessWithoutStatus(){ final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); restMethod.setStatus(null); Mockito.when(methodRepository.save(Mockito.any(RestMethod.class))).thenReturn(restMethod); final CreateRestMethodInput input = CreateRestMethodInput.builder() .projectId(restProject.getId()) .applicationId(restApplication.getId()) .resourceId(restResource.getId()) .method(restMethod) .build(); final ServiceTask<CreateRestMethodInput> serviceTask = new ServiceTask<CreateRestMethodInput>(input); final ServiceResult<CreateRestMethodOutput> serviceResult = service.process(serviceTask); final CreateRestMethodOutput createRestApplicationOutput = serviceResult.getOutput(); final RestMethod returnedRestMethod = createRestApplicationOutput.getCreatedRestMethod(); Assert.assertEquals(restMethod.getName(), returnedRestMethod.getName()); Assert.assertEquals(restMethod.getHttpMethod(), returnedRestMethod.getHttpMethod()); Assert.assertEquals(restMethod.getDefaultBody(), returnedRestMethod.getDefaultBody()); Assert.assertEquals(restMethod.getForwardedEndpoint(), returnedRestMethod.getForwardedEndpoint()); Assert.assertEquals(restMethod.getStatus(), RestMethodStatus.MOCKED); Assert.assertEquals(restMethod.getMockResponses(), returnedRestMethod.getMockResponses()); Assert.assertEquals(restMethod.getResponseStrategy(), returnedRestMethod.getResponseStrategy()); }