コード例 #1
0
  @Test
  public void shouldMapMapElements() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDto.class)
        .field("namePartsMap[\"first\"]", "firstName")
        .field("namePartsMap['second']", "lastName")
        .register();

    BasicPerson bp = new BasicPerson();
    Map<String, String> nameParamsMap = new HashMap<String, String>();
    nameParamsMap.put("first", "Jan");
    nameParamsMap.put("second", "Kowalski");
    bp.setNamePartsMap(nameParamsMap);

    // when
    MapperFacade mapperFacade = mapperFactory.getMapperFacade();
    BasicPersonDto result = mapperFacade.map(bp, BasicPersonDto.class);

    // then
    assertThat(result.getFirstName()).isEqualTo("Jan");
    assertThat(result.getLastName()).isEqualTo("Kowalski");
    assertThat(result.getBirthDate()).isNull();
    assertThat(result.getCurrentAge()).isEqualTo(0);
    assertThat(result.getFullName()).isNull();
  }
コード例 #2
0
  @Test
  public void shouldMapNestedElement() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDto.class)
        .field("nestedElement.nip", "nip")
        .register();

    BasicPerson bp = new BasicPerson();
    NestedElement nestedElement = new NestedElement();
    nestedElement.setNip(NIP);
    bp.setNestedElement(nestedElement);

    // when
    MapperFacade mapperFacade = mapperFactory.getMapperFacade();
    BasicPersonDto result = mapperFacade.map(bp, BasicPersonDto.class);

    // then
    assertThat(result.getNip()).isNotNull();
    assertThat(result.getNip()).isEqualTo(NIP);
    assertThat(result.getFirstName()).isNull();
    assertThat(result.getLastName()).isNull();
    assertThat(result.getBirthDate()).isNull();
    assertThat(result.getCurrentAge()).isEqualTo(0);
    assertThat(result.getFullName()).isNull();
  }
コード例 #3
0
  @Test
  public void shouldMapListElements() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDto.class)
        .field("nameParts[0]", "firstName")
        .field("nameParts[1]", "lastName")
        .register();

    BasicPerson bp = new BasicPerson();
    bp.setNameParts(Lists.asList("Jan", new String[] {"Kowalski"}));

    // when
    MapperFacade mapperFacade = mapperFactory.getMapperFacade();
    BasicPersonDto result = mapperFacade.map(bp, BasicPersonDto.class);

    // then
    assertThat(result.getFirstName()).isEqualTo("Jan");
    assertThat(result.getLastName()).isEqualTo("Kowalski");
    assertThat(result.getBirthDate()).isNull();
    assertThat(result.getCurrentAge()).isEqualTo(0);
    assertThat(result.getFullName()).isNull();
  }