PluginService extends DeliverableService<Plugin> { Path projectFilter(Path pluginDir, List<String> projects) { try { Path packageJson = pluginDir.resolve("package.json"); if (packageJson.toFile().isFile()) { Map<String, Object> packageMap = new ObjectMapper().readValue(packageJson.toFile(), new TypeReference<HashMap<String, Object>>() {}); if (packageMap.containsKey("private")) { if (!Boolean.parseBoolean((packageMap.get("private").toString()))) { return pluginDir; } if (packageMap.containsKey("datashare")) { LinkedHashMap datashareMap = (LinkedHashMap) packageMap.get("datashare"); List<String> pluginProjects = ofNullable((List<String>) datashareMap.get("projects")).orElse(Collections.emptyList()); return !Collections.disjoint(pluginProjects, projects) ? pluginDir : null; } return null; } } return pluginDir; } catch (IOException e) { throw new RuntimeException(e); } } PluginService(); @Inject  PluginService(PropertiesProvider propertiesProvider);  PluginService(Path pluginsDir);  PluginService(Path pluginsDir, InputStream inputStream); String addPlugins(String stringContent, List<String> userProjects); static final String DEFAULT_PLUGIN_REGISTRY_FILENAME; static final String PLUGINS_BASE_URL; }
@Test public void test_project_filter_json_without_private() throws IOException { pluginFolder.newFolder("target_dir", "my_plugin").toPath().resolve("package.json").toFile().createNewFile(); Path packageJson = pluginFolder.getRoot().toPath().resolve("target_dir").resolve("my_plugin"); Files.write(packageJson.resolve("package.json"), asList("{", "\"main\":\"dist/main.js\"", "}")); assertThat(new PluginService().projectFilter(packageJson, asList("Toto", "Tata")).toString()).isEqualTo(packageJson.toString()); }
@Test public void test_project_filter_json_with_private_false() throws IOException { pluginFolder.newFolder("target_dir", "my_plugin").toPath().resolve("package.json").toFile().createNewFile(); Path packageJson = pluginFolder.getRoot().toPath().resolve("target_dir").resolve("my_plugin"); Files.write(packageJson.resolve("package.json"),asList( "{", " \"private\": false", "}" )); assertThat(new PluginService().projectFilter(packageJson,asList("Toto","Tata")).toString()).isEqualTo(packageJson.toString()); }
@Test public void test_project_filter_json_with_private_true_without_projects() throws IOException { pluginFolder.newFolder("target_dir", "my_plugin").toPath().resolve("package.json").toFile().createNewFile(); Path packageJson = pluginFolder.getRoot().toPath().resolve("target_dir").resolve("my_plugin"); Files.write(packageJson.resolve("package.json"),asList( "{", " \"private\": true", "}" )); assertThat(new PluginService().projectFilter(packageJson,asList("Toto","Tata"))).isNull(); }
@Test public void test_project_filter_json_with_private_true_with_projects_ok() throws IOException { pluginFolder.newFolder("target_dir", "my_plugin").toPath().resolve("package.json").toFile().createNewFile(); Path packageJson = pluginFolder.getRoot().toPath().resolve("target_dir").resolve("my_plugin"); Files.write(packageJson.resolve("package.json"),asList( "{", " \"private\": true,", " \"datashare\": {", " \"projects\": [\"Titi\", \"Tata\"]", " }", "}" )); assertThat(new PluginService().projectFilter(packageJson,asList("Tata")).toString()).isEqualTo(packageJson.toString()); }
@Test public void test_project_filter_json_with_private_true_with_projects_nok() throws IOException { pluginFolder.newFolder("target_dir", "my_plugin").toPath().resolve("package.json").toFile().createNewFile(); Path packageJson = pluginFolder.getRoot().toPath().resolve("target_dir").resolve("my_plugin"); Files.write(packageJson.resolve("package.json"),asList( "{", " \"private\": true,", " \"datashare\": {", " \"projects\": [\"Titi\", \"Tata\"]", " }", "}" )); assertThat(new PluginService().projectFilter(packageJson,asList("Toto"))).isNull(); }