|
| 1 | +package com.outfitlab.project.domain.service; |
| 2 | + |
| 3 | +import com.outfitlab.project.domain.exceptions.MarcasNotFoundException; |
| 4 | +import com.outfitlab.project.domain.model.MarcaModel; |
| 5 | +import com.outfitlab.project.domain.useCases.marca.GetAllMarcas; |
| 6 | +import com.outfitlab.project.domain.useCases.marca.GetMarcaByCodigoMarca; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import static org.junit.Assert.*; |
| 14 | +import static org.mockito.Mockito.*; |
| 15 | + |
| 16 | +public class MarcaServiceTest { |
| 17 | + |
| 18 | + private GetAllMarcas getAllMarcasMock; |
| 19 | + private GetMarcaByCodigoMarca getMarcaByCodigoMarcaMock; |
| 20 | + private MarcaService marcaService; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() { |
| 24 | + getAllMarcasMock = mock(GetAllMarcas.class); |
| 25 | + getMarcaByCodigoMarcaMock = mock(GetMarcaByCodigoMarca.class); |
| 26 | + marcaService = new MarcaService(getAllMarcasMock, getMarcaByCodigoMarcaMock); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void getAllMarcasDeberiaDevolverListaDeMarcas_cuandoExistenMarcas() throws Exception, MarcasNotFoundException { |
| 31 | + MarcaModel marca1 = new MarcaModel(); |
| 32 | + marca1.setNombre("Marca1"); |
| 33 | + MarcaModel marca2 = new MarcaModel(); |
| 34 | + marca2.setNombre("Marca2"); |
| 35 | + |
| 36 | + when(getAllMarcasMock.execute()).thenReturn(Arrays.asList(marca1, marca2)); |
| 37 | + |
| 38 | + List<MarcaModel> resultado = null; |
| 39 | + try { |
| 40 | + resultado = marcaService.getAllMarcas(); |
| 41 | + } catch (MarcasNotFoundException e) { |
| 42 | + fail("No se esperaba excepción"); |
| 43 | + } |
| 44 | + |
| 45 | + assertNotNull(resultado); |
| 46 | + assertEquals(2, resultado.size()); |
| 47 | + verify(getAllMarcasMock, times(1)).execute(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void getAllMarcasDeberiaLanzarMarcasNotFoundException_cuandoUseCaseLanzaExcepcion() throws Exception, MarcasNotFoundException { |
| 52 | + when(getAllMarcasMock.execute()).thenThrow(new MarcasNotFoundException("No marcas")); |
| 53 | + |
| 54 | + try { |
| 55 | + marcaService.getAllMarcas(); |
| 56 | + fail("Se esperaba MarcasNotFoundException"); |
| 57 | + } catch (MarcasNotFoundException e) { |
| 58 | + assertEquals("No marcas", e.getMessage()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void getAllMarcasDeberiaLanzarMarcasNotFoundException_cuandoUseCaseLanzaExceptionGenerica() throws Exception, MarcasNotFoundException { |
| 64 | + when(getAllMarcasMock.execute()).thenThrow(new RuntimeException("Error genérico")); |
| 65 | + |
| 66 | + try { |
| 67 | + marcaService.getAllMarcas(); |
| 68 | + fail("Se esperaba MarcasNotFoundException"); |
| 69 | + } catch (MarcasNotFoundException e) { |
| 70 | + assertEquals("Error genérico", e.getMessage()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void getMarcaByCodigoMarcaDeberiaDevolverMarca_cuandoExiste() throws Exception, MarcasNotFoundException { |
| 76 | + String codigo = "MAR001"; |
| 77 | + MarcaModel marca = new MarcaModel(); |
| 78 | + marca.setNombre("Marca1"); |
| 79 | + |
| 80 | + when(getMarcaByCodigoMarcaMock.execute(codigo)).thenReturn(marca); |
| 81 | + |
| 82 | + MarcaModel resultado = null; |
| 83 | + try { |
| 84 | + resultado = marcaService.getMarcaByCodigoMarca(codigo); |
| 85 | + } catch (MarcasNotFoundException e) { |
| 86 | + fail("No se esperaba excepción"); |
| 87 | + } |
| 88 | + |
| 89 | + assertNotNull(resultado); |
| 90 | + assertEquals("Marca1", resultado.getNombre()); |
| 91 | + verify(getMarcaByCodigoMarcaMock, times(1)).execute(codigo); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void getMarcaByCodigoMarcaDeberiaLanzarMarcasNotFoundException_cuandoUseCaseLanzaExcepcion() throws Exception, MarcasNotFoundException { |
| 96 | + String codigo = "MAR002"; |
| 97 | + when(getMarcaByCodigoMarcaMock.execute(codigo)).thenThrow(new MarcasNotFoundException("No encontramos la marca: MAR002")); |
| 98 | + |
| 99 | + try { |
| 100 | + marcaService.getMarcaByCodigoMarca(codigo); |
| 101 | + fail("Se esperaba MarcasNotFoundException"); |
| 102 | + } catch (MarcasNotFoundException e) { |
| 103 | + assertEquals("No encontramos la marca: " + codigo, e.getMessage()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void getMarcaByCodigoMarcaDeberiaLanzarMarcasNotFoundException_cuandoUseCaseLanzaExceptionGenerica() throws Exception, MarcasNotFoundException { |
| 109 | + String codigo = "MAR003"; |
| 110 | + when(getMarcaByCodigoMarcaMock.execute(codigo)).thenThrow(new RuntimeException("Error genérico")); |
| 111 | + |
| 112 | + try { |
| 113 | + marcaService.getMarcaByCodigoMarca(codigo); |
| 114 | + fail("Se esperaba MarcasNotFoundException"); |
| 115 | + } catch (MarcasNotFoundException e) { |
| 116 | + assertEquals("No encontramos la marca: " + codigo, e.getMessage()); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
0 commit comments