Transition rollback only partially - java

When I execute this code, an order object remains in the DB at the end, in which the label is test. So the changing of the object is rolled back, but not the creation. My expectation is that the DB contains no order or chat channel entry after the call
#Autowired
private OrderRepository orderRepository;
#Autowired
private ChatChannelRepository chatChannelRepository;
#RequestMapping(value = "/order/price", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
#Transactional
public double calculatePrice() throws CustomException {
Order order = new Order();
order.setLabel("Test");
orderRepository.save(order);
ChatChannel channel = new ChatChannel();
channel.setOrder(order);
chatChannelRepository.save(channel);
order.setLabel("Test2");
orderRepository.save(order);
throw new RuntimeException("Test");
}
#Transactional
public interface ChatChannelRepository extends
CrudRepository<ChatChannel, Long> {
public ChatChannel findById(long id);
}
#Transactional
public interface OrderRepository extends CrudRepository<Order, Long>
{
public Order findById(long id);
public List<Order> findByBudgetIn(List<Budget> budgets);
public List<Order> findByBudgetInAndCurrentChainPosPhaseEquals(List<Budget> budgets, Phase phase);
public List<Order> findByProcessors(User currentUser);
}

Related

How to avoid changing object in cache before saving Spring Cache

I want to show right away with an example. There is such a repository:
#Repository
public interface UserRepository extends JpaRepository<User, Integer> {
String USER_CACHE = "users";
#Override
#CachePut(value = USER_CACHE, key = "#user.email", unless = "#result == null")
<S extends User> S save(S user);
#Override
#CacheEvict(value = USER_CACHE, key = "#user.email")
void delete(User user);
#Cacheable(value = USER_CACHE, key = "#email", unless = "#result == null")
User findByEmailIgnoreCase(String email);
}
And there is such a service that saves the user's changes and sends a confirmation code to the mail:
#Service
public class UserServiceImpl implements UserService, UserDetailsService {
private final UserRepository userRepository;
#Override
public User getUserByEmail(String email) {
return userRepository.findByEmailIgnoreCase(email);
}
#Override
#Transactional(isolation = Isolation.SERIALIZABLE)
public void createAppUser(RegistrationAppRequestDto registrationRequest) throws EmailSendingException {
User user = getUserByEmail(registrationRequest.getEmail());
user.setPassword(registrationRequest.getPassword());
user.setApp(true);
user.setActivated(false);
user.setActivationCode(UUID.randomUUID().toString());
user.setLastVisit(LocalDateTime.now());
if (Strings.isEmpty(user.getImg())) {
user.setImg(DEFAULT_IMG);
}
mailSender.sendWelcomeMessage(user);
userRepository.save(user);
}
}
And the problem is that in case of an error (For example, when sending a message to the mail), the changes that were made with this user will remain in the cache, and these changes will not get into the database (which is correct). Is there any practice of working with such a case? Alternatively, i can use object cloning, but I think this is a bad practice. I will be grateful for any help.

How can i make Testing in Spring boot when i have a Page<Object> as a return type

