for(Element resourceElement : resourceElements){ final String resourceName = resourceElement.getAttribute("path"); final RestResource restResource = new RestResource(); restResource.setName(resourceName); restResource.setUri(baseUri.orElse("") + resourceName); restApplication.getResources().add(restResource); restResource.getMethods().add(restMethod);
/** * The method will add a new {@link RestResource} and update an already existing {@link RestResource}. * @param newRestResource The new {@link RestResource} that might be added to the final list of {@link RestResource} (resultRestApplication). * @param existingRestResources A list of existing {@link RestResource} * @param resultRestResources A list of the result of {@link RestResource}. These will be the new {@link RestResource}. * @since 1.10 */ private void updateRestResource(final RestResource newRestResource, final List<RestResource> existingRestResources, final List<RestResource> resultRestResources){ // Check if the new REST resource already exists final RestResource existingRestResource = findRestResource(existingRestResources, newRestResource.getName()); // It doesn't exists. Simply add it to the existing application if (existingRestResource == null) { resultRestResources.add(newRestResource); return; } // Update resource existingRestResource.setUri(newRestResource.getUri()); final List<RestMethod> existingRestMethods = this.methodRepository.findWithResourceId(existingRestResource.getId()); final List<RestMethod> resultRestMethods = new ArrayList<RestMethod>(); for(RestMethod newRestMethod : newRestResource.getMethods()){ updateRestMethod(newRestMethod, existingRestMethods, resultRestMethods); } resultRestResources.add(existingRestResource); newRestResource.setMethods(resultRestMethods); }
RestResource createRestResource(String applicationId, String uri) { RestResource restResource = new RestResource(); restResource.setApplicationId(applicationId); restResource.setUri(uri); return restResource; } }
public static RestResource generateRestResource(){ final RestResource restResource = new RestResource(); restResource.setId("REST Resource"); restResource.setName("Rest resource name"); restResource.setMethods(new ArrayList<RestMethod>()); restResource.setUri("URI"); return restResource; }
@PreAuthorize("hasAuthority('MODIFIER') or hasAuthority('ADMIN')") @RequestMapping(value = "/{projectId}/application/{applicationId}/create/resource", method = RequestMethod.GET) public ModelAndView defaultPage(@PathVariable final String projectId, @PathVariable final String applicationId) { final RestResource restResource = new RestResource(); restResource.setUri(SLASH); final ModelAndView model = createPartialModelAndView(PAGE); model.addObject(REST_PROJECT_ID, projectId); model.addObject(REST_APPLICATION_ID, applicationId); model.addObject(REST_RESOURCE, restResource); return model; }
RestResource resource = new RestResource(); resource.setUri(restResourceV1.getUri()); resource.setName(restResourceV1.getName()); resource.setId(restResourceV1.getId()); resource.setApplicationId(application.getId()); application.getResources().add(resource); restMethod.setDefaultBody(methodV1.getDefaultBody()); restMethod.setCurrentResponseSequenceIndex(methodV1.getCurrentResponseSequenceIndex()); restMethod.setResourceId(resource.getId()); resource.getMethods().add(restMethod);
@Test(expected = IllegalArgumentException.class) public void testProcessResourceNotFound(){ RestResource restResource1 = RestResourceGenerator.generateRestResource(); restResource1.setId("Resource1"); restResource1.setUri("/user"); RestMethod restMethod1 = RestMethodGenerator.generateRestMethod(); restMethod1.setHttpMethod(HttpMethod.POST); RestMethod restMethod2 = RestMethodGenerator.generateRestMethod(); restMethod2.setHttpMethod(HttpMethod.GET); RestMethod restMethod3 = RestMethodGenerator.generateRestMethod(); restMethod3.setHttpMethod(HttpMethod.PUT); restResource1.getMethods().add(restMethod1); restResource1.getMethods().add(restMethod2); restResource1.getMethods().add(restMethod3); Mockito.when(resourceRepository.findWithApplicationId("RestApplicationId")).thenReturn(Arrays.asList(restResource1)); Mockito.when(methodRepository.findWithResourceId(restResource1.getId())).thenReturn(Arrays.asList(restMethod1, restMethod2, restMethod3)); Mockito.when(mockResponseRepository.findWithMethodId(Mockito.anyString())).thenReturn(new ArrayList<>()); IdentifyRestMethodInput input1 = IdentifyRestMethodInput.builder() .restProjectId("RestProjectId") .restApplicationId("RestApplicationId") .restResourceUri("/random") .httpMethod(HttpMethod.GET) .build(); ServiceTask<IdentifyRestMethodInput> serviceTask1 = new ServiceTask<IdentifyRestMethodInput>(input1); service.process(serviceTask1); }
RestResource restResource = restApplication.getResources().get(0); Assert.assertEquals(resourceName, restResource.getName()); Assert.assertEquals(resourceUri, restResource.getUri()); Assert.assertNull(restResource.getId()); Assert.assertNull(restResource.getApplicationId()); Assert.assertNull(restResource.getInvokeAddress()); Assert.assertEquals(1, restResource.getMethods().size()); RestMethod restMethod = restResource.getMethods().get(0); Assert.assertEquals(methodName, restMethod.getName()); Assert.assertEquals(httpMethod, restMethod.getHttpMethod());
/** * 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; }
@Test public void testVariableProcess1(){ RestResource restResource1 = RestResourceGenerator.generateRestResource(); restResource1.setId("Resource1"); restResource1.setUri("/user/{test}"); RestMethod restMethod1 = RestMethodGenerator.generateRestMethod(); restMethod1.setHttpMethod(HttpMethod.GET); restResource2.setId("Resource2"); restResource2.setUri("/user/{test}/resource"); RestMethod restMethod2 = RestMethodGenerator.generateRestMethod(); restMethod2.setHttpMethod(HttpMethod.POST); Mockito.when(methodRepository.findWithResourceId(restResource1.getId())).thenReturn(Arrays.asList(restMethod1)); Mockito.when(methodRepository.findWithResourceId(restResource2.getId())).thenReturn(Arrays.asList(restMethod2));
/** * 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<UpdateRestResourceOutput> process(final ServiceTask<UpdateRestResourceInput> serviceTask) { final UpdateRestResourceInput input = serviceTask.getInput(); final RestResource existing = this.resourceRepository.findOne(input.getRestResourceId()); final RestResource updatedRestResource = input.getRestResource(); existing.setName(updatedRestResource.getName()); existing.setUri(updatedRestResource.getUri()); this.resourceRepository.update(input.getRestResourceId(), existing); return createServiceResult(UpdateRestResourceOutput.builder() .updatedRestResource(updatedRestResource) .build()); } }
@Test public void projectFunctionalityDelete() throws Exception { final String projectId = "projectId"; final String applicationId = "applicationId"; final String[] restResourceIds = {"restApplication1", "restApplication2"}; final RestResource restResource1 = new RestResource(); restResource1.setName("restResource1"); final RestResource restResource2 = new RestResource(); restResource2.setName("restResource2"); final List<RestResource> restResources = Arrays.asList(restResource1, restResource2); Mockito.when(serviceProcessor.process(Mockito.any(ReadRestResourceInput.class))) .thenReturn(ReadRestResourceOutput.builder().restResource(restResource1).build()) .thenReturn(ReadRestResourceOutput.builder().restResource(restResource2).build()); final RestResourceModifierCommand command = new RestResourceModifierCommand(); command.setRestResourceIds(restResourceIds); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + projectId + SLASH + APPLICATION + SLASH + applicationId) .param("action", "delete").flashAttr("command", command); 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, DELETE_REST_RESOURCES_PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(REST_PROJECT_ID, projectId)) .andExpect(MockMvcResultMatchers.model().attribute(REST_APPLICATION_ID, applicationId)) .andExpect(MockMvcResultMatchers.model().attribute(REST_RESOURCES, restResources)) .andExpect(MockMvcResultMatchers.model().attributeExists(DELETE_REST_RESOURCES_COMMAND)); Mockito.verify(serviceProcessor, Mockito.times(2)).process(Mockito.any(ReadRestResourceInput.class)); }
restResource.setApplicationId(savedApplication.getId()); RestResource savedResource = this.resourceRepository.save(restResource); for(RestMethod method : restResource.getMethods()){ method.setResourceId(savedResource.getId());
if(this.resourceRepository.exists(resource.getId())){ throw new IllegalArgumentException("A resource with the following key already exists: " + resource.getId()); for (RestMethod method : resource.getMethods()) { if(this.methodRepository.exists(method.getId())){ throw new IllegalArgumentException("A method with the following key already exists: " + method.getId());
final String projectId = this.applicationRepository.getProjectId(resource.getApplicationId()); final SearchResult searchResult = new SearchResult(); searchResult.setTitle(resource.getName()); searchResult.setLink(REST + SLASH + PROJECT + SLASH + projectId + SLASH + APPLICATION + SLASH + resource.getApplicationId() + SLASH + RESOURCE + SLASH + resource.getId()); searchResult.setDescription(REST_TYPE + COMMA + resourceType); searchResults.add(searchResult);
public static RestProject generateFullRestProject(){ final RestProject project = new RestProject(); project.setId("REST PROJECT"); project.setName("Project name"); project.setDescription("Project description"); project.setCreated(new Date()); project.setUpdated(new Date()); project.setApplications(new ArrayList<RestApplication>()); for(int applicationIndex = 0; applicationIndex < 3; applicationIndex++){ final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); restApplication.setResources(new ArrayList<RestResource>()); project.getApplications().add(restApplication); for(int resourceIndex = 0; resourceIndex < 3; resourceIndex++){ final RestResource restResource = RestResourceGenerator.generateRestResource(); restResource.setMethods(new ArrayList<RestMethod>()); restApplication.getResources().add(restResource); for(int methodIndex = 0; methodIndex < 3; methodIndex++){ final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); restMethod.setMockResponses(new ArrayList<RestMockResponse>()); restResource.getMethods().add(restMethod); for(int responseIndex = 0; responseIndex < 3; responseIndex++){ final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); restMethod.getMockResponses().add(restMockResponse); } } } } return project; }
RestMethod foundRestMethod = null; if(restResource != null){ final List<RestMethod> methods = this.methodRepository.findWithResourceId(restResource.getId()); for(RestMethod restMethod : methods){ if(input.getHttpMethod().equals(restMethod.getHttpMethod())) { UrlUtility.getPathParameters(restResource.getUri(), restResourceUriParts); .restProjectId(input.getRestProjectId()) .restApplicationId(input.getRestApplicationId()) .restResourceId(restResource.getId()) .restMethodId(foundRestMethod.getId()) .restMethod(foundRestMethod)
/** * Find a REST resource with a project id, application id and a set of resource parts * @param restProjectId The id of the project that the resource belongs to * @param restApplicationId The id of the application that the resource belongs to * @param otherRestResourceUriParts The set of resources that will be used to identify the REST resource * @return A REST resource that matches the search criteria. Null otherwise */ protected RestResource findRestResource(final String restProjectId, final String restApplicationId, final String[] otherRestResourceUriParts) { final List<RestResource> resources = this.resourceRepository.findWithApplicationId(restApplicationId); for(RestResource restResource : resources){ if(UrlUtility.compareUri(restResource.getUri(), otherRestResourceUriParts)){ return restResource; } } return null; }
restProjectId, restApplicationId, restResource.getUri()); restResource.setInvokeAddress(invokeAddress); final ModelAndView model = createPartialModelAndView(PAGE); model.addObject(REST_PROJECT_ID, restProjectId);
/** * 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<CreateRestResourceOutput> process(final ServiceTask<CreateRestResourceInput> serviceTask) { final CreateRestResourceInput input = serviceTask.getInput(); final RestResource resource = input.getRestResource(); resource.setApplicationId(input.getRestApplicationId()); final RestResource createdRestResource = this.resourceRepository.save(resource); return createServiceResult(CreateRestResourceOutput.builder() .savedRestApplication(createdRestResource) .build()); } }