Untitled

mail@pastecode.io avatar
unknown
java
a year ago
3.2 kB
4
Indexable
Never
package com.cdg.fms.fleet.mgmt.application.service.usecase.vehicle;

import com.cdg.fms.fleet.mgmt.application.ports.outbound.persistence.VehicleAuditRepository;
import com.cdg.fms.fleet.mgmt.application.ports.outbound.persistence.VehicleRepository;
import com.cdg.fms.fleet.mgmt.domain.entity.Vehicle;
import com.cdg.fms.fleet.mgmt.domain.entity.Vehicle.Status;
import com.cdg.fms.fleet.mgmt.domain.exception.FieldValidationException;
import com.cdg.fms.fleet.mgmt.domain.exception.IllegalStateException;
import com.cdg.fms.fleet.mgmt.domain.exception.ResourceNotFoundException;
import java.util.Optional;
import lombok.val;
import org.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.BDDMockito;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class RejectVehicleUseCaseImplTest {

  @Mock
  private VehicleRepository vehicleRepository;

  @Spy
  private VehicleAuditRepository vehicleAuditRepository;

  @InjectMocks
  private RejectVehicleUseCaseImpl underTest;

  private Vehicle vehicle;

  @BeforeEach
  void setUp() {
    vehicle = Vehicle.builder().id(1L).build();
  }

  @Test
  void givenVehicleWithPendingApprovalStatus_whenReject_thenSuccess() {
    vehicle.setStatus(Status.PENDING_APPROVAL);
    vehicle.setStatusValue(3);

    // given
    BDDMockito.given(vehicleRepository.findByIdIncludeIdAndStatus(1L))
        .willReturn(Optional.of(vehicle));

    BDDMockito.given(vehicleRepository.updateStatus(1, Status.ONBOARDING))
        .willReturn(1);

    // when
    val result = underTest.rejectVehicle(1L, "Some reason");

    // then
    Assertions.assertNotNull(result.getData());
    Assertions.assertEquals(1L, result.getData().getId());
  }

  @Test
  void givenVehicleWithOnBoardingStatus_whenReject_thenThrowException() {
    vehicle.setStatus(Status.ONBOARDING);

    // given
    BDDMockito.given(vehicleRepository.findByIdIncludeIdAndStatus(1L))
        .willReturn(Optional.of(vehicle));

    // then
    Assertions.assertThrowsExactly(IllegalStateException.class,
        () -> underTest.rejectVehicle(1L, "Some reason"));
  }

  @Test
  void givenNonExistingVehicle_whenReject_thenThrowException() {
    vehicle.setStatus(Status.ONBOARDING);

    // given
    BDDMockito.given(vehicleRepository.findByIdIncludeIdAndStatus(2L))
        .willReturn(Optional.empty());

    // then
    Assertions.assertThrowsExactly(ResourceNotFoundException.class,
        () -> underTest.rejectVehicle(2L, "Some reason"));
  }

  @Test
  void givenVehicleWithPendingApprovalStatus_whenRejectWithNoReason_thenThrowException() {
    vehicle.setStatus(Status.PENDING_APPROVAL);
    vehicle.setStatusValue(3);

    // given
    BDDMockito.given(vehicleRepository.findByIdIncludeIdAndStatus(1L))
        .willReturn(Optional.of(vehicle));

    // then
    Assertions.assertThrowsExactly(FieldValidationException.class,
        () -> underTest.rejectVehicle(1L, Strings.EMPTY));
  }
}