Untitled

 avatar
unknown
plain_text
6 months ago
4.3 kB
3
Indexable
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Optional;

class LexisNexisValidationCommandHandlerTest {

    @InjectMocks
    private LexisNexisValidationCommandHandler handler;

    @Mock
    private MotorOffersRepository motorOffersRepository;

    @Mock
    private GetOriginationDetailsQueryHandler getOriginationDetailsQueryHandler;

    @Mock
    private UpdateOffersQueryHandler updateOffersQueryHandler;

    @Mock
    private PolicyAcceptFailEventCommandHandler policyAcceptFailEventCommandHandler;

    @Mock
    private PolicyAcceptedEventCommandHandler policyAcceptedEventCommandHandler;

    @Mock
    private PolicyAcceptedInternalEventCommandHandler policyAcceptedInternalEventCommandHandler;

    private RequestContext requestContext;
    private LexisNexisValidationCommand command;
    private MotorOffersEntity motorOffersEntity;
    private MotorInsuranceOriginationResponse originationResponse;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);

        requestContext = new RequestContext(); // Mock or initialize as needed
        command = new LexisNexisValidationCommand();
        motorOffersEntity = new MotorOffersEntity();
        motorOffersEntity.setOfferId("test-offer-id");
        motorOffersEntity.setOriginationId("test-origination-id");
        motorOffersEntity.setApplicationId("test-application-id");
        motorOffersEntity.setCustomerId("test-customer-id");

        originationResponse = new MotorInsuranceOriginationResponse();
    }

    @Test
    void handle_shouldReturnFailure_whenOriginationQueryHasError() {
        // Arrange
        command.setOfferId("test-offer-id");
        when(motorOffersRepository.findById("test-offer-id")).thenReturn(Optional.of(motorOffersEntity));
        QueryResult<MotorInsuranceOriginationResponse> originationQueryResult = mock(QueryResult.class);
        when(getOriginationDetailsQueryHandler.handleDecorators(any(), eq(requestContext)))
            .thenReturn(originationQueryResult);
        when(originationQueryResult.hasError()).thenReturn(true);

        // Act
        CommandResult result = handler.handle(command, requestContext);

        // Assert
        assertNotNull(result);
        assertTrue(result.hasFailures());
        verify(policyAcceptFailEventCommandHandler, never()).handleDecorators(any(), any());
    }

    @Test
    void handle_shouldHandleException_whenRetryFails() {
        // Arrange
        command.setOfferId("test-offer-id");
        when(motorOffersRepository.findById("test-offer-id")).thenReturn(Optional.of(motorOffersEntity));
        when(getOriginationDetailsQueryHandler.handleDecorators(any(), eq(requestContext)))
            .thenReturn(new QueryResult<>(originationResponse));

        doThrow(new RuntimeException("Test Exception"))
            .when(updateOffersQueryHandler).handleDecorators(any(), eq(requestContext));

        // Act
        CommandResult result = handler.handle(command, requestContext);

        // Assert
        assertNull(result);
        verify(policyAcceptFailEventCommandHandler, times(1)).handleDecorators(any(), eq(requestContext));
    }

    @Test
    void handle_shouldPublishEvent_whenSuccessful() {
        // Arrange
        command.setOfferId("test-offer-id");
        when(motorOffersRepository.findById("test-offer-id")).thenReturn(Optional.of(motorOffersEntity));
        when(getOriginationDetailsQueryHandler.handleDecorators(any(), eq(requestContext)))
            .thenReturn(new QueryResult<>(originationResponse));
        // Mock behavior for retry success
        when(updateOffersQueryHandler.handleDecorators(any(), eq(requestContext)))
            .thenReturn(new QueryResult<>(new QuotationResponse()));

        // Act
        CommandResult result = handler.handle(command, requestContext);

        // Assert
        assertNull(result);
        verify(policyAcceptedInternalEventCommandHandler, times(1)).handleDecorators(any(), eq(requestContext));
    }
}
Editor is loading...
Leave a Comment