ModelDiscovery { public static List<URL> findModels() { return findModels(Thread.currentThread().getContextClassLoader()); } private  ModelDiscovery(); static List<URL> findModels(); static List<URL> findModels(ClassLoader loader); static List<URL> findModels(URL jarManifestUrl); static String getSmithyModelPathFromJarUrl(URL modelUrl); static URL createSmithyJarManifestUrl(String fileOrUrl);  }
@Test public void discoversModelsInManifests() throws MalformedURLException { URL manifest = getClass().getResource("manifest-valid"); String prefix = manifest.toString().substring(0, manifest.toString().length() - "manifest".length()); List<URL> models = ModelDiscovery.findModels(manifest); assertThat(models, contains( new URL(prefix + "foo.smithy"), new URL(prefix + "baz/bar/example.json"), new URL(prefix + "test"), new URL(prefix + "test2"))); }
@Test public void discoversModelsFromClasspath() { URL[] urls = new URL[]{getClass().getResource("jar-import.jar")}; URLClassLoader classLoader = new URLClassLoader(urls); List<URL> models = ModelDiscovery.findModels(classLoader); List<String> names = models.stream() .map(ModelDiscovery::getSmithyModelPathFromJarUrl) .collect(Collectors.toList()); assertThat(names, contains("a.smithy", "b/b.smithy", "b/c/c.json")); }
@Test public void parsesEmptyManifest() { URL manifest = getClass().getResource("manifest-empty"); List<URL> models = ModelDiscovery.findModels(manifest); assertThat(models, empty()); }
@Test public void skipsCommentLines() throws IOException { URL manifest = getClass().getResource("manifest-valid-with-comments"); String prefix = manifest.toString().substring(0, manifest.toString().length() - "manifest".length()); List<URL> models = ModelDiscovery.findModels(manifest); assertThat(models, contains( new URL(prefix + "foo.smithy"), new URL(prefix + "baz/bar/example.json"), new URL(prefix + "test"), new URL(prefix + "test2"))); }
@Test public void prohibitsLeadingSlash() { Assertions.assertThrows(ModelManifestException.class, () -> { URL manifest = getClass().getResource("manifest-prohibits-leading-slash"); ModelDiscovery.findModels(manifest); }); }
@Test public void prohibitsTrailingSlash() { Assertions.assertThrows(ModelManifestException.class, () -> { URL manifest = getClass().getResource("manifest-prohibits-trailing-slash"); ModelDiscovery.findModels(manifest); }); }
@Test public void prohibitsEmptySegments() { Assertions.assertThrows(ModelManifestException.class, () -> { URL manifest = getClass().getResource("manifest-prohibits-empty-segments"); ModelDiscovery.findModels(manifest); }); }
@Test public void prohibitsDotSegments() { Assertions.assertThrows(ModelManifestException.class, () -> { URL manifest = getClass().getResource("manifest-prohibits-dot-segments"); ModelDiscovery.findModels(manifest); }); }
@Test public void prohibitsDotDotSegments() { Assertions.assertThrows(ModelManifestException.class, () -> { URL manifest = getClass().getResource("manifest-prohibits-dot-dot-segments"); ModelDiscovery.findModels(manifest); }); }
@Test public void prohibitsSpecialCharacters() { Assertions.assertThrows(ModelManifestException.class, () -> { URL manifest = getClass().getResource("manifest-prohibits-special-characters"); ModelDiscovery.findModels(manifest); }); }