Skip to content

Commit

Permalink
[TEST] Add unit tests. Refactor code. Fix bug with updateCar method, …
Browse files Browse the repository at this point in the history
…add new method in CarMapper
  • Loading branch information
AntonBabychP1T committed Feb 26, 2024
1 parent 8891031 commit a1c98f8
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 590 deletions.
250 changes: 0 additions & 250 deletions checkstyle.xml

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/service/carsharing/mapper/CarMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service.carsharing.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import service.carsharing.config.MapperConfig;
import service.carsharing.dto.cars.CarRequestDto;
import service.carsharing.dto.cars.CarResponseDto;
Expand All @@ -11,4 +12,6 @@ public interface CarMapper {
Car toModel(CarRequestDto requestDto);

CarResponseDto toDto(Car car);

void updateCar(CarRequestDto requestDto, @MappingTarget Car car);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -34,22 +33,23 @@ public List<CarResponseDto> getAllCars(Pageable pageable) {

@Override
public CarResponseDto getCar(Long id) {
Optional<Car> carById = carRepository.findByIdAndDeletedFalse(id);
if (carById.isPresent()) {
return carMapper.toDto(carById.get());
}
throw new EntityNotFoundException("Can't find car with id: " + id);
return carMapper.toDto(getCarById(id));
}

@Override
public CarResponseDto updateCar(Long id, CarRequestDto requestDto) {
carRepository.findByIdAndDeletedFalse(id).orElseThrow(
() -> new EntityNotFoundException("Can't find car with id: " + id));
return carMapper.toDto(carRepository.save(carMapper.toModel(requestDto)));
Car car = getCarById(id);
carMapper.updateCar(requestDto, car);
return carMapper.toDto(carRepository.save(car));
}

@Override
public void deleteCar(Long id) {
carRepository.softDelete(id);
}

private Car getCarById(Long id) {
return carRepository.findByIdAndDeletedFalse(id).orElseThrow(
() -> new EntityNotFoundException("Can't find car with id: " + id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
@RequiredArgsConstructor
@Service
public class RentalServiceImpl implements RentalService {
private static final Integer MIN_REQUIRED_CAR_AVAILABLE = 1;

private final RentalRepository rentalRepository;
private final UserRepository userRepository;
private final CarRepository carRepository;
Expand All @@ -39,13 +41,7 @@ public RentalResponseDto addNewRental(RentalRequestDto requestDto) {
throw new RuntimeException("You have expired payments, denied access");
}
Car car = getCarById(requestDto.carId());
if (car.getInventory() < 1) {
notificationService.sendNotification(requestDto.userId(),
"There is no free available car with id: "
+ requestDto.carId());
throw new RuntimeException("There is no free available car with id: "
+ requestDto.carId());
}
checkIsExistsAvailableCar(requestDto, car);
car.setInventory(car.getInventory() - 1);
carRepository.save(car);
RentalResponseDto responseDto = rentalMapper.toDto(rentalRepository
Expand Down Expand Up @@ -113,6 +109,16 @@ private boolean canBorrow(Long userId) {
return expiredState.isEmpty();
}

private void checkIsExistsAvailableCar(RentalRequestDto requestDto, Car car) {
if (car.getInventory() < MIN_REQUIRED_CAR_AVAILABLE) {
notificationService.sendNotification(requestDto.userId(),
"There is no free available car with id: "
+ requestDto.carId());
throw new RuntimeException("There is no free available car with id: "
+ requestDto.carId());
}
}

private String createOverdueRentalMessage(Rental rental) {
return "Overdue rental alert! Rental ID: " + rental.getId()
+ ", User ID: " + rental.getUser().getId()
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/service/carsharing/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ public UserResponseDto registration(UserRegistrationRequestDto requestDto) {

@Override
public UserWithRoleResponseDto updateRole(Long id, UserUpdateRoleRequestDto requestDto) {
User user = userRepository.findByIdAndDeletedFalse(id)
.orElseThrow(() -> new EntityNotFoundException("Can't find user with id: " + id));
Role.RoleName roleName = Role.RoleName.valueOf(requestDto.role());
Role role = roleRepository.findByName(roleName).orElseThrow(
() -> new EntityNotFoundException("Can't find role with name=" + roleName));
User user = getUserById(id);
Role role = getRoleByRoleName(requestDto.role());
user.getRoles().add(role);
return userMapper.toDtoWithRole(userRepository.save(user));
}
Expand All @@ -63,4 +60,15 @@ private User getUserByEmail(String email) {
return userRepository.findByEmail(email).orElseThrow(
() -> new EntityNotFoundException("Can't find user with email: " + email));
}

private User getUserById(Long id) {
return userRepository.findByIdAndDeletedFalse(id)
.orElseThrow(() -> new EntityNotFoundException("Can't find user with id: " + id));
}

private Role getRoleByRoleName(String role) {
Role.RoleName roleName = Role.RoleName.valueOf(role);
return roleRepository.findByName(roleName).orElseThrow(
() -> new EntityNotFoundException("Can't find role with name=" + roleName));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package service.carsharing;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class CarSharingApplicationTests {
@Test
void contextLoads(){
}
}
Loading

0 comments on commit a1c98f8

Please sign in to comment.