testes de camadas

master
Kleber Cardoso 2 years ago
parent 24cf828466
commit 27fa5053cb

@ -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<Integer> 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());
}
});

@ -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<PtoEventoModel> ptoEventoModels, PtoEquipamentoModel equipamentoModel) {
public Integer extrairEventos(Integer lastNsr, List<PtoEventoModel> ptoEventoModels, PtoEquipamentoModel equipamentoModel) {
AtomicReference<Integer> 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);

@ -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<PtoAjusteModel> findAll(){
return repository.findAll();
}
public PtoAjusteModel findById(int id) {
Optional<PtoAjusteModel> optional = repository.findById(id);
return optional.isPresent() ? optional.get() : null;
}
}

@ -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<PtoEquipamentoModel> findAll(){
return repository.findAll();
}
public PtoEquipamentoModel findById(int id){
Optional<PtoEquipamentoModel> optional = repository.findById(id);
return optional.isPresent() ? optional.get() : null;
}
}

@ -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<PtoEventoModel> findAll(){
return repository.findAll();
}
public PtoEventoModel findById(int id){
Optional<PtoEventoModel> optional = repository.findById(id);
return optional.isPresent() ? optional.get() : null;
}
}

@ -26,4 +26,4 @@ spring:
datasource:
url: jdbc:postgresql://localhost:5432/sisgep
username: postgres
password: 123123
password: Postgres2018!

@ -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<PtoEventoModel> ptoEventoModels = controller.obterPontosPorRelogio(map);
Assertions.assertTrue(ptoEventoModels.size() > 0);
}
@Test
void deveExtrairEventos(){
}
@Test
void deveObterRelogios(){
List<PtoEquipamentoModel> 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

@ -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

@ -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<PtoAjusteModel> 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<PtoAjusteModel> recover = repository.findById(id);
Assertions.assertFalse(recover.isPresent());
Assertions.assertTrue(recover.isEmpty());
}
}

@ -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<PtoEquipamentoModel>> list = repository.findByInativoEquals(false);
Assertions.assertNotEquals(null, list);
Assertions.assertFalse(list.isEmpty());
}
@Test
public void mustFindById(){
Optional<PtoEquipamentoModel> 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<PtoEquipamentoModel> recover = repository.findById(id);
Assertions.assertFalse(recover.isPresent());
Assertions.assertTrue(recover.isEmpty());
}
}

@ -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<PtoEventoModel> 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<PtoEventoModel> recover = repository.findById(id);
Assertions.assertFalse(recover.isPresent());
Assertions.assertTrue(recover.isEmpty());
}
}

@ -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<PtoAjusteModel> list = new ArrayList<>();
list.add(obj01);
list.add(obj02);
list.add(obj03);
when(repository.findAll()).thenReturn(list);
//test
List<PtoAjusteModel> 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);
}
}

@ -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<PtoEquipamentoModel> list = new ArrayList<>();
list.add(obj01);
list.add(obj02);
list.add(obj03);
when(repository.findAll()).thenReturn(list);
//test
List<PtoEquipamentoModel> empList = service.findAll();
assertEquals(3, empList.size());
verify(repository, times(1)).findAll();
}
@Test
public void mustFindByInativo(){
List<PtoEquipamentoModel> list = new ArrayList<>();
list.add(obj01);
when(repository.findByInativoEquals(false)).thenReturn(Optional.of(list));
//test
List<PtoEquipamentoModel> 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);
}
}

@ -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<PtoEventoModel> list = new ArrayList<>();
list.add(obj01);
list.add(obj02);
list.add(obj03);
when(repository.findAll()).thenReturn(list);
//test
List<PtoEventoModel> 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);
}
}
Loading…
Cancel
Save