Skip to content

Commit

Permalink
Increase coverage rate for MedicationController, PetOwnerControllerTe…
Browse files Browse the repository at this point in the history
…st and VeterinarianControllerTest
  • Loading branch information
CihatAltiparmak committed Jan 5, 2024
1 parent ff406fd commit 3843f62
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ public void testPostingByID() throws Exception{
verify(testMedicationService, times(1)).postMedication(any());
}

@Test
@Transactional
public void testUpdatingById() throws Exception {
Integer testMedicationId = 1;
when(testMedicationService.updateMedication(testMedicationId, testCreateOrUpdateMedicationDTO)).thenReturn(new MedicationDTO());

testCreateOrUpdateMedicationDTO.setMedicationName("Zula Mitingi");
this.mockMvc.perform(put("/api/medications/update/" + testMedicationId).with(httpBasic("test", "password"))
.content(objectMapper.writeValueAsString(testCreateOrUpdateMedicationDTO))
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("$.medicationName").value(testCreateOrUpdateMedicationDTO.getMedicationName()));

// assert that the method updateMedication of testMedicationService is executed once
verify(testMedicationService, times(1)).updateMedication(anyInt(), any());
}

@Test
@Transactional
public void testDeletingById() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testDeletingByID() throws Exception {

@Test
@Transactional
public void testGetAllPetOwnerCout() throws Exception {
public void testGetAllPetOwnerCount() throws Exception {
List<PetOwnerDTO> listOfAllAddedPetOwnerDTO = new ArrayList<>();

testCreateOrUpdatePetOwnerDTO.getUser().setName("ITU");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import com.production.ehayvanbackendapi.DTO.AppointmentDTO;
import com.production.ehayvanbackendapi.DTO.VeterinarianDTO;
import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdateCustomerDTO;
import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdateVeterinarianDTO;
Expand All @@ -22,6 +23,9 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -125,6 +129,24 @@ public void testPostingByID() throws Exception{
verify(testVeterinarianService, times(1)).postVeterinarian(any());
}

@Test
@Transactional
public void testUpdatingById() throws Exception {
Integer testVeterinarianId = 1;
when(testVeterinarianService.updateVeterinarian(testVeterinarianId, testCreateOrUpdateVeterinarianDTO)).thenReturn(new VeterinarianDTO());

testCreateOrUpdateVeterinarianDTO.setClinic("Zula Mitingi");
this.mockMvc.perform(put("/api/veterinarians/update/" + testVeterinarianId).with(httpBasic("test", "password"))
.content(objectMapper.writeValueAsString(testCreateOrUpdateVeterinarianDTO))
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("$.clinic").value(testCreateOrUpdateVeterinarianDTO.getClinic()));

// assert that the method updateVeterinarian of testVeterinarianService is executed once
verify(testVeterinarianService, times(1)).updateVeterinarian(anyInt(), any());
}

@Test
@Transactional
public void testDeletingById() throws Exception {
Expand Down Expand Up @@ -174,4 +196,35 @@ public void testGetAllVeterinarians() throws Exception {
testVeterinarianRepository.deleteById(addedVeterinarian.getVetID());
}
}

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

testVeterinarian.getUser().setName("anka");
testVeterinarian.setVetID(0);
testVeterinarian.getUser().setUserID(0);
listOfAllAddedVeterinarian.add(testVeterinarianRepository.save(testVeterinarian));

testVeterinarian.getUser().setName("kanka");
testVeterinarian.setVetID(0);
testVeterinarian.getUser().setUserID(0);
listOfAllAddedVeterinarian.add(testVeterinarianRepository.save(testVeterinarian));

testVeterinarian.getUser().setName("afanka");
testVeterinarian.setVetID(0);
testVeterinarian.getUser().setUserID(0);
listOfAllAddedVeterinarian.add(testVeterinarianRepository.save(testVeterinarian));

this.mockMvc.perform(get("/api/veterinarians/search/" + listOfAllAddedVeterinarian.get(0).getUser().getName()).with(httpBasic("test", "password"))
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("$[0].user.name").value(listOfAllAddedVeterinarian.get(0).getUser().getName()));

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

0 comments on commit 3843f62

Please sign in to comment.