@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<UpdateRestMethodsForwardedEndpointOutput> process(final ServiceTask<UpdateRestMethodsForwardedEndpointInput> serviceTask) { final UpdateRestMethodsForwardedEndpointInput input = serviceTask.getInput(); for(RestMethod inputRestMethod : input.getRestMethods()){ final RestMethod restMethod = this.methodRepository.findOne(inputRestMethod.getId()); restMethod.setForwardedEndpoint(input.getForwardedEndpoint()); this.methodRepository.update(restMethod.getId(), restMethod); } return createServiceResult(UpdateRestMethodsForwardedEndpointOutput.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<DeleteRestMethodsOutput> process(final ServiceTask<DeleteRestMethodsInput> serviceTask) { final DeleteRestMethodsInput input = serviceTask.getInput(); for(RestMethod restMethod : input.getRestMethods()){ this.deleteMethod(restMethod.getId()); } return createServiceResult(DeleteRestMethodsOutput.builder().build()); } }
@Test public void testProcess(){ final String projectId = "ProjectId"; final String applicationId = "ApplicationId"; final String resourceId = "ResourceId"; final RestMethod method = RestMethodGenerator.generateRestMethod(); final RestMockResponse mockResponse = RestMockResponseGenerator.generateRestMockResponse(); Mockito.when(methodRepository.findWithResourceId(resourceId)).thenReturn(Arrays.asList(method)); Mockito.when(mockResponseRepository.findWithMethodId(method.getId())).thenReturn(Arrays.asList(mockResponse)); final DeleteRestResourceInput input = DeleteRestResourceInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .restResourceId(resourceId) .build(); final ServiceTask<DeleteRestResourceInput> serviceTask = new ServiceTask<DeleteRestResourceInput>(input); final ServiceResult<DeleteRestResourceOutput> serviceResult = service.process(serviceTask); Mockito.verify(resourceRepository, Mockito.times(1)).delete(resourceId); Mockito.verify(methodRepository, Mockito.times(1)).delete(method.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).delete(mockResponse.getId()); Mockito.verify(methodRepository, Mockito.times(1)).findWithResourceId(resourceId); Mockito.verify(mockResponseRepository, Mockito.times(1)).findWithMethodId(method.getId()); } }
@Test public void testProcess(){ final String projectId = "ProjectId"; final String applicationId = "ApplicationId"; final RestResource resource = RestResourceGenerator.generateRestResource(); final RestMethod method = RestMethodGenerator.generateRestMethod(); final RestMockResponse mockResponse = RestMockResponseGenerator.generateRestMockResponse(); Mockito.when(resourceRepository.findWithApplicationId(applicationId)).thenReturn(Arrays.asList(resource)); Mockito.when(methodRepository.findWithResourceId(resource.getId())).thenReturn(Arrays.asList(method)); Mockito.when(mockResponseRepository.findWithMethodId(method.getId())).thenReturn(Arrays.asList(mockResponse)); final DeleteRestApplicationInput input = DeleteRestApplicationInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .build(); final ServiceTask<DeleteRestApplicationInput> serviceTask = new ServiceTask<DeleteRestApplicationInput>(input); final ServiceResult<DeleteRestApplicationOutput> serviceResult = service.process(serviceTask); Mockito.verify(applicationRepository, Mockito.times(1)).delete(applicationId); Mockito.verify(resourceRepository, Mockito.times(1)).delete(resource.getId()); Mockito.verify(methodRepository, Mockito.times(1)).delete(method.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).delete(mockResponse.getId()); Mockito.verify(resourceRepository, Mockito.times(1)).findWithApplicationId(applicationId); Mockito.verify(methodRepository, Mockito.times(1)).findWithResourceId(resource.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).findWithMethodId(method.getId()); } }
@Test public void testProcess(){ final String projectId = "ProjectId"; final String applicationId = "ApplicationId"; final String resourceId = "ResourceId"; final RestMethod method = RestMethodGenerator.generateRestMethod(); final RestMockResponse mockResponse = RestMockResponseGenerator.generateRestMockResponse(); final ReadRestMethodInput input = ReadRestMethodInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .restResourceId(resourceId) .restMethodId(method.getId()) .build(); final ServiceTask<ReadRestMethodInput> serviceTask = new ServiceTask<ReadRestMethodInput>(input); Mockito.when(methodRepository.findOne(method.getId())).thenReturn(method); Mockito.when(mockResponseRepository.findWithMethodId(method.getId())).thenReturn(Arrays.asList(mockResponse)); final ServiceResult<ReadRestMethodOutput> result = service.process(serviceTask); Mockito.verify(methodRepository, Mockito.times(1)).findOne(method.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).findWithMethodId(method.getId()); Assert.assertNotNull(result.getOutput()); Assert.assertEquals(method, result.getOutput().getRestMethod()); }
Mockito.when(resourceRepository.findWithApplicationId(application.getId())).thenReturn(Arrays.asList(resource)); Mockito.when(methodRepository.findWithResourceId(resource.getId())).thenReturn(Arrays.asList(method)); Mockito.when(mockResponseRepository.findWithMethodId(method.getId())).thenReturn(Arrays.asList(mockResponse)); Mockito.verify(applicationRepository, Mockito.times(1)).delete(application.getId()); Mockito.verify(resourceRepository, Mockito.times(1)).delete(resource.getId()); Mockito.verify(methodRepository, Mockito.times(1)).delete(method.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).delete(mockResponse.getId()); Mockito.verify(resourceRepository, Mockito.times(1)).findWithApplicationId(application.getId()); Mockito.verify(methodRepository, Mockito.times(1)).findWithResourceId(resource.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).findWithMethodId(method.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<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()); } }
/** * 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<UpdateRestResourcesStatusOutput> process(final ServiceTask<UpdateRestResourcesStatusInput> serviceTask) { final UpdateRestResourcesStatusInput 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(UpdateRestResourcesStatusOutput.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<UpdateRestApplicationsForwardedEndpointOutput> process(final ServiceTask<UpdateRestApplicationsForwardedEndpointInput> serviceTask) { final UpdateRestApplicationsForwardedEndpointInput input = serviceTask.getInput(); for(RestApplication restApplication : input.getRestApplications()){ List<RestResource> resources = this.resourceRepository.findWithApplicationId(restApplication.getId()); for(RestResource restResource : resources){ List<RestMethod> methods = this.methodRepository.findWithResourceId(restResource.getId()); for(RestMethod restMethod : methods){ restMethod.setForwardedEndpoint(input.getForwardedEndpoint()); this.methodRepository.update(restMethod.getId(), restMethod); } } } return createServiceResult(UpdateRestApplicationsForwardedEndpointOutput.builder().build()); } }
@Test public void testDeleteMethodConfirm() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); when(serviceProcessor.process(any(DeleteRestMockResponseInput.class))).thenReturn(DeleteRestMockResponsesOutput.builder().build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + DELETE + SLASH + CONFIRM); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(0)); }
@Test public void testConfirmDeletationOfMultipleMockResponses() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); final DeleteRestMockResponsesCommand command = new DeleteRestMockResponsesCommand(); command.setRestMockResponses(new ArrayList<RestMockResponse>()); command.getRestMockResponses().add(restMockResponse); when(serviceProcessor.process(any(DeleteRestMockResponsesInput.class))).thenReturn(DeleteRestMockResponsesOutput.builder().build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + RESPONSE + SLASH + DELETE + SLASH + CONFIRM) .flashAttr("command", command); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(1)); } }
@Test public void testUpdateRestMethod() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); when(serviceProcessor.process(any(UpdateRestResourceInput.class))).thenReturn(UpdateRestResourceOutput.builder() .updatedRestResource(restResource) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + UPDATE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(1)); }
@Test public void testDeleteMockResponseConfirm() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); when(serviceProcessor.process(any(DeleteRestMockResponseInput.class))).thenReturn(DeleteRestMockResponsesOutput.builder().build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + RESPONSE + SLASH + restMockResponse.getId() + SLASH + DELETE + SLASH + CONFIRM); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(0)); }
@Test public void testPostCreateResponse() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); when(serviceProcessor.process(any(CreateRestMockResponseInput.class))).thenReturn(CreateRestMockResponseOutput.builder() .restMockResponse(restMockResponse) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + CREATE_RESPONSE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(1)); }
@Test public void testUpdateMockResponse() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); when(serviceProcessor.process(any(UpdateRestMockResponseInput.class))).thenReturn(UpdateRestMockResponseOutput.builder() .updatedRestMockResponse(restMockResponse) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + RESPONSE + SLASH + restMockResponse.getId() + SLASH + UPDATE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(1)); }
@Test public void testDeleteMockResponse() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); when(serviceProcessor.process(any(ReadRestMockResponseInput.class))).thenReturn(ReadRestMockResponseOutput.builder() .restMockResponse(restMockResponse) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + RESPONSE + SLASH + restMockResponse.getId() + SLASH + DELETE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.model().size(5 + GLOBAL_VIEW_MODEL_COUNT)) .andExpect(MockMvcResultMatchers.forwardedUrl(INDEX)) .andExpect(MockMvcResultMatchers.model().attribute(PARTIAL, PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(REST_PROJECT_ID, restProject.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_APPLICATION_ID, restApplication.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_RESOURCE_ID, restResource.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_METHOD_ID, restMethod.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_MOCK_RESPONSE, restMockResponse)); }
@Test public void testDeleteMethod() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); when(serviceProcessor.process(any(ReadRestMethodInput.class))).thenReturn(ReadRestMethodOutput.builder() .restMethod(restMethod) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + DELETE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.model().size(4 + GLOBAL_VIEW_MODEL_COUNT)) .andExpect(MockMvcResultMatchers.forwardedUrl(INDEX)) .andExpect(MockMvcResultMatchers.model().attribute(PARTIAL, PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(REST_PROJECT_ID, restProject.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_APPLICATION_ID, restApplication.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_RESOURCE_ID, restResource.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_METHOD, restMethod)); }
@Test public void testUpdateMethod() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); when(serviceProcessor.process(any(ReadRestMethodInput.class))).thenReturn(ReadRestMethodOutput.builder() .restMethod(restMethod) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + UPDATE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.model().size(8 + GLOBAL_VIEW_MODEL_COUNT)) .andExpect(MockMvcResultMatchers.forwardedUrl(INDEX)) .andExpect(MockMvcResultMatchers.model().attribute(PARTIAL, PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(REST_PROJECT_ID, restProject.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_APPLICATION_ID, restApplication.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_RESOURCE_ID, restResource.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_METHOD, restMethod)); }