I am trying to do testing my save method in my service impl class. It has Page as return type. The test succeeds but I am writting something wrong because it succeeds for all the cases which normally shouldn't Please see my code below.
Service Class Implementation
#Service
#Transactional
public class CompanyServiceImpl implements CompanyService {
private final CompanyRepository companyRepository;
public CompanyServiceImpl(CompanyRepository companyRepository) {
this.companyRepository = companyRepository;
}
#Override
public Page<Company> findAll(Pageable pageable) {
Page<Company> result = companyRepository.findAll(pageable);
return result;
}
#Override
public Page<Company> searchCompany(String companyName, Long companyGroupId, Pageable pageable) {
Page<Company> result = companyRepository.findByParametersWeb(companyName,companyGroupId,pageable);
return result;
}
#Override
public Optional<Company> findById(Long id) {
Optional<Company> entity = companyRepository.findById(id);
return entity;
}
#Override
public Company save(Company company) {
Company entity = companyRepository.save(company);
return entity;
}
#Override
public void delete(Long id) {
companyRepository.deleteById(id);
}
}
Testing Service class
class CompanyServiceImplTest {
#Mock
private CompanyRepository companyRepository;
private CompanyService companyService;
private Company company;
#BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
companyService = new CompanyServiceImpl(companyRepository);
company = new Company();
company.setName("company");
company.setCompanyGroupId(1L);
}
#Test
void searchCompany() {
List<Company> companies = new ArrayList<>();
Pageable pageable= PageRequest.of(0,5);
Page<Company> result = new PageImpl<>(companies,pageable,1);
when(companyRepository.findByParametersWeb(anyString(),anyLong(),any(Pageable.class))).thenReturn(result);
Page<Company> newResult = companyService.searchCompany("giorgos",1L,pageable);
assertEquals(newResult.getTotalElements(),result.getTotalElements());
}
}
Finally My Company Repository
#Repository
public interface CompanyRepository extends JpaRepository<Company, Long> {
#Query("SELECT a FROM Company a WHERE (:name is null or ((a.name LIKE :name AND LENGTH(:name) > 0) OR ( a.name = '%')))")
List<Company> findByCompanyName(#Param("name") String name);
#Query("SELECT a FROM Company a WHERE (:name is null or (LENGTH(:name) > 0 " +
" AND ((:option = 'yes' AND a.name = :name) or (:option = 'start' AND a.name LIKE CONCAT(:name,'%')) " +
" or (:option = 'end' AND a.name LIKE CONCAT('%',:name)) or (a.name LIKE CONCAT('%',:name,'%'))))) " +
" AND (:companyGroupId is null or a.companyGroupId = :companyGroupId) ORDER BY a.name")
Page<Company> findByParametersWeb(String name,Long companyGroupId, Pageable pageable);
List<Company> findAllByNameOrderByName();
}
So you want to differentiate unit tests from integration or component tests here.
Your test would qualify as a unit test, it solely tests the functionality of your service layer isolated from everything else.
That is also why you mock your repository layer, to be independent from a database.
Contrary to that, integration and component tests test your whole application stack.
For this, the spring boot environment must be running, so you have to annotate your testclass with #ExtendWith(SpringExtension.class).
For these kind of tests you need an active db, so commonly you use a h2 database for your tests, that is filled with data prior to your tests. Have a look at this and this.
In the test itself you either inject your service and test from there, or you call your endpoints using RestTemplate.
#ExtendWith(SpringExtension.class)
#SpringBootTest
#Sql("/schema.sql")
public class DocumentManagementBackendApplicationTest {
#Autowired
private final CompanyServiceImpl companyServiceImpl;
#Test
#Sql("/searchCompany.sql")
public void testSearchCompany() {
List<Company> companies = new ArrayList<>();
Pageable pageable= PageRequest.of(0, 5);
Page<Company> result = companyService.searchCompany("giorgos",1L,pageable);
// now here you know, based on what you insert into your db with your sql scripts,
// what you should expect and so you can test for it
(...)
}
}

Architecture pattern for "microservice" with hard logic (Spring boot)

