From 63dcae64238124c6cfe87a96bea4e08358a6e936 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Wed, 1 Apr 2020 19:18:10 +0200 Subject: [PATCH 01/18] Practical example of Hexagonal architecture - add project --- .../baeldung/employee/config/AppConfig.java | 73 +++++++++++++++++++ .../config/ApplicationInitializer.java | 29 ++++++++ .../employee/data/EmployeeDataAdapter.java | 9 +++ .../data/EmployeeDataAdapterImpl.java | 28 +++++++ .../baeldung/employee/data/EmployeeRepo.java | 9 +++ .../baeldung/employee/domain/Employee.java | 43 +++++++++++ .../baeldung/employee/dto/EmployeeDTO.java | 9 +++ .../employee/service/EmployeeController.java | 47 ++++++++++++ .../employee/service/EmployeeService.java | 8 ++ .../employee/service/EmployeeServiceImpl.java | 25 +++++++ EmpServiceApp/src/createEmployeeTable.sql | 11 +++ .../web/WEB-INF/applicationContext.xml | 12 +++ EmpServiceApp/web/WEB-INF/web.xml | 25 +++++++ 13 files changed, 328 insertions(+) create mode 100644 EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/config/ApplicationInitializer.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapter.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapterImpl.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/domain/Employee.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/dto/EmployeeDTO.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/service/EmployeeController.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/service/EmployeeService.java create mode 100644 EmpServiceApp/src/com/baeldung/employee/service/EmployeeServiceImpl.java create mode 100644 EmpServiceApp/src/createEmployeeTable.sql create mode 100644 EmpServiceApp/web/WEB-INF/applicationContext.xml create mode 100644 EmpServiceApp/web/WEB-INF/web.xml diff --git a/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java b/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java new file mode 100644 index 0000000000..3f6919b8f5 --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java @@ -0,0 +1,73 @@ +package com.baeldung.employee.config; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.modelmapper.ModelMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.TransactionManager; + +@Configuration +@EnableJpaRepositories(basePackages = "com.baeldung.employee.data", entityManagerFactoryRef = "emf") +@ComponentScan(basePackages = "com.baeldung.employee") +public class AppConfig { + + @Bean + public DataSource h2DataSource() { + + EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); + EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2) + .addScript("createEmployeeTable.sql") + .build(); + return db; + + } + + @Bean + public ModelMapper mapper() { + + return new ModelMapper(); + } + + /* @Bean + public ObjectMapper objectMapper() { + + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true); + + return mapper; + } + */ + + @Bean(name = "emf") + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + + HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); + factory.setJpaVendorAdapter(vendorAdapter); + factory.setPackagesToScan("com.baeldung.employee"); + factory.setDataSource(h2DataSource()); + // factory.setJpaProperties(jpaProperties()); + + return factory; + } + + @Bean + public TransactionManager jpaTransactionManager(EntityManagerFactory emf) { + JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(emf); + return transactionManager; + } + +} diff --git a/EmpServiceApp/src/com/baeldung/employee/config/ApplicationInitializer.java b/EmpServiceApp/src/com/baeldung/employee/config/ApplicationInitializer.java new file mode 100644 index 0000000000..491e97b2c8 --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/config/ApplicationInitializer.java @@ -0,0 +1,29 @@ +package com.baeldung.employee.config; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; + +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +public class ApplicationInitializer implements WebApplicationInitializer { + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.register(AppConfig.class); + + servletContext.addListener(new ContextLoaderListener(context)); + + // Create DispatcherServlet + + ServletRegistration.Dynamic dispatcher = servletContext.addServlet("EmployeeSpringApp", new DispatcherServlet()); + dispatcher.setLoadOnStartup(1); + dispatcher.addMapping("/app"); + + } +} diff --git a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapter.java b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapter.java new file mode 100644 index 0000000000..da5d5bb1a3 --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapter.java @@ -0,0 +1,9 @@ +package com.baeldung.employee.data; + +import com.baeldung.employee.dto.EmployeeDTO; + +public interface EmployeeDataAdapter { + + void addEmployee(EmployeeDTO emp); + +} diff --git a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapterImpl.java b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapterImpl.java new file mode 100644 index 0000000000..54b3079844 --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapterImpl.java @@ -0,0 +1,28 @@ +package com.baeldung.employee.data; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.baeldung.employee.domain.Employee; +import com.baeldung.employee.dto.EmployeeDTO; + +@Component +public class EmployeeDataAdapterImpl implements EmployeeDataAdapter { + + @PersistenceContext + private EntityManager em; + + @Autowired + private ModelMapper modelMapper; + + @Override + public void addEmployee(EmployeeDTO emp) { + + em.persist(modelMapper.map(emp, Employee.class)); + + } +} diff --git a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java new file mode 100644 index 0000000000..4d63dc391d --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java @@ -0,0 +1,9 @@ +package com.baeldung.employee.data; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.baeldung.employee.domain.Employee; + +public interface EmployeeRepo extends JpaRepository { + +} diff --git a/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java b/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java new file mode 100644 index 0000000000..f9cd937c21 --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java @@ -0,0 +1,43 @@ +package com.baeldung.employee.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "EMPLOYEE") +public class Employee { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + private String name; + + public Employee() { + } + + public Employee(int id, String name) { + this.id = id; + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + // Standard constructors, getters and setters +} diff --git a/EmpServiceApp/src/com/baeldung/employee/dto/EmployeeDTO.java b/EmpServiceApp/src/com/baeldung/employee/dto/EmployeeDTO.java new file mode 100644 index 0000000000..3b6b048a9c --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/dto/EmployeeDTO.java @@ -0,0 +1,9 @@ +package com.baeldung.employee.dto; + +public class EmployeeDTO { + + private long id; + private String name; + + // Standard constructors, getters and setters +} diff --git a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeController.java b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeController.java new file mode 100644 index 0000000000..b258d11018 --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeController.java @@ -0,0 +1,47 @@ +package com.baeldung.employee.service; + +import java.net.URI; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import com.baeldung.employee.domain.Employee; + +@RestController +public class EmployeeController { + + @Autowired + private EmployeeService empService; + + @GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE) + public String home() { + + return "This is employee app page"; + } + + @PostMapping(path = "/employees", consumes = "application/json", produces = "application/json") + public ResponseEntity addEmployee(@RequestBody Map empBody) { + + String id = empBody.get("id"); + String name = empBody.get("name"); + + Employee emp = new Employee(Integer.valueOf(id), name); + empService.addEmployee(emp); + + // Create resource uri + URI uri = ServletUriComponentsBuilder.fromCurrentRequest() + .buildAndExpand(emp.getId()) + .toUri(); + + // Send uri in response + return ResponseEntity.created(uri) + .build(); + } +} diff --git a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeService.java b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeService.java new file mode 100644 index 0000000000..81c7665a50 --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeService.java @@ -0,0 +1,8 @@ +package com.baeldung.employee.service; + +import com.baeldung.employee.domain.Employee; + +public interface EmployeeService { + + void addEmployee(Employee emp); +} diff --git a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeServiceImpl.java b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeServiceImpl.java new file mode 100644 index 0000000000..45aba2898b --- /dev/null +++ b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeServiceImpl.java @@ -0,0 +1,25 @@ +package com.baeldung.employee.service; + +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.employee.data.EmployeeDataAdapter; +import com.baeldung.employee.domain.Employee; +import com.baeldung.employee.dto.EmployeeDTO; + +@Service +public class EmployeeServiceImpl implements EmployeeService { + + @Autowired + private EmployeeDataAdapter employeeDataAdapter; + + @Autowired + private ModelMapper modelMapper; + + @Override + public void addEmployee(Employee emp) { + employeeDataAdapter.addEmployee(modelMapper.map(emp, EmployeeDTO.class)); + } + +} diff --git a/EmpServiceApp/src/createEmployeeTable.sql b/EmpServiceApp/src/createEmployeeTable.sql new file mode 100644 index 0000000000..14b8347bff --- /dev/null +++ b/EmpServiceApp/src/createEmployeeTable.sql @@ -0,0 +1,11 @@ +DROP TABLE IF EXISTS EMPLOYEE; + +CREATE TABLE EMPLOYEE ( + + ID IDENTITY PRIMARY KEY, + NAME VARCHAR(64) NOT NULL, + +); + +INSERT INTO EMPLOYEE (ID, NAME) VALUES (10, 'EMP 10'); +INSERT INTO EMPLOYEE (ID, NAME) VALUES (20, 'EMP 20'); diff --git a/EmpServiceApp/web/WEB-INF/applicationContext.xml b/EmpServiceApp/web/WEB-INF/applicationContext.xml new file mode 100644 index 0000000000..54b3429708 --- /dev/null +++ b/EmpServiceApp/web/WEB-INF/applicationContext.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/EmpServiceApp/web/WEB-INF/web.xml b/EmpServiceApp/web/WEB-INF/web.xml new file mode 100644 index 0000000000..7731773f82 --- /dev/null +++ b/EmpServiceApp/web/WEB-INF/web.xml @@ -0,0 +1,25 @@ + + + Employee Spring App + + + org.springframework.web.context.ContextLoaderListener + + + + EmployeeSpringApp + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/applicationContext.xml + + 1 + + + EmployeeSpringApp + /* + + \ No newline at end of file From ce412e718e223baa73b018c0654a0d83e0a34806 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Thu, 2 Apr 2020 18:53:23 +0200 Subject: [PATCH 02/18] Refactoring domain and config classes --- .../src/com/baeldung/employee/config/AppConfig.java | 12 +----------- .../src/com/baeldung/employee/data/EmployeeRepo.java | 2 +- .../src/com/baeldung/employee/domain/Employee.java | 2 +- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java b/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java index 3f6919b8f5..3bde8fceed 100644 --- a/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java +++ b/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java @@ -38,16 +38,6 @@ public class AppConfig { return new ModelMapper(); } - /* @Bean - public ObjectMapper objectMapper() { - - ObjectMapper mapper = new ObjectMapper(); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true); - - return mapper; - } - */ @Bean(name = "emf") public LocalContainerEntityManagerFactoryBean entityManagerFactory() { @@ -58,7 +48,7 @@ public class AppConfig { factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.baeldung.employee"); factory.setDataSource(h2DataSource()); - // factory.setJpaProperties(jpaProperties()); + return factory; } diff --git a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java index 4d63dc391d..0712f7823a 100644 --- a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java +++ b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java @@ -4,6 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import com.baeldung.employee.domain.Employee; -public interface EmployeeRepo extends JpaRepository { +public interface EmployeeRepo extends JpaRepository { } diff --git a/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java b/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java index f9cd937c21..3b93b8a97e 100644 --- a/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java +++ b/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java @@ -7,7 +7,7 @@ import javax.persistence.Id; import javax.persistence.Table; @Entity -@Table(name = "EMPLOYEE") +@Table public class Employee { @Id From 6205b4965841c366c97fcb62bfe9f2d26ceb17b9 Mon Sep 17 00:00:00 2001 From: Sasa m Date: Thu, 9 Apr 2020 20:56:42 +0200 Subject: [PATCH 03/18] Remove unused commit --- .../baeldung/employee/config/AppConfig.java | 63 ------------------- .../config/ApplicationInitializer.java | 29 --------- .../employee/data/EmployeeDataAdapter.java | 9 --- .../data/EmployeeDataAdapterImpl.java | 28 --------- .../baeldung/employee/data/EmployeeRepo.java | 9 --- .../baeldung/employee/domain/Employee.java | 43 ------------- .../baeldung/employee/dto/EmployeeDTO.java | 9 --- .../employee/service/EmployeeController.java | 47 -------------- .../employee/service/EmployeeService.java | 8 --- .../employee/service/EmployeeServiceImpl.java | 25 -------- EmpServiceApp/src/createEmployeeTable.sql | 11 ---- .../web/WEB-INF/applicationContext.xml | 12 ---- EmpServiceApp/web/WEB-INF/web.xml | 25 -------- 13 files changed, 318 deletions(-) delete mode 100644 EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/config/ApplicationInitializer.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapter.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapterImpl.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/domain/Employee.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/dto/EmployeeDTO.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/service/EmployeeController.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/service/EmployeeService.java delete mode 100644 EmpServiceApp/src/com/baeldung/employee/service/EmployeeServiceImpl.java delete mode 100644 EmpServiceApp/src/createEmployeeTable.sql delete mode 100644 EmpServiceApp/web/WEB-INF/applicationContext.xml delete mode 100644 EmpServiceApp/web/WEB-INF/web.xml diff --git a/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java b/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java deleted file mode 100644 index 3bde8fceed..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/config/AppConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.employee.config; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; - -import org.modelmapper.ModelMapper; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; -import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; -import org.springframework.transaction.TransactionManager; - -@Configuration -@EnableJpaRepositories(basePackages = "com.baeldung.employee.data", entityManagerFactoryRef = "emf") -@ComponentScan(basePackages = "com.baeldung.employee") -public class AppConfig { - - @Bean - public DataSource h2DataSource() { - - EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); - EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.H2) - .addScript("createEmployeeTable.sql") - .build(); - return db; - - } - - @Bean - public ModelMapper mapper() { - - return new ModelMapper(); - } - - - @Bean(name = "emf") - public LocalContainerEntityManagerFactoryBean entityManagerFactory() { - - HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - - LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); - factory.setJpaVendorAdapter(vendorAdapter); - factory.setPackagesToScan("com.baeldung.employee"); - factory.setDataSource(h2DataSource()); - - - return factory; - } - - @Bean - public TransactionManager jpaTransactionManager(EntityManagerFactory emf) { - JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManager.setEntityManagerFactory(emf); - return transactionManager; - } - -} diff --git a/EmpServiceApp/src/com/baeldung/employee/config/ApplicationInitializer.java b/EmpServiceApp/src/com/baeldung/employee/config/ApplicationInitializer.java deleted file mode 100644 index 491e97b2c8..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/config/ApplicationInitializer.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.employee.config; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; - -import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; - -public class ApplicationInitializer implements WebApplicationInitializer { - - @Override - public void onStartup(ServletContext servletContext) throws ServletException { - - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.register(AppConfig.class); - - servletContext.addListener(new ContextLoaderListener(context)); - - // Create DispatcherServlet - - ServletRegistration.Dynamic dispatcher = servletContext.addServlet("EmployeeSpringApp", new DispatcherServlet()); - dispatcher.setLoadOnStartup(1); - dispatcher.addMapping("/app"); - - } -} diff --git a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapter.java b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapter.java deleted file mode 100644 index da5d5bb1a3..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapter.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.employee.data; - -import com.baeldung.employee.dto.EmployeeDTO; - -public interface EmployeeDataAdapter { - - void addEmployee(EmployeeDTO emp); - -} diff --git a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapterImpl.java b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapterImpl.java deleted file mode 100644 index 54b3079844..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeDataAdapterImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.employee.data; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - -import org.modelmapper.ModelMapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import com.baeldung.employee.domain.Employee; -import com.baeldung.employee.dto.EmployeeDTO; - -@Component -public class EmployeeDataAdapterImpl implements EmployeeDataAdapter { - - @PersistenceContext - private EntityManager em; - - @Autowired - private ModelMapper modelMapper; - - @Override - public void addEmployee(EmployeeDTO emp) { - - em.persist(modelMapper.map(emp, Employee.class)); - - } -} diff --git a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java b/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java deleted file mode 100644 index 0712f7823a..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/data/EmployeeRepo.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.employee.data; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.baeldung.employee.domain.Employee; - -public interface EmployeeRepo extends JpaRepository { - -} diff --git a/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java b/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java deleted file mode 100644 index 3b93b8a97e..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/domain/Employee.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.employee.domain; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -@Entity -@Table -public class Employee { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private int id; - - private String name; - - public Employee() { - } - - public Employee(int id, String name) { - this.id = id; - this.name = name; - } - - public long getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - // Standard constructors, getters and setters -} diff --git a/EmpServiceApp/src/com/baeldung/employee/dto/EmployeeDTO.java b/EmpServiceApp/src/com/baeldung/employee/dto/EmployeeDTO.java deleted file mode 100644 index 3b6b048a9c..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/dto/EmployeeDTO.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.employee.dto; - -public class EmployeeDTO { - - private long id; - private String name; - - // Standard constructors, getters and setters -} diff --git a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeController.java b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeController.java deleted file mode 100644 index b258d11018..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeController.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.employee.service; - -import java.net.URI; -import java.util.Map; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.support.ServletUriComponentsBuilder; - -import com.baeldung.employee.domain.Employee; - -@RestController -public class EmployeeController { - - @Autowired - private EmployeeService empService; - - @GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE) - public String home() { - - return "This is employee app page"; - } - - @PostMapping(path = "/employees", consumes = "application/json", produces = "application/json") - public ResponseEntity addEmployee(@RequestBody Map empBody) { - - String id = empBody.get("id"); - String name = empBody.get("name"); - - Employee emp = new Employee(Integer.valueOf(id), name); - empService.addEmployee(emp); - - // Create resource uri - URI uri = ServletUriComponentsBuilder.fromCurrentRequest() - .buildAndExpand(emp.getId()) - .toUri(); - - // Send uri in response - return ResponseEntity.created(uri) - .build(); - } -} diff --git a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeService.java b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeService.java deleted file mode 100644 index 81c7665a50..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeService.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.baeldung.employee.service; - -import com.baeldung.employee.domain.Employee; - -public interface EmployeeService { - - void addEmployee(Employee emp); -} diff --git a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeServiceImpl.java b/EmpServiceApp/src/com/baeldung/employee/service/EmployeeServiceImpl.java deleted file mode 100644 index 45aba2898b..0000000000 --- a/EmpServiceApp/src/com/baeldung/employee/service/EmployeeServiceImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.employee.service; - -import org.modelmapper.ModelMapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.employee.data.EmployeeDataAdapter; -import com.baeldung.employee.domain.Employee; -import com.baeldung.employee.dto.EmployeeDTO; - -@Service -public class EmployeeServiceImpl implements EmployeeService { - - @Autowired - private EmployeeDataAdapter employeeDataAdapter; - - @Autowired - private ModelMapper modelMapper; - - @Override - public void addEmployee(Employee emp) { - employeeDataAdapter.addEmployee(modelMapper.map(emp, EmployeeDTO.class)); - } - -} diff --git a/EmpServiceApp/src/createEmployeeTable.sql b/EmpServiceApp/src/createEmployeeTable.sql deleted file mode 100644 index 14b8347bff..0000000000 --- a/EmpServiceApp/src/createEmployeeTable.sql +++ /dev/null @@ -1,11 +0,0 @@ -DROP TABLE IF EXISTS EMPLOYEE; - -CREATE TABLE EMPLOYEE ( - - ID IDENTITY PRIMARY KEY, - NAME VARCHAR(64) NOT NULL, - -); - -INSERT INTO EMPLOYEE (ID, NAME) VALUES (10, 'EMP 10'); -INSERT INTO EMPLOYEE (ID, NAME) VALUES (20, 'EMP 20'); diff --git a/EmpServiceApp/web/WEB-INF/applicationContext.xml b/EmpServiceApp/web/WEB-INF/applicationContext.xml deleted file mode 100644 index 54b3429708..0000000000 --- a/EmpServiceApp/web/WEB-INF/applicationContext.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/EmpServiceApp/web/WEB-INF/web.xml b/EmpServiceApp/web/WEB-INF/web.xml deleted file mode 100644 index 7731773f82..0000000000 --- a/EmpServiceApp/web/WEB-INF/web.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - Employee Spring App - - - org.springframework.web.context.ContextLoaderListener - - - - EmployeeSpringApp - org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - /WEB-INF/applicationContext.xml - - 1 - - - EmployeeSpringApp - /* - - \ No newline at end of file From 0a42d945f85a0418e7e8de9980ff601f5ecb3ccf Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sun, 19 Apr 2020 21:02:17 +0200 Subject: [PATCH 04/18] Source project for the draft article --- model-mapper/src/Main.java | 58 +++++++++++++++ model-mapper/src/com/baeldung/model/User.java | 70 +++++++++++++++++++ .../src/com/baeldung/model/UserDTO.java | 49 +++++++++++++ .../src/com/baeldung/model/UserList.java | 21 ++++++ .../src/com/baeldung/util/MapperUtil.java | 47 +++++++++++++ .../com/baeldung/util/UserPropertyMap.java | 36 ++++++++++ 6 files changed, 281 insertions(+) create mode 100644 model-mapper/src/Main.java create mode 100644 model-mapper/src/com/baeldung/model/User.java create mode 100644 model-mapper/src/com/baeldung/model/UserDTO.java create mode 100644 model-mapper/src/com/baeldung/model/UserList.java create mode 100644 model-mapper/src/com/baeldung/util/MapperUtil.java create mode 100644 model-mapper/src/com/baeldung/util/UserPropertyMap.java diff --git a/model-mapper/src/Main.java b/model-mapper/src/Main.java new file mode 100644 index 0000000000..a6deb49168 --- /dev/null +++ b/model-mapper/src/Main.java @@ -0,0 +1,58 @@ +import com.baeldung.model.User; +import com.baeldung.model.UserDTO; +import com.baeldung.model.UserList; +import com.baeldung.util.MapperUtil; +import com.baeldung.util.UserPropertyMap; +import org.modelmapper.ModelMapper; +import org.modelmapper.TypeToken; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ + +public class Main { + + public static void main(String[] args) { + + //Instantiate ModelMapper + + ModelMapper mapper = new ModelMapper(); + mapper.addMappings(new UserPropertyMap()); + + // Mapping lists using TypeToken generic class + + List integers = new ArrayList(); + + integers.add(1); + integers.add(2); + integers.add(3); + + List characters = mapper.map(integers, new TypeToken>() {}.getType()); + + System.out.println("Character list: " + characters); + + // Mapping lists using generic type methods + + List users = new ArrayList(); + users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); + users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); + users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN")); + + List userDtoList = MapperUtil.mapList(users, UserDTO.class); + userDtoList.stream().map(userDto -> userDto.getEmail()).forEachOrdered(System.out::println); + + // Mapping lists using PropertyMap and Converter + + UserList userList = new UserList(); + userList.setUsers(users); + UserDTO dto = new UserDTO(); + + mapper.map(userList, dto); + dto.getUsernames().forEach(System.out::println); + + } +} diff --git a/model-mapper/src/com/baeldung/model/User.java b/model-mapper/src/com/baeldung/model/User.java new file mode 100644 index 0000000000..73e4baafb5 --- /dev/null +++ b/model-mapper/src/com/baeldung/model/User.java @@ -0,0 +1,70 @@ +package com.baeldung.model; + +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ +public class User { + + private String userId; + private String userName; + private String email; + private String contactNumber; + private String userType; + + // Standard constructors, getters and setters + + public User(){} + + public User(String userId, String userName, String email, String contactNumber, String userType) { + this.userId = userId; + this.userName = userName; + this.email = email; + this.contactNumber = contactNumber; + this.userType = userType; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getContactNumber() { + return contactNumber; + } + + public void setContactNumber(String contactNumber) { + this.contactNumber = contactNumber; + } + + public String getUserType() { + return userType; + } + + public void setUserType(String userType) { + this.userType = userType; + } + + +} diff --git a/model-mapper/src/com/baeldung/model/UserDTO.java b/model-mapper/src/com/baeldung/model/UserDTO.java new file mode 100644 index 0000000000..ed056ace8c --- /dev/null +++ b/model-mapper/src/com/baeldung/model/UserDTO.java @@ -0,0 +1,49 @@ +package com.baeldung.model; + +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ +public class UserDTO { + + private String userId; + private String userName; + private String email; + private List usernames; + + // getters and setters + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public List getUsernames() { + return usernames; + } + + public void setUsernames(List usernames) { + this.usernames = usernames; + } +} diff --git a/model-mapper/src/com/baeldung/model/UserList.java b/model-mapper/src/com/baeldung/model/UserList.java new file mode 100644 index 0000000000..b30d5507d5 --- /dev/null +++ b/model-mapper/src/com/baeldung/model/UserList.java @@ -0,0 +1,21 @@ +package com.baeldung.model; + +import java.util.Collection; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ + +public class UserList { + + private Collection users; + + public Collection getUsers() { + return users; + } + + public void setUsers(Collection users) { + this.users = users; + } +} diff --git a/model-mapper/src/com/baeldung/util/MapperUtil.java b/model-mapper/src/com/baeldung/util/MapperUtil.java new file mode 100644 index 0000000000..fe10b7777d --- /dev/null +++ b/model-mapper/src/com/baeldung/util/MapperUtil.java @@ -0,0 +1,47 @@ +package com.baeldung.util; + +import org.modelmapper.ModelMapper; +import org.modelmapper.convention.MatchingStrategies; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ +public class MapperUtil { + + + private static ModelMapper modelMapper = new ModelMapper(); + + + static { + + modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); + + } + + private MapperUtil() { + + + } + + public static T mapTo(final S source, final Class target) { + + return modelMapper.map(source, target); + } + + public static List mapList(final List sourceList, final Class target) { + + List targetList = new ArrayList(); + + for (S source : sourceList) { + + targetList.add(modelMapper.map(source, target)); + } + + return targetList; + } + +} diff --git a/model-mapper/src/com/baeldung/util/UserPropertyMap.java b/model-mapper/src/com/baeldung/util/UserPropertyMap.java new file mode 100644 index 0000000000..4346174440 --- /dev/null +++ b/model-mapper/src/com/baeldung/util/UserPropertyMap.java @@ -0,0 +1,36 @@ +package com.baeldung.util; + +import com.baeldung.model.User; +import com.baeldung.model.UserDTO; +import com.baeldung.model.UserList; +import org.modelmapper.AbstractConverter; +import org.modelmapper.Converter; +import org.modelmapper.PropertyMap; +import java.util.ArrayList; +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ + +public class UserPropertyMap extends PropertyMap { + + + Converter, List> converter = new AbstractConverter, List>() { + + List usernames = new ArrayList<>(); + + protected List convert(List users) { + + users.forEach(user -> usernames.add(user.getUserName())); + return usernames; + } + + }; + + @Override + protected void configure() { + using(converter).map(source.getUsers(), destination.getUsernames()); + } +} From 43852c4303b90e0ee4e11f15c7a52a1de1e7f8d8 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Tue, 21 Apr 2020 18:46:45 +0200 Subject: [PATCH 05/18] Moving com.baeldung.modelmmaper package to java-collections-conversations module --- java-collections-conversions-2/pom.xml | 17 ++++ .../com/baeldung/modelmapper}/MapperUtil.java | 9 +- .../java/com/baeldung/modelmapper}/User.java | 6 +- .../com/baeldung/modelmapper}/UserDTO.java | 13 +-- .../com/baeldung/modelmapper}/UserList.java | 5 +- .../com/baeldung/modelmapper/UserListDTO.java | 20 +++++ .../modelmapper}/UserPropertyMap.java | 12 ++- .../baeldung/modelmapper/UserMappingTest.java | 85 +++++++++++++++++++ model-mapper/src/Main.java | 58 ------------- parent-java/pom.xml | 3 + 10 files changed, 140 insertions(+), 88 deletions(-) rename {model-mapper/src/com/baeldung/util => java-collections-conversions-2/src/main/java/com/baeldung/modelmapper}/MapperUtil.java (76%) rename {model-mapper/src/com/baeldung/model => java-collections-conversions-2/src/main/java/com/baeldung/modelmapper}/User.java (94%) rename {model-mapper/src/com/baeldung/model => java-collections-conversions-2/src/main/java/com/baeldung/modelmapper}/UserDTO.java (68%) rename {model-mapper/src/com/baeldung/model => java-collections-conversions-2/src/main/java/com/baeldung/modelmapper}/UserList.java (74%) create mode 100644 java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java rename {model-mapper/src/com/baeldung/util => java-collections-conversions-2/src/main/java/com/baeldung/modelmapper}/UserPropertyMap.java (68%) create mode 100644 java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java delete mode 100644 model-mapper/src/Main.java diff --git a/java-collections-conversions-2/pom.xml b/java-collections-conversions-2/pom.xml index b04d764719..ca4ed882dd 100644 --- a/java-collections-conversions-2/pom.xml +++ b/java-collections-conversions-2/pom.xml @@ -20,6 +20,23 @@ commons-lang3 ${commons-lang3.version} + + org.modelmapper + modelmapper + ${modelmapper.version} + + + junit + junit + ${junit.version} + test + + + org.hamcrest + hamcrest-all + ${hamcrest.version} + test + diff --git a/model-mapper/src/com/baeldung/util/MapperUtil.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java similarity index 76% rename from model-mapper/src/com/baeldung/util/MapperUtil.java rename to java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java index fe10b7777d..c02f012126 100644 --- a/model-mapper/src/com/baeldung/util/MapperUtil.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java @@ -1,4 +1,4 @@ -package com.baeldung.util; +package com.baeldung.modelmapper; import org.modelmapper.ModelMapper; import org.modelmapper.convention.MatchingStrategies; @@ -8,7 +8,9 @@ import java.util.List; /** * @author sasam0320 - * @date 4/18/2020 + * @description + * This is a helper class that contains methods for generic mapping of the users list. + * Initially, an instance of ModelMapper was created. In the static block we set the matching configuration to STRICT. */ public class MapperUtil { @@ -28,16 +30,13 @@ public class MapperUtil { } public static T mapTo(final S source, final Class target) { - return modelMapper.map(source, target); } public static List mapList(final List sourceList, final Class target) { - List targetList = new ArrayList(); for (S source : sourceList) { - targetList.add(modelMapper.map(source, target)); } diff --git a/model-mapper/src/com/baeldung/model/User.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java similarity index 94% rename from model-mapper/src/com/baeldung/model/User.java rename to java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java index 73e4baafb5..8f13b44894 100644 --- a/model-mapper/src/com/baeldung/model/User.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java @@ -1,10 +1,8 @@ -package com.baeldung.model; - -import java.util.List; +package com.baeldung.modelmapper; /** * @author sasam0320 - * @date 4/18/2020 + * @description User model entity class */ public class User { diff --git a/model-mapper/src/com/baeldung/model/UserDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java similarity index 68% rename from model-mapper/src/com/baeldung/model/UserDTO.java rename to java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java index ed056ace8c..ce75eecefc 100644 --- a/model-mapper/src/com/baeldung/model/UserDTO.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java @@ -1,17 +1,14 @@ -package com.baeldung.model; - -import java.util.List; +package com.baeldung.modelmapper; /** * @author sasam0320 - * @date 4/18/2020 + * @description UserDTO model class */ public class UserDTO { private String userId; private String userName; private String email; - private List usernames; // getters and setters @@ -39,11 +36,5 @@ public class UserDTO { this.email = email; } - public List getUsernames() { - return usernames; - } - public void setUsernames(List usernames) { - this.usernames = usernames; - } } diff --git a/model-mapper/src/com/baeldung/model/UserList.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java similarity index 74% rename from model-mapper/src/com/baeldung/model/UserList.java rename to java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java index b30d5507d5..02361da469 100644 --- a/model-mapper/src/com/baeldung/model/UserList.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java @@ -1,12 +1,11 @@ -package com.baeldung.model; +package com.baeldung.modelmapper; import java.util.Collection; /** * @author sasam0320 - * @date 4/18/2020 + * @description UserList class that contain collection of users */ - public class UserList { private Collection users; diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java new file mode 100644 index 0000000000..fba311c1a3 --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java @@ -0,0 +1,20 @@ +package com.baeldung.modelmapper; + +import java.util.List; + +/** + * @author sasam0320 + * @description UserListDTO class that contain list of username properties + */ +public class UserListDTO { + + private List usernames; + + public List getUsernames() { + return usernames; + } + + public void setUsernames(List usernames) { + this.usernames = usernames; + } +} diff --git a/model-mapper/src/com/baeldung/util/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java similarity index 68% rename from model-mapper/src/com/baeldung/util/UserPropertyMap.java rename to java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 4346174440..9fa945ce32 100644 --- a/model-mapper/src/com/baeldung/util/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -1,8 +1,5 @@ -package com.baeldung.util; +package com.baeldung.modelmapper; -import com.baeldung.model.User; -import com.baeldung.model.UserDTO; -import com.baeldung.model.UserList; import org.modelmapper.AbstractConverter; import org.modelmapper.Converter; import org.modelmapper.PropertyMap; @@ -11,10 +8,11 @@ import java.util.List; /** * @author sasam0320 - * @date 4/18/2020 + * @description + * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. + * In the configuration method, we call a converter to do the mapping. */ - -public class UserPropertyMap extends PropertyMap { +public class UserPropertyMap extends PropertyMap { Converter, List> converter = new AbstractConverter, List>() { diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java new file mode 100644 index 0000000000..44a929621b --- /dev/null +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java @@ -0,0 +1,85 @@ +package com.baeldung.modelmapper; + +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.modelmapper.ModelMapper; +import org.modelmapper.TypeToken; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + + +/** + * @sasam0320 + * @description + * This class has test methods of mapping Integer to Character list, + * mapping user list to DTO list using MapperUtil generic methods and Converter + */ +public class UserMappingTest { + + private ModelMapper mapper; + private List users; + + @Before + public void init() { + + mapper = new ModelMapper(); + mapper.addMappings(new UserPropertyMap()); + users = new ArrayList(); + users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); + users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); + users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN")); + + } + + @Test + public void testMapIntegerList() { + + List integers = new ArrayList(); + + integers.add(1); + integers.add(2); + integers.add(3); + + List characters = mapper.map(integers, new TypeToken>() { + }.getType()); + + assertThat(characters, hasItems('1','2','3')); + + } + + @Test + public void testMapGenericTypeLists() { + + // Mapping lists using generic type methods + + List userDtoList = MapperUtil.mapList(users, UserDTO.class); + + assertThat(userDtoList, Matchers.hasItem( + Matchers.both(hasProperty("userId", equalTo("b100"))) + .and(hasProperty("email", equalTo("user1@baeldung.com"))) + .and(hasProperty("userName", equalTo("user1"))))); + + // Mapping lists using PropertyMap and Converter + + UserList userList = new UserList(); + userList.setUsers(users); + UserListDTO dto = new UserListDTO(); + mapper.map(userList, dto); + + assertNotNull(dto); + assertThat(dto, Matchers.hasProperty("usernames")); + assertThat(dto.getUsernames(), hasSize(3)); + + } + +} \ No newline at end of file diff --git a/model-mapper/src/Main.java b/model-mapper/src/Main.java deleted file mode 100644 index a6deb49168..0000000000 --- a/model-mapper/src/Main.java +++ /dev/null @@ -1,58 +0,0 @@ -import com.baeldung.model.User; -import com.baeldung.model.UserDTO; -import com.baeldung.model.UserList; -import com.baeldung.util.MapperUtil; -import com.baeldung.util.UserPropertyMap; -import org.modelmapper.ModelMapper; -import org.modelmapper.TypeToken; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author sasam0320 - * @date 4/18/2020 - */ - -public class Main { - - public static void main(String[] args) { - - //Instantiate ModelMapper - - ModelMapper mapper = new ModelMapper(); - mapper.addMappings(new UserPropertyMap()); - - // Mapping lists using TypeToken generic class - - List integers = new ArrayList(); - - integers.add(1); - integers.add(2); - integers.add(3); - - List characters = mapper.map(integers, new TypeToken>() {}.getType()); - - System.out.println("Character list: " + characters); - - // Mapping lists using generic type methods - - List users = new ArrayList(); - users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); - users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); - users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN")); - - List userDtoList = MapperUtil.mapList(users, UserDTO.class); - userDtoList.stream().map(userDto -> userDto.getEmail()).forEachOrdered(System.out::println); - - // Mapping lists using PropertyMap and Converter - - UserList userList = new UserList(); - userList.setUsers(users); - UserDTO dto = new UserDTO(); - - mapper.map(userList, dto); - dto.getUsernames().forEach(System.out::println); - - } -} diff --git a/parent-java/pom.xml b/parent-java/pom.xml index 4828bc2abb..ba786d912c 100644 --- a/parent-java/pom.xml +++ b/parent-java/pom.xml @@ -45,6 +45,9 @@ 23.0 2.6 1.19 + 2.3.6 + 4.12 + 1.3 From d1b282220d4c4671343424b5737312a25dd2e2b1 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Tue, 21 Apr 2020 21:29:38 +0200 Subject: [PATCH 06/18] Renaming test methods and formatting --- java-collections-conversions-2/pom.xml | 14 +++++++------- .../java/com/baeldung/modelmapper/MapperUtil.java | 4 ++-- .../main/java/com/baeldung/modelmapper/User.java | 2 +- .../java/com/baeldung/modelmapper/UserDTO.java | 2 +- .../java/com/baeldung/modelmapper/UserList.java | 2 +- .../java/com/baeldung/modelmapper/UserListDTO.java | 2 +- .../com/baeldung/modelmapper/UserPropertyMap.java | 3 +-- ...pingTest.java => UsersListMappingUnitTest.java} | 13 ++++++------- parent-java/pom.xml | 12 ++++++------ 9 files changed, 26 insertions(+), 28 deletions(-) rename java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/{UserMappingTest.java => UsersListMappingUnitTest.java} (90%) diff --git a/java-collections-conversions-2/pom.xml b/java-collections-conversions-2/pom.xml index ca4ed882dd..892d73cb2b 100644 --- a/java-collections-conversions-2/pom.xml +++ b/java-collections-conversions-2/pom.xml @@ -20,23 +20,23 @@ commons-lang3 ${commons-lang3.version} - + org.modelmapper modelmapper ${modelmapper.version} - - + + junit junit ${junit.version} - test - - + test + + org.hamcrest hamcrest-all ${hamcrest.version} test - + diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java index c02f012126..bbc2153c63 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java @@ -7,10 +7,10 @@ import java.util.ArrayList; import java.util.List; /** - * @author sasam0320 - * @description * This is a helper class that contains methods for generic mapping of the users list. * Initially, an instance of ModelMapper was created. In the static block we set the matching configuration to STRICT. + * + * @author sasam0320 */ public class MapperUtil { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java index 8f13b44894..7fe22fb262 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java @@ -1,8 +1,8 @@ package com.baeldung.modelmapper; /** + * User model entity class * @author sasam0320 - * @description User model entity class */ public class User { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java index ce75eecefc..d36b829232 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java @@ -1,8 +1,8 @@ package com.baeldung.modelmapper; /** + * UserDTO model class * @author sasam0320 - * @description UserDTO model class */ public class UserDTO { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java index 02361da469..7c410727fd 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java @@ -3,8 +3,8 @@ package com.baeldung.modelmapper; import java.util.Collection; /** + * UserList class that contain collection of users * @author sasam0320 - * @description UserList class that contain collection of users */ public class UserList { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java index fba311c1a3..ac25185635 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java @@ -3,8 +3,8 @@ package com.baeldung.modelmapper; import java.util.List; /** + * UserListDTO class that contain list of username properties * @author sasam0320 - * @description UserListDTO class that contain list of username properties */ public class UserListDTO { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 9fa945ce32..3d9059c520 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -7,10 +7,9 @@ import java.util.ArrayList; import java.util.List; /** - * @author sasam0320 - * @description * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. * In the configuration method, we call a converter to do the mapping. + * @author sasam0320 */ public class UserPropertyMap extends PropertyMap { diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java similarity index 90% rename from java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java rename to java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java index 44a929621b..e03ddf81cf 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java @@ -13,18 +13,17 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasProperty; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; - import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; /** - * @sasam0320 - * @description * This class has test methods of mapping Integer to Character list, * mapping user list to DTO list using MapperUtil generic methods and Converter + * + * @author sasam0320 */ -public class UserMappingTest { +public class UsersListMappingUnitTest { private ModelMapper mapper; private List users; @@ -42,7 +41,7 @@ public class UserMappingTest { } @Test - public void testMapIntegerList() { + public void whenMapIntegerToCharList() { List integers = new ArrayList(); @@ -53,12 +52,12 @@ public class UserMappingTest { List characters = mapper.map(integers, new TypeToken>() { }.getType()); - assertThat(characters, hasItems('1','2','3')); + assertThat(characters, hasItems('1', '2', '3')); } @Test - public void testMapGenericTypeLists() { + public void givenUsersList_whenUseGenericType_thenMapToDto() { // Mapping lists using generic type methods diff --git a/parent-java/pom.xml b/parent-java/pom.xml index ba786d912c..5c347ef176 100644 --- a/parent-java/pom.xml +++ b/parent-java/pom.xml @@ -1,8 +1,8 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 parent-java 0.0.1-SNAPSHOT @@ -45,9 +45,9 @@ 23.0 2.6 1.19 - 2.3.6 - 4.12 - 1.3 + 2.3.6 + 4.12 + 1.3 From 32d1cc8a13f87217f906dabcc68afbf47fe466fe Mon Sep 17 00:00:00 2001 From: Sasa M Date: Tue, 21 Apr 2020 22:26:22 +0200 Subject: [PATCH 07/18] changes in build pom.xml --- .../src/main/java/com/baeldung/modelmapper/MapperUtil.java | 2 +- .../src/main/java/com/baeldung/modelmapper/User.java | 6 ++++-- .../src/main/java/com/baeldung/modelmapper/UserDTO.java | 3 ++- .../src/main/java/com/baeldung/modelmapper/UserList.java | 3 ++- .../src/main/java/com/baeldung/modelmapper/UserListDTO.java | 3 ++- .../main/java/com/baeldung/modelmapper/UserPropertyMap.java | 4 +++- .../com/baeldung/modelmapper/UsersListMappingUnitTest.java | 2 +- parent-java/pom.xml | 2 +- 8 files changed, 16 insertions(+), 9 deletions(-) diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java index bbc2153c63..546e415755 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java @@ -10,7 +10,7 @@ import java.util.List; * This is a helper class that contains methods for generic mapping of the users list. * Initially, an instance of ModelMapper was created. In the static block we set the matching configuration to STRICT. * - * @author sasam0320 + * @author Sasa Milenkovic */ public class MapperUtil { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java index 7fe22fb262..23a424dedd 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java @@ -2,7 +2,8 @@ package com.baeldung.modelmapper; /** * User model entity class - * @author sasam0320 + * + * @author Sasa Milenkovic */ public class User { @@ -14,7 +15,8 @@ public class User { // Standard constructors, getters and setters - public User(){} + public User() { + } public User(String userId, String userName, String email, String contactNumber, String userType) { this.userId = userId; diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java index d36b829232..e31414b629 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java @@ -2,7 +2,8 @@ package com.baeldung.modelmapper; /** * UserDTO model class - * @author sasam0320 + * + * @author Sasa Milenkovic */ public class UserDTO { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java index 7c410727fd..7b6bed807b 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java @@ -4,7 +4,8 @@ import java.util.Collection; /** * UserList class that contain collection of users - * @author sasam0320 + * + * @author Sasa Milenkovic */ public class UserList { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java index ac25185635..c001cbbc3c 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java @@ -4,7 +4,8 @@ import java.util.List; /** * UserListDTO class that contain list of username properties - * @author sasam0320 + * + * @author Sasa Milenkovic */ public class UserListDTO { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 3d9059c520..d2c32a307d 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -3,13 +3,15 @@ package com.baeldung.modelmapper; import org.modelmapper.AbstractConverter; import org.modelmapper.Converter; import org.modelmapper.PropertyMap; + import java.util.ArrayList; import java.util.List; /** * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. * In the configuration method, we call a converter to do the mapping. - * @author sasam0320 + * + * @author Sasa Milenkovic */ public class UserPropertyMap extends PropertyMap { diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java index e03ddf81cf..05792fda38 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java @@ -21,7 +21,7 @@ import static org.junit.Assert.assertThat; * This class has test methods of mapping Integer to Character list, * mapping user list to DTO list using MapperUtil generic methods and Converter * - * @author sasam0320 + * @author Sasa Milenkovic */ public class UsersListMappingUnitTest { diff --git a/parent-java/pom.xml b/parent-java/pom.xml index 5c347ef176..3b2b5f59de 100644 --- a/parent-java/pom.xml +++ b/parent-java/pom.xml @@ -45,7 +45,7 @@ 23.0 2.6 1.19 - 2.3.6 + 2.3.7 4.12 1.3 From a5264182cd0faed8a9cae5368ea84d1372ad3be2 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sat, 25 Apr 2020 21:57:43 +0200 Subject: [PATCH 08/18] Refactoring code in com.baeldung.modelmapper package --- .../com/baeldung/modelmapper/MapperUtil.java | 16 ++-------------- .../main/java/com/baeldung/modelmapper/User.java | 14 +++++++------- .../java/com/baeldung/modelmapper/UserDTO.java | 10 +++++----- .../baeldung/modelmapper/UserPropertyMap.java | 7 ++++--- .../modelmapper/UsersListMappingUnitTest.java | 12 ++++++------ 5 files changed, 24 insertions(+), 35 deletions(-) diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java index 546e415755..60af44cc76 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java @@ -1,39 +1,27 @@ package com.baeldung.modelmapper; import org.modelmapper.ModelMapper; -import org.modelmapper.convention.MatchingStrategies; import java.util.ArrayList; import java.util.List; /** * This is a helper class that contains methods for generic mapping of the users list. - * Initially, an instance of ModelMapper was created. In the static block we set the matching configuration to STRICT. + * Initially, an instance of ModelMapper was created. * * @author Sasa Milenkovic */ public class MapperUtil { - private static ModelMapper modelMapper = new ModelMapper(); - static { - - modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); - - } - private MapperUtil() { } - public static T mapTo(final S source, final Class target) { - return modelMapper.map(source, target); - } - - public static List mapList(final List sourceList, final Class target) { + public static List mapList(List sourceList, Class target) { List targetList = new ArrayList(); for (S source : sourceList) { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java index 23a424dedd..8ed674d86a 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java @@ -8,7 +8,7 @@ package com.baeldung.modelmapper; public class User { private String userId; - private String userName; + private String username; private String email; private String contactNumber; private String userType; @@ -18,9 +18,9 @@ public class User { public User() { } - public User(String userId, String userName, String email, String contactNumber, String userType) { + public User(String userId, String username, String email, String contactNumber, String userType) { this.userId = userId; - this.userName = userName; + this.username = username; this.email = email; this.contactNumber = contactNumber; this.userType = userType; @@ -34,12 +34,12 @@ public class User { this.userId = userId; } - public String getUserName() { - return userName; + public String getUsername() { + return username; } - public void setUserName(String userName) { - this.userName = userName; + public void setUsername(String userName) { + this.username = userName; } public String getEmail() { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java index e31414b629..b67bb58ef4 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java @@ -8,7 +8,7 @@ package com.baeldung.modelmapper; public class UserDTO { private String userId; - private String userName; + private String username; private String email; // getters and setters @@ -21,12 +21,12 @@ public class UserDTO { this.userId = userId; } - public String getUserName() { - return userName; + public String getUsername() { + return username; } - public void setUserName(String userName) { - this.userName = userName; + public void setUsername(String username) { + this.username = username; } public String getEmail() { diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index d2c32a307d..0d2ebf7b4c 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -18,14 +18,15 @@ public class UserPropertyMap extends PropertyMap { Converter, List> converter = new AbstractConverter, List>() { - List usernames = new ArrayList<>(); + protected List usernames; + @Override protected List convert(List users) { - users.forEach(user -> usernames.add(user.getUserName())); + usernames = new ArrayList(); + users.forEach(user -> usernames.add(user.getUsername())); return usernames; } - }; @Override diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java index 05792fda38..aaeb8711a8 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java @@ -25,14 +25,14 @@ import static org.junit.Assert.assertThat; */ public class UsersListMappingUnitTest { - private ModelMapper mapper; + private ModelMapper modelMapper; private List users; @Before public void init() { - mapper = new ModelMapper(); - mapper.addMappings(new UserPropertyMap()); + modelMapper = new ModelMapper(); + modelMapper.addMappings(new UserPropertyMap()); users = new ArrayList(); users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); @@ -49,7 +49,7 @@ public class UsersListMappingUnitTest { integers.add(2); integers.add(3); - List characters = mapper.map(integers, new TypeToken>() { + List characters = modelMapper.map(integers, new TypeToken>() { }.getType()); assertThat(characters, hasItems('1', '2', '3')); @@ -66,14 +66,14 @@ public class UsersListMappingUnitTest { assertThat(userDtoList, Matchers.hasItem( Matchers.both(hasProperty("userId", equalTo("b100"))) .and(hasProperty("email", equalTo("user1@baeldung.com"))) - .and(hasProperty("userName", equalTo("user1"))))); + .and(hasProperty("username", equalTo("user1"))))); // Mapping lists using PropertyMap and Converter UserList userList = new UserList(); userList.setUsers(users); UserListDTO dto = new UserListDTO(); - mapper.map(userList, dto); + modelMapper.map(userList, dto); assertNotNull(dto); assertThat(dto, Matchers.hasProperty("usernames")); From 8d7d98a144b2af7038efa1a266a284df92b9b7d9 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sun, 26 Apr 2020 18:37:31 +0200 Subject: [PATCH 09/18] Customizing Java 8 code in modemmapper package --- .../com/baeldung/modelmapper/MapperUtil.java | 16 +++++++--------- .../baeldung/modelmapper/UserPropertyMap.java | 13 +++++++------ .../modelmapper/UsersListMappingUnitTest.java | 12 ++++++------ 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java index 60af44cc76..9c6a5a0320 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java @@ -2,11 +2,11 @@ package com.baeldung.modelmapper; import org.modelmapper.ModelMapper; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** - * This is a helper class that contains methods for generic mapping of the users list. + * This is a helper class that contains method for generic mapping of the users list. * Initially, an instance of ModelMapper was created. * * @author Sasa Milenkovic @@ -21,14 +21,12 @@ public class MapperUtil { } - public static List mapList(List sourceList, Class target) { - List targetList = new ArrayList(); + public static List mapList(List source, Class targetClass) { - for (S source : sourceList) { - targetList.add(modelMapper.map(source, target)); - } - - return targetList; + return source + .stream() + .map(element -> modelMapper.map(element, targetClass)) + .collect(Collectors.toList()); } } diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 0d2ebf7b4c..5b2942b158 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -4,11 +4,11 @@ import org.modelmapper.AbstractConverter; import org.modelmapper.Converter; import org.modelmapper.PropertyMap; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** - * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. + * UserPropertyMap class instantiates the converter to map the data from the UserList to the UsersLisDTO. * In the configuration method, we call a converter to do the mapping. * * @author Sasa Milenkovic @@ -18,19 +18,20 @@ public class UserPropertyMap extends PropertyMap { Converter, List> converter = new AbstractConverter, List>() { - protected List usernames; @Override protected List convert(List users) { - usernames = new ArrayList(); - users.forEach(user -> usernames.add(user.getUsername())); - return usernames; + return users + .stream() + .map(User::getUsername) + .collect(Collectors.toList()); } }; @Override protected void configure() { + using(converter).map(source.getUsers(), destination.getUsernames()); } } diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java index aaeb8711a8..982622e1f5 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java @@ -59,7 +59,7 @@ public class UsersListMappingUnitTest { @Test public void givenUsersList_whenUseGenericType_thenMapToDto() { - // Mapping lists using generic type methods + // Mapping lists using custom type methods List userDtoList = MapperUtil.mapList(users, UserDTO.class); @@ -72,12 +72,12 @@ public class UsersListMappingUnitTest { UserList userList = new UserList(); userList.setUsers(users); - UserListDTO dto = new UserListDTO(); - modelMapper.map(userList, dto); + UserListDTO dtos = new UserListDTO(); + modelMapper.map(userList, dtos); - assertNotNull(dto); - assertThat(dto, Matchers.hasProperty("usernames")); - assertThat(dto.getUsernames(), hasSize(3)); + assertNotNull(dtos); + assertThat(dtos, Matchers.hasProperty("usernames")); + assertThat(dtos.getUsernames(), hasSize(3)); } From 9bf7546d42eae5dd8d191c29ee7bb56c242120bf Mon Sep 17 00:00:00 2001 From: Sasa M Date: Wed, 29 Apr 2020 21:55:30 +0200 Subject: [PATCH 10/18] Creating TypeMap to use property mapping and converter class --- .../com/baeldung/modelmapper/MapperUtil.java | 2 +- .../baeldung/modelmapper/UserPropertyMap.java | 37 ------------------- .../modelmapper/UsersListConverter.java | 23 ++++++++++++ .../modelmapper/UsersListMappingUnitTest.java | 30 +++++++++------ 4 files changed, 43 insertions(+), 49 deletions(-) delete mode 100644 java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java create mode 100644 java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UsersListConverter.java diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java index 9c6a5a0320..23a549e652 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java @@ -6,7 +6,7 @@ import java.util.List; import java.util.stream.Collectors; /** - * This is a helper class that contains method for generic mapping of the users list. + * This is a helper class that contains method for custom mapping of the users list. * Initially, an instance of ModelMapper was created. * * @author Sasa Milenkovic diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java deleted file mode 100644 index 5b2942b158..0000000000 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.modelmapper; - -import org.modelmapper.AbstractConverter; -import org.modelmapper.Converter; -import org.modelmapper.PropertyMap; - -import java.util.List; -import java.util.stream.Collectors; - -/** - * UserPropertyMap class instantiates the converter to map the data from the UserList to the UsersLisDTO. - * In the configuration method, we call a converter to do the mapping. - * - * @author Sasa Milenkovic - */ -public class UserPropertyMap extends PropertyMap { - - - Converter, List> converter = new AbstractConverter, List>() { - - - @Override - protected List convert(List users) { - - return users - .stream() - .map(User::getUsername) - .collect(Collectors.toList()); - } - }; - - @Override - protected void configure() { - - using(converter).map(source.getUsers(), destination.getUsernames()); - } -} diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UsersListConverter.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UsersListConverter.java new file mode 100644 index 0000000000..19423713e2 --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UsersListConverter.java @@ -0,0 +1,23 @@ +package com.baeldung.modelmapper; + +import org.modelmapper.AbstractConverter; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * UsersListConverter class map the property data from the list of users into the list of user names. + * + * @author Sasa Milenkovic + */ +public class UsersListConverter extends AbstractConverter, List> { + + @Override + protected List convert(List users) { + + return users + .stream() + .map(User::getUsername) + .collect(Collectors.toList()); + } +} diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java index 982622e1f5..a8a72b12f7 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java @@ -4,6 +4,7 @@ import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import org.modelmapper.ModelMapper; +import org.modelmapper.TypeMap; import org.modelmapper.TypeToken; import java.util.ArrayList; @@ -12,14 +13,12 @@ import java.util.List; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; /** * This class has test methods of mapping Integer to Character list, - * mapping user list to DTO list using MapperUtil generic methods and Converter + * mapping users list to DTO list using MapperUtil custom type method and property mapping using converter class * * @author Sasa Milenkovic */ @@ -32,7 +31,12 @@ public class UsersListMappingUnitTest { public void init() { modelMapper = new ModelMapper(); - modelMapper.addMappings(new UserPropertyMap()); + + TypeMap typeMap = modelMapper.createTypeMap(UserList.class, UserListDTO.class); + + typeMap.addMappings(mapper -> mapper.using(new UsersListConverter()) + .map(UserList::getUsers, UserListDTO::setUsernames)); + users = new ArrayList(); users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); @@ -41,7 +45,7 @@ public class UsersListMappingUnitTest { } @Test - public void whenMapIntegerToCharList() { + public void whenInteger_thenMapToCharacter() { List integers = new ArrayList(); @@ -57,9 +61,9 @@ public class UsersListMappingUnitTest { } @Test - public void givenUsersList_whenUseGenericType_thenMapToDto() { + public void givenUsersList_whenUseGenericType_thenMapToUserDTO() { - // Mapping lists using custom type methods + // Mapping lists using custom (generic) type mapping List userDtoList = MapperUtil.mapList(users, UserDTO.class); @@ -68,16 +72,20 @@ public class UsersListMappingUnitTest { .and(hasProperty("email", equalTo("user1@baeldung.com"))) .and(hasProperty("username", equalTo("user1"))))); - // Mapping lists using PropertyMap and Converter + + } + + @Test + public void givenUsersList_whenUseConverter_thenMapToUsernames() { + + // Mapping lists using property mapping and converter UserList userList = new UserList(); userList.setUsers(users); UserListDTO dtos = new UserListDTO(); modelMapper.map(userList, dtos); - assertNotNull(dtos); - assertThat(dtos, Matchers.hasProperty("usernames")); - assertThat(dtos.getUsernames(), hasSize(3)); + assertThat(dtos.getUsernames(), hasItems("user1", "user2", "user3")); } From 4bb9f166b6df24f11549f9f2627aaf26fbb9e7b6 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sun, 19 Apr 2020 21:02:17 +0200 Subject: [PATCH 11/18] Source project for the draft article --- model-mapper/src/Main.java | 58 +++++++++++++++ model-mapper/src/com/baeldung/model/User.java | 70 +++++++++++++++++++ .../src/com/baeldung/model/UserDTO.java | 49 +++++++++++++ .../src/com/baeldung/model/UserList.java | 21 ++++++ .../src/com/baeldung/util/MapperUtil.java | 47 +++++++++++++ .../com/baeldung/util/UserPropertyMap.java | 36 ++++++++++ 6 files changed, 281 insertions(+) create mode 100644 model-mapper/src/Main.java create mode 100644 model-mapper/src/com/baeldung/model/User.java create mode 100644 model-mapper/src/com/baeldung/model/UserDTO.java create mode 100644 model-mapper/src/com/baeldung/model/UserList.java create mode 100644 model-mapper/src/com/baeldung/util/MapperUtil.java create mode 100644 model-mapper/src/com/baeldung/util/UserPropertyMap.java diff --git a/model-mapper/src/Main.java b/model-mapper/src/Main.java new file mode 100644 index 0000000000..a6deb49168 --- /dev/null +++ b/model-mapper/src/Main.java @@ -0,0 +1,58 @@ +import com.baeldung.model.User; +import com.baeldung.model.UserDTO; +import com.baeldung.model.UserList; +import com.baeldung.util.MapperUtil; +import com.baeldung.util.UserPropertyMap; +import org.modelmapper.ModelMapper; +import org.modelmapper.TypeToken; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ + +public class Main { + + public static void main(String[] args) { + + //Instantiate ModelMapper + + ModelMapper mapper = new ModelMapper(); + mapper.addMappings(new UserPropertyMap()); + + // Mapping lists using TypeToken generic class + + List integers = new ArrayList(); + + integers.add(1); + integers.add(2); + integers.add(3); + + List characters = mapper.map(integers, new TypeToken>() {}.getType()); + + System.out.println("Character list: " + characters); + + // Mapping lists using generic type methods + + List users = new ArrayList(); + users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); + users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); + users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN")); + + List userDtoList = MapperUtil.mapList(users, UserDTO.class); + userDtoList.stream().map(userDto -> userDto.getEmail()).forEachOrdered(System.out::println); + + // Mapping lists using PropertyMap and Converter + + UserList userList = new UserList(); + userList.setUsers(users); + UserDTO dto = new UserDTO(); + + mapper.map(userList, dto); + dto.getUsernames().forEach(System.out::println); + + } +} diff --git a/model-mapper/src/com/baeldung/model/User.java b/model-mapper/src/com/baeldung/model/User.java new file mode 100644 index 0000000000..73e4baafb5 --- /dev/null +++ b/model-mapper/src/com/baeldung/model/User.java @@ -0,0 +1,70 @@ +package com.baeldung.model; + +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ +public class User { + + private String userId; + private String userName; + private String email; + private String contactNumber; + private String userType; + + // Standard constructors, getters and setters + + public User(){} + + public User(String userId, String userName, String email, String contactNumber, String userType) { + this.userId = userId; + this.userName = userName; + this.email = email; + this.contactNumber = contactNumber; + this.userType = userType; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getContactNumber() { + return contactNumber; + } + + public void setContactNumber(String contactNumber) { + this.contactNumber = contactNumber; + } + + public String getUserType() { + return userType; + } + + public void setUserType(String userType) { + this.userType = userType; + } + + +} diff --git a/model-mapper/src/com/baeldung/model/UserDTO.java b/model-mapper/src/com/baeldung/model/UserDTO.java new file mode 100644 index 0000000000..ed056ace8c --- /dev/null +++ b/model-mapper/src/com/baeldung/model/UserDTO.java @@ -0,0 +1,49 @@ +package com.baeldung.model; + +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ +public class UserDTO { + + private String userId; + private String userName; + private String email; + private List usernames; + + // getters and setters + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public List getUsernames() { + return usernames; + } + + public void setUsernames(List usernames) { + this.usernames = usernames; + } +} diff --git a/model-mapper/src/com/baeldung/model/UserList.java b/model-mapper/src/com/baeldung/model/UserList.java new file mode 100644 index 0000000000..b30d5507d5 --- /dev/null +++ b/model-mapper/src/com/baeldung/model/UserList.java @@ -0,0 +1,21 @@ +package com.baeldung.model; + +import java.util.Collection; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ + +public class UserList { + + private Collection users; + + public Collection getUsers() { + return users; + } + + public void setUsers(Collection users) { + this.users = users; + } +} diff --git a/model-mapper/src/com/baeldung/util/MapperUtil.java b/model-mapper/src/com/baeldung/util/MapperUtil.java new file mode 100644 index 0000000000..fe10b7777d --- /dev/null +++ b/model-mapper/src/com/baeldung/util/MapperUtil.java @@ -0,0 +1,47 @@ +package com.baeldung.util; + +import org.modelmapper.ModelMapper; +import org.modelmapper.convention.MatchingStrategies; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ +public class MapperUtil { + + + private static ModelMapper modelMapper = new ModelMapper(); + + + static { + + modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); + + } + + private MapperUtil() { + + + } + + public static T mapTo(final S source, final Class target) { + + return modelMapper.map(source, target); + } + + public static List mapList(final List sourceList, final Class target) { + + List targetList = new ArrayList(); + + for (S source : sourceList) { + + targetList.add(modelMapper.map(source, target)); + } + + return targetList; + } + +} diff --git a/model-mapper/src/com/baeldung/util/UserPropertyMap.java b/model-mapper/src/com/baeldung/util/UserPropertyMap.java new file mode 100644 index 0000000000..4346174440 --- /dev/null +++ b/model-mapper/src/com/baeldung/util/UserPropertyMap.java @@ -0,0 +1,36 @@ +package com.baeldung.util; + +import com.baeldung.model.User; +import com.baeldung.model.UserDTO; +import com.baeldung.model.UserList; +import org.modelmapper.AbstractConverter; +import org.modelmapper.Converter; +import org.modelmapper.PropertyMap; +import java.util.ArrayList; +import java.util.List; + +/** + * @author sasam0320 + * @date 4/18/2020 + */ + +public class UserPropertyMap extends PropertyMap { + + + Converter, List> converter = new AbstractConverter, List>() { + + List usernames = new ArrayList<>(); + + protected List convert(List users) { + + users.forEach(user -> usernames.add(user.getUserName())); + return usernames; + } + + }; + + @Override + protected void configure() { + using(converter).map(source.getUsers(), destination.getUsernames()); + } +} From b9cbfa49c4aa8c4ff114f634b82a3514f0785d84 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Tue, 21 Apr 2020 18:46:45 +0200 Subject: [PATCH 12/18] Moving com.baeldung.modelmmaper package to java-collections-conversations module --- .../modelmapper}/UserPropertyMap.java | 12 ++- .../baeldung/modelmapper/UserMappingTest.java | 85 +++++++++++++++++++ model-mapper/src/Main.java | 58 ------------- model-mapper/src/com/baeldung/model/User.java | 70 --------------- .../src/com/baeldung/model/UserDTO.java | 49 ----------- .../src/com/baeldung/model/UserList.java | 21 ----- .../src/com/baeldung/util/MapperUtil.java | 47 ---------- 7 files changed, 90 insertions(+), 252 deletions(-) rename {model-mapper/src/com/baeldung/util => java-collections-conversions-2/src/main/java/com/baeldung/modelmapper}/UserPropertyMap.java (68%) create mode 100644 java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java delete mode 100644 model-mapper/src/Main.java delete mode 100644 model-mapper/src/com/baeldung/model/User.java delete mode 100644 model-mapper/src/com/baeldung/model/UserDTO.java delete mode 100644 model-mapper/src/com/baeldung/model/UserList.java delete mode 100644 model-mapper/src/com/baeldung/util/MapperUtil.java diff --git a/model-mapper/src/com/baeldung/util/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java similarity index 68% rename from model-mapper/src/com/baeldung/util/UserPropertyMap.java rename to java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 4346174440..9fa945ce32 100644 --- a/model-mapper/src/com/baeldung/util/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -1,8 +1,5 @@ -package com.baeldung.util; +package com.baeldung.modelmapper; -import com.baeldung.model.User; -import com.baeldung.model.UserDTO; -import com.baeldung.model.UserList; import org.modelmapper.AbstractConverter; import org.modelmapper.Converter; import org.modelmapper.PropertyMap; @@ -11,10 +8,11 @@ import java.util.List; /** * @author sasam0320 - * @date 4/18/2020 + * @description + * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. + * In the configuration method, we call a converter to do the mapping. */ - -public class UserPropertyMap extends PropertyMap { +public class UserPropertyMap extends PropertyMap { Converter, List> converter = new AbstractConverter, List>() { diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java new file mode 100644 index 0000000000..44a929621b --- /dev/null +++ b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java @@ -0,0 +1,85 @@ +package com.baeldung.modelmapper; + +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.modelmapper.ModelMapper; +import org.modelmapper.TypeToken; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + + +/** + * @sasam0320 + * @description + * This class has test methods of mapping Integer to Character list, + * mapping user list to DTO list using MapperUtil generic methods and Converter + */ +public class UserMappingTest { + + private ModelMapper mapper; + private List users; + + @Before + public void init() { + + mapper = new ModelMapper(); + mapper.addMappings(new UserPropertyMap()); + users = new ArrayList(); + users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); + users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); + users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN")); + + } + + @Test + public void testMapIntegerList() { + + List integers = new ArrayList(); + + integers.add(1); + integers.add(2); + integers.add(3); + + List characters = mapper.map(integers, new TypeToken>() { + }.getType()); + + assertThat(characters, hasItems('1','2','3')); + + } + + @Test + public void testMapGenericTypeLists() { + + // Mapping lists using generic type methods + + List userDtoList = MapperUtil.mapList(users, UserDTO.class); + + assertThat(userDtoList, Matchers.hasItem( + Matchers.both(hasProperty("userId", equalTo("b100"))) + .and(hasProperty("email", equalTo("user1@baeldung.com"))) + .and(hasProperty("userName", equalTo("user1"))))); + + // Mapping lists using PropertyMap and Converter + + UserList userList = new UserList(); + userList.setUsers(users); + UserListDTO dto = new UserListDTO(); + mapper.map(userList, dto); + + assertNotNull(dto); + assertThat(dto, Matchers.hasProperty("usernames")); + assertThat(dto.getUsernames(), hasSize(3)); + + } + +} \ No newline at end of file diff --git a/model-mapper/src/Main.java b/model-mapper/src/Main.java deleted file mode 100644 index a6deb49168..0000000000 --- a/model-mapper/src/Main.java +++ /dev/null @@ -1,58 +0,0 @@ -import com.baeldung.model.User; -import com.baeldung.model.UserDTO; -import com.baeldung.model.UserList; -import com.baeldung.util.MapperUtil; -import com.baeldung.util.UserPropertyMap; -import org.modelmapper.ModelMapper; -import org.modelmapper.TypeToken; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author sasam0320 - * @date 4/18/2020 - */ - -public class Main { - - public static void main(String[] args) { - - //Instantiate ModelMapper - - ModelMapper mapper = new ModelMapper(); - mapper.addMappings(new UserPropertyMap()); - - // Mapping lists using TypeToken generic class - - List integers = new ArrayList(); - - integers.add(1); - integers.add(2); - integers.add(3); - - List characters = mapper.map(integers, new TypeToken>() {}.getType()); - - System.out.println("Character list: " + characters); - - // Mapping lists using generic type methods - - List users = new ArrayList(); - users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); - users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); - users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN")); - - List userDtoList = MapperUtil.mapList(users, UserDTO.class); - userDtoList.stream().map(userDto -> userDto.getEmail()).forEachOrdered(System.out::println); - - // Mapping lists using PropertyMap and Converter - - UserList userList = new UserList(); - userList.setUsers(users); - UserDTO dto = new UserDTO(); - - mapper.map(userList, dto); - dto.getUsernames().forEach(System.out::println); - - } -} diff --git a/model-mapper/src/com/baeldung/model/User.java b/model-mapper/src/com/baeldung/model/User.java deleted file mode 100644 index 73e4baafb5..0000000000 --- a/model-mapper/src/com/baeldung/model/User.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.baeldung.model; - -import java.util.List; - -/** - * @author sasam0320 - * @date 4/18/2020 - */ -public class User { - - private String userId; - private String userName; - private String email; - private String contactNumber; - private String userType; - - // Standard constructors, getters and setters - - public User(){} - - public User(String userId, String userName, String email, String contactNumber, String userType) { - this.userId = userId; - this.userName = userName; - this.email = email; - this.contactNumber = contactNumber; - this.userType = userType; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public String getContactNumber() { - return contactNumber; - } - - public void setContactNumber(String contactNumber) { - this.contactNumber = contactNumber; - } - - public String getUserType() { - return userType; - } - - public void setUserType(String userType) { - this.userType = userType; - } - - -} diff --git a/model-mapper/src/com/baeldung/model/UserDTO.java b/model-mapper/src/com/baeldung/model/UserDTO.java deleted file mode 100644 index ed056ace8c..0000000000 --- a/model-mapper/src/com/baeldung/model/UserDTO.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.baeldung.model; - -import java.util.List; - -/** - * @author sasam0320 - * @date 4/18/2020 - */ -public class UserDTO { - - private String userId; - private String userName; - private String email; - private List usernames; - - // getters and setters - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public List getUsernames() { - return usernames; - } - - public void setUsernames(List usernames) { - this.usernames = usernames; - } -} diff --git a/model-mapper/src/com/baeldung/model/UserList.java b/model-mapper/src/com/baeldung/model/UserList.java deleted file mode 100644 index b30d5507d5..0000000000 --- a/model-mapper/src/com/baeldung/model/UserList.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.model; - -import java.util.Collection; - -/** - * @author sasam0320 - * @date 4/18/2020 - */ - -public class UserList { - - private Collection users; - - public Collection getUsers() { - return users; - } - - public void setUsers(Collection users) { - this.users = users; - } -} diff --git a/model-mapper/src/com/baeldung/util/MapperUtil.java b/model-mapper/src/com/baeldung/util/MapperUtil.java deleted file mode 100644 index fe10b7777d..0000000000 --- a/model-mapper/src/com/baeldung/util/MapperUtil.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.util; - -import org.modelmapper.ModelMapper; -import org.modelmapper.convention.MatchingStrategies; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author sasam0320 - * @date 4/18/2020 - */ -public class MapperUtil { - - - private static ModelMapper modelMapper = new ModelMapper(); - - - static { - - modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); - - } - - private MapperUtil() { - - - } - - public static T mapTo(final S source, final Class target) { - - return modelMapper.map(source, target); - } - - public static List mapList(final List sourceList, final Class target) { - - List targetList = new ArrayList(); - - for (S source : sourceList) { - - targetList.add(modelMapper.map(source, target)); - } - - return targetList; - } - -} From 4bc8c329fc8e8c247f296a1a68b2f436737ed3b1 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Tue, 21 Apr 2020 21:29:38 +0200 Subject: [PATCH 13/18] Renaming test methods and formatting --- .../baeldung/modelmapper/UserPropertyMap.java | 3 +- .../baeldung/modelmapper/UserMappingTest.java | 85 ------------------- 2 files changed, 1 insertion(+), 87 deletions(-) delete mode 100644 java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 9fa945ce32..3d9059c520 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -7,10 +7,9 @@ import java.util.ArrayList; import java.util.List; /** - * @author sasam0320 - * @description * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. * In the configuration method, we call a converter to do the mapping. + * @author sasam0320 */ public class UserPropertyMap extends PropertyMap { diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java deleted file mode 100644 index 44a929621b..0000000000 --- a/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UserMappingTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.baeldung.modelmapper; - -import org.hamcrest.Matchers; -import org.junit.Before; -import org.junit.Test; -import org.modelmapper.ModelMapper; -import org.modelmapper.TypeToken; - -import java.util.ArrayList; -import java.util.List; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - - -/** - * @sasam0320 - * @description - * This class has test methods of mapping Integer to Character list, - * mapping user list to DTO list using MapperUtil generic methods and Converter - */ -public class UserMappingTest { - - private ModelMapper mapper; - private List users; - - @Before - public void init() { - - mapper = new ModelMapper(); - mapper.addMappings(new UserPropertyMap()); - users = new ArrayList(); - users.add(new User("b100", "user1", "user1@baeldung.com", "111-222", "USER")); - users.add(new User("b101", "user2", "user2@baeldung.com", "111-333", "USER")); - users.add(new User("b102", "user3", "user3@baeldung.com", "111-444", "ADMIN")); - - } - - @Test - public void testMapIntegerList() { - - List integers = new ArrayList(); - - integers.add(1); - integers.add(2); - integers.add(3); - - List characters = mapper.map(integers, new TypeToken>() { - }.getType()); - - assertThat(characters, hasItems('1','2','3')); - - } - - @Test - public void testMapGenericTypeLists() { - - // Mapping lists using generic type methods - - List userDtoList = MapperUtil.mapList(users, UserDTO.class); - - assertThat(userDtoList, Matchers.hasItem( - Matchers.both(hasProperty("userId", equalTo("b100"))) - .and(hasProperty("email", equalTo("user1@baeldung.com"))) - .and(hasProperty("userName", equalTo("user1"))))); - - // Mapping lists using PropertyMap and Converter - - UserList userList = new UserList(); - userList.setUsers(users); - UserListDTO dto = new UserListDTO(); - mapper.map(userList, dto); - - assertNotNull(dto); - assertThat(dto, Matchers.hasProperty("usernames")); - assertThat(dto.getUsernames(), hasSize(3)); - - } - -} \ No newline at end of file From 58dc739c3efd0f03dcbae6fe9743a2f60baadab2 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Tue, 21 Apr 2020 22:26:22 +0200 Subject: [PATCH 14/18] changes in build pom.xml --- .../main/java/com/baeldung/modelmapper/UserPropertyMap.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 3d9059c520..d2c32a307d 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -3,13 +3,15 @@ package com.baeldung.modelmapper; import org.modelmapper.AbstractConverter; import org.modelmapper.Converter; import org.modelmapper.PropertyMap; + import java.util.ArrayList; import java.util.List; /** * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. * In the configuration method, we call a converter to do the mapping. - * @author sasam0320 + * + * @author Sasa Milenkovic */ public class UserPropertyMap extends PropertyMap { From 9f0d8cf6aba2043abddb6bb2c5ef63785ffbb4d0 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sat, 25 Apr 2020 21:57:43 +0200 Subject: [PATCH 15/18] Refactoring code in com.baeldung.modelmapper package --- .../java/com/baeldung/modelmapper/UserPropertyMap.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index d2c32a307d..0d2ebf7b4c 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -18,14 +18,15 @@ public class UserPropertyMap extends PropertyMap { Converter, List> converter = new AbstractConverter, List>() { - List usernames = new ArrayList<>(); + protected List usernames; + @Override protected List convert(List users) { - users.forEach(user -> usernames.add(user.getUserName())); + usernames = new ArrayList(); + users.forEach(user -> usernames.add(user.getUsername())); return usernames; } - }; @Override From c2e64bea7273cb971caa03e750a1683a7e7a0fd8 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Sun, 26 Apr 2020 18:37:31 +0200 Subject: [PATCH 16/18] Customizing Java 8 code in modemmapper package --- .../com/baeldung/modelmapper/UserPropertyMap.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java index 0d2ebf7b4c..5b2942b158 100644 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java @@ -4,11 +4,11 @@ import org.modelmapper.AbstractConverter; import org.modelmapper.Converter; import org.modelmapper.PropertyMap; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** - * UserPropertyMap class instantiates the converter to map the data from the user list to the user name list. + * UserPropertyMap class instantiates the converter to map the data from the UserList to the UsersLisDTO. * In the configuration method, we call a converter to do the mapping. * * @author Sasa Milenkovic @@ -18,19 +18,20 @@ public class UserPropertyMap extends PropertyMap { Converter, List> converter = new AbstractConverter, List>() { - protected List usernames; @Override protected List convert(List users) { - usernames = new ArrayList(); - users.forEach(user -> usernames.add(user.getUsername())); - return usernames; + return users + .stream() + .map(User::getUsername) + .collect(Collectors.toList()); } }; @Override protected void configure() { + using(converter).map(source.getUsers(), destination.getUsernames()); } } From 9e6dd6f41bbe105e52e59139c5114ce5ae1fbf40 Mon Sep 17 00:00:00 2001 From: Sasa M Date: Wed, 29 Apr 2020 21:55:30 +0200 Subject: [PATCH 17/18] Creating TypeMap to use property mapping and converter class --- .../baeldung/modelmapper/UserPropertyMap.java | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java b/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java deleted file mode 100644 index 5b2942b158..0000000000 --- a/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserPropertyMap.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.modelmapper; - -import org.modelmapper.AbstractConverter; -import org.modelmapper.Converter; -import org.modelmapper.PropertyMap; - -import java.util.List; -import java.util.stream.Collectors; - -/** - * UserPropertyMap class instantiates the converter to map the data from the UserList to the UsersLisDTO. - * In the configuration method, we call a converter to do the mapping. - * - * @author Sasa Milenkovic - */ -public class UserPropertyMap extends PropertyMap { - - - Converter, List> converter = new AbstractConverter, List>() { - - - @Override - protected List convert(List users) { - - return users - .stream() - .map(User::getUsername) - .collect(Collectors.toList()); - } - }; - - @Override - protected void configure() { - - using(converter).map(source.getUsers(), destination.getUsernames()); - } -} From cd07a21b8e3bfe637016823dd5afd2e362893e1b Mon Sep 17 00:00:00 2001 From: Sasa M Date: Thu, 30 Apr 2020 16:20:39 +0200 Subject: [PATCH 18/18] Add hamcrest jar dependency in parent-java build --- java-collections-conversions-2/pom.xml | 2 +- parent-java/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/java-collections-conversions-2/pom.xml b/java-collections-conversions-2/pom.xml index 892d73cb2b..0f7cdadeb2 100644 --- a/java-collections-conversions-2/pom.xml +++ b/java-collections-conversions-2/pom.xml @@ -33,7 +33,7 @@ org.hamcrest - hamcrest-all + hamcrest ${hamcrest.version} test diff --git a/parent-java/pom.xml b/parent-java/pom.xml index 3b2b5f59de..baad9fecf4 100644 --- a/parent-java/pom.xml +++ b/parent-java/pom.xml @@ -47,7 +47,7 @@ 1.19 2.3.7 4.12 - 1.3 + 2.2