From 715147e7dc2ef0f13cf8b9845b2de049bc66073f Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sun, 24 May 2020 11:44:26 +0200 Subject: [PATCH] Refactored packages, added test cases, added structured class case --- .../main/java/com/baeldung/Application.java | 54 ------------- .../PartialUpdateApplication.java | 20 +++++ .../partialupdate/model/ContactPhone.java | 22 ++++++ .../{ => partialupdate}/model/Customer.java | 0 .../model/CustomerDto.java | 0 .../model/CustomerStructured.java | 26 ++++++ .../repository/ContactPhoneRepository.java | 12 +++ .../repository/CustomerRepository.java | 4 +- .../CustomerStructuredRepository.java | 11 +++ .../service/CustomerService.java | 79 +++++++++++++++++++ .../util/CustomerMapper.java | 6 +- .../com/baeldung/service/CustomerService.java | 47 ----------- .../partialupdate/PartialUpdateUnitTest.java | 56 +++++++++++++ 13 files changed, 231 insertions(+), 106 deletions(-) delete mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/model/Customer.java (100%) rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/model/CustomerDto.java (100%) create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/repository/CustomerRepository.java (76%) create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/util/CustomerMapper.java (73%) delete mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java create mode 100644 persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java deleted file mode 100644 index 34e86fe135..0000000000 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung; - -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; - -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; -import com.baeldung.service.CustomerService; - -@SpringBootApplication @EnableCaching -public class Application { - - @Autowired CustomerService service; - @Autowired CacheManager cacheManager; - private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Application.class); - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } - - @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) throws Exception { - logger.info("-- BASIC LOAD AND SAVE --"); - basicLoadAndSave(); - logger.info("-- BASIC LOAD AND SAVE + MAPPER --"); - basicLoadAndSaveWithMapper(); - return null; - } - - private void basicLoadAndSave() { - Customer myCustomer = service.addCustomer("John"); - logger.info("Insert -- " + myCustomer.toString()); - myCustomer = service.updateCustomer(myCustomer.id, "+00"); - logger.info("Update -- " + myCustomer.toString()); - } - - private void basicLoadAndSaveWithMapper() { - CustomerDto dto = new CustomerDto(null); - dto.name = "Johnny"; - Customer entity = service.addCustomer(dto); - logger.info("Insert -- " + entity.toString()); - CustomerDto dto2 = new CustomerDto(entity.id); - dto2.phone = "+44"; - entity = service.updateCustomer(dto2); - logger.info("Update -- " + entity.toString()); - } - -} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java new file mode 100644 index 0000000000..30f5dfa03d --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java @@ -0,0 +1,20 @@ +package com.baeldung.partialupdate; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication +@EnableCaching +public class PartialUpdateApplication { + + @Autowired + CacheManager cacheManager; + + public static void main(String[] args) { + SpringApplication.run(PartialUpdateApplication.class, args); + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java new file mode 100644 index 0000000000..9d611fa755 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java @@ -0,0 +1,22 @@ +package com.baeldung.partialupdate.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class ContactPhone { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + @Column(nullable=false) + public long customerId; + public String phone; + + @Override + public String toString() { + return phone; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java similarity index 100% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java similarity index 100% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java new file mode 100644 index 0000000000..67b534add2 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java @@ -0,0 +1,26 @@ +package com.baeldung.partialupdate.model; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class CustomerStructured { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + public String name; + @OneToMany(fetch = FetchType.EAGER, targetEntity=ContactPhone.class, mappedBy="customerId") + public List contactPhones; + + @Override public String toString() { + return String.format("Customer %s, Phone: %s", + this.name, this.contactPhones.stream().map(e -> e.toString()).reduce("", String::concat)); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java new file mode 100644 index 0000000000..2e0b687227 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java @@ -0,0 +1,12 @@ +package com.baeldung.partialupdate.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.partialupdate.model.ContactPhone; + +@Repository +public interface ContactPhoneRepository extends CrudRepository { + ContactPhone findById(long id); + ContactPhone findByCustomerId(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java similarity index 76% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java index dcde6c3b46..bd33c03b6b 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java @@ -1,10 +1,10 @@ -package com.baeldung.repository; +package com.baeldung.partialupdate.repository; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; -import com.baeldung.model.Customer; +import com.baeldung.partialupdate.model.Customer; @Repository public interface CustomerRepository extends CrudRepository { diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java new file mode 100644 index 0000000000..0f9fd1e92e --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.partialupdate.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.partialupdate.model.CustomerStructured; + +@Repository +public interface CustomerStructuredRepository extends CrudRepository { + CustomerStructured findById(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java new file mode 100644 index 0000000000..f3a1c65ef1 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java @@ -0,0 +1,79 @@ +package com.baeldung.partialupdate.service; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.partialupdate.model.ContactPhone; +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; +import com.baeldung.partialupdate.model.CustomerStructured; +import com.baeldung.partialupdate.repository.ContactPhoneRepository; +import com.baeldung.partialupdate.repository.CustomerRepository; +import com.baeldung.partialupdate.repository.CustomerStructuredRepository; +import com.baeldung.partialupdate.util.CustomerMapper; + +@Service +@Transactional +public class CustomerService { + + @Autowired + CustomerRepository repo; + @Autowired + CustomerStructuredRepository repo2; + @Autowired + ContactPhoneRepository repo3; + @Autowired + CustomerMapper mapper; + + public Customer addCustomer(String name) { + Customer myCustomer = new Customer(); + myCustomer.name = name; + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(long id, String phone) { + Customer myCustomer = repo.findById(id); + myCustomer.phone = phone; + repo.save(myCustomer); + return myCustomer; + } + + public Customer addCustomer(CustomerDto dto) { + Customer myCustomer = new Customer(); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(CustomerDto dto) { + Customer myCustomer = repo.findById(dto.getId()); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public CustomerStructured addCustomerStructured(String name) { + CustomerStructured myCustomer = new CustomerStructured(); + myCustomer.name = name; + repo2.save(myCustomer); + return myCustomer; + } + + public void addCustomerPhone(long customerId, String phone) { + ContactPhone myPhone = new ContactPhone(); + myPhone.phone = phone; + myPhone.customerId = customerId; + repo3.save(myPhone); + } + + public CustomerStructured updateCustomerStructured(long id, String name) { + CustomerStructured myCustomer = repo2.findById(id); + myCustomer.name = name; + repo2.save(myCustomer); + return myCustomer; + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java similarity index 73% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java index d4f264e33a..8a666e3e6c 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java @@ -1,12 +1,12 @@ -package com.baeldung.util; +package com.baeldung.partialupdate.util; import org.mapstruct.BeanMapping; import org.mapstruct.Mapper; import org.mapstruct.MappingTarget; import org.mapstruct.NullValuePropertyMappingStrategy; -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; @Mapper(componentModel = "spring") public interface CustomerMapper { diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java deleted file mode 100644 index 9ebbb0c814..0000000000 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.service; - -import javax.transaction.Transactional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; -import com.baeldung.repository.CustomerRepository; -import com.baeldung.util.CustomerMapper; - -@Service @Transactional -public class CustomerService { - - @Autowired CustomerRepository repo; - @Autowired CustomerMapper mapper; - - public Customer addCustomer(String name) { - Customer myCustomer = new Customer(); - myCustomer.name = name; - repo.save(myCustomer); - return myCustomer; - } - - public Customer updateCustomer(long id, String phone) { - Customer myCustomer = repo.findById(id); - myCustomer.phone = phone; - repo.save(myCustomer); - return myCustomer; - } - - public Customer addCustomer(CustomerDto dto) { - Customer myCustomer = new Customer(); - mapper.updateCustomerFromDto(dto, myCustomer); - repo.save(myCustomer); - return myCustomer; - } - - public Customer updateCustomer(CustomerDto dto) { - Customer myCustomer = repo.findById(dto.getId()); - mapper.updateCustomerFromDto(dto, myCustomer); - repo.save(myCustomer); - return myCustomer; - } - -} diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java new file mode 100644 index 0000000000..dc9c8821ac --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java @@ -0,0 +1,56 @@ +package com.baeldung.partialupdate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; +import com.baeldung.partialupdate.model.CustomerStructured; +import com.baeldung.partialupdate.service.CustomerService; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = PartialUpdateApplication.class) +public class PartialUpdateUnitTest { + + @Autowired + CustomerService service; + + @Test + public void loadAndSave_whenUpdate_thenSuccess() { + Customer myCustomer = service.addCustomer("John"); + myCustomer = service.updateCustomer(myCustomer.id, "+00"); + + assertEquals("+00", myCustomer.phone); + } + + @Test + public void loadAndSaveWithMapper_whenUpdate_thenSuccess() { + CustomerDto dto = new CustomerDto(new Customer()); + dto.name = "Johnny"; + Customer entity = service.addCustomer(dto); + + CustomerDto dto2 = new CustomerDto(entity.id); + dto2.phone = "+44"; + entity = service.updateCustomer(dto2); + + assertEquals("Johnny", entity.name); + } + + @Test + public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { + CustomerStructured myCustomer = service.addCustomerStructured("John"); + assertEquals(null, myCustomer.contactPhones); + + service.addCustomerPhone(myCustomer.id, "+44"); + myCustomer = service.updateCustomerStructured(myCustomer.id, "Mr. John"); + + assertNotEquals(null, myCustomer.contactPhones); + assertEquals(1, myCustomer.contactPhones.size()); + } +}