i've got a microservice which implements some optimization function by calling many times another microservice (the second one calculates so called target function value and the first micriservice changes paramters of this tagrget function)
It leads to necessity of writing some logic in Rest Controller layer. To be clear some simplified code will be represented below
#RestController
public class OptimizerController {
private OptimizationService service;
private RestTemplate restTemplate;
#GetMapping("/run_opt")
public DailyOptResponse doOpt(){
Data iniData = service.prepareData(null);
Result r = restTemplate.postForObject(http://calc-service/plain_calc", iniData, Result.class);
double dt = service.assessResult(r);
while(dt > 0.1){
Data newData = service.preapreData(r);
r = restTemplate.postForObject(http://calc-service/plain_calc", newData , Result.class);
dt = service.assessResult(r);
}
return service.prepareResponce(r);
}
As i saw in examples all people are striving to keep rest controller as simple as possible and move all logic to service layer. But what if i have to call some other microservices from service layer? Should i keep logic of data formin in service layer and return it to controller layer, use RestTemplate object in service layer or something else?
Thank you for your help
It is straightforward.
The whole logic is in the service layer (including other services).
Simple example:
Controller:
#RestController
#RequestMapping("/api/users")
public class UserController {
private final UserManager userManager;
#Autowired
public UserController(UserManager userManager) {
super();
this.userManager = userManager;
}
#GetMapping()
public List<UserResource> getUsers() {
return userManager.getUsers();
}
#GetMapping("/{userId}")
public UserResource getUser(#PathVariable Integer userId) {
return userManager.getUser(userId);
}
#PutMapping
public void updateUser(#RequestBody UserResource resource) {
userManager.updateUser(resource);
}
}
Service:
#Service
public class UserManager {
private static final Logger log = LoggerFactory.getLogger(UserManager.class);
private final UserRepository userRepository;
private final UserResourceAssembler userResourceAssembler;
private final PictureManager pictureManager;
#Autowired
public UserManager(
UserRepository userRepository,
UserResourceAssembler userResourceAssembler,
PictureManager pictureManager
) {
super();
this.userRepository = userRepository;
this.userResourceAssembler = userResourceAssembler;
this.pictureManager= pictureManager;
}
public UserResource getUser(Integer userId) {
User user = userRepository.findById(userId).orElseThrow(() -> new NotFoundException("User with ID " + userId + " not found!"));
return userResourceAssembler.toResource(user);
}
public List<UserResource> getUsers() {
return userResourceAssembler.toResources(userRepository.findAll());
}
public void updateUser(UserResource resource) {
User user = userRepository.findById(resource.getId()).orElseThrow(() -> new NotFoundException("User with ID " + resource.getId() + " not found!"));
PictureResource pictureResource = pictureManager.savePicture(user);
user = userResourceAssembler.fromResource(user, resource);
user = userRepository.save(user);
log.debug("User {} updated.", user);
}
}
Service 2:
#Service
public class PictureManager {
private static final Logger log = LoggerFactory.getLogger(PictureManager.class);
private final RestTemplate restTemplate;
#Autowired
public PictureManager(RestTemplate restTemplate) {
super();
this.restTemplate = restTemplate;
}
public PictureResource savePicture(User user) {
//do some logic with user
ResponseEntity<PictureResource> response = restTemplate.exchange(
"url",
HttpMethod.POST,
requestEntity,
PictureResource.class);
return response.getBody();
}
}
Repository:
public interface UserRepository extends JpaRepository<User, Integer> {
User findByUsername(String username);
}

How can I assert value created in void method?

I have class
public class CloneUserService {
private final UserRepository userRepository;
private final PersonRepository personRepository;
private final OrderRepository orderRepository;
public CloneUserService(UserRepository userRepository, PersonRepository personRepository, OrderRepository orderRepository) {
this.userRepository = userRepository;
this.personRepository = personRepository;
this.orderRepository = orderRepository;
}
public void createFromTemplate(String templateUserId) {
User templateUser = userRepository.getUserById(templateUserId);
Person templatePerson = personRepository.getPersonByUserId(templateUserId);
List<Order> templateOrders = orderRepository.getOrdersByUserId(templateUserId);
User newUser = cloneUserFromTemplate(templateUser);
newUser.setId("newId");
userRepository.save(newUser);
Person newPerson = clonePersonFromTemplate(templatePerson);
newPerson.setUser(newUser);
newPerson.setId("newId");
personRepository.save(newPerson);
for (Order templateOrder : templateOrders) {
Order newOrder = cloneOrderFromTemplate(templateOrder);
newOrder.setId("newId");
newOrder.setUSer(newUser);
orderRepository.save(newOrder);
}
}
private Order cloneOrderFromTemplate(Order templateOrder) {
//logic
return null;
}
private Person clonePersonFromTemplate(Person templatePerson) {
//logic
return null;
}
private User cloneUserFromTemplate(User templateUser) {
//logic
return null;
}
}
I need to test this method createFromTemplate.
I create this test. I create stabs for each repository and store saved object into this stub. And I add the additional method for getting this object for the assertion.
It works. But I have 2 problems:
My template object is mutable. It is not a big problem but it is a fact.
If I add new methods to repository interface I must implement it in stubs.
Mu question - How can I test cloned objects like theses from my example?
I don't use spring and H2DB or another in-memory database.
I have a MongoDB database.
If I use mockito I will not understand how to assert new objects in void method.
class CloneUserServiceTest {
private CloneUserService cloneUserService;
private UserRepositoryStub userRepository;
private PersonRepositoryStub personRepository;
private OrderRepositoryStub orderRepository;
#Before
public void setUp() {
User templateUser = new User();
Person templatePerson = new Person();
List<Order> templateOrders = Collections.singletonList(new Order());
userRepository = new UserRepositoryStub(templateUser);
personRepository = new PersonRepositoryStub(templatePerson);
orderRepository = new OrderRepositoryStub(templateOrders);
cloneUserService = new CloneUserService(userRepository, personRepository, orderRepository);
}
#Test
void createFromTemplate() {
cloneUserService.createFromTemplate("templateUserId");
User newUser = userRepository.getNewUser();
// assert newUser
Person newPerson = personRepository.getNewPerson();
// assert newPerson
Order newOrder = orderRepository.getNewOrder();
// assert newOrder
}
private static class UserRepositoryStub implements UserRepository {
private User templateUser;
private User newUser;
public UserRepositoryStub(User templateUser) {
this.templateUser = templateUser;
}
public User getUserById(String templateUserId) {
return templateUser;
}
public void save(User newUser) {
this.newUser = newUser;
}
public User getNewUser() {
return newUser;
}
}
private static class PersonRepositoryStub implements PersonRepository {
private Person templatePerson;
private Person newPerson;
public PersonRepositoryStub(Person templatePerson) {
this.templatePerson = templatePerson;
}
public Person getPersonByUserId(String templateUserId) {
return templatePerson;
}
public void save(Person newPerson) {
this.newPerson = newPerson;
}
public Person getNewPerson() {
return newPerson;
}
}
private static class OrderRepositoryStub implements OrderRepository {
private List<Order> templateOrders;
private Order newOrder;
public OrderRepositoryStub(List<Order> templateOrders) {
this.templateOrders = templateOrders;
}
public List<Order> getOrdersByUserId(String templateUserId) {
return templateOrders;
}
public void save(Order newOrder) {
this.newOrder = newOrder;
}
public Order getNewOrder() {
return newOrder;
}
}
}
In your scenario I would consider using mocking framework like Mockito.
Some main advantages:
Adding new methods to repository interface doesn't require implementing it in stubs
Supports exact-number-of-times and at-least-once verification
Allows flexible verification in order (e.g: verify in order what you want, not every single interaction)
Very nice and simple annotation syntax - #Mock, #InjectMocks, #Spy
Here is an example - maybe it will interest you:
// arrange
Warehouse mock = Mockito.mock(Warehouse.class);
//act
Order order = new Order(TALISKER, 50);
order.fill(warehouse); // fill will call remove() implicitly
// assert
Mockito.verify(warehouse).remove(TALISKER, 50); // verify that remove() method was actually called

Post Method using DTO

I want to use DTO to communicate with the Angular, but actually it doesn't work. I want to create POST request to add data from my application to the database using Dto model.
You can see my errors on the picture:
My class Customer:
#Entity
#Table(name = "customer")
public class Customer {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Column(name = "name")
private String name;
#OneToMany
private List<Ticket> ticket;
...
Class CustomerDto:
public class CustomerDto {
private String name;
private List<TicketDto> ticket;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<TicketDto> getTicket() {
return ticket;
}
public void setTicket(List<TicketDto> ticket) {
this.ticket = ticket;
}
}
Class CustomerController:
#Autowired
CustomerService customerService;
#PostMapping(value = "/customers/create")
public Customer postCustomer(#RequestBody CustomerDto customerDto, List<TicketDto> ticketDtos) {
//ArrayList<TicketDto> tickets = new ArrayList<>();
ticketDtos.add(customerDto.getName());
ticketDtos.add(customerDto.getTicket());
Customer _customer = customerService.save(new Customer(customerDto.getName(), ticketDtos ));
return _customer;
}
CustomerService:
public interface CustomerService {
void save(CustomerDto customerDto, List<TicketDto> ticketDtos);
}
CustomerServiceImpl:
#Service
public class CustomerServiceImpl implements CustomerService {
#Autowired
CustomerRepository repository;
#Override
public void save(CustomerDto customerDto, List<TicketDto> ticketDtos) {
Customer customer = new Customer();
customer.setName(customerDto.getName());
customer.setTicket(customerDto.getTicket());
List<Ticket> tickets = new ArrayList<>();
for (TicketDto ticketDto : ticketDtos) {
Ticket ticket = new Ticket();
ticket.setDestinationCity(ticketDto.getDepartureCity());
ticket.setDestinationCity(ticketDto.getDestinationCity());
tickets.add(ticket);
}
}
Since you CustomerServiceImpl is taking CustomerDto and list of TicketDtos, you need to change your method call on controller as below:
Class CustomerController:
#Autowired
CustomerService customerService;
#PostMapping(value = "/customers/create")
public Customer postCustomer(#RequestBody CustomerDto customerDto) {
Customer _customer = customerService.save(customerDto));
return _customer;
}
And update CustomerServiceImpl as:
#Service
public class CustomerServiceImpl implements CustomerService {
#Autowired
CustomerRepository repository;
// change save to return saved customer
#Override
public Customer save(CustomerDto customerDto) {
Customer customer = new Customer();
customer.setName(customerDto.getName());
// customer.setTicket(customerDto.getTicket()); // remove this
List<Ticket> tickets = new ArrayList<>();
for (TicketDto ticketDto : customerDto.getTicketDtos) {
Ticket ticket = new Ticket();
ticket.setDestinationCity(ticketDto.getDepartureCity());
ticket.setDestinationCity(ticketDto.getDestinationCity());
tickets.add(ticket);
}
customer.setTickets(tickets); // add this to set tickets on customer
return repository.save(customer);
}
Obviously, you need to change your interface as well:
public interface CustomerService {
Customer save(CustomerDto customerDto);
}
For entity-DTO conversion, we need to use ModelMapper or mapstruct library.
With the help of these libraries, we can easily convert from Dto to entity and entity to dto object. After adding any of the dependency, We are able to use it.
How can we use, Let see...
Define modelMapper bean in spring configuration.
#Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
Suppose we need to convert List to List obj then we can perform simply like that :
List<TicketDto> ticketDtos = .... //Suppose It is holding some data
List<Ticket> tickets = ticketDtos.stream()
.map(tkt-> mappper.map(tkt, ticket.class))
.collect(Collectors.toList());
It is very simple to use like mappper.map(targetClass, DestinationClass.class)
I used Java8 code here but you can use anyone. I hope It would be very helpful to you.

Categories