From 1f7bbd3c2266cc815437247649a0caffb07f4802 Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 3 Feb 2019 11:55:43 +0530 Subject: [PATCH] [BAEL-12114] - Upgraded MapStruct article --- mapstruct/pom.xml | 13 +++++++- .../main/java/com/baeldung/dto/CarDTO.java | 11 +++++++ .../main/java/com/baeldung/dto/PersonDTO.java | 33 +++++++++++++++++++ .../main/java/com/baeldung/entity/Car.java | 11 +++++++ .../main/java/com/baeldung/entity/Person.java | 33 +++++++++++++++++++ .../java/com/baeldung/mapper/CarMapper.java | 15 +++++++++ .../com/baeldung/mapper/PersonMapper.java | 17 ++++++++++ .../baeldung/mapper/CarMapperUnitTest.java | 24 ++++++++++++++ .../baeldung/mapper/PersonMapperUnitTest.java | 26 +++++++++++++++ 9 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 mapstruct/src/main/java/com/baeldung/dto/CarDTO.java create mode 100644 mapstruct/src/main/java/com/baeldung/dto/PersonDTO.java create mode 100644 mapstruct/src/main/java/com/baeldung/entity/Car.java create mode 100644 mapstruct/src/main/java/com/baeldung/entity/Person.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapper/CarMapper.java create mode 100644 mapstruct/src/main/java/com/baeldung/mapper/PersonMapper.java create mode 100644 mapstruct/src/test/java/com/baeldung/mapper/CarMapperUnitTest.java create mode 100644 mapstruct/src/test/java/com/baeldung/mapper/PersonMapperUnitTest.java diff --git a/mapstruct/pom.xml b/mapstruct/pom.xml index 6c274dfcf2..0493775f85 100644 --- a/mapstruct/pom.xml +++ b/mapstruct/pom.xml @@ -30,6 +30,11 @@ ${springframework.version} test + + org.projectlombok + lombok + ${org.projectlombok.version} + @@ -48,6 +53,11 @@ mapstruct-processor ${org.mapstruct.version} + + org.projectlombok + lombok + ${org.projectlombok.version} + @@ -55,10 +65,11 @@ - 1.1.0.Final + 1.3.0.Beta2 4.3.4.RELEASE 1.8 1.8 + 1.18.4 diff --git a/mapstruct/src/main/java/com/baeldung/dto/CarDTO.java b/mapstruct/src/main/java/com/baeldung/dto/CarDTO.java new file mode 100644 index 0000000000..51aa8ccac2 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/dto/CarDTO.java @@ -0,0 +1,11 @@ +package com.baeldung.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CarDTO { + private int id; + private String name; +} diff --git a/mapstruct/src/main/java/com/baeldung/dto/PersonDTO.java b/mapstruct/src/main/java/com/baeldung/dto/PersonDTO.java new file mode 100644 index 0000000000..ace7a2bc7d --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/dto/PersonDTO.java @@ -0,0 +1,33 @@ +package com.baeldung.dto; + +public class PersonDTO { + + private String id; + private String name; + + public PersonDTO() { + + } + + public PersonDTO(String id, String name) { + super(); + this.id = id; + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/entity/Car.java b/mapstruct/src/main/java/com/baeldung/entity/Car.java new file mode 100644 index 0000000000..8559b4a77d --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/entity/Car.java @@ -0,0 +1,11 @@ +package com.baeldung.entity; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class Car { + private int id; + private String name; +} diff --git a/mapstruct/src/main/java/com/baeldung/entity/Person.java b/mapstruct/src/main/java/com/baeldung/entity/Person.java new file mode 100644 index 0000000000..5f73ee1130 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/entity/Person.java @@ -0,0 +1,33 @@ +package com.baeldung.entity; + +public class Person { + + private String id; + private String name; + + public Person() { + + } + + public Person(String id, String name) { + super(); + this.id = id; + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/mapper/CarMapper.java b/mapstruct/src/main/java/com/baeldung/mapper/CarMapper.java new file mode 100644 index 0000000000..c18fa44f8d --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapper/CarMapper.java @@ -0,0 +1,15 @@ +package com.baeldung.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +import com.baeldung.dto.CarDTO; +import com.baeldung.entity.Car; + +@Mapper +public interface CarMapper { + + CarMapper INSTANCE = Mappers.getMapper(CarMapper.class); + + CarDTO carToCarDTO(Car car); +} diff --git a/mapstruct/src/main/java/com/baeldung/mapper/PersonMapper.java b/mapstruct/src/main/java/com/baeldung/mapper/PersonMapper.java new file mode 100644 index 0000000000..9b9e132b5d --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapper/PersonMapper.java @@ -0,0 +1,17 @@ +package com.baeldung.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +import com.baeldung.dto.PersonDTO; +import com.baeldung.entity.Person; + +@Mapper +public interface PersonMapper { + + PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class); + + @Mapping(target = "id", source = "person.id", defaultExpression = "java(java.util.UUID.randomUUID().toString())") + PersonDTO personToPersonDTO(Person person); +} diff --git a/mapstruct/src/test/java/com/baeldung/mapper/CarMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/CarMapperUnitTest.java new file mode 100644 index 0000000000..32cae56c2e --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/CarMapperUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.mapper; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.baeldung.dto.CarDTO; +import com.baeldung.entity.Car; + +public class CarMapperUnitTest { + + @Test + public void givenCarEntitytoCar_whenMaps_thenCorrect() { + + Car entity = new Car(); + entity.setId(1); + entity.setName("Toyota"); + + CarDTO carDto = CarMapper.INSTANCE.carToCarDTO(entity); + + assertEquals(carDto.getId(), entity.getId()); + assertEquals(carDto.getName(), entity.getName()); + } +} diff --git a/mapstruct/src/test/java/com/baeldung/mapper/PersonMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/PersonMapperUnitTest.java new file mode 100644 index 0000000000..fe4c52ac89 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/PersonMapperUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.mapper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import com.baeldung.dto.PersonDTO; +import com.baeldung.entity.Person; + +public class PersonMapperUnitTest { + + @Test + public void givenPersonEntitytoPersonWithExpression_whenMaps_thenCorrect() { + + Person entity = new Person(); + entity.setName("Micheal"); + + PersonDTO personDto = PersonMapper.INSTANCE.personToPersonDTO(entity); + + assertNull(entity.getId()); + assertNotNull(personDto.getId()); + assertEquals(personDto.getName(), entity.getName()); + } +} \ No newline at end of file