@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();
  }
  @Test
  public void testMapElementProperties() {

    MapperFactory factory = MappingUtil.getMapperFactory();

    factory
        .classMap(Person.class, PersonDto.class)
        .field("name.first", "names['first']")
        .field("name.last", "names[\"last\"]")
        .register();

    MapperFacade mapper = factory.getMapperFacade();

    Person person = new Person();
    person.name = new Name();
    person.name.first = "Chuck";
    person.name.last = "Testa";

    PersonDto result = mapper.map(person, PersonDto.class);

    Assert.assertNotNull(result.names);
    Assert.assertEquals(person.name.first, result.names.get("first"));
    Assert.assertEquals(person.name.last, result.names.get("last"));

    Person mapBack = mapper.map(result, Person.class);

    Assert.assertNotNull(mapBack.name.first);
    Assert.assertEquals(person.name.first, mapBack.name.first);
    Assert.assertEquals(person.name.last, mapBack.name.last);
  }
  @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();
  }
  @Test
  public void shouldMapWithCustomizedClassMapBuilder() {
    // given
    MapperFactory mapperFactory =
        new DefaultMapperFactory.Builder()
            .classMapBuilderFactory(new ScoringClassMapBuilder.Factory())
            .build();

    mapperFactory.classMap(Source.class, Destination.class).byDefault().register();

    MapperFacade mapperFacade = mapperFactory.getMapperFacade();

    Source src = new Source();
    src.firstName = "Jan";
    src.postalAddress = new PostalAddress();
    src.postalAddress.country = new Country();
    src.postalAddress.country.alphaCode = "PL";

    // when
    Destination result = mapperFacade.map(src, Destination.class);

    // then
    assertThat(result.name.first).isEqualTo("Jan");
    assertThat(result.countryCode).isEqualTo("PL");
  }
  @Test
  public void shouldCustomizeClassMapBuilder() {
    // given
    MapperFactory mapperFactory =
        new DefaultMapperFactory.Builder()
            .classMapBuilderFactory(new ScoringClassMapBuilder.Factory())
            .build();

    // when
    ClassMap<Source, Destination> map =
        mapperFactory.classMap(Source.class, Destination.class).byDefault().toClassMap();

    Map<String, String> mapping = new HashMap<String, String>();
    for (FieldMap f : map.getFieldsMapping()) {
      mapping.put(f.getSource().getExpression(), f.getDestination().getExpression());
    }

    // then
    assertThat("name.first").isEqualTo(mapping.get("firstName"));
    assertThat("name.last").isEqualTo(mapping.get("lastName"));
    assertThat("streetAddress").isEqualTo(mapping.get("postalAddress.street"));
    assertThat("countryCode").isEqualTo(mapping.get("postalAddress.country.alphaCode"));
    assertThat("currentAge").isEqualTo(mapping.get("age"));
    assertThat("birthState").isEqualTo(mapping.get("stateOfBirth"));

    assertThat(mapping.containsKey("driversLicenseNumber")).isFalse();
    assertThat(mapping.containsKey("eyeColor")).isFalse();
  }
 private void registerOrganization(MapperFactory factory) {
   factory.registerClassMap(
       factory
           .classMap(Organization.class, com.sparc.knappsack.models.api.v1.Organization.class)
           .byDefault()
           .toClassMap());
 }
  @Test
  public void shouldExplicitlySpecifyConstructor() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDtoWithConstructor.class)
        .constructorB("birthDate")
        .fieldAToB("name", "fullName")
        //                .field("age", "currentAge")
        .byDefault()
        .register();

    BasicPerson bp = createBasicPerson("Jan", 20, Calendar.getInstance().getTime());

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

    // then
    assertThat(result.getFullName()).isEqualTo(bp.getName());
    assertThat(result.getCurrentAge()).isEqualTo(0); // !!
    assertThat(result.getBirthDate()).isNotNull();
  }
 private void registerGroup(MapperFactory factory) {
   factory.registerClassMap(
       factory
           .classMap(Group.class, com.sparc.knappsack.models.api.v1.Group.class)
           .byDefault()
           .toClassMap());
 }
  @Test
  public void shouldNotMapNulls_ClassLevel() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(true).build();

    mapperFactory.classMap(Container.class, Container.class).mapNulls(false).byDefault().register();

    Container a = new Container();
    Container b = new Container();

    b.longValue = 1L;
    b.stringValue = "TEST A";
    b.arrayOfString = new String[] {"a", "b", "c"};
    b.arrayOfInt = new int[] {1, 2, 3};
    b.listOfString = Arrays.asList("l1", "l2");
    b.map = Collections.singletonMap("key", (Object) "value");
    b.enumValue = Position.FIRST;

    // when
    mapperFactory.getMapperFacade().map(a, b);

    // then
    assertThat(b.stringValue).isNotNull();
    assertThat(b.arrayOfString).isNotNull();
    assertThat(b.arrayOfInt).isNotNull();
    assertThat(b.listOfString).isNotNull();
    assertThat(b.map).isNotNull();
    assertThat(b.enumValue).isNotNull();
  }
  @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();
  }
 @Override
 public void configure(final MapperFactory mapperFactory) {
   mapperFactory.getConverterFactory().registerConverter(new MeterTypeConverter());
   mapperFactory.getConverterFactory().registerConverter(new DaliConfigurationConverter());
   mapperFactory
       .getConverterFactory()
       .registerConverter(new XMLGregorianCalendarToDateTimeConverter());
 }
