Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.2 kB
2
Indexable
Never
package br.com.senior.erpxcomven.pedido.converter.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import br.com.senior.erpxcomven.foundation.E075derEntity;
import br.com.senior.erpxcomven.foundation.repository.E075derRepository;
import br.com.senior.erpxcomven.pedido.E120imiEntity;
import br.com.senior.erpxcomven.pedido.E120ipdEntity;
import br.com.senior.erpxcomven.pedido.Item;
import br.com.senior.erpxcomven.pedido.Tax;

public class ConvertItemTest {

    @InjectMocks
    private ConvertItem itemConverter;
    
    @Mock
    private E075derRepository productRepository;
    @Mock
    private ConvertTax taxConverter;

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

    @Test
    public void testConvertShouldReturnNullWhenItemIsNull() {
        Item item = itemConverter.convert(null);
        assertNull(item);
    }

    @Test
    public void testConvertShouldReturnCorrectItemDto() {
        E075derEntity product = new E075derEntity();
        product.setId(UUID.randomUUID());
        product.setCodDer("PRODUCT001");
        Mockito.when(productRepository.findById(Mockito.any())).thenReturn(Optional.of(product));
        
        E120ipdEntity item = new E120ipdEntity();
        item.setE075der(UUID.randomUUID());
        item.setQtdPed(BigDecimal.TEN);
        item.setPreUni(BigDecimal.valueOf(15.5));
        item.setVlrDsc(BigDecimal.valueOf(2.5));
        item.setVlrLiq(BigDecimal.valueOf(135.0));
        item.setDatEnt("2022-05-20");
        item.setObsIpd("Test observation");
        
        E120imiEntity taxEntity = new E120imiEntity();
        taxEntity.setCodTri("001");
        taxEntity.setAliImi(BigDecimal.TEN);
        taxEntity.setVlrBas(BigDecimal.valueOf(100.0));
        List<E120imiEntity> taxesEntityList = new ArrayList<>();
        taxesEntityList.add(taxEntity);
        item.setE120imi(taxesEntityList);
        
        Tax taxDto = new Tax();
        taxDto.code = taxEntity.getCodTri();
        taxDto.percentage = taxEntity.getAliImi();
        taxDto.baseValue = taxEntity.getVlrBas();
        Mockito.when(taxConverter.convert(Mockito.any())).thenReturn(taxDto);
        
        Item itemDto = itemConverter.convert(item);

        assertEquals(product.getId().toString(), itemDto.id);
        assertEquals(product.getCodDer(), itemDto.code);
        assertEquals(item.getQtdPed(), itemDto.quantity);
        assertEquals(item.getPreUni(), itemDto.price);
        assertEquals(item.getVlrDsc(), itemDto.discount);
        assertEquals(item.getVlrLiq(), itemDto.total);
        assertEquals(item.getDatEnt(), itemDto.expectedDeliveryDate);
        assertEquals(item.getObsIpd(), itemDto.observation);
        assertEquals(1, itemDto.taxes.size());
        assertEquals(taxDto, itemDto.taxes.get(0));
    }
}