/** * 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); }
final List<RestMethod> methods = this.methodRepository.findWithResourceId(restResource.getId()); for(RestMethod restMethod : methods){ if(input.getHttpMethod().equals(restMethod.getHttpMethod())) { foundRestMethod = restMethod; break;
@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()); }
@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); }
Assert.assertEquals("getAllMockServices", getAllMockServicesMethod.getName()); Assert.assertEquals("http://castlemock.com/v1", getAllMockServicesMethod.getForwardedEndpoint()); Assert.assertEquals(HttpMethod.GET, getAllMockServicesMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, getAllMockServicesMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, getAllMockServicesMethod.getResponseStrategy()); Assert.assertEquals("createMock", createMockMethod.getName()); Assert.assertEquals("http://castlemock.com/v1", createMockMethod.getForwardedEndpoint()); Assert.assertEquals(HttpMethod.POST, createMockMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, createMockMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, createMockMethod.getResponseStrategy()); Assert.assertEquals("headMock", headMockMethod.getName()); Assert.assertEquals("http://castlemock.com/v1", headMockMethod.getForwardedEndpoint()); Assert.assertEquals(HttpMethod.HEAD, headMockMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, headMockMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, headMockMethod.getResponseStrategy()); Assert.assertEquals("optionsMock", optionsMockMethod.getName()); Assert.assertEquals("http://castlemock.com/v1", optionsMockMethod.getForwardedEndpoint()); Assert.assertEquals(HttpMethod.OPTIONS, optionsMockMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, optionsMockMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, optionsMockMethod.getResponseStrategy()); Assert.assertEquals("Get mock by mock id", getMockByIdMethod.getName()); Assert.assertEquals("http://castlemock.com/v1", getMockByIdMethod.getForwardedEndpoint()); Assert.assertEquals(HttpMethod.GET, getMockByIdMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, getMockByIdMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.SEQUENCE, getMockByIdMethod.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<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 = restResource.getMethods().get(0); Assert.assertEquals(methodName, restMethod.getName()); Assert.assertEquals(httpMethod, restMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, restMethod.getStatus()); Assert.assertEquals(RestResponseStrategy.RANDOM, restMethod.getResponseStrategy());