From 3843f62703441bfe0741987ebe2c507faeb34bcb Mon Sep 17 00:00:00 2001 From: CihatAltiparmak Date: Fri, 5 Jan 2024 21:39:27 +0300 Subject: [PATCH] Increase coverage rate for MedicationController, PetOwnerControllerTest and VeterinarianControllerTest --- .../MedicationControllerTest.java | 18 +++++++ .../PetOwnerControllerTest.java | 2 +- .../VeterinarianControllerTest.java | 53 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/production/ehayvanbackendapi/ControllerTests/MedicationControllerTest.java b/src/test/java/com/production/ehayvanbackendapi/ControllerTests/MedicationControllerTest.java index 73cbb09..6783ebe 100644 --- a/src/test/java/com/production/ehayvanbackendapi/ControllerTests/MedicationControllerTest.java +++ b/src/test/java/com/production/ehayvanbackendapi/ControllerTests/MedicationControllerTest.java @@ -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 { diff --git a/src/test/java/com/production/ehayvanbackendapi/ControllerTests/PetOwnerControllerTest.java b/src/test/java/com/production/ehayvanbackendapi/ControllerTests/PetOwnerControllerTest.java index d6e4a07..6ced502 100644 --- a/src/test/java/com/production/ehayvanbackendapi/ControllerTests/PetOwnerControllerTest.java +++ b/src/test/java/com/production/ehayvanbackendapi/ControllerTests/PetOwnerControllerTest.java @@ -132,7 +132,7 @@ public void testDeletingByID() throws Exception { @Test @Transactional - public void testGetAllPetOwnerCout() throws Exception { + public void testGetAllPetOwnerCount() throws Exception { List listOfAllAddedPetOwnerDTO = new ArrayList<>(); testCreateOrUpdatePetOwnerDTO.getUser().setName("ITU"); diff --git a/src/test/java/com/production/ehayvanbackendapi/ControllerTests/VeterinarianControllerTest.java b/src/test/java/com/production/ehayvanbackendapi/ControllerTests/VeterinarianControllerTest.java index 1345cec..2dbe157 100644 --- a/src/test/java/com/production/ehayvanbackendapi/ControllerTests/VeterinarianControllerTest.java +++ b/src/test/java/com/production/ehayvanbackendapi/ControllerTests/VeterinarianControllerTest.java @@ -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; @@ -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; @@ -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 { @@ -174,4 +196,35 @@ public void testGetAllVeterinarians() throws Exception { testVeterinarianRepository.deleteById(addedVeterinarian.getVetID()); } } + + @Test + @Transactional + public void testGetAllVeterinariansByName() throws Exception { + List 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()); + } + } }