Untitled

mail@pastecode.io avatar
unknown
plain_text
9 days ago
5.9 kB
2
Indexable
Never
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 RSADocumentUploadHandlerTest {

    @Mock
    private TravelOffersRepository travelOfferRepository;
    
    @Mock
    private HomeOffersRepository homeOffersRepository;
    
    @Mock
    private MaktabaMapper maktabaMapper;
    
    @Mock
    private GetEidImagesQueryHandler getEidImagesQueryHandler;
    
    @Mock
    private UploadDocumentQueryHandler uploadDocumentQueryHandler;
    
    @Mock
    private UploadHomeDocumentQueryHandler uploadHomeDocumentQueryHandler;
    
    @Mock
    private CustomerDetailsAPIHandler customerDetailsAPIHandler;
    
    @Mock
    private DocumentActionCompletedEventCommandHandler documentActionCompletedEventCommandHandler;
    
    @Mock
    private DocumentActionFailedEventCommandHandler documentActionFailedEventCommandHandler;
    
    @Mock
    private DocumentActionCompletedInternalEventCommandHandler documentActionCompletedInternalEventCommandHandler;
    
    @Mock
    private QuoteRepository quoteRepository;
    
    @InjectMocks
    private RSADocumentUploadHandler rsaDocumentUploadHandler;

    @BeforeEach
    void setup() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    void testProcessRSADocumentUpload_Success() throws Exception {
        // Arrange
        DocumentUploadRequest documentUploadRequest = mock(DocumentUploadRequest.class);
        RequestContext context = mock(RequestContext.class);
        QuoteEntity quoteEntity = mock(QuoteEntity.class);
        Optional<QuoteEntity> optionalQuoteEntity = Optional.of(quoteEntity);

        when(documentUploadRequest.getApplicationId()).thenReturn("app123");
        when(documentUploadRequest.getQuotationId()).thenReturn(123L);
        when(documentUploadRequest.getProductCode()).thenReturn("TRAVEL");

        // Mocking the quote repository call
        when(quoteRepository.findByQuotationIdAndApplicationId(123L, "app123")).thenReturn(optionalQuoteEntity);

        // Mocking customerDetailsAPIHandler response
        CustomerDetailsResponse customerDetailsResponse = new CustomerDetailsResponse();
        QueryResult<CustomerDetailsResponse> customerDetailsQueryResult = new QueryResult<>();
        customerDetailsQueryResult.setResult(customerDetailsResponse);

        when(customerDetailsAPIHandler.handleDecorators(any(), eq(context))).thenReturn(customerDetailsQueryResult);

        // Mocking Maktaba API to return EIDA images
        MaktabaGetResults maktabaGetResults = mock(MaktabaGetResults.class);
        QueryResult<MaktabaGetResults> eidImagesResult = new QueryResult<>();
        when(getEidImagesQueryHandler.handleDecorators(any(), eq(context))).thenReturn(eidImagesResult);

        // Act
        UploadDocumentResponse response = rsaDocumentUploadHandler.proceesRSADocumentUpload(documentUploadRequest, context);

        // Assert
        assertNull(response); // Modify this based on actual return type
        verify(quoteRepository, times(1)).findByQuotationIdAndApplicationId(123L, "app123");
        verify(customerDetailsAPIHandler, times(1)).handleDecorators(any(), eq(context));
        verify(getEidImagesQueryHandler, times(2)).handleDecorators(any(), eq(context));  // For front and back
    }

    @Test
    void testProcessRSADocumentUpload_MissingEidaImages() throws Exception {
        // Arrange
        DocumentUploadRequest documentUploadRequest = mock(DocumentUploadRequest.class);
        RequestContext context = mock(RequestContext.class);
        QuoteEntity quoteEntity = mock(QuoteEntity.class);
        Optional<QuoteEntity> optionalQuoteEntity = Optional.of(quoteEntity);

        when(documentUploadRequest.getApplicationId()).thenReturn("app123");
        when(documentUploadRequest.getQuotationId()).thenReturn(123L);
        when(documentUploadRequest.getProductCode()).thenReturn("TRAVEL");

        when(quoteRepository.findByQuotationIdAndApplicationId(123L, "app123")).thenReturn(optionalQuoteEntity);

        CustomerDetailsResponse customerDetailsResponse = new CustomerDetailsResponse();
        QueryResult<CustomerDetailsResponse> customerDetailsQueryResult = new QueryResult<>();
        customerDetailsQueryResult.setResult(customerDetailsResponse);
        
        when(customerDetailsAPIHandler.handleDecorators(any(), eq(context))).thenReturn(customerDetailsQueryResult);

        MaktabaGetResults maktabaGetResults = mock(MaktabaGetResults.class);
        QueryResult<MaktabaGetResults> eidImagesResult = new QueryResult<>();
        when(getEidImagesQueryHandler.handleDecorators(any(), eq(context))).thenReturn(eidImagesResult);

        // Simulating missing EIDA images
        Optional<MaktabaResult> emptyOptional = Optional.empty();

        when(maktabaMapper.buildGetEidNameByEIDANumber(anyString(), anyString())).thenReturn(mock(GetMaktabaEIDAImagesQuery.class));
        when(getEidImagesQueryHandler.handleDecorators(any(GetMaktabaEIDAImagesQuery.class), eq(context))).thenReturn(eidImagesResult);

        // Act & Assert: Expect the code to throw a FrameworkException when EIDA images are missing
        FrameworkException thrown = assertThrows(
                FrameworkException.class,
                () -> rsaDocumentUploadHandler.proceesRSADocumentUpload(documentUploadRequest, context),
                "Expected processRSADocumentUpload to throw FrameworkException when images are missing"
        );

        assertEquals(InsuranceFulfillmentConstants.MSG.EID_FRONT_BACK_MISSING, thrown.getMessage());
        verify(getEidImagesQueryHandler, times(2)).handleDecorators(any(), eq(context));  // Front and back image calls
    }
}
Leave a Comment