Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
3
Indexable
Never
package br.com.senior.erpxfnd.produto.service;

import java.util.Optional;
import java.util.UUID;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

import br.com.senior.erpxfnd.produto.E075derImageBaseRepository;
import br.com.senior.erpxfnd.produto.E075derImageEntity;
import br.com.senior.messaging.ErrorCategory;
import br.com.senior.messaging.model.ServiceException;
import br.com.senior.platform.blobservice.BlobDetails;
import br.com.senior.platform.translationhub.api.TranslationHubApi;
import br.com.senior.utils.TranslationConstants;

@RunWith(MockitoJUnitRunner.class)
public class DeleteProductImageServiceImplTest {

    @Mock
    private E075derImageBaseRepository e075derImageBaseRepository;

    @Mock
    private TranslationHubApi translation;

    @Mock
    private BlobService blobService;

    @InjectMocks
    private DeleteProductImageServiceImpl service;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test(expected = ServiceException.class)
    public void shouldThrowExceptionWhenImageNotFound() {
        String productId = UUID.randomUUID().toString();
        when(e075derImageBaseRepository.findById(any(UUID.class))).thenReturn(Optional.empty());
        when(translation.getMessage(any(String.class))).thenReturn("Imagem não encontrada para o produto.");
        service.deleteProductImage(productId);
    }

    @Test(expected = ServiceException.class)
    public void shouldThrowExceptionWhenDeleteFileFails() {
        String productId = UUID.randomUUID().toString();
        E075derImageEntity productImage = new E075derImageEntity();
        productImage.setFileName("product.jpg");
        productImage.setTargetObjectId(UUID.randomUUID());
        when(e075derImageBaseRepository.findById(any(UUID.class))).thenReturn(Optional.of(productImage));
        when(translation.getMessage(any(String.class))).thenReturn("Erro ao excluir a imagem do produto.");
        when(blobService.deleteFile(any(BlobDetails.class))).thenReturn(false);
        service.deleteProductImage(productId);
    }

    @Test
    public void shouldDeleteImageSuccessfully() {
        String productId = UUID.randomUUID().toString();
        E075derImageEntity productImage = new E075derImageEntity();
        productImage.setFileName("product.jpg");
        productImage.setTargetObjectId(UUID.randomUUID());
        when(e075derImageBaseRepository.findById(any(UUID.class))).thenReturn(Optional.of(productImage));
        when(blobService.deleteFile(any(BlobDetails.class))).thenReturn(true);
        service.deleteProductImage(productId);
        verify(e075derImageBaseRepository).delete(productImage);
    }

}