Skip to content

Commit 093d90a

Browse files
committed
add tests to business logic
1 parent fa45884 commit 093d90a

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.capgemini.training.appointmentbooking.logic.impl;
2+
3+
import com.capgemini.training.appointmentbooking.common.BaseTest;
4+
import com.capgemini.training.appointmentbooking.common.to.TreatmentCto;
5+
import com.capgemini.training.appointmentbooking.dataaccess.entity.TreatmentEntity;
6+
import com.capgemini.training.appointmentbooking.dataaccess.repository.TreatmentRepository;
7+
import com.capgemini.training.appointmentbooking.logic.mapper.TreatmentCtoMapper;
8+
import com.capgemini.training.appointmentbooking.logic.mapper.TreatmentMapper;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.mapstruct.factory.Mappers;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
14+
import org.mockito.Spy;
15+
import org.mockito.junit.jupiter.MockitoExtension;
16+
17+
import java.util.List;
18+
import java.util.Optional;
19+
20+
import static org.mockito.Mockito.when;
21+
22+
@ExtendWith(MockitoExtension.class)
23+
public class FindTreatmentUcImplTest extends BaseTest {
24+
25+
@Mock
26+
private TreatmentRepository treatmentRepository;
27+
28+
@InjectMocks
29+
private FindTreatmentUcImpl findTreatmentUc;
30+
31+
@Spy
32+
private static TreatmentMapper treatmentMapper = Mappers.getMapper(TreatmentMapper.class);
33+
34+
@Spy
35+
private static TreatmentCtoMapper treatmentCtoMapper = Mappers.getMapper(TreatmentCtoMapper.class);
36+
37+
@Test
38+
void shouldFindTreatmentById() {
39+
// given
40+
Long treatmentId = -1L;
41+
TreatmentEntity treatmentEntity = new TreatmentEntity();
42+
treatmentEntity.setId(treatmentId);
43+
treatmentEntity.setName("Dummy Name");
44+
treatmentEntity.setDescription("Dummy Description");
45+
46+
when(treatmentRepository.findById(treatmentId)).thenReturn(Optional.of(treatmentEntity));
47+
48+
// when
49+
Optional<TreatmentCto> treatmentCto = findTreatmentUc.findById(treatmentId);
50+
51+
// then
52+
assertThat(treatmentCto).isPresent();
53+
treatmentCto.ifPresent(a -> {
54+
assertThat(a.treatmentEto().id()).isEqualTo(treatmentEntity.getId());
55+
assertThat(a.treatmentEto().name()).isEqualTo(treatmentEntity.getName());
56+
assertThat(a.treatmentEto().description()).isEqualTo(treatmentEntity.getDescription());
57+
});
58+
}
59+
60+
@Test
61+
void shouldFindAllTreatments() {
62+
// given
63+
String name = "Dummy Name";
64+
TreatmentEntity treatmentEntity = new TreatmentEntity();
65+
treatmentEntity.setName(name);
66+
when(treatmentRepository.findAll()).thenReturn(List.of(treatmentEntity, treatmentEntity, treatmentEntity));
67+
68+
// when
69+
List<TreatmentCto> result = findTreatmentUc.findAll();
70+
71+
// then
72+
assertThat(result).hasSize(3);
73+
assertThat(result).allMatch(appointment -> name.equals(appointment.treatmentEto().name()));
74+
}
75+
76+
}

0 commit comments

Comments
 (0)