public static RestMockResponse generateRestMockResponse(){ final RestParameterQuery parameterQuery = new RestParameterQuery(); parameterQuery.setParameter("Parameter"); parameterQuery.setQuery("Query"); parameterQuery.setMatchAny(false); parameterQuery.setMatchCase(false); parameterQuery.setMatchRegex(false); final RestMockResponse restMockResponse = new RestMockResponse(); restMockResponse.setName("Rest mock response name"); restMockResponse.setBody("Rest mock response body"); restMockResponse.setId("REST MOCK RESPONSE"); restMockResponse.setStatus(RestMockResponseStatus.ENABLED); restMockResponse.setHttpStatusCode(200); restMockResponse.setParameterQueries(ImmutableList.of(parameterQuery)); return restMockResponse; } }
private Optional<RestMockResponse> getDefaultMockResponse(final RestMethod restMethod, final List<RestMockResponse> mockResponses){ final String defaultResponseId = restMethod.getDefaultMockResponseId(); if(defaultResponseId != null && !defaultResponseId.isEmpty()){ LOGGER.info("Use the default response"); for (RestMockResponse tmpMockResponse : mockResponses) { if(defaultResponseId.equals(tmpMockResponse.getId())){ return Optional.of(tmpMockResponse); } } LOGGER.error("Unable to find the default response"); } return Optional.empty(); }
/** * 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()); } }
/** * The method generates a default response. * @return The newly generated {@link RestMockResponse}. */ protected RestMockResponse generateResponse(){ RestMockResponse restMockResponse = new RestMockResponse(); restMockResponse.setName(AUTO_GENERATED_MOCK_RESPONSE_DEFAULT_NAME); restMockResponse.setHttpStatusCode(DEFAULT_RESPONSE_CODE); restMockResponse.setStatus(RestMockResponseStatus.ENABLED); restMockResponse.setUsingExpressions(true); return restMockResponse; }
private RestMockResponse createrestMockResponse(String name) { RestMockResponse dto = new RestMockResponse(); dto.setName(name); return dto; } }
final RestMockResponse restMockResponse1 = new RestMockResponse(); restMockResponse1.setBody(XML_RESPONSE_BODY); restMockResponse1.setContentEncodings(new ArrayList<>()); restMockResponse1.setHttpHeaders(Arrays.asList(contentTypeHeader, acceptHeader)); restMockResponse1.setHttpStatusCode(200); restMockResponse1.setId("MockResponseId1"); restMockResponse1.setName("Mocked response 1"); restMockResponse1.setStatus(RestMockResponseStatus.ENABLED); restMockResponse1.setUsingExpressions(false); restMockResponse1.setParameterQueries(ImmutableList.of(parameterQuery)); final RestMockResponse restMockResponse2 = new RestMockResponse(); restMockResponse2.setBody(QUERY_DEFAULT_RESPONSE_BODY); restMockResponse2.setContentEncodings(new ArrayList<>()); restMockResponse2.setHttpHeaders(Arrays.asList(contentTypeHeader, acceptHeader)); restMockResponse2.setHttpStatusCode(200); restMockResponse2.setId("MockResponseId2"); restMockResponse2.setName("Mocked response 2"); restMockResponse2.setStatus(RestMockResponseStatus.ENABLED); restMockResponse2.setUsingExpressions(false); restMockResponse2.setParameterQueries(ImmutableList.of());
String responseCode = response.code().value(); int httpStatusCode = super.extractHttpStatusCode(responseCode); RestMockResponse restMockResponse = new RestMockResponse(); restMockResponse.setName(RESPONSE_NAME_PREFIX + (index + 1)); restMockResponse.setHttpStatusCode(httpStatusCode); restMockResponse.setStatus(RestMockResponseStatus.ENABLED); } else { restMockResponse.setStatus(RestMockResponseStatus.DISABLED); restMockResponse.setBody(body); httpHeader.setName(parameter.name()); httpHeader.setValue(parameter.defaultValue()); restMockResponse.getHttpHeaders().add(httpHeader);
/** * 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 * @param restRequest The incoming request * @param restMethod The REST method which the incoming request belongs to * @return The response received from the external endpoint */ protected RestResponse forwardRequestAndRecordResponse(final RestRequest restRequest, final String projectId, final String applicationId, final String resourceId, final RestMethod restMethod, final Map<String, String> pathParameters){ final RestResponse response = forwardRequest(restRequest, projectId, applicationId, resourceId, restMethod, pathParameters); final RestMockResponse mockResponse = new RestMockResponse(); final Date date = new Date(); mockResponse.setBody(response.getBody()); mockResponse.setStatus(RestMockResponseStatus.ENABLED); mockResponse.setHttpHeaders(response.getHttpHeaders()); mockResponse.setName(RECORDED_RESPONSE_NAME + SPACE + DATE_FORMAT.format(date)); mockResponse.setHttpStatusCode(response.getHttpStatusCode()); serviceProcessor.processAsync(CreateRestMockResponseInput.builder() .projectId(projectId) .applicationId(applicationId) .resourceId(resourceId) .methodId(restMethod.getId()) .mockResponse(mockResponse) .build()); return response; }
.filter(method -> method.getName().equals("successful operation (application/xml)")) .findFirst() .get(); Assert.assertEquals("successful operation (application/xml)", response200Xml.getName()); Assert.assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + "<Mock><name>${RANDOM_STRING()}</name><id>${RANDOM_LONG()}</id><createdBy>${RANDOM_STRING()}</createdBy>" + "<mockStatus>${RANDOM_INTEGER()}</mockStatus></Mock>", response200Xml.getBody()); Assert.assertEquals(Integer.valueOf(200), response200Xml.getHttpStatusCode()); Assert.assertEquals(RestMockResponseStatus.ENABLED, response200Xml.getStatus()); Assert.assertTrue(response200Xml.isUsingExpressions()); Assert.assertTrue(response200Xml.getContentEncodings().isEmpty()); Assert.assertNull(response200Xml.getId()); Assert.assertNull(response200Xml.getMethodId()); Assert.assertEquals(1, response200Xml.getHttpHeaders().size()); Assert.assertEquals("Content-Type", response200Xml.getHttpHeaders().get(0).getName()); Assert.assertEquals("application/xml", response200Xml.getHttpHeaders().get(0).getValue()); .filter(method -> method.getName().equals("successful operation (application/json)")) .findFirst() .get(); Assert.assertEquals("successful operation (application/json)", response200Json.getName()); Assert.assertEquals("{\"name\":\"${RANDOM_STRING()}\",\"id\":\"${RANDOM_LONG()}\"," + "\"createdBy\":\"${RANDOM_STRING()}\",\"mockStatus\":\"${RANDOM_INTEGER()}\"}", response200Json.getBody()); Assert.assertEquals(Integer.valueOf(200), response200Json.getHttpStatusCode());
if(mockResponse.getStatus().equals(RestMockResponseStatus.ENABLED)){ enabledMockResponses.add(mockResponse); final Collection<String> mockResponseContentTypeValues = getHeaderValues(CONTENT_TYPE, mockResponse.getHttpHeaders()); mockResponseContentTypeValues.retainAll(acceptHeaderValues); if(!mockResponseContentTypeValues.isEmpty()){ } else if(restMethod.getResponseStrategy().equals(RestResponseStrategy.QUERY_MATCH)){ mockResponse = mockResponses.stream() .filter(tmp -> RestParameterQueryValidator.validate(tmp.getParameterQueries(), pathParameters)) .findFirst() .orElse(null); for(RestXPathExpression xPathExpression : testedMockResponse.getXpathExpressions()){ if (XPathUtility.isValidXPathExpr(restRequest.getBody(), xPathExpression.getExpression())) { mockResponse = testedMockResponse; for(RestJsonPathExpression jsonPathExpression : testedMockResponse.getJsonPathExpressions()){ if (JsonPathUtility.isValidJsonPathExpr(restRequest.getBody(), jsonPathExpression.getExpression())) { mockResponse = testedMockResponse; String body = mockResponse.getBody(); if(mockResponse.isUsingExpressions()){ final ExpressionArgumentMap pathParametersArgument = new ExpressionArgumentMap(); pathParameters.forEach((key, value) -> { response.setMockResponseName(mockResponse.getName()); response.setHttpStatusCode(mockResponse.getHttpStatusCode()); response.setHttpHeaders(mockResponse.getHttpHeaders());
restMockResponse.setStatus(status); serviceProcessor.process(UpdateRestMockResponseInput.builder() .restProjectId(restProjectId) restMockResponse.setId(null); restMockResponse.setName(String.format("%s %s", copyOfLabel, restMockResponse.getName())); serviceProcessor.process(CreateRestMockResponseInput.builder() .projectId(restProjectId)
String body = bodyEntry.getValue(); RestMockResponse restMockResponse = generateResponse(httpStatusCode, response); restMockResponse.setName(restMockResponse.getName() + " (" + contentType + ")"); restMockResponse.setBody(body); restMockResponse.getHttpHeaders().add(httpHeader); mockResponses.add(restMockResponse);
@PreAuthorize("hasAuthority('READER') or hasAuthority('MODIFIER') or hasAuthority('ADMIN')") @RequestMapping(value = "/{restProjectId}/application/{restApplicationId}/resource/{restResourceId}/method/{restMethodId}/create/response", method = RequestMethod.GET) public ModelAndView displayCreatePage(@PathVariable final String restProjectId, @PathVariable final String restApplicationId, @PathVariable final String restResourceId, @PathVariable final String restMethodId) { final ReadRestMethodOutput output = serviceProcessor.process(ReadRestMethodInput.builder() .restProjectId(restProjectId) .restApplicationId(restApplicationId) .restResourceId(restResourceId) .restMethodId(restMethodId).build()); final ReadRestResourceQueryParametersOutput resourceQueryParameters = serviceProcessor.process(ReadRestResourceQueryParametersInput.builder() .projectId(restProjectId) .applicationId(restApplicationId) .resourceId(restResourceId) .build()); final RestMockResponse mockResponse = new RestMockResponse(); mockResponse.setBody(output.getRestMethod().getDefaultBody()); mockResponse.setHttpStatusCode(DEFAULT_HTTP_STATUS_CODE); final ModelAndView model = createPartialModelAndView(PAGE); model.addObject(REST_PROJECT_ID, restProjectId); model.addObject(REST_APPLICATION_ID, restApplicationId); model.addObject(REST_RESOURCE_ID, restResourceId); model.addObject(REST_METHOD_ID, restMethodId); model.addObject(REST_MOCK_RESPONSE, mockResponse); model.addObject(REST_MOCK_RESPONSE_STATUSES, RestMockResponseStatus.values()); model.addObject(REST_QUERY_PARAMETERS, resourceQueryParameters.getQueries()); return model; }
@Test public void testServiceFunctionalityDuplicate() throws Exception { final String projectId = "projectId"; final String applicationId = "applicationId"; final String resourceId = "resourceId"; final String methodId = "resourceId"; final String[] restMockResponseIds = {"MockResponse1", "MockResponse2"}; final RestMockResponse restMockResponse1 = new RestMockResponse(); restMockResponse1.setId("MockResponseId1"); final RestMockResponse restMockResponse2 = new RestMockResponse(); restMockResponse2.setId("MockResponseId2"); Mockito.when(serviceProcessor.process(Mockito.any(ReadRestMockResponseInput.class))) .thenReturn(ReadRestMockResponseOutput.builder().restMockResponse(restMockResponse1).build()) .thenReturn(ReadRestMockResponseOutput.builder().restMockResponse(restMockResponse2).build()); final RestMockResponseModifierCommand command = new RestMockResponseModifierCommand(); command.setRestMockResponseIds(restMockResponseIds); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + projectId + SLASH + APPLICATION + SLASH + applicationId + SLASH + RESOURCE + SLASH + resourceId + SLASH + METHOD + SLASH + methodId) .param("action", "duplicate").flashAttr("command", command); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().is3xxRedirection()) .andExpect(MockMvcResultMatchers.model().size(1)) .andExpect(MockMvcResultMatchers.redirectedUrl("/web/rest/project/" + projectId + "/application/" + applicationId + "/resource/" + resourceId + "/method/" + methodId)); Mockito.verify(serviceProcessor, Mockito.times(2)).process(Mockito.isA(ReadRestMockResponseInput.class)); Mockito.verify(serviceProcessor, Mockito.times(2)).process(Mockito.isA(CreateRestMockResponseInput.class)); }
final String resourceId = this.methodRepository.getResourceId(mockResponse.getMethodId()); final String applicationId = this.resourceRepository.getApplicationId(resourceId); final String projectId = this.applicationRepository.getProjectId(applicationId); final SearchResult searchResult = new SearchResult(); searchResult.setTitle(mockResponse.getName()); searchResult.setLink(REST + SLASH + PROJECT + SLASH + projectId + SLASH + APPLICATION + SLASH + applicationId + SLASH + RESOURCE + SLASH + resourceId + SLASH + METHOD + SLASH + mockResponse.getMethodId() + SLASH + RESPONSE + SLASH + mockResponse.getId()); searchResult.setDescription(REST_TYPE + COMMA + responseType); searchResults.add(searchResult);
if(this.mockResponseRepository.exists(mockResponse.getId())){ throw new IllegalArgumentException("A mocked response with the following key already exists: " + mockResponse.getId()); if(mockResponse.getParameterQueries() == null){ List<RestParameterQuery> parameterQueries = new CopyOnWriteArrayList<RestParameterQuery>(); mockResponse.setParameterQueries(parameterQueries);
final RestMockResponse restMockResponse = new RestMockResponse(); restMockResponse.setBody(XML_RESPONSE_BODY); restMockResponse.setContentEncodings(new ArrayList<>()); restMockResponse.setHttpHeaders(Arrays.asList(contentTypeHeader, acceptHeader)); restMockResponse.setHttpStatusCode(200); restMockResponse.setId("MockResponseId"); restMockResponse.setName("Mocked response"); restMockResponse.setStatus(RestMockResponseStatus.ENABLED); restMockResponse.setUsingExpressions(false);
String responseCode = response.code().value(); int httpStatusCode = super.extractHttpStatusCode(responseCode); RestMockResponse restMockResponse = new RestMockResponse(); restMockResponse.setName(RESPONSE_NAME_PREFIX + (index + 1)); restMockResponse.setHttpStatusCode(httpStatusCode); restMockResponse.setStatus(RestMockResponseStatus.ENABLED); } else { restMockResponse.setStatus(RestMockResponseStatus.DISABLED); restMockResponse.setBody(body); httpHeader.setName(parameter.name()); httpHeader.setValue(parameter.defaultValue()); restMockResponse.getHttpHeaders().add(httpHeader);