for(RestXPathExpression xPathExpression : testedMockResponse.getXpathExpressions()){ if (XPathUtility.isValidXPathExpr(restRequest.getBody(), xPathExpression.getExpression())) { mockResponse = testedMockResponse;
@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)); }
/** * 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<UpdateRestMockResponseOutput> process(final ServiceTask<UpdateRestMockResponseInput> serviceTask) { final UpdateRestMockResponseInput input = serviceTask.getInput(); final RestMockResponse existing = this.mockResponseRepository.findOne(input.getRestMockResponseId()); final RestMockResponse updatedRestMockResponse = input.getRestMockResponse(); existing.setName(updatedRestMockResponse.getName()); existing.setBody(updatedRestMockResponse.getBody()); existing.setHttpStatusCode(updatedRestMockResponse.getHttpStatusCode()); existing.setHttpHeaders(updatedRestMockResponse.getHttpHeaders()); existing.setStatus(updatedRestMockResponse.getStatus()); existing.setUsingExpressions(updatedRestMockResponse.isUsingExpressions()); existing.setParameterQueries(updatedRestMockResponse.getParameterQueries()); existing.setXpathExpressions(updatedRestMockResponse.getXpathExpressions()); existing.setJsonPathExpressions(updatedRestMockResponse.getJsonPathExpressions()); this.mockResponseRepository.update(input.getRestMockResponseId(), existing); return createServiceResult(UpdateRestMockResponseOutput.builder() .updatedRestMockResponse(updatedRestMockResponse) .build()); } }