WidgetFileBasedLoader extends JsonFileBasedLoader<Widget> { @Override public Widget load(Path path) { return getWigetWithView(path, JsonViewPersistence.class); } @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_load_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.load(widgetDirectory.resolve("input/input.json")); assertThat(widget.getTemplate()).isEqualTo(htmlContent); assertThat(widget.getController()).isEqualTo(jsContent); }
@Test public void should_load_a_single_page_in_the_import_folder() throws Exception { Widget input = aWidget().id("input").build(); addToDirectory(widgetDirectory, input); Widget widget = widgetLoader.load(widgetDirectory.resolve("input/input.json")); assertThat(widget).isEqualTo(input); }
@Test(expected = NotFoundException.class) public void should_throw_notfound_exception_when_there_are_no_pages_in_folder() throws Exception { widgetLoader.load(widgetDirectory.resolve("test")); }
@Test(expected = JsonReadException.class) public void should_throw_json_read_exception_when_loaded_file_is_not_valid_json() throws Exception { write(widgetDirectory.resolve("wrongjson.json"), "notJson".getBytes()); widgetLoader.load(widgetDirectory.resolve("wrongjson.json")); }
@Test(expected = NotFoundException.class) public void should_throw_notfound_exception_when_widget_template_is_not_found() throws Exception { Widget input = aWidget().id("input").build(); input.setTemplate("@missingTemplate.tpl.html"); addToDirectory(widgetDirectory, input); widgetLoader.load(widgetDirectory.resolve("input/input.json")); }
@Test(expected = NotFoundException.class) public void should_throw_notfound_exception_when_widget_controller_is_not_found() throws Exception { Widget input = aWidget().id("input").build(); input.setController("@missingController.ctrl.js"); addToDirectory(widgetDirectory, input); widgetLoader.load(widgetDirectory.resolve("input/input.json")); }