Refactored packages, added test cases, added structured class case

This commit is contained in:
Michele Guarnaccia 2020-05-24 11:44:26 +02:00
parent cd51e118df
commit 715147e7dc
13 changed files with 231 additions and 106 deletions

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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<ContactPhone> contactPhones;
@Override public String toString() {
return String.format("Customer %s, Phone: %s",
this.name, this.contactPhones.stream().map(e -> e.toString()).reduce("", String::concat));
}
}

View File

@ -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, Long> {
ContactPhone findById(long id);
ContactPhone findByCustomerId(long id);
}

View File

@ -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<Customer, Long> {

View File

@ -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, Long> {
CustomerStructured findById(long id);
}

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -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());
}
}