testes de camadas
parent
24cf828466
commit
27fa5053cb
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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…
Reference in New Issue