-
Notifications
You must be signed in to change notification settings - Fork 1
test: add unit tests for MedicalRecord entity and response DTOs #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
src/test/java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordResponseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| package org.example.vet1177.dto.response.medicalrecord; | ||
|
|
||
| import org.example.vet1177.entities.Clinic; | ||
| import org.example.vet1177.entities.MedicalRecord; | ||
| import org.example.vet1177.entities.Pet; | ||
| import org.example.vet1177.entities.RecordStatus; | ||
| import org.example.vet1177.entities.Role; | ||
| import org.example.vet1177.entities.User; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
| import java.time.LocalDate; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| class MedicalRecordResponseTest { | ||
|
|
||
| private MedicalRecord record; | ||
| private Pet pet; | ||
| private User owner; | ||
| private Clinic clinic; | ||
| private User vet; | ||
| private User createdBy; | ||
|
|
||
| private UUID recordId; | ||
| private UUID petId; | ||
| private UUID ownerId; | ||
| private UUID clinicId; | ||
| private UUID vetId; | ||
| private UUID createdById; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| recordId = UUID.randomUUID(); | ||
| petId = UUID.randomUUID(); | ||
| ownerId = UUID.randomUUID(); | ||
| clinicId = UUID.randomUUID(); | ||
| vetId = UUID.randomUUID(); | ||
| createdById = UUID.randomUUID(); | ||
|
|
||
| owner = new User("Anna Ägare", "anna@mail.se", "hash", Role.OWNER); | ||
| setPrivateField(owner, "id", ownerId); | ||
|
|
||
| vet = new User("Dr. Elin Svensson", "elin@vet.se", "hash", Role.VET); | ||
| setPrivateField(vet, "id", vetId); | ||
|
|
||
| createdBy = new User("Dr. Sara Lindqvist", "sara@vet.se", "hash", Role.VET); | ||
| setPrivateField(createdBy, "id", createdById); | ||
|
|
||
| pet = new Pet(owner, "Lassie", "Hund", "Collie", LocalDate.of(2020, 1, 1), null); | ||
| setPrivateField(pet, "id", petId); | ||
|
|
||
| clinic = new Clinic("Vetkliniken", "Storgatan 1", "0701234567"); | ||
| setPrivateField(clinic, "id", clinicId); | ||
|
|
||
| record = new MedicalRecord(); | ||
| record.setId(recordId); | ||
| record.setTitle("Årlig kontroll"); | ||
| record.setDescription("Patienten är vid god hälsa."); | ||
| record.setStatus(RecordStatus.OPEN); | ||
| record.setPet(pet); | ||
| record.setOwner(owner); | ||
| record.setClinic(clinic); | ||
| record.setAssignedVet(vet); | ||
| record.setCreatedBy(createdBy); | ||
| callProtectedMethod(record, "onCreate"); | ||
| } | ||
|
|
||
| // --- Happy path --- | ||
|
|
||
| @Test | ||
| void from_shouldMapAllFieldsCorrectly() { | ||
| MedicalRecordResponse response = MedicalRecordResponse.from(record); | ||
|
|
||
| assertThat(response.id()).isEqualTo(recordId); | ||
| assertThat(response.title()).isEqualTo("Årlig kontroll"); | ||
| assertThat(response.description()).isEqualTo("Patienten är vid god hälsa."); | ||
| assertThat(response.status()).isEqualTo(RecordStatus.OPEN); | ||
| assertThat(response.petId()).isEqualTo(petId); | ||
| assertThat(response.petName()).isEqualTo("Lassie"); | ||
| assertThat(response.petSpecies()).isEqualTo("Hund"); | ||
| assertThat(response.ownerId()).isEqualTo(ownerId); | ||
| assertThat(response.ownerName()).isEqualTo("Anna Ägare"); | ||
| assertThat(response.clinicId()).isEqualTo(clinicId); | ||
| assertThat(response.clinicName()).isEqualTo("Vetkliniken"); | ||
| assertThat(response.assignedVetId()).isEqualTo(vetId); | ||
| assertThat(response.assignedVetName()).isEqualTo("Dr. Elin Svensson"); | ||
| assertThat(response.createdById()).isEqualTo(createdById); | ||
| assertThat(response.createdByName()).isEqualTo("Dr. Sara Lindqvist"); | ||
| assertThat(response.createdAt()).isNotNull(); | ||
| assertThat(response.updatedAt()).isNotNull(); | ||
| assertThat(response.closedAt()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void from_timestampsShouldReflectRecordTimestamps() { | ||
| MedicalRecordResponse response = MedicalRecordResponse.from(record); | ||
|
|
||
| assertThat(response.createdAt()).isEqualTo(record.getCreatedAt()); | ||
| assertThat(response.updatedAt()).isEqualTo(record.getUpdatedAt()); | ||
| } | ||
|
|
||
| @Test | ||
| void from_shouldMapClosedAtWhenStatusIsClosed() { | ||
| record.setStatus(RecordStatus.CLOSED); | ||
|
|
||
| MedicalRecordResponse response = MedicalRecordResponse.from(record); | ||
|
|
||
| assertThat(response.status()).isEqualTo(RecordStatus.CLOSED); | ||
| assertThat(response.closedAt()).isEqualTo(record.getClosedAt()); | ||
| } | ||
|
|
||
| @Test | ||
| void from_shouldMapNullAssignedVetToNullIdAndName() { | ||
| record.setAssignedVet(null); | ||
|
|
||
| MedicalRecordResponse response = MedicalRecordResponse.from(record); | ||
|
|
||
| assertThat(response.assignedVetId()).isNull(); | ||
| assertThat(response.assignedVetName()).isNull(); | ||
| } | ||
|
|
||
| // --- Sad paths --- | ||
|
|
||
| @Test | ||
| void from_shouldThrowWhenPetIsNull() { | ||
| record.setPet(null); | ||
|
|
||
| assertThatThrownBy(() -> MedicalRecordResponse.from(record)) | ||
| .isInstanceOf(NullPointerException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void from_shouldThrowWhenOwnerIsNull() { | ||
| record.setOwner(null); | ||
|
|
||
| assertThatThrownBy(() -> MedicalRecordResponse.from(record)) | ||
| .isInstanceOf(NullPointerException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void from_shouldThrowWhenClinicIsNull() { | ||
| record.setClinic(null); | ||
|
|
||
| assertThatThrownBy(() -> MedicalRecordResponse.from(record)) | ||
| .isInstanceOf(NullPointerException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void from_shouldThrowWhenCreatedByIsNull() { | ||
| record.setCreatedBy(null); | ||
|
|
||
| assertThatThrownBy(() -> MedicalRecordResponse.from(record)) | ||
| .isInstanceOf(NullPointerException.class); | ||
| } | ||
|
|
||
| // --- Helpers --- | ||
|
|
||
| private static void setPrivateField(Object target, String fieldName, Object value) throws Exception { | ||
| Field field = target.getClass().getDeclaredField(fieldName); | ||
| field.setAccessible(true); | ||
| field.set(target, value); | ||
| } | ||
|
|
||
| private static void callProtectedMethod(Object target, String methodName) throws Exception { | ||
| Method method = target.getClass().getDeclaredMethod(methodName); | ||
| method.setAccessible(true); | ||
| method.invoke(target); | ||
| } | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
...java/org/example/vet1177/dto/response/medicalrecord/MedicalRecordSummaryResponseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package org.example.vet1177.dto.response.medicalrecord; | ||
|
|
||
| import org.example.vet1177.entities.Clinic; | ||
| import org.example.vet1177.entities.MedicalRecord; | ||
| import org.example.vet1177.entities.Pet; | ||
| import org.example.vet1177.entities.RecordStatus; | ||
| import org.example.vet1177.entities.Role; | ||
| import org.example.vet1177.entities.User; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
| import java.time.LocalDate; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| class MedicalRecordSummaryResponseTest { | ||
|
|
||
| private MedicalRecord record; | ||
| private Pet pet; | ||
| private User owner; | ||
| private Clinic clinic; | ||
| private User vet; | ||
| private User createdBy; | ||
| private UUID recordId; | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws Exception { | ||
| recordId = UUID.randomUUID(); | ||
|
|
||
| owner = new User("Anna Ägare", "anna@mail.se", "hash", Role.OWNER); | ||
| setPrivateField(owner, "id", UUID.randomUUID()); | ||
|
|
||
| vet = new User("Dr. Elin Svensson", "elin@vet.se", "hash", Role.VET); | ||
| setPrivateField(vet, "id", UUID.randomUUID()); | ||
|
|
||
| createdBy = new User("Dr. Sara Lindqvist", "sara@vet.se", "hash", Role.VET); | ||
| setPrivateField(createdBy, "id", UUID.randomUUID()); | ||
|
|
||
| pet = new Pet(owner, "Lassie", "Hund", "Collie", LocalDate.of(2020, 1, 1), null); | ||
| setPrivateField(pet, "id", UUID.randomUUID()); | ||
|
|
||
| clinic = new Clinic("Vetkliniken", "Storgatan 1", "0701234567"); | ||
| setPrivateField(clinic, "id", UUID.randomUUID()); | ||
|
|
||
| record = new MedicalRecord(); | ||
| record.setId(recordId); | ||
| record.setTitle("Årlig kontroll"); | ||
| record.setStatus(RecordStatus.OPEN); | ||
| record.setPet(pet); | ||
| record.setOwner(owner); | ||
| record.setClinic(clinic); | ||
| record.setAssignedVet(vet); | ||
| record.setCreatedBy(createdBy); | ||
| callProtectedMethod(record, "onCreate"); | ||
| } | ||
|
|
||
| // --- Happy path --- | ||
|
|
||
| @Test | ||
| void from_shouldMapAllFieldsCorrectly() { | ||
| MedicalRecordSummaryResponse response = MedicalRecordSummaryResponse.from(record); | ||
|
|
||
| assertThat(response.id()).isEqualTo(recordId); | ||
| assertThat(response.title()).isEqualTo("Årlig kontroll"); | ||
| assertThat(response.status()).isEqualTo(RecordStatus.OPEN); | ||
| assertThat(response.petName()).isEqualTo("Lassie"); | ||
| assertThat(response.ownerName()).isEqualTo("Anna Ägare"); | ||
| assertThat(response.assignedVetName()).isEqualTo("Dr. Elin Svensson"); | ||
| assertThat(response.createdAt()).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void from_createdAtShouldReflectRecordCreatedAt() { | ||
| MedicalRecordSummaryResponse response = MedicalRecordSummaryResponse.from(record); | ||
|
|
||
| assertThat(response.createdAt()).isEqualTo(record.getCreatedAt()); | ||
| } | ||
|
|
||
| @Test | ||
| void from_shouldMapNullAssignedVetToNullName() { | ||
| record.setAssignedVet(null); | ||
|
|
||
| MedicalRecordSummaryResponse response = MedicalRecordSummaryResponse.from(record); | ||
|
|
||
| assertThat(response.assignedVetName()).isNull(); | ||
| } | ||
|
|
||
| // --- Sad paths --- | ||
|
|
||
| @Test | ||
| void from_shouldThrowWhenPetIsNull() { | ||
| record.setPet(null); | ||
|
|
||
| assertThatThrownBy(() -> MedicalRecordSummaryResponse.from(record)) | ||
| .isInstanceOf(NullPointerException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void from_shouldThrowWhenOwnerIsNull() { | ||
| record.setOwner(null); | ||
|
|
||
| assertThatThrownBy(() -> MedicalRecordSummaryResponse.from(record)) | ||
| .isInstanceOf(NullPointerException.class); | ||
| } | ||
|
|
||
| // --- Helpers --- | ||
|
|
||
| private static void setPrivateField(Object target, String fieldName, Object value) throws Exception { | ||
| Field field = target.getClass().getDeclaredField(fieldName); | ||
| field.setAccessible(true); | ||
| field.set(target, value); | ||
| } | ||
|
|
||
| private static void callProtectedMethod(Object target, String methodName) throws Exception { | ||
| Method method = target.getClass().getDeclaredMethod(methodName); | ||
| method.setAccessible(true); | ||
| method.invoke(target); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
closedAttest can pass on a false positive.This test should also assert non-null
closedAt; currently it passes if both values are null.✅ Tighten assertion
`@Test` void from_shouldMapClosedAtWhenStatusIsClosed() { record.setStatus(RecordStatus.CLOSED); MedicalRecordResponse response = MedicalRecordResponse.from(record); assertThat(response.status()).isEqualTo(RecordStatus.CLOSED); + assertThat(record.getClosedAt()).isNotNull(); + assertThat(response.closedAt()).isNotNull(); assertThat(response.closedAt()).isEqualTo(record.getClosedAt()); }📝 Committable suggestion
🤖 Prompt for AI Agents