ContractInputVisitorImpl implements ContractInputVisitor { @Override public void visit(NodeContractInput contractInput) { if (shouldCreateContainer(contractInput)) { Container newContainer = contractInputToWidgetMapper.toContainer(contractInput, container.getRows()); ContractInputVisitorImpl containerContractInputVisitor = new ContractInputVisitorImpl(newContainer, contractInputToWidgetMapper); for (ContractInput childInput : contractInput.getInput()) { childInput.accept(containerContractInputVisitor); } if (contractInput.isMultiple() && !contractInput.isReadOnly()) { newContainer.getRows() .add(Collections.<Element> singletonList(contractInputToWidgetMapper.createRemoveButton())); } container.getRows().add(Collections.<Element> singletonList(newContainer)); addButtonBar(contractInput, container); } else { for (ContractInput childInput : contractInput.getInput()) { childInput.accept(this); } } } ContractInputVisitorImpl(ElementContainer container,
            ContractInputToWidgetMapper contractInputToWidgetMapper); @Override void visit(NodeContractInput contractInput); @Override void visit(LeafContractInput contractInput);  }
@Test public void add_a_one_row_component_when_visiting_a_leaf_contract_input() throws Exception { Page page = new Page(); new ContractInputVisitorImpl(page, contractInputToWidgetMapper) .visit(ContractInputBuilder.aStringContractInput("name")); assertThat(page.getRows()).hasSize(1); assertThat(page.getRows().get(0)).hasSize(1); assertThat(page.getRows().get(0).get(0)).isInstanceOf(Component.class); }
@Test public void do_nothing_when_visiting_a_leaf_contract_input_with_unsupported_type() throws Exception { Page page = new Page(); new ContractInputVisitorImpl(page, contractInputToWidgetMapper) .visit(new LeafContractInput("unsupported", IllegalArgumentException.class)); assertThat(page.getRows()).hasSize(0); }
@Test public void add_a_component_embedded_in_a_container_with_buttons_when_visiting_a_multiple_leaf_contract_input() throws Exception { Page page = new Page(); new ContractInputVisitorImpl(page, contractInputToWidgetMapper) .visit((LeafContractInput) ContractInputBuilder.aContractInput("names").mulitple().build()); assertThat(page.getRows()).hasSize(3); List<Element> firstRow = page.getRows().get(0); assertThat(firstRow).hasSize(1); assertThat(firstRow.get(0)).isInstanceOf(Component.class); assertThat(((Component) firstRow.get(0)).getId()).isEqualTo("pbTitle"); assertThat(firstRow.get(0).getPropertyValues()).contains( entry(ParameterConstants.TEXT_PARAMETER, aInterpolationPropertyValue("Names"))); List<Element> secondRow = page.getRows().get(1); assertThat(secondRow).hasSize(1); assertThat(secondRow.get(0)).isInstanceOf(Container.class); Container container = (Container) secondRow.get(0); assertThat(container.getRows()).hasSize(1); List<Element> secondRowInner = container.getRows().get(0); assertThat(secondRowInner).hasSize(2); Component component = (Component) secondRowInner.get(0); assertThat(component.getId()).isEqualTo("pbInput"); assertThat(component.getPropertyValues()).contains( entry(ParameterConstants.LABEL_PARAMETER, aInterpolationPropertyValue(""))); Component removeButton = (Component) secondRowInner.get(1); assertThat(removeButton.getId()).isEqualTo("pbButton"); List<Element> thirdRow = page.getRows().get(2); assertThat(thirdRow).hasSize(1); Component addButton = (Component) thirdRow.get(0); assertThat(addButton.getId()).isEqualTo("pbButton"); assertThat(addButton.getPropertyValues().get("valueToAdd").getValue()).isNull(); }
@Test public void add_a_children_components_embedded_in_a_container_when_visiting_a_node_contract_input() throws Exception { Page page = new Page(); new ContractInputVisitorImpl(page, contractInputToWidgetMapper) .visit((NodeContractInput) ContractInputBuilder.aNodeContractInput("employee").withInput( ContractInputBuilder.aStringContractInput("firstName"), ContractInputBuilder.aStringContractInput("lastName")).build()); assertThat(page.getRows()).hasSize(2); List<Element> firstRow = page.getRows().get(0); assertThat(firstRow).hasSize(1); assertThat(firstRow.get(0)).isInstanceOf(Component.class); assertThat(((Component) firstRow.get(0)).getId()).isEqualTo("pbTitle"); assertThat(firstRow.get(0).getPropertyValues()).contains( entry(ParameterConstants.TEXT_PARAMETER, aInterpolationPropertyValue("Employee"))); List<Element> secondRow = page.getRows().get(1); assertThat(secondRow).hasSize(1); assertThat(secondRow.get(0)).isInstanceOf(Container.class); Container container = (Container) secondRow.get(0); assertThat(container.getRows()).hasSize(2); assertThat(container.getRows().get(0)).hasSize(1); Component firstNameComponent = (Component) container.getRows().get(0).get(0); assertThat(firstNameComponent.getId()).isEqualTo("pbInput"); assertThat(firstNameComponent.getPropertyValues()).contains( entry(ParameterConstants.LABEL_PARAMETER, aInterpolationPropertyValue("First Name"))); Component lastNameComponent = (Component) container.getRows().get(1).get(0); assertThat(lastNameComponent.getId()).isEqualTo("pbInput"); assertThat(lastNameComponent.getPropertyValues()).contains( entry(ParameterConstants.LABEL_PARAMETER, aInterpolationPropertyValue("Last Name"))); }
@Test public void add_a_children_components_embedded_in_a_container_with_buttons_when_visiting_a_multiple_node_contract_input() throws Exception { Page page = new Page(); new ContractInputVisitorImpl(page, contractInputToWidgetMapper).visit( (NodeContractInput) ContractInputBuilder.aNodeContractInput("employee").withInput( ContractInputBuilder.aStringContractInput("firstName"), ContractInputBuilder.aStringContractInput("lastName")).mulitple().build()); assertThat(page.getRows()).hasSize(3); List<Element> firstRow = page.getRows().get(0); assertThat(firstRow).hasSize(1); assertThat(firstRow.get(0)).isInstanceOf(Component.class); assertThat(((Component) firstRow.get(0)).getId()).isEqualTo("pbTitle"); assertThat(firstRow.get(0).getPropertyValues()).contains( entry(ParameterConstants.TEXT_PARAMETER, aInterpolationPropertyValue("Employee"))); List<Element> secondRow = page.getRows().get(1); assertThat(secondRow).hasSize(1); assertThat(secondRow.get(0)).isInstanceOf(Container.class); Container container = (Container) secondRow.get(0); assertThat(container.getRows()).hasSize(3); assertThat(container.getRows().get(0)).hasSize(1); Component firstNameComponent = (Component) container.getRows().get(0).get(0); assertThat(firstNameComponent.getId()).isEqualTo("pbInput"); assertThat(firstNameComponent.getPropertyValues()).contains( entry(ParameterConstants.LABEL_PARAMETER, aInterpolationPropertyValue("First Name"))); assertThat(container.getRows().get(1)).hasSize(1); Component lastNameComponent = (Component) container.getRows().get(1).get(0); assertThat(lastNameComponent.getId()).isEqualTo("pbInput"); assertThat(lastNameComponent.getPropertyValues()).contains( entry(ParameterConstants.LABEL_PARAMETER, aInterpolationPropertyValue("Last Name"))); assertThat(container.getRows().get(2)).hasSize(1); Component removeButton = (Component) container.getRows().get(2).get(0); assertThat(removeButton.getId()).isEqualTo("pbButton"); List<Element> thirdRow = page.getRows().get(2); assertThat(thirdRow).hasSize(1); Component addButton = (Component) thirdRow.get(0); assertThat(addButton.getId()).isEqualTo("pbButton"); assertThat(addButton.getPropertyValues().get("valueToAdd").getValue()).isEqualTo("{" + System.lineSeparator() + " \"firstName\" : \"\"," + System.lineSeparator() + " \"lastName\" : \"\"" + System.lineSeparator() + "}"); }
@Test public void add_a_complex_children_components_embedded_in_a_container_when_visiting_a_multiple_complex_node_contract_input() throws Exception { Page page = new Page(); new ContractInputVisitorImpl(page, contractInputToWidgetMapper).visit( (NodeContractInput) ContractInputBuilder.aNodeContractInput("complex").mulitple() .withInput( ContractInputBuilder.aNodeContractInput("subcomplex") .withInput(ContractInputBuilder.aStringContractInput("lastName")).build()) .withInput( ContractInputBuilder.aNodeContractInput("subcomplexMultiple").mulitple() .withInput(ContractInputBuilder.aStringContractInput("friend")).build()) .build()); assertThat(page.getRows()).hasSize(3); checkSubcomplexTitle(page.getRows().get(0), "Complex"); List<Element> complexSecondRow = page.getRows().get(1); assertThat(complexSecondRow).hasSize(1); assertThat(complexSecondRow.get(0)).isInstanceOf(Container.class); Container complexContainer = (Container) complexSecondRow.get(0); assertThat(complexContainer.getPropertyValues()) .contains(entry(ParameterConstants.REPEATED_COLLECTION_PARAMETER, aDataPropertyValue("formInput.complex"))); assertThat(complexContainer.getRows()).hasSize(6); assertThat(complexContainer.getRows().get(0)).hasSize(1); checkSubcomplex(complexContainer); checkComplexRemoveButton(complexContainer, 5); List<Element> thirdRow = page.getRows().get(2); assertThat(thirdRow).hasSize(1); Component addButton = (Component) thirdRow.get(0); assertThat(addButton.getId()).isEqualTo("pbButton"); assertThat(addButton.getPropertyValues().get("valueToAdd").getValue()).isEqualTo("{" + System.lineSeparator() + " \"subcomplex\" : {" + System.lineSeparator() + " \"lastName\" : \"\"" + System.lineSeparator() + " }," + System.lineSeparator() + " \"subcomplexMultiple\" : [ ]" + System.lineSeparator() + "}"); }
@Test public void should_not_add_simple_aggregated_relation_in_a_container() throws Exception { Page page = new Page(); new ContractInputVisitorImpl(page, contractInputToWidgetMapper) .visit((NodeContractInput) ContractBuilder.aContractWithDataRefAndAggregation(EditMode.CREATE).getInput().get(0)); Container formContainer = (Container) page.getRows().get(1).get(0); Component select = (Component) formContainer.getRows().get(3).get(0); assertThat(select.getId()).isEqualTo("pbSelect"); }