1+ package com .carvalhotechsolutions .mundoanimal .repositories ;
2+
3+ import com .carvalhotechsolutions .mundoanimal .database .JPAutil ;
4+ import com .carvalhotechsolutions .mundoanimal .enums .EspecieAnimal ;
5+ import com .carvalhotechsolutions .mundoanimal .model .Agendamento ;
6+ import com .carvalhotechsolutions .mundoanimal .model .Animal ;
7+ import com .carvalhotechsolutions .mundoanimal .model .Cliente ;
8+ import com .carvalhotechsolutions .mundoanimal .model .Servico ;
9+ import jakarta .persistence .EntityManager ;
10+ import jakarta .persistence .NoResultException ;
11+ import org .junit .jupiter .api .BeforeEach ;
12+ import org .junit .jupiter .api .Test ;
13+
14+ import java .math .BigDecimal ;
15+ import java .time .LocalDate ;
16+ import java .time .LocalTime ;
17+ import java .util .List ;
18+ import java .util .Random ;
19+
20+ import static org .junit .jupiter .api .Assertions .*;
21+
22+ public class AgendamentoRepositoryTest {
23+
24+ private AgendamentoRepository agendamentoRepository = new AgendamentoRepository ();
25+ private EntityManager em ;
26+
27+ @ BeforeEach
28+ void setUp () {
29+ em = JPAutil .getEntityManager ();
30+ limparBaseDeDados ();
31+ }
32+
33+ @ Test
34+ void save () {
35+ // Arrange
36+ Agendamento agendamento = criarAgendamentoCompleto ();
37+
38+ // Act
39+ Agendamento savedAgendamento = agendamentoRepository .save (agendamento );
40+
41+ // Assert
42+ assertNotNull (savedAgendamento .getId ());
43+ assertEquals (agendamento .getDataAgendamento (), savedAgendamento .getDataAgendamento ());
44+ assertEquals (agendamento .getHorarioAgendamento (), savedAgendamento .getHorarioAgendamento ());
45+ }
46+
47+ @ Test
48+ void update () {
49+ // Arrange
50+ Agendamento agendamento = criarAgendamentoCompleto ();
51+ Agendamento savedAgendamento = agendamentoRepository .save (agendamento );
52+
53+ LocalTime novoHorario = LocalTime .of (15 , 0 );
54+ savedAgendamento .setHorarioAgendamento (novoHorario );
55+
56+ // Act
57+ Agendamento updatedAgendamento = agendamentoRepository .save (savedAgendamento );
58+
59+ // Assert
60+ assertEquals (novoHorario , updatedAgendamento .getHorarioAgendamento ());
61+ assertEquals (savedAgendamento .getId (), updatedAgendamento .getId ());
62+ }
63+
64+ @ Test
65+ void findById () {
66+ // Arrange
67+ Agendamento agendamento = criarAgendamentoCompleto ();
68+ Agendamento savedAgendamento = agendamentoRepository .save (agendamento );
69+
70+ // Act
71+ Agendamento foundAgendamento = agendamentoRepository .findById (savedAgendamento .getId ());
72+
73+ // Assert
74+ assertNotNull (foundAgendamento );
75+ assertEquals (savedAgendamento .getId (), foundAgendamento .getId ());
76+ }
77+
78+ @ Test
79+ void findAll () {
80+ // Arrange
81+ Agendamento agendamento1 = criarAgendamento (LocalDate .now (), LocalTime .of (14 , 0 ));
82+ Agendamento agendamento2 = criarAgendamento (LocalDate .now (), LocalTime .of (9 , 0 ));
83+ Agendamento agendamento3 = criarAgendamento (LocalDate .now ().plusDays (1 ), LocalTime .of (10 , 0 ));
84+
85+ agendamentoRepository .save (agendamento1 );
86+ agendamentoRepository .save (agendamento2 );
87+ agendamentoRepository .save (agendamento3 );
88+
89+ // Act
90+ List <Agendamento > agendamentos = agendamentoRepository .findAll ();
91+
92+ // Assert
93+ assertEquals (3 , agendamentos .size ());
94+ assertTrue (agendamentos .get (0 ).getHorarioAgendamento ().isBefore (agendamentos .get (1 ).getHorarioAgendamento ()));
95+ assertTrue (agendamentos .get (0 ).getDataAgendamento ().isBefore (agendamentos .get (2 ).getDataAgendamento ()) ||
96+ agendamentos .get (0 ).getDataAgendamento ().isEqual (agendamentos .get (2 ).getDataAgendamento ()));
97+ }
98+
99+ @ Test
100+ void deleteById () {
101+ // Arrange
102+ Agendamento agendamento = criarAgendamentoCompleto ();
103+ Agendamento savedAgendamento = agendamentoRepository .save (agendamento );
104+
105+ // Act
106+ agendamentoRepository .deleteById (savedAgendamento .getId ());
107+
108+ // Assert
109+ assertThrows (NoResultException .class , () -> {
110+ agendamentoRepository .findById (savedAgendamento .getId ());
111+ });
112+ }
113+
114+ @ Test
115+ void verificarDisponibilidadeHorario () {
116+ // Arrange
117+ LocalDate data = LocalDate .now ();
118+ LocalTime horario = LocalTime .of (14 , 0 );
119+ Agendamento agendamento = criarAgendamento (data , horario );
120+ agendamentoRepository .save (agendamento );
121+
122+ // Act & Assert
123+ assertFalse (agendamentoRepository .verificarDisponibilidadeHorario (data , horario ));
124+ assertTrue (agendamentoRepository .verificarDisponibilidadeHorario (data , horario .plusHours (1 )));
125+ }
126+
127+ @ Test
128+ void buscarAgendamentosPorData () {
129+ // Arrange
130+ LocalDate hoje = LocalDate .now ();
131+ LocalDate amanha = hoje .plusDays (1 );
132+
133+ Agendamento agendamento1 = criarAgendamento (hoje , LocalTime .of (9 , 0 ));
134+ Agendamento agendamento2 = criarAgendamento (hoje , LocalTime .of (14 , 0 ));
135+ Agendamento agendamento3 = criarAgendamento (amanha , LocalTime .of (10 , 0 ));
136+
137+ agendamentoRepository .save (agendamento1 );
138+ agendamentoRepository .save (agendamento2 );
139+ agendamentoRepository .save (agendamento3 );
140+
141+ // Act
142+ List <Agendamento > agendamentosHoje = agendamentoRepository .buscarAgendamentosPorData (hoje );
143+ List <Agendamento > agendamentosAmanha = agendamentoRepository .buscarAgendamentosPorData (amanha );
144+
145+ // Assert
146+ assertEquals (2 , agendamentosHoje .size ());
147+ assertEquals (1 , agendamentosAmanha .size ());
148+ }
149+
150+ private Agendamento criarAgendamentoCompleto () {
151+ Cliente cliente = new Cliente ();
152+ cliente .setNome ("Cliente Teste " + System .nanoTime ());
153+ cliente .setTelefone (gerarNumero11Digitos ());
154+
155+ Animal animal = new Animal ();
156+ animal .setNome ("Pet Teste" );
157+ animal .setEspecie (EspecieAnimal .CACHORRO );
158+ animal .setDono (cliente );
159+
160+ Servico servico = new Servico ();
161+ servico .setNomeServico ("Banho" );
162+ servico .setDescricao ("Banho completo" );
163+ servico .setValorServico (BigDecimal .valueOf (50.0 ));
164+
165+ em .getTransaction ().begin ();
166+ em .persist (cliente );
167+ em .persist (animal );
168+ em .persist (servico );
169+ em .getTransaction ().commit ();
170+
171+ Agendamento agendamento = new Agendamento ();
172+ agendamento .setCliente (cliente );
173+ agendamento .setAnimal (animal );
174+ agendamento .setServico (servico );
175+ agendamento .setDataAgendamento (LocalDate .now ());
176+ agendamento .setHorarioAgendamento (LocalTime .of (14 , 0 ));
177+ agendamento .setResponsavelAtendimento ("Funcionário Teste" );
178+
179+ return agendamento ;
180+ }
181+
182+ private Agendamento criarAgendamento (LocalDate data , LocalTime horario ) {
183+ Agendamento agendamento = criarAgendamentoCompleto ();
184+ agendamento .setDataAgendamento (data );
185+ agendamento .setHorarioAgendamento (horario );
186+ return agendamento ;
187+ }
188+
189+ private void limparBaseDeDados () {
190+ em .getTransaction ().begin ();
191+ em .createQuery ("DELETE FROM Agendamento" ).executeUpdate ();
192+ em .createQuery ("DELETE FROM Animal" ).executeUpdate ();
193+ em .createQuery ("DELETE FROM Cliente" ).executeUpdate ();
194+ em .createQuery ("DELETE FROM Servico" ).executeUpdate ();
195+ em .getTransaction ().commit ();
196+ }
197+
198+ private String gerarNumero11Digitos () {
199+ Random random = new Random ();
200+
201+ // Gera um número entre 10000000000L e 99999999999L (11 dígitos)
202+ long minimo = 10000000000L ;
203+ long maximo = 99999999999L ;
204+
205+ // Calcula um número aleatório dentro do intervalo
206+ long numeroAleatorio = minimo + ((long )(random .nextDouble () * (maximo - minimo )));
207+
208+ return String .valueOf (numeroAleatorio );
209+ }
210+ }
0 commit comments