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.setName(httpMethod.name()); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setResponseStrategy(RestResponseStrategy.RANDOM); restMethod.setHttpMethod(httpMethod);
restMethod.setName(method.displayName().value()); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setResponseStrategy(RestResponseStrategy.RANDOM); restMethod.setHttpMethod(httpMethod);
restMethod.setName(methodName); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); restMethod.setForwardedEndpoint(forwardAddress);
@Test public void testMockedSequence(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(""); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestMethod restMethod = getMockedRestMethod(); restMethod.setResponseStrategy(RestResponseStrategy.RANDOM); 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_RESPONSE_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(ACCEPT_HEADER)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(ACCEPT_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<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()); } }
@Test public void testMockedXpathMatch(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(XML_REQUEST_BODY); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestXPathExpression xPathExpression = new RestXPathExpression(); xPathExpression.setExpression("//request/variable[text()='Value 1']"); final RestMethod restMethod = getMockedRestMethod(); restMethod.getMockResponses().get(0).getXpathExpressions().add(xPathExpression); restMethod.setResponseStrategy(RestResponseStrategy.XPATH); 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.postMethod(PROJECT_ID, APPLICATION_ID, httpServletRequest, httpServletResponse); Assert.assertEquals(XML_RESPONSE_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(ACCEPT_HEADER)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(ACCEPT_HEADER).get(0)); }
@Test public void testMockedJsonPathMatch(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(JSON_REQUEST_BODY); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestJsonPathExpression restJsonPathExpression = new RestJsonPathExpression(); restJsonPathExpression.setExpression("$.request[?(@.variable == 'Value 1')]"); final RestMethod restMethod = getMockedRestMethod(); restMethod.getMockResponses().get(0).getJsonPathExpressions().add(restJsonPathExpression); restMethod.setResponseStrategy(RestResponseStrategy.JSON_PATH); 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.postMethod(PROJECT_ID, APPLICATION_ID, httpServletRequest, httpServletResponse); Assert.assertEquals(XML_RESPONSE_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(ACCEPT_HEADER)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(ACCEPT_HEADER).get(0)); }
@Test public void testMockedRandom(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(""); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestMethod restMethod = getMockedRestMethod(); restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); 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); final ResponseEntity responseEntity = restServiceController.getMethod(PROJECT_ID, APPLICATION_ID, httpServletRequest, httpServletResponse); Assert.assertEquals(XML_RESPONSE_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(ACCEPT_HEADER)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(ACCEPT_HEADER).get(0)); }
@Test public void testMockedQuery(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(""); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestMethod restMethod = getQueryRestMethod(); restMethod.setResponseStrategy(RestResponseStrategy.QUERY_MATCH); 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); final ResponseEntity responseEntity = restServiceController.getMethod(PROJECT_ID, APPLICATION_ID, httpServletRequest, httpServletResponse); Assert.assertEquals(XML_RESPONSE_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(ACCEPT_HEADER)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(ACCEPT_HEADER).get(0)); }
@Test public void testMockedQueryDefaultMockResponse(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(""); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestMethod restMethod = getQueryRestMethod(); restMethod.setResponseStrategy(RestResponseStrategy.QUERY_MATCH); final IdentifyRestMethodOutput identifyRestMethodOutput = IdentifyRestMethodOutput.builder() .restProjectId(PROJECT_ID) .restApplicationId(APPLICATION_ID) .restResourceId(RESOURCE_ID) .restMethodId(METHOD_ID) .restMethod(restMethod) .pathParameters(NO_MATCHING_PATH_PARAMETERS) .build(); when(serviceProcessor.process(any(IdentifyRestMethodInput.class))).thenReturn(identifyRestMethodOutput); final ResponseEntity responseEntity = restServiceController.getMethod(PROJECT_ID, APPLICATION_ID, httpServletRequest, httpServletResponse); Assert.assertEquals(QUERY_DEFAULT_RESPONSE_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(ACCEPT_HEADER)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(ACCEPT_HEADER).get(0)); }
restMethod.setName("Method name"); restMethod.setNetworkDelay(0); restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); restMethod.setSimulateNetworkDelay(false); restMethod.setStatus(RestMethodStatus.MOCKED);
/** * 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.setSimulateNetworkDelay(methodV1.getSimulateNetworkDelay()); restMethod.setNetworkDelay(methodV1.getNetworkDelay()); restMethod.setResponseStrategy(methodV1.getResponseStrategy()); restMethod.setName(methodV1.getName()); restMethod.setId(methodV1.getId());
restMethod.setName("Method name"); restMethod.setNetworkDelay(0); restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); restMethod.setSimulateNetworkDelay(false); restMethod.setStatus(RestMethodStatus.MOCKED);
@Test public void testProcessWithoutResponseStrategy(){ final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); restMethod.setResponseStrategy(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(), returnedRestMethod.getStatus()); Assert.assertEquals(restMethod.getMockResponses(), returnedRestMethod.getMockResponses()); Assert.assertEquals(restMethod.getResponseStrategy(), RestResponseStrategy.RANDOM); }