예제 #12
0
 public static void register(MapperFactory factory) {
   factory.getConverterFactory().registerConverter(new ToOptionalConverter());
   factory.getConverterFactory().registerConverter(new OptionalImmutableConverter());
   factory.getConverterFactory().registerConverter(new OptionalEntitityReferenceConverter());
   factory.registerFilter(new Filter());
   factory.registerFilter(new CollectionFilter());
   factory.registerMapper(new Mapper());
 }
 private void registerApplicationVersion(MapperFactory factory) {
   factory.registerClassMap(
       factory
           .classMap(
               ApplicationVersion.class,
               com.sparc.knappsack.models.api.v1.ApplicationVersion.class)
           .byDefault()
           .toClassMap());
 }
예제 #14
0
 @Test
 public void testMapOfEnum() {
   DefaultMapperFactory.Builder builder = new DefaultMapperFactory.Builder();
   MapperFactory factory = builder.build();
   MapperFacade mapperFacade = factory.getMapperFacade();
   Entity entity = new Entity();
   entity.setState(State.B);
   final Dto dto = mapperFacade.map(entity, Dto.class);
   Assert.assertEquals(dto.getState(), entity.getState());
 }
예제 #15
0
 public MapperFacade getClientMapperFacade() {
   MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
   ClassMapBuilder<Client, ClientDetailsEntity> clientClassMap =
       mapperFactory.classMap(Client.class, ClientDetailsEntity.class);
   clientClassMap.field("name", "clientName");
   clientClassMap.field("description", "clientDescription");
   clientClassMap.byDefault();
   clientClassMap.register();
   return mapperFactory.getMapperFacade();
 }
 private void registerBranding(MapperFactory factory) {
   factory.registerClassMap(
       factory
           .classMap(CustomBranding.class, com.sparc.knappsack.models.api.v1.Branding.class)
           .fieldMap("logo")
           .converter("imageConverter")
           .add()
           .byDefault()
           .toClassMap());
 }
 private void registerCategory(MapperFactory factory) {
   factory.registerClassMap(
       factory
           .classMap(Category.class, com.sparc.knappsack.models.api.v1.Category.class)
           .fieldMap("icon")
           .converter("imageConverter")
           .add()
           .byDefault()
           .toClassMap());
 }
