private Raml parse(String filePath) { final Raml raml = builder.build(filePath); // TODO: Error handling. return raml; }
public void generate() throws IOException { Path path = codegenConfig.getInputPath(); LOG.info("RAML: {}", path.toAbsolutePath()); if (path.endsWith(".raml")) { LOG.warn("Wrong path - should end with .raml"); return; } Raml raml = new RamlDocumentBuilder(new FileResourceLoader(path.getParent().toFile())) .build(path.getFileName().toString()); ReqSpecSupplField baseReqSpec = new ReqSpecSupplField(); ReqSpecField req = new ReqSpecField(); new RootApiClase(new NestedConfigClass(raml.getTitle(), baseReqSpec, req)) .javaFile(raml, codegenConfig.getBasePackage()) .writeTo(codegenConfig.getOutputPath()); raml.getResources().values().parallelStream().forEach(resource -> { new ResourceClassBuilder().withCodegenConfig(codegenConfig).withResource(resource).withReq(req).generate(); }); } }
if (ValidationResult.areValid(results)) { return run(new RamlDocumentBuilder(new CompositeResourceLoader( loaderArray)).build(ramlBuffer,readerLocation), configuration); } else { final List<String> validationErrors = Lists.transform(results,
public Set<String> run(final Reader ramlReader, final Configuration configuration) throws Exception { final String ramlBuffer = IOUtils.toString(ramlReader); ResourceLoader[] loaderArray = prepareResourceLoaders(configuration); final List<ValidationResult> results = RamlValidationService.createDefault( new CompositeResourceLoader(loaderArray)).validate(ramlBuffer, ""); if (ValidationResult.areValid(results)) { return run(new RamlDocumentBuilder(new CompositeResourceLoader( loaderArray)).build(ramlBuffer, ""), configuration); } else { final List<String> validationErrors = Lists.transform(results, new Function<ValidationResult, String>() { @Override public String apply(final ValidationResult vr) { return String.format("%s %s", vr.getStartColumn(), vr.getMessage()); } }); throw new IllegalArgumentException("Invalid RAML definition:\n" + join(validationErrors, "\n")); } }
/** * {@inheritDoc} * <p> * This implementation always requests the {@code sourceRaml} {@link java.net.URI} given in the {@code config}. It * uses the {@link ResourceLoader} from {@link #prepareResourceLoader()} to resolve additional resources that are * referenced in the RAML API. */ @Override public Raml obtainRaml(final Configuration config) { LOG.info("Obtaining RAML from " + config.getSourceRaml() + " ..."); final RamlDocumentBuilder builder = new RamlDocumentBuilder(prepareResourceLoader()); final String ramlSourceContent; try { ramlSourceContent = IOUtils.toString(config.getSourceRaml()); return builder.build(ramlSourceContent, config.getSourceRaml().toString()); } catch (final ClassCastException e) { throw new RammlerException( "Thy syntax in the RAML is invalid, verify the source RAML from URI " + config.getSourceRaml() + ".", e); } catch (final IOException e) { throw new RammlerException("Failed to obtain source RAML from URI " + config.getSourceRaml() + ".", e); } }
private Raml readSource(final Resource resource) throws DeserializerException { final ResourceLoader resourceLoader = new CompositeResourceLoader( new ResourceRelativeUrlResourceLoader(resource), new ClassPathResourceLoader(), new UrlResourceLoader() ); final RamlDocumentBuilder builder = new RamlDocumentBuilder(resourceLoader); LOGGER.debug("Loading RAML file '" + resource.getDescription() + "'"); try (InputStream resourceStream = resource.getInputStream()) { return builder.build(resourceStream, ""); } catch (final IOException e) { throw new DeserializerException("Problem while parsing RAML file '" + resource.getDescription() + "'", e); } } }
@Test public void docTemplate() { String mountPoint = "/foo"; Utils.addTemplateValue(DocumentationRestApi.ID_TEMPLATE, "fooID"); Utils.addTemplateValue(DocumentationRestApi.MOUNT_TEMPLATE, mountPoint); processResource("ApiLevelTemplateDocs.java", "raml", "all"); Map.Entry<String, String> entry = output.entrySet().iterator().next(); entry.getKey(); AssertJUnit.assertTrue("expected file named AuthorizationScopes.raml", entry.getKey().endsWith("ApiLevelTemplateDocs-fooID.raml")); Raml raml = new RamlDocumentBuilder().build(entry.getValue(), "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); List<DocumentationItem> documentation = raml.getDocumentation(); AssertJUnit.assertEquals("RAML baseUri is incorrect", mountPoint, raml.getBaseUri()); Resource resource = raml.getResource(mountPoint); AssertJUnit.assertNotNull("Cannot find resource related to:" + mountPoint, resource); }
@Test public void apiLevelDocs() { processResource("ApiLevelDocs.java", "raml", "all"); AssertJUnit.assertEquals("ApiLevelDocs should have produced exactly 1 results document", 1, output.size()); Map.Entry<String,String> entry = output.entrySet().iterator().next(); AssertJUnit.assertTrue("expected file named ApiLevelDocs-UltimateApi.raml", entry.getKey().endsWith("ApiLevelDocs-UltimateApi.raml")); Raml raml = new RamlDocumentBuilder().build(entry.getValue(), "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); AssertJUnit.assertEquals("RAML title is incorrect", "The Ultimate REST API", raml.getTitle()); AssertJUnit.assertEquals("RAML version is incorrect", "v1", raml.getVersion()); AssertJUnit.assertEquals("RAML baseUri is incorrect", "/ultimate/api/v1", raml.getBaseUri()); List<DocumentationItem> documentation = raml.getDocumentation(); AssertJUnit.assertNotNull("RAML has no documentation items", documentation); AssertJUnit.assertEquals("RAML has too many documentation items", 1, documentation.size()); AssertJUnit.assertEquals("RAML documentation item has wrong title", "Overview", documentation.get(0).getTitle()); AssertJUnit.assertEquals("RAML documentation item has wrong content", "Some documentation of the API itself.", documentation.get(0).getContent().trim()); }
@Test public void assertNoRedundantUriParametersForResource() { processResource("RestDocEndpoint.java", "raml", "all"); Raml raml = new RamlDocumentBuilder().build(defaultApiOutput, "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); Resource resource = raml.getResource("/mount/api/v1/widgets/{id1}/gizmos"); AssertJUnit.assertNotNull("Resource /mount/api/v1/widgets/{id1}/gizmos not found", resource); resource = resource.getResource("/{id2}"); AssertJUnit.assertNotNull("Resource /mount/api/v1/widgets/{id1}/gizmos/{id2} not found", resource); }
@Test public void testEnumsTypesInPathForRaml() { processResource("RestDocEndpoint.java", "raml", "all"); Raml raml = new RamlDocumentBuilder().build(defaultApiOutput, "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); Resource resource = raml.getResource("/mount/api/v1/colors/{color}"); AssertJUnit.assertNotNull("Resource /mount/api/v1/colors/{color} not found", resource); UriParameter up = resource.getUriParameters().get("color"); AssertJUnit.assertNotNull("No color path param found on GET method of /mount/api/v1/colors/{color}", up); List<String> enums = up.getEnumeration(); AssertJUnit.assertNotNull("Color path param on GET method of /mount/api/v1/colors/{color} not enum", enums); AssertJUnit.assertEquals("Color path param on GET /mount/api/v1/colors/{color} is wrong size", 3, enums.size()); }
AssertJUnit.assertTrue("expected file named MultiApiLevelDocs-RestApi1.raml", entry.getKey().endsWith("MultiApiLevelDocs-RestApi1.raml")); Raml raml = new RamlDocumentBuilder().build(entry.getValue(), "http://example.com"); AssertJUnit.assertNotNull("RAML for MultiApiLevelDocs-RestApi1.raml not parseable", raml); AssertJUnit.assertEquals("RAML title is incorrect", "The RestApi1 API", raml.getTitle()); AssertJUnit.assertTrue("expected file named MultiApiLevelDocs-RestApi2.raml", entry.getKey().endsWith("MultiApiLevelDocs-RestApi2.raml")); raml = new RamlDocumentBuilder().build(entry.getValue(), "http://example.com"); AssertJUnit.assertNotNull("RAML for MultiApiLevelDocs-RestApi2.raml not parseable", raml); AssertJUnit.assertEquals("RAML title is incorrect", "The RestApi2 API", raml.getTitle());
@Test public void assertUriParameterNormalization() { processResource("UriParameterNormalization.java", "raml", "all"); Raml raml = new RamlDocumentBuilder().build(defaultApiOutput, "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); Resource resource = raml.getResource("/widgets/{id}"); AssertJUnit.assertNotNull("Resource /widgets/{id} not found", resource); UriParameter id = resource.getUriParameters().get("id"); AssertJUnit.assertNotNull("Resource /widgets/{id} has no id URI parameter", id); // Flakey test depends on order of resource processing // AssertJUnit.assertEquals("Resource /widgets/{id} id URI parameter description is wrong", // "The widget identifier documented in POST.", id.getDescription().trim()); resource = resource.getResource("/gadgets"); AssertJUnit.assertNotNull("Resource /widgets/{id}/gadgets not found", resource); id = resource.getUriParameters().get("id"); AssertJUnit.assertNull("Resource /widgets/{id}/gadgets has it's own id URI parameter when it should not", id); }
@Test public void testEnumsTypesQueryForRaml() { processResource("RestDocEndpoint.java", "raml", "all"); Raml raml = new RamlDocumentBuilder().build(defaultApiOutput, "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); Resource resource = raml.getResource("/mount/api/v1/whirlygigs"); AssertJUnit.assertNotNull("Resource /mount/api/v1/whirlygigs not found", resource); Action action = resource.getAction(ActionType.GET); AssertJUnit.assertNotNull("Method GET not found on /mount/api/v1/whirlygigs", action); QueryParameter qp = action.getQueryParameters().get("color"); AssertJUnit.assertNotNull("No color query param found on GET method of /mount/api/v1/whirlygigs", qp); List<String> enums = qp.getEnumeration(); AssertJUnit.assertNotNull("Color query param on GET method of /mount/api/v1/whirlygigs not enum", enums); AssertJUnit.assertEquals("Color query param on GET /mount/api/v1/whirlygigs is wrong size", 3, enums.size()); }
AssertJUnit.assertTrue("expected file named AuthorizationScopes.raml", entry.getKey().endsWith("AuthorizationScopes.raml")); Raml raml = new RamlDocumentBuilder().build(entry.getValue(), "http://example.com"); AssertJUnit.assertNotNull("RAML not parseable", raml); List<DocumentationItem> documentation = raml.getDocumentation();
entry.getKey().endsWith("TraitsAnnotations.raml")); Raml raml = new RamlDocumentBuilder().build(entry.getValue(), "http://example.com"); AssertJUnit.assertNotNull("RAML for Stability.raml not parseable", raml);