ParametrizedWidgetFactory { public AbstractParametrizedWidget createParametrizedWidget(ContractInput input) { switch (inputTypeResolver.getContractInputType(input)) { case TEXT: return createWidgetForTextInput(input); case NUMERIC: return input.isReadOnly() ? createTextWidget(input) : createInputWidget(input, InputType.NUMBER); case LOCAL_DATE: return input.isReadOnly() ? createTextWidget(input) : createDatePicker(input); case LOCAL_DATE_TIME: return input.isReadOnly() ? createTextWidget(input) : createDateTimePicker(input, false); case OFFSET_DATE_TIME: return input.isReadOnly() ? createTextWidget(input) : createDateTimePicker(input, true); case BOOLEAN: return createCheckBox(input); case FILE: return createFileUploadWidget(input); default: throw new IllegalStateException( String.format("Unable to create a widget for contract input %s.", input.getName())); } } ParametrizedWidgetFactory(); AbstractParametrizedWidget createParametrizedWidget(ContractInput input); boolean isSupported(ContractInput input); WidgetContainer createWidgetContainer(ContractInput contractInput); WidgetContainer createWidgetContainer(); TitleWidget createTitle(String title); TitleWidget createTitle(ContractInput input); ButtonWidget createSubmitButton(ButtonAction actionType); ButtonWidget createAddButton(ContractInput contractInput); ButtonWidget createRemoveButton(); LinkWidget createLink(String text, String url, ButtonStyle style); static final String ITEM_ITERATOR; }
@Test public void create_an_input_widget_for_string_contract_input() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); AbstractParametrizedWidget component = elementFactory.createParametrizedWidget(aStringContractInput("name")); assertThat(component).isInstanceOf(InputWidget.class); }
@Test public void create_a_datepicker_for_localdate_contract_input_with_description_keeping_default_placeholder() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); LeafContractInput contractInput = aLocalDateContractInput("name"); contractInput.setDescription("My description"); InputWidget component = (InputWidget) elementFactory.createParametrizedWidget(contractInput); InputWidgetAssert.assertThat(component).hasPlaceholder("Enter a date (mm/dd/yyyy)"); }
@Test public void create_a_text_input_for_string_contract_input_with_full_with() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); AbstractParametrizedWidget component = elementFactory.createParametrizedWidget(aStringContractInput("name")); assertThat(component.getDimension()).isEqualTo(12); }
@Test public void create_an_input_widget_for_numeric_contract_input() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); AbstractParametrizedWidget component = elementFactory.createParametrizedWidget(aLongContractInput("name")); assertThat(component).isInstanceOf(InputWidget.class); }
@Deprecated @Test public void create_a_datepicker_for_date_contract_input() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); AbstractParametrizedWidget component = elementFactory.createParametrizedWidget(aDateContractInput("creationDate")); assertThat(component).isInstanceOf(DatePickerWidget.class); }
@Test public void create_a_datepicker_for_local_date_contract_input() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); AbstractParametrizedWidget component = elementFactory .createParametrizedWidget(aLocalDateContractInput("creationLocalDate")); assertThat(component).isInstanceOf(DatePickerWidget.class); }
@Test public void create_a_datetimepicker_for_local_date_time_contract_input() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); AbstractParametrizedWidget component = elementFactory .createParametrizedWidget(aLocalDateTimeContractInput("creationLocalDateTime")); assertThat(component).isInstanceOf(DateTimePickerWidget.class); }
@Test public void create_a_checkbox_for_boolean_contract_input() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); AbstractParametrizedWidget component = elementFactory .createParametrizedWidget(new LeafContractInput("isValidated", Boolean.class)); assertThat(component).isInstanceOf(CheckboxWidget.class); assertThat(((CheckboxWidget)component).getLabel()).isEqualTo("Is Validated"); }
@Test public void create_a_file_upload_for_file_contract_input_with_a_default_url() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); AbstractParametrizedWidget component = elementFactory.createParametrizedWidget(aFileContractInput("document")); assertThat(component).isInstanceOf(FileUploadWidget.class); assertThat(((FileUploadWidget)component).getLabel()).isEqualTo("Document"); assertThat(((FileUploadWidget) component).getUrl()).isEqualTo("../API/formFileUpload"); }
@Test public void should_create_a_datepicker_for_date_contract_input_with_date_format() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); DatePickerWidget component = (DatePickerWidget) elementFactory .createParametrizedWidget(aDateContractInput("creationDate")); DatePickerWidgetAssert.assertThat(component).hasPlaceholder(); DatePickerWidgetAssert.assertThat(component).hasDateFormat("MM/dd/yyyy"); }
@Test public void should_create_a_datepicker_for_date_contract_input_with_local_date_format() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); DatePickerWidget component = (DatePickerWidget) elementFactory .createParametrizedWidget(aLocalDateContractInput("creationLocalDate")); DatePickerWidgetAssert.assertThat(component).hasPlaceholder(); DatePickerWidgetAssert.assertThat(component).hasDateFormat("MM/dd/yyyy"); }
@Test public void should_create_a_datetimepicker_for_date_contract_input_with_local_date_time_format() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); DateTimePickerWidget component = (DateTimePickerWidget) elementFactory .createParametrizedWidget(aLocalDateTimeContractInput("creationLocalDateTime")); DateTimePickerWidgetAssert.assertThat(component).hasDateFormat("MM/dd/yyyy"); DateTimePickerWidgetAssert.assertThat(component).hasTimeFormat("h:mm:ss a"); DateTimePickerWidgetAssert.assertThat(component).hasDatePlaceholder(); DateTimePickerWidgetAssert.assertThat(component).hasTimePlaceholder(); assertThat(component.isWithTimeZone()).isEqualTo(false); }
@Test public void should_create_a_datetimepicker_for_date_contract_input_with_off_date_time_format() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); DateTimePickerWidget component = (DateTimePickerWidget) elementFactory .createParametrizedWidget(aOffsetDateTimeContractInput("creationOffSetDateTime")); assertThat(component.isWithTimeZone()).isEqualTo(true); DateTimePickerWidgetAssert.assertThat(component).hasDateFormat("MM/dd/yyyy"); DateTimePickerWidgetAssert.assertThat(component).hasTimeFormat("h:mm:ss a"); DateTimePickerWidgetAssert.assertThat(component).hasDatePlaceholder(); DateTimePickerWidgetAssert.assertThat(component).hasTimePlaceholder(); }
@Test public void throw_a_IllegalArgumentException_for_unsupported_input_type() throws Exception { ParametrizedWidgetFactory elementFactory = createFactory(); thrown.expect(IllegalArgumentException.class); elementFactory.createParametrizedWidget(new LeafContractInput("unsupported", IllegalArgumentException.class)); }
@Test public void should_create_read_only_widget_for_text_input() { DataReference dataReference = new DataReference("reference", String.class.getName()); LeafContractInput input = new LeafContractInput("readOnlyText", String.class); input.setReadonly(true); input.setDataReference(dataReference); input.setMode(EditMode.EDIT); AbstractParametrizedWidget widget = createFactory().createParametrizedWidget(input); assertThat(widget).isInstanceOf(TextWidget.class); assertThat(widget.getHidden()).isEqualTo("!reference"); assertThat(((TextWidget) widget).isLabelHidden()).isFalse(); assertThat(((TextWidget) widget).getLabel()).isEqualTo("Reference"); assertThat(((TextWidget) widget).getText()).isEqualTo("{{reference}}"); }
@Test public void should_create_read_only_widget_for_numeric_input() { DataReference dataReference = new DataReference("reference", Integer.class.getName()); LeafContractInput input = new LeafContractInput("readOnlyInteger", Integer.class); input.setReadonly(true); input.setDataReference(dataReference); input.setMode(EditMode.EDIT); AbstractParametrizedWidget widget = createFactory().createParametrizedWidget(input); assertThat(widget).isInstanceOf(TextWidget.class); assertThat(widget.getHidden()).isEqualTo("!reference"); assertThat(((TextWidget) widget).isLabelHidden()).isFalse(); assertThat(((TextWidget) widget).getLabel()).isEqualTo("Reference"); assertThat(((TextWidget) widget).getText()).isEqualTo("{{reference}}"); }
@Test public void should_create_read_only_widget_for_date_input() { DataReference dataReference = new DataReference("reference", LocalDate.class.getName()); LeafContractInput input = new LeafContractInput("readOnlyDate", LocalDate.class); input.setReadonly(true); input.setDataReference(dataReference); input.setMode(EditMode.EDIT); AbstractParametrizedWidget widget = createFactory().createParametrizedWidget(input); assertThat(widget).isInstanceOf(TextWidget.class); assertThat(widget.getHidden()).isEqualTo("!reference"); assertThat(((TextWidget) widget).isLabelHidden()).isFalse(); assertThat(((TextWidget) widget).getLabel()).isEqualTo("Reference"); assertThat(((TextWidget) widget).getText()).isEqualTo("{{reference|uiDate}}"); }
@Test public void should_create_read_only_widget_for_aggregated_input() { BusinessDataReference dataReference = new BusinessDataReference("aggregatedReference", Object.class.getName(), RelationType.AGGREGATION, LoadingType.EAGER); NodeContractInput parent = new NodeContractInput("aggregatedObject"); parent.setDataReference(dataReference); parent.setMode(EditMode.EDIT); LeafContractInput input = new LeafContractInput(ContractInputDataHandler.PERSISTENCEID_INPUT_NAME, String.class); input.setReadonly(true); input.setMode(EditMode.EDIT); parent.addInput(input); AbstractParametrizedWidget widget = createFactory().createParametrizedWidget(input); assertThat(widget).isInstanceOf(TextWidget.class); assertThat(widget.getHidden()).isEqualTo("!aggregatedReference"); assertThat(((TextWidget) widget).isLabelHidden()).isFalse(); assertThat(((TextWidget) widget).getLabel()).isEqualTo("Aggregated Reference"); assertThat(((TextWidget) widget).getText()).isEqualTo("{{aggregatedReference}}"); }
@Test public void should_set_required_false_for_non_mandatory_contract_input() { LeafContractInput aLocalDateContractInput = aLocalDateContractInput("myDate"); aLocalDateContractInput.setMandatory(false); AbstractParametrizedWidget widget = createFactory().createParametrizedWidget(aLocalDateContractInput); assertThat(widget).isInstanceOf(DatePickerWidget.class); assertThat(((DatePickerWidget) widget).isRequired()).isFalse(); aLocalDateContractInput.setMandatory(true); widget = createFactory().createParametrizedWidget(aLocalDateContractInput); assertThat(widget).isInstanceOf(DatePickerWidget.class); assertThat(((DatePickerWidget) widget).isRequired()).isTrue(); LeafContractInput aLocalDateTimeContractInput = aLocalDateTimeContractInput("myLocalDate"); aLocalDateTimeContractInput.setMandatory(false); widget = createFactory().createParametrizedWidget(aLocalDateTimeContractInput); assertThat(widget).isInstanceOf(DateTimePickerWidget.class); assertThat(((DateTimePickerWidget) widget).isRequired()).isFalse(); aLocalDateTimeContractInput.setMandatory(true); widget = createFactory().createParametrizedWidget(aLocalDateTimeContractInput); assertThat(widget).isInstanceOf(DateTimePickerWidget.class); assertThat(((DateTimePickerWidget) widget).isRequired()).isTrue(); LeafContractInput aInputWidget = aStringContractInput("myString"); aInputWidget.setMandatory(false); widget = createFactory().createParametrizedWidget(aInputWidget); assertThat(widget).isInstanceOf(InputWidget.class); assertThat(((InputWidget) widget).isRequired()).isFalse(); aInputWidget.setMandatory(true); widget = createFactory().createParametrizedWidget(aInputWidget); assertThat(widget).isInstanceOf(InputWidget.class); assertThat(((InputWidget) widget).isRequired()).isTrue(); aInputWidget.setMultiple(true); aInputWidget.setMandatory(false); widget = createFactory().createParametrizedWidget(aInputWidget); assertThat(widget).isInstanceOf(InputWidget.class); assertThat(((InputWidget) widget).isRequired()).isTrue(); }
@Test public void should_create_file_upload_with_required_expression_for_multiple_document_in_edit_mode() { LeafContractInput documentsContractInput = aFileContractInput("documents"); documentsContractInput.setMultiple(true); documentsContractInput.setMode(EditMode.EDIT); AbstractParametrizedWidget widget = createFactory().createParametrizedWidget(documentsContractInput); assertThat(widget).isInstanceOf(FileUploadWidget.class); assertThat(((FileUploadWidget) widget).getRequiredExpression()).isPresent(); assertThat(((FileUploadWidget) widget).getRequiredExpression().get()) .isEqualTo(String.format("!%s.id", ParametrizedWidgetFactory.ITEM_ITERATOR)); documentsContractInput.setMode(EditMode.CREATE); widget = createFactory().createParametrizedWidget(documentsContractInput); assertThat(widget).isInstanceOf(FileUploadWidget.class); assertThat(((FileUploadWidget) widget).getRequiredExpression()).isNotPresent(); }