final String applicationName = file.getName().replace(".wadl", ""); final Optional<String> baseUri = getResourceBase(applicationElement); final RestApplication restApplication = new RestApplication(); restApplication.setName(applicationName); applications.add(restApplication); restResource.setName(resourceName); restResource.setUri(baseUri.orElse("") + resourceName); restApplication.getResources().add(restResource);
/** * Count the method statuses for {@link RestApplication} * @param restApplication The application which statuses will be counted * @return The result of the status count */ protected Map<RestMethodStatus, Integer> getRestMethodStatusCount(final RestApplication restApplication){ Preconditions.checkNotNull(restApplication, "The REST application cannot be null"); final Map<RestMethodStatus, Integer> statuses = new HashMap<RestMethodStatus, Integer>(); for(RestMethodStatus restMethodStatus : RestMethodStatus.values()){ statuses.put(restMethodStatus, 0); } final List<String> resourceIds = this.resourceRepository.findIdsWithApplicationId(restApplication.getId()); for(String resourceId : resourceIds){ final List<RestMethod> methods = this.methodRepository.findWithResourceId(resourceId); for(RestMethod restMethod : methods){ RestMethodStatus restMethodStatus = restMethod.getStatus(); statuses.put(restMethodStatus, statuses.get(restMethodStatus)+1); } } return statuses; }
/** * The method will add a new {@link RestApplication} and update an already existing {@link RestApplication}. * @param newRestApplication The new {@link RestApplication} that might be added to the final list of {@link RestApplication} (resultRestApplication). * @param existingRestApplications A list of existing {@link RestApplication} * @param resultRestApplication A list of the result of {@link RestApplication}. These will be the new {@link RestApplication}. * @since 1.10 */ private void updateRestApplication(final RestApplication newRestApplication, final List<RestApplication> existingRestApplications, final List<RestApplication> resultRestApplication){ final RestApplication existingRestApplication = findRestApplication(existingRestApplications, newRestApplication.getName()); if(existingRestApplication == null){ resultRestApplication.add(newRestApplication); return; } final List<RestResource> existingRestResources = this.resourceRepository.findWithApplicationId(existingRestApplication.getId()); final List<RestResource> resultRestResources = new ArrayList<RestResource>(); for(RestResource newRestResource : newRestApplication.getResources()) { updateRestResource(newRestResource, existingRestResources, resultRestResources); } resultRestApplication.add(existingRestApplication); newRestApplication.setResources(resultRestResources); // Remove the existing REST application from the list of existing REST application // This is done so that we can add the REST applications which have not been // either updated or re-added from the import. existingRestApplications.remove(existingRestApplication); }
public static RestApplication generateRestApplication(){ final RestApplication restApplication = new RestApplication(); restApplication.setId("REST APPLICATION"); restApplication.setName("Rest application name"); restApplication.setResources(new ArrayList<RestResource>()); return restApplication; }
RestApplication application = new RestApplication(); application.setId(applicationV1.getId()); application.setName(applicationV1.getName()); application.setProjectId(project.getId()); project.getApplications().add(application); resource.setName(restResourceV1.getName()); resource.setId(restResourceV1.getId()); resource.setApplicationId(application.getId()); application.getResources().add(resource);
final HttpMethod httpMethod, final boolean generatedResponse){ Assert.assertEquals(applicationName, restApplication.getName()); Assert.assertNull(restApplication.getId()); Assert.assertNull(restApplication.getProjectId()); Assert.assertEquals(1, restApplication.getResources().size()); RestResource restResource = restApplication.getResources().get(0);
@Test public void projectFunctionalityDelete() throws Exception { final String projectId = "projectId"; final String[] restApplicationIds = {"restApplication1", "restApplication2"}; final RestApplication restApplication1 = new RestApplication(); restApplication1.setName("restApplication1"); final RestApplication restApplication2 = new RestApplication(); restApplication1.setName("restApplication2"); final List<RestApplication> restApplications = Arrays.asList(restApplication1, restApplication2); Mockito.when(serviceProcessor.process(Mockito.any(ReadRestApplicationInput.class))) .thenReturn(ReadRestApplicationOutput.builder().restApplication(restApplication1).build()) .thenReturn(ReadRestApplicationOutput.builder().restApplication(restApplication2).build()); final RestApplicationModifierCommand command = new RestApplicationModifierCommand(); command.setRestApplicationIds(restApplicationIds); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + projectId + SLASH) .param("action", "delete").flashAttr("command", command); 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, DELETE_REST_APPLICATIONS_PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(REST_PROJECT_ID, projectId)) .andExpect(MockMvcResultMatchers.model().attribute(REST_APPLICATIONS, restApplications)) .andExpect(MockMvcResultMatchers.model().attributeExists(DELETE_REST_APPLICATIONS_COMMAND)); Mockito.verify(serviceProcessor, Mockito.times(2)).process(Mockito.any(ReadRestApplicationInput.class)); }
/** * The convert method provides the functionality to convert the provided {@link File} into * a list of {@link RestApplication}. * @param ramlModelResult The RAML model result * @param generateResponse Will generate a default response if true. No response will be generated if false. * @return A list of {@link RestApplication} based on the provided file. * @throws IllegalStateException In case the {@link RamlModelResult} is not parsable. */ private List<RestApplication> convert(final RamlModelResult ramlModelResult, final boolean generateResponse){ if(!ramlModelResult.getValidationResults().isEmpty()){ throw new IllegalStateException("Unable to parse the RAML file"); } String title = null; List<RestResource> restResources = new ArrayList<>(); if(ramlModelResult.getApiV08() != null){ org.raml.v2.api.model.v08.api.Api api = ramlModelResult.getApiV08(); title = api.title(); new RAML08Parser().getResources(api.resources(), restResources, "", generateResponse); } else if(ramlModelResult.getApiV10() != null){ org.raml.v2.api.model.v10.api.Api api = ramlModelResult.getApiV10(); title = api.title().value(); new RAML10Parser().getResources(api.resources(), restResources, "", generateResponse); } RestApplication restApplication = new RestApplication(); restApplication.setName(title); restApplication.setResources(restResources); return Arrays.asList(restApplication); }
application.setProjectId(projectId); RestApplication savedApplication = this.applicationRepository.save(application); for(RestResource restResource : application.getResources()){ restResource.setApplicationId(savedApplication.getId()); RestResource savedResource = this.resourceRepository.save(restResource);
if(this.applicationRepository.exists(application.getId())){ throw new IllegalArgumentException("An application with the following key already exists: " + application.getId()); for (RestResource resource : application.getResources()) { if(this.resourceRepository.exists(resource.getId())){ throw new IllegalArgumentException("A resource with the following key already exists: " + resource.getId());
searchResult.setTitle(application.getName()); searchResult.setLink(REST + SLASH + PROJECT + SLASH + application.getProjectId() + SLASH + APPLICATION + SLASH + application.getId()); searchResult.setDescription(REST_TYPE + COMMA + applicationType); 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; }
/** * 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<UpdateRestApplicationOutput> process(final ServiceTask<UpdateRestApplicationInput> serviceTask) { final UpdateRestApplicationInput input = serviceTask.getInput(); final RestApplication updated = input.getRestApplication(); final RestApplication existing = this.applicationRepository.findOne(input.getRestApplicationId()); existing.setName(updated.getName()); final RestApplication updatedRestApplication = this.applicationRepository.update(input.getRestApplicationId(), existing); return createServiceResult(UpdateRestApplicationOutput.builder() .updatedRestApplication(updatedRestApplication) .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<ReadRestApplicationOutput> process(final ServiceTask<ReadRestApplicationInput> serviceTask) { final ReadRestApplicationInput input = serviceTask.getInput(); final RestApplication application = this.applicationRepository.findOne(input.getRestApplicationId()); final List<RestResource> resources = this.resourceRepository.findWithApplicationId(application.getId()); for(RestResource restResource : resources){ Map<RestMethodStatus, Integer> restMethodStatusCount = getRestMethodStatusCount(restResource); restResource.setStatusCount(restMethodStatusCount); } application.setResources(resources); return createServiceResult(ReadRestApplicationOutput.builder() .restApplication(application) .build()); } }
@PreAuthorize("hasAuthority('MODIFIER') or hasAuthority('ADMIN')") @RequestMapping(value = "/{projectId}/create/application", method = RequestMethod.GET) public ModelAndView defaultPage(@PathVariable final String projectId) { final ModelAndView model = createPartialModelAndView(PAGE); model.addObject(REST_PROJECT_ID, projectId); model.addObject(REST_APPLICATION, new RestApplication()); return model; }
/** * Find a REST application with a specific name for a rest project * @param name The name of the REST application * @return A REST application that matches the search criteria. Null otherwise. */ public RestApplication findRestApplication(List<RestApplication> restApplications, String name){ for(RestApplication restApplication : restApplications){ if(restApplication.getName().equals(name)){ return restApplication; } } return null; }
/** * 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<CreateRestApplicationOutput> process(final ServiceTask<CreateRestApplicationInput> serviceTask) { final CreateRestApplicationInput input = serviceTask.getInput(); final RestApplication application = input.getApplication(); application.setProjectId(input.getProjectId()); final RestApplication createdRestApplication = this.applicationRepository.save(application); return createServiceResult(CreateRestApplicationOutput.builder() .savedRestApplication(createdRestApplication) .build()); } }
Assert.assertEquals("Castle Mock Swagger", restApplication.getName()); Assert.assertNull(restApplication.getId()); Assert.assertNull(restApplication.getProjectId()); Assert.assertEquals(2, restApplication.getResources().size()); final RestResource mockResource = restApplication.getResources().stream() .filter(resource -> resource.getName().equals("/mock")) .findFirst() final RestResource mockWithParameterResource = restApplication.getResources().stream() .filter(resource -> resource.getName().equals("/mock/{mockId}")) .findFirst()
final RestApplication restApplication1 = new RestApplication(); restApplication1.setName("restApplication1"); final RestApplication restApplication2 = new RestApplication(); restApplication1.setName("restApplication2");
@PreAuthorize("hasAuthority('MODIFIER') or hasAuthority('ADMIN')") @RequestMapping(value = "/{projectId}/create/application", method = RequestMethod.POST) public ModelAndView createProject(@PathVariable final String projectId, @ModelAttribute final RestApplication restApplication) { final CreateRestApplicationOutput output = serviceProcessor.process(CreateRestApplicationInput.builder() .projectId(projectId) .application(restApplication) .build()); return redirect("/rest/project/" + projectId + "/application/" + output.getSavedRestApplication().getId()); }