/** * Count the method statuses for a {@link RestResource} * @param restResource The resource which statuses will be counted * @return The result of the status count */ protected Map<RestMethodStatus, Integer> getRestMethodStatusCount(final RestResource restResource){ Preconditions.checkNotNull(restResource, "The REST resource cannot be null"); final Map<RestMethodStatus, Integer> statuses = ImmutableSet.copyOf(RestMethodStatus.values()).stream() .collect(toMap(status -> status, status -> 0)); final List<RestMethod> methods = this.methodRepository.findWithResourceId(restResource.getId()); methods.stream() .map(RestMethod::getStatus) .forEach(status -> statuses.put(status, statuses.get(status)+1)); return statuses; }
@PreAuthorize("hasAuthority('MODIFIER') or hasAuthority('ADMIN')") @RequestMapping(value = "/{projectId}/application/{applicationId}/create/resource", method = RequestMethod.POST) public ModelAndView createResource(@PathVariable final String projectId, @PathVariable final String applicationId, @ModelAttribute final RestResource restResource) { final CreateRestResourceOutput output = serviceProcessor.process(CreateRestResourceInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .restResource(restResource) .build()); return redirect("/rest/project/" + projectId + "/application/" + applicationId + "/resource/" + output.getCreatedRestResource().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<DeleteRestResourcesOutput> process(final ServiceTask<DeleteRestResourcesInput> serviceTask) { final DeleteRestResourcesInput input = serviceTask.getInput(); for(RestResource restResource : input.getRestResources()){ this.deleteResource(restResource.getId()); } return createServiceResult(DeleteRestResourcesOutput.builder().build()); } }
@Test public void testProcess(){ final String projectId = "ProjectId"; final String applicationId = "ApplicationId"; final RestResource resource = RestResourceGenerator.generateRestResource(); final RestMethod method = RestMethodGenerator.generateRestMethod(); final ReadRestResourceInput input = ReadRestResourceInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .restResourceId(resource.getId()) .build(); final ServiceTask<ReadRestResourceInput> serviceTask = new ServiceTask<ReadRestResourceInput>(input); Mockito.when(resourceRepository.findOne(resource.getId())).thenReturn(resource); Mockito.when(methodRepository.findWithResourceId(resource.getId())).thenReturn(Arrays.asList(method)); final ServiceResult<ReadRestResourceOutput> result = service.process(serviceTask); Mockito.verify(resourceRepository, Mockito.times(1)).findOne(resource.getId()); Mockito.verify(methodRepository, Mockito.times(1)).findWithResourceId(resource.getId()); Assert.assertNotNull(result.getOutput()); Assert.assertEquals(resource, result.getOutput().getRestResource()); }
@Test public void testProcess(){ final String projectId = "ProjectId"; final RestApplication application = RestApplicationGenerator.generateRestApplication(); final RestResource resource = RestResourceGenerator.generateRestResource(); final RestMethod method = RestMethodGenerator.generateRestMethod(); final ReadRestApplicationInput input = ReadRestApplicationInput.builder() .restProjectId(projectId) .restApplicationId(application.getId()) .build(); final ServiceTask<ReadRestApplicationInput> serviceTask = new ServiceTask<ReadRestApplicationInput>(input); Mockito.when(applicationRepository.findOne(application.getId())).thenReturn(application); Mockito.when(resourceRepository.findWithApplicationId(application.getId())).thenReturn(Arrays.asList(resource)); Mockito.when(methodRepository.findWithResourceId(resource.getId())).thenReturn(Arrays.asList(method)); final ServiceResult<ReadRestApplicationOutput> result = service.process(serviceTask); Mockito.verify(applicationRepository, Mockito.times(1)).findOne(application.getId()); Mockito.verify(resourceRepository, Mockito.times(1)).findWithApplicationId(application.getId()); Mockito.verify(methodRepository, Mockito.times(1)).findWithResourceId(resource.getId()); Assert.assertNotNull(result.getOutput()); Assert.assertEquals(application, result.getOutput().getRestApplication()); }
/** * 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()); } }
@Test public void testDeleteMethodConfirm() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); 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 + DELETE + SLASH + CONFIRM); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(0)); }
@Test public void testUpdateRestResource() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); 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 + UPDATE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(1)); }
@Test public void testConfirmDeletationOfMultipleMethods() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final DeleteRestMethodsCommand command = new DeleteRestMethodsCommand(); command.setRestMethods(new ArrayList<RestMethod>()); command.getRestMethods().add(restMethod); when(serviceProcessor.process(any(DeleteRestMethodInput.class))).thenReturn(DeleteRestMethodOutput.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 + DELETE + SLASH + CONFIRM) .flashAttr("command", command); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(1)); } }
@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 testPostCreateMethod() 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(CreateRestMethodInput.class))).thenReturn(CreateRestMethodOutput.builder() .createdRestMethod(restMethod) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + CREATE_METHOD); 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 testGetRestResource() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); when(serviceProcessor.process(any(ReadRestResourceInput.class))).thenReturn(ReadRestResourceOutput.builder().restResource(restResource).build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + UPDATE); 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_RESOURCE, restResource)); }
@Test public void testCreateMethod() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); when(serviceProcessor.process(any(ReadRestResourceInput.class))).thenReturn(ReadRestResourceOutput.builder() .restResource(restResource) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + CREATE_METHOD); 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().attributeExists(REST_METHOD)); }
@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 testDeleteMethod() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); when(serviceProcessor.process(any(ReadRestResourceInput.class))).thenReturn(ReadRestResourceOutput.builder() .restResource(restResource) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + DELETE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.model().size(3 + 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, restResource)); }
@Test public void testGetServiceValid() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); when(serviceProcessor.process(isA(ReadRestResourceInput.class))).thenReturn(ReadRestResourceOutput.builder().restResource(restResource).build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId()); ResultActions result = 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, restResource)); RestResource restResourceResponse = (RestResource) result.andReturn().getModelAndView().getModel().get(REST_RESOURCE); String hostAddress = restResourceController.getHostAddress(); }