Skip to content

Commit

Permalink
Added missing tests for AppointmentService. But will be reviewed later
Browse files Browse the repository at this point in the history
  • Loading branch information
CihatAltiparmak committed Jan 2, 2024
1 parent a6b3aa9 commit 7afcf92
Showing 1 changed file with 202 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import com.production.ehayvanbackendapi.DTO.AppointmentDTO;
import com.production.ehayvanbackendapi.DTO.PetDTO;
import com.production.ehayvanbackendapi.DTO.VeterinarianDTO;
import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdateAppointmentDTO;
import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdatePetDTO;
import com.production.ehayvanbackendapi.Entities.*;
import com.production.ehayvanbackendapi.Mappers.AppointmentMapper;
import com.production.ehayvanbackendapi.Mappers.PetMapper;
import com.production.ehayvanbackendapi.Repositories.AppointmentRepository;
import com.production.ehayvanbackendapi.Repositories.PetOwnerRepository;
import com.production.ehayvanbackendapi.Repositories.PetRepository;
import com.production.ehayvanbackendapi.Repositories.VeterinarianRepository;
import com.production.ehayvanbackendapi.Services.AppointmentService;
import com.production.ehayvanbackendapi.Services.PetService;
import com.production.ehayvanbackendapi.Services.VeterinarianService;
import com.production.ehayvanbackendapi.TestUtils.DataSeed;
import org.checkerframework.checker.units.qual.C;
import org.junit.jupiter.api.*;
Expand All @@ -20,11 +24,15 @@
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.resource.VersionResourceResolver;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -40,6 +48,14 @@ public class AppointmentServiceTest {
@Autowired
private AppointmentRepository testAppointmentRepository;

@SpyBean
@Autowired
private PetOwnerRepository testPetOwnerRepository;

@SpyBean
@Autowired
private VeterinarianRepository testVeterinarianRepository;

@Autowired
DataSeed dataSeed;

Expand Down Expand Up @@ -101,7 +117,7 @@ public void testServiceGetByIdWhichInDatabase() {

@Test
@Transactional
public void testServicePostPet() {
public void testServicePostAppointment() {
AppointmentDTO returnedAppointmentDTO = testAppointmentService.postAppointment(testCreateOrUpdateAppointmentDTO);

Optional<Appointment> searchedAppointmentOptional = testAppointmentRepository.findById(returnedAppointmentDTO.getAppointmentID());
Expand All @@ -113,7 +129,7 @@ public void testServicePostPet() {

@Test
@Transactional
public void testServiceUpdatePet() {
public void testServiceUpdateAppointment() {
// firstly save new Appointment
Appointment returnedAppointment = testAppointmentRepository.save(testAppointment);

Expand All @@ -135,7 +151,7 @@ public void testServiceUpdatePet() {

@Test
@Transactional
public void testServiceDeleteSchedule() {
public void testServiceDeleteAppointment() {
// firstly save new Appointment
Appointment returnedAppointment = testAppointmentRepository.save(testAppointment);
Optional<Appointment> searchedAppointment = testAppointmentRepository.findById(returnedAppointment.getAppointmentID());
Expand All @@ -148,4 +164,187 @@ public void testServiceDeleteSchedule() {
searchedAppointment = testAppointmentRepository.findById(returnedAppointment.getAppointmentID());
assertThat(searchedAppointment.isPresent()).isEqualTo(false);
}

@Test
@Transactional
public void testServiceGetAllAppointments() {
List<Appointment> listOfAllAddedAppointments = new ArrayList<>();
Appointment returnedAppointment;

testAppointment.setAppointmentID(17);
returnedAppointment = testAppointmentRepository.save(testAppointment);
listOfAllAddedAppointments.add(returnedAppointment);

testAppointment.setAppointmentID(18);
returnedAppointment = testAppointmentRepository.save(testAppointment);
listOfAllAddedAppointments.addLast(returnedAppointment);

testAppointment.setAppointmentID(19);
returnedAppointment = testAppointmentRepository.save(testAppointment);
listOfAllAddedAppointments.addLast(returnedAppointment);

List<AppointmentDTO> appointmentList = testAppointmentService.getAllAppointments();

// we include seeded data before tests start. So we expect that size of returned list
// is added_items_size + 1. "1" is seeded data in there.
assertThat(appointmentList.size() - 1).isEqualTo(listOfAllAddedAppointments.size());

// check if there is seeded data's appointment id
assertThat(appointmentList.stream().map(
AppointmentDTO::getAppointmentID).toList().contains(1))
.isEqualTo(true);
for (Appointment addedAppointment : listOfAllAddedAppointments) {
assertThat(appointmentList.stream().map(
AppointmentDTO::getAppointmentID).toList().contains(addedAppointment.getAppointmentID()))
.isEqualTo(true);
}

for (Appointment addedAppointment : listOfAllAddedAppointments) {
testAppointmentRepository.deleteById(addedAppointment.getAppointmentID());
}
}

@Test
@Transactional
public void testServiceGetAllAppointmentsForPetOwner() {
List<PetOwner> listOfAllAddedPetOwner = new ArrayList<>();

PetOwner otherPetOwner = new PetOwner();
otherPetOwner.setPetOwnerID(17);
otherPetOwner.setUser(new Customer());
otherPetOwner.getUser().setEmail("[email protected]");
otherPetOwner.getUser().setPassword("Sopa");
otherPetOwner.getUser().setSurname("Geyik");
otherPetOwner.getUser().setName("Erdem");
listOfAllAddedPetOwner.add(testPetOwnerRepository.save(otherPetOwner));

otherPetOwner.setPetOwnerID(19);
otherPetOwner.setUser(new Customer());
otherPetOwner.getUser().setEmail("[email protected]");
otherPetOwner.getUser().setPassword("Gec bile kaldim");
otherPetOwner.getUser().setSurname("Maden");
otherPetOwner.getUser().setName("Muzaffer");
listOfAllAddedPetOwner.add(testPetOwnerRepository.save(otherPetOwner));

List<Appointment> listOfAllAddedAppointmentsForPetOwner = new ArrayList<>();
List<Appointment> listOfAllAddedAppointments = new ArrayList<>();
Appointment returnedAppointment;

testAppointment.setAppointmentID(17);
testAppointment.getPetOwnerID().setPetOwnerID(listOfAllAddedPetOwner.get(0).getPetOwnerID());
returnedAppointment = testAppointmentRepository.save(testAppointment);
listOfAllAddedAppointmentsForPetOwner.add(returnedAppointment);
listOfAllAddedAppointments.add(returnedAppointment);

testAppointment.setAppointmentID(18);
testAppointment.getPetOwnerID().setPetOwnerID(listOfAllAddedPetOwner.get(0).getPetOwnerID());
returnedAppointment = testAppointmentRepository.save(testAppointment);
listOfAllAddedAppointmentsForPetOwner.addLast(returnedAppointment);
listOfAllAddedAppointments.add(returnedAppointment);

testAppointment.setAppointmentID(19);
testAppointment.getPetOwnerID().setPetOwnerID(listOfAllAddedPetOwner.get(1).getPetOwnerID());
returnedAppointment = testAppointmentRepository.save(testAppointment);
// listOfAllAddedAppointmentsForPetOwner.addLast(returnedAppointment);
listOfAllAddedAppointments.add(returnedAppointment);

Integer testPetOwnerId = listOfAllAddedPetOwner.get(0).getPetOwnerID();
List<AppointmentDTO> appointmentList = testAppointmentService.getAllAppointmentsForPetOwner(testPetOwnerId);

// we exlude seeded data before tests start. Because pet owner id of seededAppointment data
// is not inside list of new added pet owner id.
assertThat(appointmentList.size()).isEqualTo(listOfAllAddedAppointmentsForPetOwner.size());

// check if there is not seeded data's appointment id
assertThat(appointmentList.stream().map(
AppointmentDTO::getAppointmentID).toList().contains(1))
.isEqualTo(false);
for (Appointment addedAppointment : listOfAllAddedAppointmentsForPetOwner) {
assertThat(appointmentList.stream().map(
AppointmentDTO::getAppointmentID).toList().contains(addedAppointment.getAppointmentID()))
.isEqualTo(true);
}

for (Appointment addedAppointment : listOfAllAddedAppointments) {
testAppointmentRepository.deleteById(addedAppointment.getAppointmentID());
}

for (PetOwner addedPetOwner : listOfAllAddedPetOwner) {
testPetOwnerRepository.deleteById(addedPetOwner.getPetOwnerID());
}
}

@Test
@Transactional
public void testServiceGetAllAppointmentsForVeterinarian() {
List<Veterinarian> listOfAllAddedVeterinarian = new ArrayList<>();

Veterinarian otherVeterinarian = new Veterinarian();
otherVeterinarian.setVetID(15);
otherVeterinarian.setClinic("cincinella");
otherVeterinarian.setUser(new Customer());
otherVeterinarian.getUser().setUserTypeID(new UserType(2));
otherVeterinarian.getUser().setName("Yakisikli");
otherVeterinarian.getUser().setSurname("Guvenlik");
otherVeterinarian.getUser().setEmail("[email protected]");
otherVeterinarian.getUser().setPassword("akaryakit");
listOfAllAddedVeterinarian.add(testVeterinarianRepository.save(otherVeterinarian));

otherVeterinarian.setVetID(16);
otherVeterinarian.setClinic("Medico");
otherVeterinarian.setUser(new Customer());
otherVeterinarian.getUser().setUserTypeID(new UserType(2));
otherVeterinarian.getUser().setName("Erkan");
otherVeterinarian.getUser().setSurname("Abe");
otherVeterinarian.getUser().setEmail("[email protected]");
otherVeterinarian.getUser().setPassword("dogalgaz");
listOfAllAddedVeterinarian.add(testVeterinarianRepository.save(otherVeterinarian));

List<Appointment> listOfAllAddedAppointmentsForVeterinarian = new ArrayList<>();
List<Appointment> listOfAllAddedAppointments = new ArrayList<>();
Appointment returnedAppointment;

testAppointment.setAppointmentID(17);
testAppointment.getVetID().setVetID(listOfAllAddedVeterinarian.get(0).getVetID());
returnedAppointment = testAppointmentRepository.save(testAppointment);
listOfAllAddedAppointmentsForVeterinarian.add(returnedAppointment);
listOfAllAddedAppointments.add(returnedAppointment);

testAppointment.setAppointmentID(18);
testAppointment.getVetID().setVetID(listOfAllAddedVeterinarian.get(0).getVetID());
returnedAppointment = testAppointmentRepository.save(testAppointment);
listOfAllAddedAppointmentsForVeterinarian.addLast(returnedAppointment);
listOfAllAddedAppointments.add(returnedAppointment);

testAppointment.setAppointmentID(19);
testAppointment.getVetID().setVetID(listOfAllAddedVeterinarian.get(1).getVetID());
returnedAppointment = testAppointmentRepository.save(testAppointment);
// listOfAllAddedAppointmentsForVeterinarian.addLast(returnedAppointment);
listOfAllAddedAppointments.add(returnedAppointment);

Integer testVeterinarianId = listOfAllAddedVeterinarian.get(0).getVetID();
List<AppointmentDTO> appointmentList = testAppointmentService.getAllAppointmentsForVeterinarian(testVeterinarianId);

// we exlude seeded data before tests start. Because pet owner id of seededAppointment data
// is not inside list of new added pet owner id.
assertThat(appointmentList.size()).isEqualTo(listOfAllAddedAppointmentsForVeterinarian.size());

// check if there is not seeded data's appointment id
assertThat(appointmentList.stream().map(
AppointmentDTO::getAppointmentID).toList().contains(1))
.isEqualTo(false);
for (Appointment addedAppointment : listOfAllAddedAppointmentsForVeterinarian) {
assertThat(appointmentList.stream().map(
AppointmentDTO::getAppointmentID).toList().contains(addedAppointment.getAppointmentID()))
.isEqualTo(true);
}

for (Appointment addedAppointment : listOfAllAddedAppointments) {
testAppointmentRepository.deleteById(addedAppointment.getAppointmentID());
}

for (Veterinarian addedVeterinarian : listOfAllAddedVeterinarian) {
testVeterinarianRepository.deleteById(addedVeterinarian.getVetID());
}
}
}

0 comments on commit 7afcf92

Please sign in to comment.