From 27fa5053cb5d1cb779263dd3d53ae435d513d343 Mon Sep 17 00:00:00 2001 From: Kleber Cardoso Date: Wed, 5 Jul 2023 20:45:42 -0400 Subject: [PATCH] testes de camadas --- .../controller/PtoAjusteController.java | 4 +- .../controller/PtoEventoController.java | 6 +- .../tangerino/service/PtoAjusteService.java | 15 ++- .../service/PtoEquipamentoService.java | 13 +- .../tangerino/service/PtoEventoService.java | 15 ++- src/main/resources/application-dev.yml | 2 +- .../controller/PtoEventoControllerTest.java | 15 ++- .../tangerino/model/PtoEventoModelTest.java | 9 +- .../repository/PtoAjusteRepositoryTest.java | 80 ++++++++++++ .../PtoEquipamentoRepositoryTest.java | 84 +++++++++++++ .../repository/PtoEventoRepositoryTest.java | 85 +++++++++++++ .../service/PtoAjusteServiceTest.java | 104 ++++++++++++++++ .../service/PtoEquipamentoServiceTest.java | 109 +++++++++++++++++ .../service/PtoEventoServiceTest.java | 115 ++++++++++++++++++ 14 files changed, 641 insertions(+), 15 deletions(-) create mode 100644 src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoAjusteRepositoryTest.java create mode 100644 src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoEquipamentoRepositoryTest.java create mode 100644 src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoEventoRepositoryTest.java create mode 100644 src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoAjusteServiceTest.java create mode 100644 src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEquipamentoServiceTest.java create mode 100644 src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEventoServiceTest.java diff --git a/src/main/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoAjusteController.java b/src/main/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoAjusteController.java index 92fbda0..f4cc60d 100644 --- a/src/main/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoAjusteController.java +++ b/src/main/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoAjusteController.java @@ -60,7 +60,7 @@ public class PtoAjusteController { } // ao final, atualiza a ptoEquipamento com o ultimoNSR salvo eq.setUltimoNsr(lastNsr.get()); - equipamentoService.saveOrUpdate(eq); + equipamentoService.save(eq); } @@ -68,7 +68,7 @@ public class PtoAjusteController { AtomicReference nsr = new AtomicReference<>(lastNsr); ptoEventoModels.stream().sorted(Comparator.comparing(PtoAjusteModel::getNsr)).forEach(x -> { if (x.getNsr() > lastNsr) { - ajusteService.saveOrUpdate(x); + ajusteService.save(x); nsr.set(x.getNsr()); } }); diff --git a/src/main/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoEventoController.java b/src/main/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoEventoController.java index f48bfac..f1acccf 100644 --- a/src/main/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoEventoController.java +++ b/src/main/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoEventoController.java @@ -75,17 +75,17 @@ public class PtoEventoController { } // ao final, atualiza a ptoEquipamento com o ultimoNSR salvo eq.setUltimoNsr(lastNsr.get()); - equipamentoService.saveOrUpdate(eq); + equipamentoService.save(eq); }); } - private Integer extrairEventos(Integer lastNsr, List ptoEventoModels, PtoEquipamentoModel equipamentoModel) { + public Integer extrairEventos(Integer lastNsr, List ptoEventoModels, PtoEquipamentoModel equipamentoModel) { AtomicReference nsr = new AtomicReference<>(0); AtomicInteger nsrForEach = new AtomicInteger(lastNsr); ptoEventoModels.stream().sorted(Comparator.comparing(PtoEventoModel::getNsr)).forEach(x -> { if (x.getNsr() > lastNsr) { // tratando para nao duplicar os nsr - eventoService.saveOrUpdate(x); + eventoService.save(x); nsr.set(x.getNsr()); if(x.getNsr() - nsrForEach.get() > 1){ //se a diferença entre o ultimo nsr e o atual for maior que um, chamará o Ajustes PtoAjusteController ajusteController = new PtoAjusteController(equipamentoService, ajusteService); diff --git a/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoAjusteService.java b/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoAjusteService.java index e9a5624..67fa407 100644 --- a/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoAjusteService.java +++ b/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoAjusteService.java @@ -1,19 +1,30 @@ package br.com.ae3tecnologia.ms.tangerino.service; import br.com.ae3tecnologia.ms.tangerino.model.PtoAjusteModel; -import br.com.ae3tecnologia.ms.tangerino.model.PtoEventoModel; import br.com.ae3tecnologia.ms.tangerino.repository.PtoAjusteRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.Optional; + @Service public class PtoAjusteService { @Autowired private PtoAjusteRepository repository; - public PtoAjusteModel saveOrUpdate(PtoAjusteModel model){ + public PtoAjusteModel save(PtoAjusteModel model){ return repository.save(model); } + public List findAll(){ + return repository.findAll(); + } + + public PtoAjusteModel findById(int id) { + Optional optional = repository.findById(id); + return optional.isPresent() ? optional.get() : null; + } + } diff --git a/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEquipamentoService.java b/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEquipamentoService.java index 8437f30..c49ac5c 100644 --- a/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEquipamentoService.java +++ b/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEquipamentoService.java @@ -1,7 +1,6 @@ package br.com.ae3tecnologia.ms.tangerino.service; import br.com.ae3tecnologia.ms.tangerino.model.PtoEquipamentoModel; -import br.com.ae3tecnologia.ms.tangerino.model.PtoEventoModel; import br.com.ae3tecnologia.ms.tangerino.repository.PtoEquipamentoRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -23,7 +22,17 @@ public class PtoEquipamentoService { return Arrays.asList(); } - public PtoEquipamentoModel saveOrUpdate(PtoEquipamentoModel equipamentoModel){ + public PtoEquipamentoModel save(PtoEquipamentoModel equipamentoModel){ return repository.save(equipamentoModel); } + + public List findAll(){ + return repository.findAll(); + } + + public PtoEquipamentoModel findById(int id){ + Optional optional = repository.findById(id); + return optional.isPresent() ? optional.get() : null; + } + } diff --git a/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEventoService.java b/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEventoService.java index a3ba8f8..f4aa73c 100644 --- a/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEventoService.java +++ b/src/main/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEventoService.java @@ -5,14 +5,27 @@ import br.com.ae3tecnologia.ms.tangerino.repository.PtoEventoRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + @Service public class PtoEventoService { @Autowired private PtoEventoRepository repository; - public PtoEventoModel saveOrUpdate(PtoEventoModel eventoModel){ + public PtoEventoModel save(PtoEventoModel eventoModel){ return repository.save(eventoModel); } + public List findAll(){ + return repository.findAll(); + } + + public PtoEventoModel findById(int id){ + Optional optional = repository.findById(id); + return optional.isPresent() ? optional.get() : null; + } + } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 5a7d5ae..319435c 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -26,4 +26,4 @@ spring: datasource: url: jdbc:postgresql://localhost:5432/sisgep username: postgres - password: 123123 + password: Postgres2018! diff --git a/src/test/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoEventoControllerTest.java b/src/test/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoEventoControllerTest.java index c4632f6..b783c3f 100644 --- a/src/test/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoEventoControllerTest.java +++ b/src/test/java/br/com/ae3tecnologia/ms/tangerino/controller/PtoEventoControllerTest.java @@ -1,5 +1,6 @@ package br.com.ae3tecnologia.ms.tangerino.controller; +import br.com.ae3tecnologia.ms.tangerino.model.PtoEquipamentoModel; import br.com.ae3tecnologia.ms.tangerino.model.PtoEventoModel; import br.com.ae3tecnologia.ms.tangerino.service.PtoEquipamentoService; import br.com.ae3tecnologia.ms.tangerino.service.PtoEventoService; @@ -28,7 +29,7 @@ class PtoEventoControllerTest { } @Test - void testExtrairPontos() { + void deveExtrairPontos() { controller.extrairPontos(); } @@ -41,6 +42,18 @@ class PtoEventoControllerTest { List ptoEventoModels = controller.obterPontosPorRelogio(map); Assertions.assertTrue(ptoEventoModels.size() > 0); } + + @Test + void deveExtrairEventos(){ + + } + + @Test + void deveObterRelogios(){ + List ptoEquipamentoModels = controller.obterRelogios(false); + Assertions.assertNotEquals(null, ptoEquipamentoModels); + Assertions.assertFalse(ptoEquipamentoModels.isEmpty()); + } } //Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme \ No newline at end of file diff --git a/src/test/java/br/com/ae3tecnologia/ms/tangerino/model/PtoEventoModelTest.java b/src/test/java/br/com/ae3tecnologia/ms/tangerino/model/PtoEventoModelTest.java index 589bf1c..dfee126 100644 --- a/src/test/java/br/com/ae3tecnologia/ms/tangerino/model/PtoEventoModelTest.java +++ b/src/test/java/br/com/ae3tecnologia/ms/tangerino/model/PtoEventoModelTest.java @@ -1,5 +1,6 @@ package br.com.ae3tecnologia.ms.tangerino.model; +import jakarta.persistence.Column; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -7,6 +8,7 @@ import org.mockito.InjectMocks; import org.mockito.MockitoAnnotations; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.LocalTime; class PtoEventoModelTest { @@ -14,17 +16,18 @@ class PtoEventoModelTest { PtoEventoModel ptoEventoModel; PtoEventoModel other = new PtoEventoModel( - 123, + 1, 123, 1, LocalDate.now(), LocalTime.now(), - "default", + "123123123123", 123, 123, false, 1, - "202304" + "202304", + LocalDateTime.now() ); @BeforeEach diff --git a/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoAjusteRepositoryTest.java b/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoAjusteRepositoryTest.java new file mode 100644 index 0000000..e37a80f --- /dev/null +++ b/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoAjusteRepositoryTest.java @@ -0,0 +1,80 @@ +package br.com.ae3tecnologia.ms.tangerino.repository; + +import br.com.ae3tecnologia.ms.tangerino.model.PtoAjusteModel; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.Optional; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +public class PtoAjusteRepositoryTest { + + @Autowired + private TestEntityManager em; + + @Autowired + private PtoAjusteRepository repository; + + private PtoAjusteModel obj = null; + + @BeforeEach + void setUp(){ + obj = new PtoAjusteModel( + 123, + 123, + 1, + LocalDate.now(), + LocalDate.now(), + LocalTime.now().minusHours(10), + LocalTime.now(), + 2, + "202304" + ); + } + + @Test + public void contextLoads() { + Assertions.assertNotNull(em); + } + + @Test + public void mustFindById(){ + obj = repository.save(obj); + Assertions.assertNotEquals(null, obj); + Assertions.assertTrue(obj.getId() > 0); + int id = obj.getId(); + Optional recover = repository.findById(id); + Assertions.assertTrue(recover.isPresent()); + Assertions.assertFalse(recover.isEmpty()); + } + + @Test + public void mustSaveObject(){ + obj = repository.save(obj); + Assertions.assertNotEquals(null, obj); + Assertions.assertTrue(obj.getId() > 0); + } + + @Test + public void mustDeleteObject(){ + obj = repository.save(obj); + Assertions.assertNotEquals(null, obj); + Assertions.assertTrue(obj.getId() > 0); + int id = obj.getId(); + + repository.delete(obj); + Optional recover = repository.findById(id); + Assertions.assertFalse(recover.isPresent()); + Assertions.assertTrue(recover.isEmpty()); + } + +} diff --git a/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoEquipamentoRepositoryTest.java b/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoEquipamentoRepositoryTest.java new file mode 100644 index 0000000..fd5b3fd --- /dev/null +++ b/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoEquipamentoRepositoryTest.java @@ -0,0 +1,84 @@ +package br.com.ae3tecnologia.ms.tangerino.repository; + +import br.com.ae3tecnologia.ms.tangerino.model.PtoEquipamentoModel; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; + +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Optional; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +public class PtoEquipamentoRepositoryTest { + + @Autowired + private TestEntityManager em; + + @Autowired + private PtoEquipamentoRepository repository; + + @Test + public void contextLoads() { + Assertions.assertNotNull(em); + } + + @Test + public void mustFindByInativoEqualsFalse(){ + Optional> list = repository.findByInativoEquals(false); + Assertions.assertNotEquals(null, list); + Assertions.assertFalse(list.isEmpty()); + } + + @Test + public void mustFindById(){ + Optional recover = repository.findById(2); + Assertions.assertTrue(recover.isPresent()); + Assertions.assertFalse(recover.isEmpty()); + } + + @Test + public void mustSaveObject(){ + PtoEquipamentoModel obj = new PtoEquipamentoModel( + 123, + "codigo", + 1, + 1, + GregorianCalendar.getInstance().getTime(), + null, + "default", + true + ); + obj = repository.save(obj); + Assertions.assertNotEquals(null, obj); + Assertions.assertTrue(obj.getId() > 0); + } + + @Test + public void mustDeleteObject(){ + PtoEquipamentoModel obj = new PtoEquipamentoModel( + 123, + "codigo", + 1, + 1, + GregorianCalendar.getInstance().getTime(), + null, + "default", + true + ); + obj = repository.save(obj); + Assertions.assertNotEquals(null, obj); + Assertions.assertTrue(obj.getId() > 0); + int id = obj.getId(); + + repository.delete(obj); + Optional recover = repository.findById(id); + Assertions.assertFalse(recover.isPresent()); + Assertions.assertTrue(recover.isEmpty()); + } + +} diff --git a/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoEventoRepositoryTest.java b/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoEventoRepositoryTest.java new file mode 100644 index 0000000..5fe1d6a --- /dev/null +++ b/src/test/java/br/com/ae3tecnologia/ms/tangerino/repository/PtoEventoRepositoryTest.java @@ -0,0 +1,85 @@ +package br.com.ae3tecnologia.ms.tangerino.repository; + +import br.com.ae3tecnologia.ms.tangerino.model.PtoEventoModel; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.GregorianCalendar; +import java.util.Optional; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +public class PtoEventoRepositoryTest { + + @Autowired + private TestEntityManager em; + + @Autowired + private PtoEventoRepository repository; + + private PtoEventoModel obj = null; + + @BeforeEach + void setUp(){ + obj = new PtoEventoModel( + 1, + 123, + 1, + LocalDate.now(), + LocalTime.now(), + "123123123123", + 123, + 123, + false, + 2, + "202304", + LocalDateTime.now() + ); + } + + @Test + public void contextLoads() { + Assertions.assertNotNull(em); + } + + @Test + public void mustFindById(){ + obj = repository.save(obj); + Assertions.assertNotEquals(null, obj); + Assertions.assertTrue(obj.getId() > 0); + int id = obj.getId(); + Optional recover = repository.findById(id); + Assertions.assertTrue(recover.isPresent()); + Assertions.assertFalse(recover.isEmpty()); + } + + @Test + public void mustSaveObject(){ + obj = repository.save(obj); + Assertions.assertNotEquals(null, obj); + Assertions.assertTrue(obj.getId() > 0); + } + + @Test + public void mustDeleteObject(){ + obj = repository.save(obj); + Assertions.assertNotEquals(null, obj); + Assertions.assertTrue(obj.getId() > 0); + int id = obj.getId(); + + repository.delete(obj); + Optional recover = repository.findById(id); + Assertions.assertFalse(recover.isPresent()); + Assertions.assertTrue(recover.isEmpty()); + } + +} diff --git a/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoAjusteServiceTest.java b/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoAjusteServiceTest.java new file mode 100644 index 0000000..896e264 --- /dev/null +++ b/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoAjusteServiceTest.java @@ -0,0 +1,104 @@ +package br.com.ae3tecnologia.ms.tangerino.service; + + +import br.com.ae3tecnologia.ms.tangerino.model.PtoAjusteModel; +import br.com.ae3tecnologia.ms.tangerino.repository.PtoAjusteRepository; +import br.com.ae3tecnologia.ms.tangerino.repository.PtoEventoRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class PtoAjusteServiceTest { + + @InjectMocks + PtoAjusteService service; + + @Mock + PtoAjusteRepository repository; + + private PtoAjusteModel obj01; + private PtoAjusteModel obj02; + private PtoAjusteModel obj03; + + @BeforeEach + void setUp() { + obj01 = new PtoAjusteModel( + 123, + 123, + 1, + LocalDate.now(), + LocalDate.now(), + LocalTime.now().minusHours(10), + LocalTime.now(), + 1, + "202304" + ); + obj02 = new PtoAjusteModel( + 124, + 123, + 1, + LocalDate.now(), + LocalDate.now(), + LocalTime.now().minusHours(10), + LocalTime.now(), + 1, + "202304" + ); + obj03 = new PtoAjusteModel( + 125, + 123, + 1, + LocalDate.now(), + LocalDate.now(), + LocalTime.now().minusHours(10), + LocalTime.now(), + 1, + "202304" + ); + } + + @Test + public void mustFindAll() { + List list = new ArrayList<>(); + list.add(obj01); + list.add(obj02); + list.add(obj03); + + when(repository.findAll()).thenReturn(list); + + //test + List empList = service.findAll(); + + assertEquals(3, empList.size()); + verify(repository, times(1)).findAll(); + } + + @Test + public void mustFindById(){ + when(repository.findById(1)).thenReturn(Optional.of(obj01)); + PtoAjusteModel empList = service.findById(1); + assertTrue(empList != null); + verify(repository, times(1)).findById(1); + } + + @Test + public void mustSave() { + service.save(obj01); + verify(repository, times(1)).save(obj01); + } +} diff --git a/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEquipamentoServiceTest.java b/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEquipamentoServiceTest.java new file mode 100644 index 0000000..bf73ff9 --- /dev/null +++ b/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEquipamentoServiceTest.java @@ -0,0 +1,109 @@ +package br.com.ae3tecnologia.ms.tangerino.service; + + +import br.com.ae3tecnologia.ms.tangerino.model.PtoEquipamentoModel; +import br.com.ae3tecnologia.ms.tangerino.repository.PtoEquipamentoRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.ArrayList; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class PtoEquipamentoServiceTest { + + @InjectMocks + PtoEquipamentoService service; + + @Mock + PtoEquipamentoRepository repository; + + private PtoEquipamentoModel obj01; + private PtoEquipamentoModel obj02; + private PtoEquipamentoModel obj03; + + @BeforeEach + void setUp() { + obj01 = new PtoEquipamentoModel( + 123, + "codigo", + 1, + 1, + GregorianCalendar.getInstance().getTime(), + null, + "default", + true + ); + obj02 = new PtoEquipamentoModel( + 124, + "codigo", + 1, + 1, + GregorianCalendar.getInstance().getTime(), + null, + "default", + true + ); + obj03 = new PtoEquipamentoModel( + 123, + "codigo", + 1, + 1, + GregorianCalendar.getInstance().getTime(), + null, + "default", + true + ); + } + + @Test + public void mustFindAll() { + List list = new ArrayList<>(); + list.add(obj01); + list.add(obj02); + list.add(obj03); + + when(repository.findAll()).thenReturn(list); + + //test + List empList = service.findAll(); + + assertEquals(3, empList.size()); + verify(repository, times(1)).findAll(); + } + + @Test + public void mustFindByInativo(){ + List list = new ArrayList<>(); + list.add(obj01); + when(repository.findByInativoEquals(false)).thenReturn(Optional.of(list)); + //test + List empList = service.findByInativo(false); + assertEquals(1, empList.size()); + verify(repository, times(1)).findByInativoEquals(false); + } + + @Test + public void mustFindById(){ + when(repository.findById(1)).thenReturn(Optional.of(obj01)); + PtoEquipamentoModel empList = service.findById(1); + assertTrue(empList != null); + verify(repository, times(1)).findById(1); + } + + @Test + public void mustSave() { + service.save(obj01); + verify(repository, times(1)).save(obj01); + } +} diff --git a/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEventoServiceTest.java b/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEventoServiceTest.java new file mode 100644 index 0000000..96821ed --- /dev/null +++ b/src/test/java/br/com/ae3tecnologia/ms/tangerino/service/PtoEventoServiceTest.java @@ -0,0 +1,115 @@ +package br.com.ae3tecnologia.ms.tangerino.service; + + +import br.com.ae3tecnologia.ms.tangerino.model.PtoEquipamentoModel; +import br.com.ae3tecnologia.ms.tangerino.model.PtoEventoModel; +import br.com.ae3tecnologia.ms.tangerino.repository.PtoEquipamentoRepository; +import br.com.ae3tecnologia.ms.tangerino.repository.PtoEventoRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.ArrayList; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class PtoEventoServiceTest { + + @InjectMocks + PtoEventoService service; + + @Mock + PtoEventoRepository repository; + + private PtoEventoModel obj01; + private PtoEventoModel obj02; + private PtoEventoModel obj03; + + @BeforeEach + void setUp() { + obj01 = new PtoEventoModel( + 1, + 123, + 1, + LocalDate.now(), + LocalTime.now(), + "123123123123", + 123, + 123, + false, + 1, + "202304", + LocalDateTime.now() + ); + obj02 = new PtoEventoModel( + 2, + 123, + 1, + LocalDate.now(), + LocalTime.now(), + "123123123123", + 123, + 123, + false, + 1, + "202304", + LocalDateTime.now() + ); + obj03 = new PtoEventoModel( + 3, + 123, + 1, + LocalDate.now(), + LocalTime.now(), + "123123123123", + 123, + 123, + false, + 1, + "202304", + LocalDateTime.now() + ); + } + + @Test + public void mustFindAll() { + List list = new ArrayList<>(); + list.add(obj01); + list.add(obj02); + list.add(obj03); + + when(repository.findAll()).thenReturn(list); + + //test + List empList = service.findAll(); + + assertEquals(3, empList.size()); + verify(repository, times(1)).findAll(); + } + + @Test + public void mustFindById(){ + when(repository.findById(1)).thenReturn(Optional.of(obj01)); + PtoEventoModel empList = service.findById(1); + assertTrue(empList != null); + verify(repository, times(1)).findById(1); + } + + @Test + public void mustSave() { + service.save(obj01); + verify(repository, times(1)).save(obj01); + } +}