/** * Find a REST method with a specific name for a REST resource * @param name The name of the REST method * @return A REST method that matches the search criteria. Null otherwise. */ public RestMethod findRestMethod(List<RestMethod> restMethods, String name){ for(RestMethod restMethod : restMethods){ if(restMethod.getName().equals(name)){ return restMethod; } } return null; }
/** * 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); }
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()); throw new RestException("No mocked response created for operation " + restMethod.getName());
final String projectId = this.applicationRepository.getProjectId(applicationId); final SearchResult searchResult = new SearchResult(); searchResult.setTitle(method.getName()); searchResult.setLink(REST + SLASH + PROJECT + SLASH + projectId + SLASH + APPLICATION + SLASH + applicationId
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);
@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); }
.filter(method -> method.getName().equals("getAllMockServices")) .findFirst() .get(); Assert.assertEquals("getAllMockServices", getAllMockServicesMethod.getName()); Assert.assertEquals("http://castlemock.com/v1", getAllMockServicesMethod.getForwardedEndpoint()); Assert.assertEquals(HttpMethod.GET, getAllMockServicesMethod.getHttpMethod()); .filter(method -> method.getName().equals("createMock")) .findFirst() .get(); Assert.assertEquals("createMock", createMockMethod.getName()); Assert.assertEquals("http://castlemock.com/v1", createMockMethod.getForwardedEndpoint()); Assert.assertEquals(HttpMethod.POST, createMockMethod.getHttpMethod()); .filter(method -> method.getName().equals("headMock")) .findFirst() .get(); Assert.assertEquals("headMock", headMockMethod.getName()); Assert.assertEquals("http://castlemock.com/v1", headMockMethod.getForwardedEndpoint()); Assert.assertEquals(HttpMethod.HEAD, headMockMethod.getHttpMethod()); .filter(method -> method.getName().equals("optionsMock")) .findFirst() .get(); Assert.assertEquals("optionsMock", optionsMockMethod.getName());
/** * 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(methodName, restMethod.getName()); Assert.assertEquals(httpMethod, restMethod.getHttpMethod()); Assert.assertEquals(RestMethodStatus.MOCKED, restMethod.getStatus());