예제 #18
0
  @BeforeClass
  public static void init() {

    DefaultMapperFactory.Builder factoryBuilder = new DefaultMapperFactory.Builder();
    factoryBuilder.compilerStrategy(new EclipseJdtCompilerStrategy());
    MapperFactory factory = factoryBuilder.build();

    factory.getConverterFactory().registerConverter(new Converter.Object2String());
    factory.getConverterFactory().registerConverter(new Converter.String2Object());

    mapper = factory.getMapperFacade();
  }
예제 #19
0
 public MapperFacade getNameMapperFacade() {
   MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
   ClassMapBuilder<Name, RecordNameEntity> nameClassMap =
       mapperFactory.classMap(Name.class, RecordNameEntity.class);
   addV2DateFields(nameClassMap);
   nameClassMap.field("creditName.content", "creditName");
   nameClassMap.field("givenNames.content", "givenNames");
   nameClassMap.field("familyName.content", "familyName");
   nameClassMap.field("path", "profile.id");
   nameClassMap.byDefault();
   nameClassMap.register();
   return mapperFactory.getMapperFacade();
 }
예제 #20
0
 public MapperFacade getEmailMapperFacade() {
   MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
   ClassMapBuilder<Email, EmailEntity> emailClassMap =
       mapperFactory.classMap(Email.class, EmailEntity.class);
   emailClassMap.byDefault();
   emailClassMap.field("email", "id");
   emailClassMap.field("primary", "primary");
   emailClassMap.field("verified", "verified");
   addV2DateFields(emailClassMap);
   registerSourceConverters(mapperFactory, emailClassMap);
   emailClassMap.register();
   return mapperFactory.getMapperFacade();
 }
예제 #21
0
 public MapperFacade getKeywordMapperFacade() {
   MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
   ClassMapBuilder<Keyword, ProfileKeywordEntity> keywordClassMap =
       mapperFactory.classMap(Keyword.class, ProfileKeywordEntity.class);
   addV2DateFields(keywordClassMap);
   registerSourceConverters(mapperFactory, keywordClassMap);
   keywordClassMap.field("putCode", "id");
   keywordClassMap.field("content", "keywordName");
   keywordClassMap.fieldBToA("displayIndex", "displayIndex");
   keywordClassMap.byDefault();
   keywordClassMap.register();
   return mapperFactory.getMapperFacade();
 }
  @Test
  public void testBeanToCustomMapGeneration() throws Exception {

    MapperFactory factory = MappingUtil.getMapperFactory();

    Type<Map<String, String>> mapType = new TypeBuilder<Map<String, String>>() {}.build();
    Type<Student> studentType = TypeFactory.valueOf(Student.class);

    factory
        .classMap(Student.class, mapType)
        .field("grade.letter", "letterGrade")
        .field("grade.point", "GPA")
        .field("grade.percentage", "gradePercentage")
        .field("name.first", "firstName")
        .field("name.last", "lastName")
        .byDefault()
        .register();

    MapperFacade mapper = factory.getMapperFacade();

    Student student = new Student();
    student.id = "1";
    student.email = "*****@*****.**";
    student.name = new Name();
    student.name.first = "Chuck";
    student.name.last = "Testa";
    student.grade = new Grade();
    student.grade.letter = "B-";
    student.grade.percentage = 81.5;
    student.grade.point = 2.7;

    Map<String, String> result = mapper.map(student, studentType, mapType);

    Assert.assertEquals(student.id, result.get("id"));
    Assert.assertEquals(student.email, result.get("email"));
    Assert.assertEquals(student.name.first, result.get("firstName"));
    Assert.assertEquals(student.name.last, result.get("lastName"));
    Assert.assertEquals(student.grade.letter, result.get("letterGrade"));
    Assert.assertEquals("" + student.grade.percentage, result.get("gradePercentage"));
    Assert.assertEquals("" + student.grade.point, result.get("GPA"));

    Student mapBack = mapper.map(result, mapType, studentType);

    Assert.assertEquals(student.id, mapBack.id);
    Assert.assertEquals(student.email, mapBack.email);
    Assert.assertEquals(student.name.first, mapBack.name.first);
    Assert.assertEquals(student.name.last, mapBack.name.last);
    Assert.assertEquals(student.grade.letter, mapBack.grade.letter);
    Assert.assertEquals(student.grade.percentage, mapBack.grade.percentage);
    Assert.assertEquals(student.grade.point, mapBack.grade.point);
  }
