if(mockResponses.isEmpty()){ 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()){ .currentRestMockResponseSequenceIndex(currentSequenceNumber + 1) .build()); } else if(restMethod.getResponseStrategy().equals(RestResponseStrategy.QUERY_MATCH)){ mockResponse = mockResponses.stream() .filter(tmp -> RestParameterQueryValidator.validate(tmp.getParameterQueries(), pathParameters)) mockResponse = this.getDefaultMockResponse(restMethod, mockResponses).orElse(null); } 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()){
/** * 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 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()); }
@Test public void testProcess(){ final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); 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(), returnedRestMethod.getResponseStrategy()); }
Assert.assertEquals(HttpMethod.GET, getAllMockServicesMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, getAllMockServicesMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, getAllMockServicesMethod.getResponseStrategy()); Assert.assertEquals(Integer.valueOf(0), getAllMockServicesMethod.getCurrentResponseSequenceIndex()); Assert.assertEquals(0L, getAllMockServicesMethod.getNetworkDelay()); Assert.assertEquals(HttpMethod.POST, createMockMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, createMockMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, createMockMethod.getResponseStrategy()); Assert.assertEquals(Integer.valueOf(0), createMockMethod.getCurrentResponseSequenceIndex()); Assert.assertEquals(0L, createMockMethod.getNetworkDelay()); Assert.assertEquals(HttpMethod.HEAD, headMockMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, headMockMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, headMockMethod.getResponseStrategy()); Assert.assertEquals(Integer.valueOf(0), headMockMethod.getCurrentResponseSequenceIndex()); Assert.assertEquals(0L, headMockMethod.getNetworkDelay()); Assert.assertEquals(HttpMethod.OPTIONS, optionsMockMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, optionsMockMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, optionsMockMethod.getResponseStrategy()); Assert.assertEquals(Integer.valueOf(0), optionsMockMethod.getCurrentResponseSequenceIndex()); Assert.assertEquals(0L, optionsMockMethod.getNetworkDelay()); Assert.assertEquals(HttpMethod.GET, getMockByIdMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, getMockByIdMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, getMockByIdMethod.getResponseStrategy()); Assert.assertEquals(Integer.valueOf(0), getMockByIdMethod.getCurrentResponseSequenceIndex()); Assert.assertEquals(0L, getMockByIdMethod.getNetworkDelay());
/** * 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()); } }
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());
@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); }