WidgetFileBasedLoader extends JsonFileBasedLoader<Widget> { @Override public Widget get(Path path) { try { Widget widget = getWigetWithView(path, null); Path metadata = path.getParent().getParent().resolve(format(".metadata/%s.json", path.getParent().getFileName())); if (exists(metadata)) { widget = objectMapper.assign(widget, readAllBytes(metadata)); } return widget; } catch (IOException e) { throw new RepositoryException(format("Error while getting metadata (for file [%s])", path.getFileName()), e); } } @Inject  WidgetFileBasedLoader(JacksonObjectMapper objectMapper); @Override Widget get(Path path); @Override List<Widget> findByObjectId(Path directory, String objectId); @Override boolean contains(Path directory, String widgetId); List<Widget> loadAllCustom(Path widgetsFolder); @Override Widget getByUUID(Path directory, String uuid); @Override Widget load(Path path);  }
@Test public void should_get_a_widget_by_its_id() throws Exception { Widget expectedWidget = aWidget().id("input").build(); Widget notExpectedWidget = aWidget().id("label").build(); addToDirectory(widgetDirectory, expectedWidget, notExpectedWidget); Widget widget = widgetLoader.get(widgetDirectory.resolve("input/input.json")); assertThat(widget).isEqualTo(expectedWidget); }
@Test public void should_get_a_widget_with_template_and_controller_by_its_id() throws Exception { String templateFileName = "input.tpl.html"; String controllerFileName = "input.ctrl.js"; Widget expectedWidget = aWidget().id("input").template("@" + templateFileName).controller("@" + controllerFileName).build(); addToDirectory(widgetDirectory, expectedWidget); String htmlContent = "<div></div>"; Files.write(widgetDirectory.resolve(expectedWidget.getId()).resolve(templateFileName), htmlContent.getBytes(StandardCharsets.UTF_8)); String jsContent = "function ($scope) {}"; Files.write(widgetDirectory.resolve(expectedWidget.getId()).resolve(controllerFileName), jsContent.getBytes(StandardCharsets.UTF_8)); Widget widget = widgetLoader.get(widgetDirectory.resolve("input/input.json")); assertThat(widget.getTemplate()).isEqualTo(htmlContent); assertThat(widget.getController()).isEqualTo(jsContent); }