Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
2
Indexable
Never
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
public class ProductImageServiceImplTest {

    @Mock
    private E075derBaseRepository e075derBaseRepositoryMock;
    @Mock
    private E075derImageBaseRepository e075derImageBaseRepositoryMock;
    @Mock
    private TranslationHubApi translationMock;
    @Mock
    private BlobService blobServiceMock;

    @InjectMocks
    private ProductImageServiceImpl productImageService;

    private UUID validProductId;
    private String validTargetObjectId;
    private String validFileName;
    private Integer validVersion;

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

        validProductId = UUID.randomUUID();
        validTargetObjectId = "targetObjectId";
        validFileName = "fileName";
        validVersion = 1;
    }

    @Test
    public void testPersistProductImage() throws Exception {
        // Arrange
        PersistProductImageInput input = new PersistProductImageInput();
        input.productId = validProductId.toString();
        input.targetObjectId = validTargetObjectId;
        input.fileName = validFileName;
        input.version = validVersion;

        E075derEntity productMock = mock(E075derEntity.class);
        E075derImageEntity imageMock = mock(E075derImageEntity.class);

        when(e075derBaseRepositoryMock.findById(validProductId)).thenReturn(Optional.of(productMock));
        when(e075derImageBaseRepositoryMock.save(any(E075derImageEntity.class))).thenReturn(imageMock);

        // Act
        productImageService.persistProductImage(input);

        // Assert
        verify(e075derBaseRepositoryMock).findById(validProductId);
        verify(e075derImageBaseRepositoryMock).save(any(E075derImageEntity.class));
        verify(blobServiceMock).commitFile(validFileName, validVersion, validTargetObjectId);
    }

    @Test(expected = ServiceException.class)
    public void testPersistProductImageWithInvalidProductId() throws Exception {
        // Arrange
        PersistProductImageInput input = new PersistProductImageInput();
        input.productId = validProductId.toString();
        input.targetObjectId = validTargetObjectId;
        input.fileName = validFileName;
        input.version = validVersion;

        when(e075derBaseRepositoryMock.findById(validProductId)).thenReturn(Optional.empty());
        when(translationMock.getMessage(TranslationConstants.PRODUTO_IMAGEM_NAO_ECONTRADO)).thenReturn("Produto não encontrado");

        // Act
        productImageService.persistProductImage(input);

        // Assert
        fail("ServiceException expected but not thrown");
    }