예제 #23
0
 public MapperFacade getAddressMapperFacade() {
   MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
   ClassMapBuilder<Address, AddressEntity> addressClassMap =
       mapperFactory.classMap(Address.class, AddressEntity.class);
   addV2DateFields(addressClassMap);
   registerSourceConverters(mapperFactory, addressClassMap);
   addressClassMap.field("putCode", "id");
   addressClassMap.field("country.value", "iso2Country");
   addressClassMap.field("visibility", "visibility");
   addressClassMap.fieldBToA("displayIndex", "displayIndex");
   addressClassMap.byDefault();
   addressClassMap.register();
   return mapperFactory.getMapperFacade();
 }
예제 #24
0
 public MapperFacade getOtherNameMapperFacade() {
   MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
   ClassMapBuilder<OtherName, OtherNameEntity> otherNameClassMap =
       mapperFactory.classMap(OtherName.class, OtherNameEntity.class);
   addV2DateFields(otherNameClassMap);
   registerSourceConverters(mapperFactory, otherNameClassMap);
   otherNameClassMap.field("putCode", "id");
   otherNameClassMap.field("content", "displayName");
   otherNameClassMap.field("path", "profile.orcid");
   otherNameClassMap.fieldBToA("displayIndex", "displayIndex");
   otherNameClassMap.byDefault();
   otherNameClassMap.register();
   return mapperFactory.getMapperFacade();
 }
예제 #25
0
 public MapperFacade getResearcherUrlMapperFacade() {
   MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
   ClassMapBuilder<ResearcherUrl, ResearcherUrlEntity> researcherUrlClassMap =
       mapperFactory.classMap(ResearcherUrl.class, ResearcherUrlEntity.class);
   addV2DateFields(researcherUrlClassMap);
   registerSourceConverters(mapperFactory, researcherUrlClassMap);
   researcherUrlClassMap.field("putCode", "id");
   researcherUrlClassMap.field("url.value", "url");
   researcherUrlClassMap.field("urlName", "urlName");
   researcherUrlClassMap.fieldBToA("displayIndex", "displayIndex");
   researcherUrlClassMap.byDefault();
   researcherUrlClassMap.register();
   return mapperFactory.getMapperFacade();
 }
  @Test
  public void testBeanToMapGeneration() throws Exception {

    MapperFactory factory = MappingUtil.getMapperFactory();

    factory
        .classMap(Student.class, Map.class)
        .field("grade.letter", "letterGrade")
        .field("grade.point", "GPA")
        .field("grade.percentage", "gradePercentage")
        .field("name.first", "firstName")
        .field("name.last", "lastName")
        .byDefault()
        .register();

    MapperFacade mapper = factory.getMapperFacade();

    Student student = new Student();
    student.id = "1";
    student.email = "*****@*****.**";
    student.name = new Name();
    student.name.first = "Chuck";
    student.name.last = "Testa";
    student.grade = new Grade();
    student.grade.letter = "B-";
    student.grade.percentage = 81.5;
    student.grade.point = 2.7;

    @SuppressWarnings("unchecked")
    Map<String, Object> result = mapper.map(student, Map.class);

    Assert.assertEquals(student.id, result.get("id"));
    Assert.assertEquals(student.email, result.get("email"));
    Assert.assertEquals(student.name.first, result.get("firstName"));
    Assert.assertEquals(student.name.last, result.get("lastName"));
    Assert.assertEquals(student.grade.letter, result.get("letterGrade"));
    Assert.assertEquals(student.grade.percentage, result.get("gradePercentage"));
    Assert.assertEquals(student.grade.point, result.get("GPA"));

    Student mapBack = mapper.map(result, Student.class);

    Assert.assertEquals(student.id, mapBack.id);
    Assert.assertEquals(student.email, mapBack.email);
    Assert.assertEquals(student.name.first, mapBack.name.first);
    Assert.assertEquals(student.name.last, mapBack.name.last);
    Assert.assertEquals(student.grade.letter, mapBack.grade.letter);
    Assert.assertEquals(student.grade.percentage, mapBack.grade.percentage);
    Assert.assertEquals(student.grade.point, mapBack.grade.point);
  }
 private void registerApplication(MapperFactory factory) {
   factory.registerClassMap(
       factory
           .classMap(Application.class, com.sparc.knappsack.models.api.v1.Application.class)
           .fieldMap("icon")
           .converter("imageConverter")
           .add()
           .fieldMap("screenshots", "screenShots")
           .converter("imageCollectionConverter")
           .add()
           .field("ownedGroup.id", "groupId")
           .field("category.id", "categoryId")
           .byDefault()
           .toClassMap());
 }
