final RestMethod restMethod = new RestMethod(); restMethod.setCurrentResponseSequenceIndex(0); restMethod.setForwardedEndpoint(FORWARD_ENDPOINT); restMethod.setHttpMethod(HttpMethod.GET); restMethod.setId(METHOD_ID); restMethod.setInvokeAddress("http://localhost:8080" + CONTEXT + SLASH + MOCK + SLASH + REST + SLASH + PROJECT + SLASH + PROJECT_ID + SLASH + APPLICATION + SLASH + APPLICATION_ID + "/method/test"); restMethod.setName("Method name"); restMethod.setNetworkDelay(0); restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); restMethod.setSimulateNetworkDelay(false); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setMockResponses(Arrays.asList(restMockResponse));
final HttpMethod httpMethod, final String forwardAddress, final boolean generateResponse){ final RestMethod restMethod = new RestMethod(); restMethod.setHttpMethod(httpMethod); restMethod.setName(methodName); restMethod.setStatus(RestMethodStatus.MOCKED); restMethod.setResponseStrategy(RestResponseStrategy.SEQUENCE); restMethod.setForwardedEndpoint(forwardAddress); restMethod.getMockResponses().addAll(mockResponses); } else { RestMockResponse generatedResponse = generateResponse(); restMethod.getMockResponses().add(generatedResponse);
method.setResourceId(savedResource.getId()); if(!Strings.isNullOrEmpty(method.getDefaultQueryMockResponseId())){ method.setDefaultMockResponseId(method.getDefaultQueryMockResponseId()); method.setDefaultQueryMockResponseId(null); for(RestMockResponse mockResponse : method.getMockResponses()){ mockResponse.setMethodId(savedMethod.getId()); this.mockResponseRepository.save(mockResponse);
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; } }
/** * The method will add a new {@link RestMethod} and update an already existing {@link RestMethod}. * @param newRestMethod The new {@link RestMethod} that might be added to the final list of {@link RestMethod} (resultRestApplication). * @param existingRestMethods A list of existing {@link RestMethod} * @param resultRestMethods A list of the result of {@link RestMethod}. These will be the new {@link RestMethod}. * @since 1.10 */ private void updateRestMethod(final RestMethod newRestMethod, final List<RestMethod> existingRestMethods, final List<RestMethod> resultRestMethods) { final RestMethod existingRestMethod = findRestMethod(existingRestMethods, newRestMethod.getName()); // The new REST method does not exists. Add it to the resource if (existingRestMethod == null) { resultRestMethods.add(newRestMethod); return; } // THe REST method already exists. Update it. existingRestMethod.setHttpMethod(newRestMethod.getHttpMethod()); resultRestMethods.add(existingRestMethod); }
/** * 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 restMethod = new RestMethod(); restMethod.setStatus(methodV1.getStatus()); restMethod.setSimulateNetworkDelay(methodV1.getSimulateNetworkDelay()); restMethod.setNetworkDelay(methodV1.getNetworkDelay()); restMethod.setResponseStrategy(methodV1.getResponseStrategy()); restMethod.setName(methodV1.getName()); restMethod.setId(methodV1.getId()); restMethod.setHttpMethod(methodV1.getHttpMethod()); restMethod.setForwardedEndpoint(methodV1.getForwardedEndpoint()); restMethod.setDefaultBody(methodV1.getDefaultBody()); restMethod.setCurrentResponseSequenceIndex(methodV1.getCurrentResponseSequenceIndex()); restMethod.setResourceId(resource.getId()); resource.getMethods().add(restMethod); mockResponse.setMethodId(restMethod.getId()); restMethod.getMockResponses().add(mockResponse);
Assert.assertEquals(methodName, restMethod.getName()); Assert.assertEquals(httpMethod, restMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, restMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.RANDOM, restMethod.getResponseStrategy()); Assert.assertEquals(0, restMethod.getNetworkDelay()); Assert.assertFalse(restMethod.getSimulateNetworkDelay()); Assert.assertNull(restMethod.getId()); Assert.assertNull(restMethod.getResourceId()); Assert.assertNull(restMethod.getInvokeAddress()); Assert.assertNull(restMethod.getForwardedEndpoint()); Assert.assertNull(restMethod.getDefaultBody()); Assert.assertEquals(1, restMethod.getMockResponses().size()); RestMockResponse restMockResponse = restMethod.getMockResponses().get(0); Assert.assertEquals(AUTO_GENERATED_MOCK_RESPONSE_DEFAULT_NAME, restMockResponse.getName()); Assert.assertEquals(Integer.valueOf(DEFAULT_RESPONSE_CODE), restMockResponse.getHttpStatusCode()); Assert.assertEquals(0, restMethod.getMockResponses().size());
@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()); }
/** * 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()); } }
for(RestMockResponse mockResponse : restMethod.getMockResponses()){ if(mockResponse.getStatus().equals(RestMockResponseStatus.ENABLED)){ enabledMockResponses.add(mockResponse); throw new RestException("No mocked response created for operation " + restMethod.getName()); } else if(restMethod.getResponseStrategy().equals(RestResponseStrategy.RANDOM)){ final Integer responseIndex = RANDOM.nextInt(mockResponses.size()); mockResponse = mockResponses.get(responseIndex); } else if(restMethod.getResponseStrategy().equals(RestResponseStrategy.SEQUENCE)){ Integer currentSequenceNumber = restMethod.getCurrentResponseSequenceIndex(); if(currentSequenceNumber >= mockResponses.size()){ currentSequenceNumber = 0; .restApplicationId(applicationId) .restResourceId(resourceId) .restMethodId(restMethod.getId()) .currentRestMockResponseSequenceIndex(currentSequenceNumber + 1) .build()); } else if(restMethod.getResponseStrategy().equals(RestResponseStrategy.QUERY_MATCH)){ mockResponse = mockResponses.stream() .filter(tmp -> RestParameterQueryValidator.validate(tmp.getParameterQueries(), pathParameters)) } else if(restMethod.getResponseStrategy().equals(RestResponseStrategy.XPATH)){ for (RestMockResponse testedMockResponse : mockResponses) { for(RestXPathExpression xPathExpression : testedMockResponse.getXpathExpressions()){ } else if(restMethod.getResponseStrategy().equals(RestResponseStrategy.JSON_PATH)){ for (RestMockResponse testedMockResponse : mockResponses) { for(RestJsonPathExpression jsonPathExpression : testedMockResponse.getJsonPathExpressions()){
@PreAuthorize("hasAuthority('MODIFIER') or hasAuthority('ADMIN')") @RequestMapping(value = "/{projectId}/application/{applicationId}/resource/{resourceId}/create/method", method = RequestMethod.POST) public ModelAndView createMethod(@PathVariable final String projectId, @PathVariable final String applicationId, @PathVariable final String resourceId, @ModelAttribute(name="command") final CreateRestMethodCommand command) { final CreateRestMethodOutput output = serviceProcessor.process(CreateRestMethodInput.builder() .projectId(projectId) .applicationId(applicationId) .resourceId(resourceId) .method(command.getRestMethod()) .build()); return redirect("/rest/project/" + projectId + "/application/" + applicationId + "/resource/" + resourceId + "/method/" + output.getCreatedRestMethod().getId()); }
/** * 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<UpdateRestResourcesForwardedEndpointOutput> process(final ServiceTask<UpdateRestResourcesForwardedEndpointInput> serviceTask) { final UpdateRestResourcesForwardedEndpointInput input = serviceTask.getInput(); final List<RestResource> resources = this.resourceRepository.findWithApplicationId(input.getRestApplicationId()); resources.stream() .map(RestResource::getId) .map(this.methodRepository::findWithResourceId) .flatMap(List::stream) .forEach(restMethod -> { restMethod.setForwardedEndpoint(input.getForwardedEndpoint()); this.methodRepository.update(restMethod.getId(), restMethod); }); return createServiceResult(UpdateRestResourcesForwardedEndpointOutput.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()); } }
RestResponse response = null; try { event = new RestEvent(restMethod.getName(), restRequest, projectId, applicationId, resourceId, restMethod.getId()); if (RestMethodStatus.DISABLED.equals(restMethod.getStatus())) { throw new RestException("The requested REST method, " + restMethod.getName() + ", is disabled"); } else if (RestMethodStatus.FORWARDED.equals(restMethod.getStatus())) { response = forwardRequest(restRequest, projectId, applicationId, resourceId, restMethod, pathParameters); } else if (RestMethodStatus.RECORDING.equals(restMethod.getStatus())) { response = forwardRequestAndRecordResponse(restRequest, projectId, applicationId, resourceId, restMethod, pathParameters); } else if (RestMethodStatus.RECORD_ONCE.equals(restMethod.getStatus())) { response = forwardRequestAndRecordResponseOnce(restRequest, projectId, applicationId, resourceId, restMethod, pathParameters); } else if (RestMethodStatus.ECHO.equals(restMethod.getStatus())) { response = echoResponse(restRequest); } else { // Status.MOCKED if(restMethod.getSimulateNetworkDelay() && restMethod.getNetworkDelay() >= 0){ try { Thread.sleep(restMethod.getNetworkDelay()); } catch (InterruptedException e) { LOGGER.error("Unable to simulate network delay", e);
@Test public void projectFunctionalityUpdate() throws Exception { final String projectId = "projectId"; final String applicationId = "applicationId"; final String resourceId = "resourceId"; final String[] restMethodIds = {"restMethod1", "restMethod2"}; final RestMethodModifierCommand command = new RestMethodModifierCommand(); command.setRestMethodIds(restMethodIds); command.setRestMethodStatus("MOCKED"); final RestMethod restMethod1 = new RestMethod(); restMethod1.setId("restMethod1"); restMethod1.setName("restMethod1"); final RestMethod restMethod2 = new RestMethod(); restMethod2.setId("restMethod2"); restMethod2.setName("restMethod2"); Mockito.when(serviceProcessor.process(Mockito.any(ReadRestMethodInput.class))) .thenReturn(ReadRestMethodOutput.builder().restMethod(restMethod1).build()) .thenReturn(ReadRestMethodOutput.builder().restMethod(restMethod2).build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + projectId + SLASH + APPLICATION + SLASH + applicationId + SLASH + RESOURCE + SLASH + resourceId) .param("action", "update") .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)); Mockito.verify(serviceProcessor, Mockito.times(2)).process(Mockito.isA(ReadRestMethodInput.class)); Mockito.verify(serviceProcessor, Mockito.times(2)).process(Mockito.isA(UpdateRestMethodInput.class)); }
@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)); }
final String[] restMethodIds = {"restMethod1", "restMethod2"}; final RestMethod restMethod1 = new RestMethod(); restMethod1.setName("restMethod1"); final RestMethod restMethod2 = new RestMethod(); restMethod2.setName("restMethod2");
@Test(expected = IllegalArgumentException.class) public void testProcessResourceNotFound(){ RestResource restResource1 = RestResourceGenerator.generateRestResource(); restResource1.setId("Resource1"); restResource1.setUri("/user"); RestMethod restMethod1 = RestMethodGenerator.generateRestMethod(); restMethod1.setHttpMethod(HttpMethod.POST); RestMethod restMethod2 = RestMethodGenerator.generateRestMethod(); restMethod2.setHttpMethod(HttpMethod.GET); RestMethod restMethod3 = RestMethodGenerator.generateRestMethod(); restMethod3.setHttpMethod(HttpMethod.PUT); restResource1.getMethods().add(restMethod1); restResource1.getMethods().add(restMethod2); restResource1.getMethods().add(restMethod3); Mockito.when(resourceRepository.findWithApplicationId("RestApplicationId")).thenReturn(Arrays.asList(restResource1)); Mockito.when(methodRepository.findWithResourceId(restResource1.getId())).thenReturn(Arrays.asList(restMethod1, restMethod2, restMethod3)); Mockito.when(mockResponseRepository.findWithMethodId(Mockito.anyString())).thenReturn(new ArrayList<>()); IdentifyRestMethodInput input1 = IdentifyRestMethodInput.builder() .restProjectId("RestProjectId") .restApplicationId("RestApplicationId") .restResourceUri("/random") .httpMethod(HttpMethod.GET) .build(); ServiceTask<IdentifyRestMethodInput> serviceTask1 = new ServiceTask<IdentifyRestMethodInput>(input1); service.process(serviceTask1); }
final List<RestMethod> methods = this.methodRepository.findWithResourceId(restResource.getId()); for(RestMethod restMethod : methods){ if(input.getHttpMethod().equals(restMethod.getHttpMethod())) { foundRestMethod = restMethod; break; UrlUtility.getPathParameters(restResource.getUri(), restResourceUriParts); final List<RestMockResponse> mockResponses = this.mockResponseRepository.findWithMethodId(foundRestMethod.getId()); foundRestMethod.setMockResponses(mockResponses); .restApplicationId(input.getRestApplicationId()) .restResourceId(restResource.getId()) .restMethodId(foundRestMethod.getId()) .restMethod(foundRestMethod) .pathParameters(pathParameters)