예제 #28
0
  public MapperFacade getGroupIdRecordMapperFacade() {
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    ClassMapBuilder<GroupIdRecord, GroupIdRecordEntity> classMap =
        mapperFactory.classMap(GroupIdRecord.class, GroupIdRecordEntity.class);
    addV2CommonFields(classMap);
    registerSourceConverters(mapperFactory, classMap);
    classMap.field("name", "groupName");
    classMap.field("groupId", "groupId");
    classMap.field("description", "groupDescription");
    classMap.field("type", "groupType");
    classMap.register();

    return mapperFactory.getMapperFacade();
  }
예제 #29
0
  @Bean
  public Mapper mapper() {
    EditorMapper mapper = new EditorMapper();
    // http://stackoverflow.com/a/32309223/1203690
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    ConverterFactory converterFactory = mapperFactory.getConverterFactory();

    converterFactory.registerConverter(new PassThroughConverter(LocalDate.class));

    converterFactory.registerConverter(new PassThroughConverter(LocalDateTime.class));
    mapper.setMapperFacade(mapperFactory.getMapperFacade());

    return mapper;
  }
  @Test
  public void shouldUseOnlyOneDirectionMapping() {
    // given
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

    mapperFactory
        .classMap(BasicPerson.class, BasicPersonDto.class)
        .fieldAToB("name", "fullName")
        .field("age", "currentAge")
        .field("birthDate", "birthDate")
        .register();

    BasicPerson bpSrc = createBasicPerson("Jan", 20, Calendar.getInstance().getTime());

    BasicPersonDto bpDtoSrc = new BasicPersonDto();
    bpDtoSrc.setFullName("Jan");
    bpDtoSrc.setCurrentAge(20);
    bpDtoSrc.setBirthDate(Calendar.getInstance().getTime());

    // when
    BoundMapperFacade<BasicPerson, BasicPersonDto> boundMapper =
        mapperFactory.getMapperFacade(BasicPerson.class, BasicPersonDto.class);

    BasicPersonDto bpDtoDest = boundMapper.map(bpSrc);
    BasicPerson bpDest = boundMapper.mapReverse(bpDtoSrc);

    // then
    assertThat(bpDtoDest.getCurrentAge()).isEqualTo(bpSrc.getAge());
    assertThat(bpDtoDest.getFullName()).isEqualTo(bpSrc.getName());
    assertThat(bpDtoDest.getBirthDate()).isEqualTo(bpSrc.getBirthDate());

    assertThat(bpDest.getAge()).isEqualTo(bpDtoSrc.getCurrentAge());
    assertThat(bpDest.getName()).isNull();
    assertThat(bpDest.getBirthDate()).isEqualTo(bpDtoSrc.getBirthDate());
  }