-
Notifications
You must be signed in to change notification settings - Fork 2
Test/integration tests activitylog #189
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
7 commits
Select commit
Hold shift + click to select a range
d05a819
Test: add ActivityLog integration test with H2 setup
TatjanaTrajkovic 3767d6c
Test: add ActivityLog integration tests and improve error handling
TatjanaTrajkovic 562e527
Test: fix issue with H2 in CI
TatjanaTrajkovic bad03de
Test: fix issue with H2 in CI, v2
TatjanaTrajkovic f7433df
Test: fix issue with H2 in CI, v3
TatjanaTrajkovic 48d1fc6
Test: fix issue with H2 in CI, v4
TatjanaTrajkovic 22ffb6d
Test: fix issue with H2 in CI, v5
TatjanaTrajkovic 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
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
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
67 changes: 67 additions & 0 deletions
67
src/test/java/org/example/vet1177/integration/TestDataFactory.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,67 @@ | ||
| package org.example.vet1177.integration; | ||
|
|
||
| import org.example.vet1177.entities.*; | ||
| import org.example.vet1177.repository.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDate; | ||
| import java.util.UUID; | ||
|
|
||
| public class TestDataFactory { | ||
|
|
||
| public static Clinic createClinic(ClinicRepository clinicRepository) { | ||
| Clinic clinic = new Clinic(); | ||
| clinic.setName("Test Clinic " + UUID.randomUUID()); | ||
| return clinicRepository.save(clinic); | ||
| } | ||
|
|
||
| public static User createOwner(UserRepository userRepository, Clinic clinic) { | ||
| User owner = new User( | ||
| "Owner", | ||
| UUID.randomUUID() + "@test.com", | ||
| "password123", | ||
| Role.OWNER, | ||
| clinic | ||
| ); | ||
| return userRepository.save(owner); | ||
| } | ||
|
|
||
| public static Pet createPet(PetRepository petRepository, User owner) { | ||
| Pet pet = new Pet( | ||
| owner, | ||
| "Doggo", | ||
| "Dog", | ||
| "Labrador", | ||
| LocalDate.of(2020, 1, 1), | ||
| new BigDecimal("20.5") | ||
| ); | ||
| return petRepository.save(pet); | ||
| } | ||
|
|
||
| public static MedicalRecord createRecord( | ||
| MedicalRecordRepository medicalRecordRepository, | ||
| User owner, | ||
| Clinic clinic, | ||
| Pet pet | ||
| ) { | ||
| MedicalRecord record = new MedicalRecord(); | ||
| record.setTitle("Test Record"); | ||
| record.setOwner(owner); | ||
| record.setClinic(clinic); | ||
| record.setCreatedBy(owner); | ||
| record.setPet(pet); | ||
|
|
||
| return medicalRecordRepository.save(record); | ||
| } | ||
|
|
||
| public static ActivityLog createLog( | ||
| ActivityLogRepository repo, | ||
| User user, | ||
| MedicalRecord record, | ||
| ActivityType type, | ||
| String desc | ||
| ) { | ||
| ActivityLog log = new ActivityLog(type, desc, user, record); | ||
| return repo.save(log); | ||
| } | ||
| } |
211 changes: 211 additions & 0 deletions
211
src/test/java/org/example/vet1177/integration/activitylog/ActivityLogIntegrationTest.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,211 @@ | ||
| package org.example.vet1177.integration.activitylog; | ||
|
|
||
| import org.checkerframework.checker.units.qual.C; | ||
| import org.example.vet1177.config.AwsS3Properties; | ||
| import org.example.vet1177.entities.*; | ||
| import org.example.vet1177.integration.TestDataFactory; | ||
| import org.example.vet1177.repository.*; | ||
| import org.example.vet1177.services.FileStorageService; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.test.context.TestPropertySource; | ||
| import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDate; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
|
|
||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| @ActiveProfiles("test") | ||
| @TestPropertySource(properties = { | ||
| "spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL", | ||
| "spring.datasource.driver-class-name=org.h2.Driver" | ||
| }) | ||
| public class ActivityLogIntegrationTest { | ||
|
|
||
| @Autowired | ||
| private ClinicRepository clinicRepository; | ||
|
|
||
| @Autowired | ||
| private UserRepository userRepository; | ||
|
|
||
| @Autowired | ||
| private MedicalRecordRepository medicalRecordRepository; | ||
|
|
||
| @Autowired | ||
| private ActivityLogRepository activityLogRepository; | ||
| @Autowired | ||
| private MockMvc mockMvc; | ||
| @Autowired | ||
| private PetRepository petRepository; | ||
|
|
||
| @MockitoBean | ||
| private FileStorageService fileStorageService; | ||
|
|
||
| @MockitoBean | ||
| private AwsS3Properties awsS3Properties; | ||
|
|
||
|
|
||
| // @Test | ||
| // void should_return_logs_for_owner_only() throws Exception{ | ||
| // //Arrange | ||
| // //1. Skapa klinik | ||
| // Clinic clinic = new Clinic(); | ||
| // clinic.setName("Test Clinic"); | ||
| // clinic = clinicRepository.save(clinic); | ||
| // | ||
| // //2. Skapa owner user | ||
| // User owner = new User( | ||
| // "Owner Test", | ||
| // "owner@test.com", | ||
| // "password123", | ||
| // Role.OWNER, | ||
| // clinic | ||
| // ); | ||
| // owner = userRepository.save(owner); | ||
| // | ||
| // Pet pet = new Pet( | ||
| // owner, | ||
| // "Doggo", | ||
| // "Dog", | ||
| // "Labrador", | ||
| // LocalDate.of(2020, 1, 1), | ||
| // new BigDecimal("20.5") | ||
| // ); | ||
| // | ||
| // pet = petRepository.save(pet); | ||
| // | ||
| // | ||
| // // 3. Skapa medical record | ||
| // MedicalRecord record = new MedicalRecord(); | ||
| // record.setTitle("Test Record"); | ||
| // record.setOwner(owner); | ||
| // record.setClinic(clinic); | ||
| // record.setCreatedBy(owner); | ||
| // record.setPet(pet); | ||
| // record = medicalRecordRepository.save(record); | ||
| // | ||
| // // 4. skapa activity logs | ||
| // ActivityLog log1 = new ActivityLog( | ||
| // ActivityType.CASE_CREATED, | ||
| // "First log", | ||
| // owner, | ||
| // record | ||
| // ); | ||
| // | ||
| // ActivityLog log2 = new ActivityLog( | ||
| // ActivityType.UPDATED, | ||
| // "Second log", | ||
| // owner, | ||
| // record | ||
| // ); | ||
| // | ||
| // activityLogRepository.save(log1); | ||
| // activityLogRepository.save(log2); | ||
| // | ||
| // // Act och Assert | ||
| // mockMvc.perform(get("/api/activity-logs/record/" + record.getId()) | ||
| // .header("userId", owner.getId().toString())) | ||
| // .andExpect(status().isOk()) | ||
| // .andExpect(jsonPath("$.length()").value(2)); | ||
| // } | ||
| @Test | ||
| void should_return_logs_for_owner_only() throws Exception { | ||
|
|
||
| // Arrange | ||
| Clinic clinic = TestDataFactory.createClinic(clinicRepository); | ||
| User owner = TestDataFactory.createOwner(userRepository, clinic); | ||
| Pet pet = TestDataFactory.createPet(petRepository, owner); | ||
| MedicalRecord record = TestDataFactory.createRecord( | ||
| medicalRecordRepository, owner, clinic, pet | ||
| ); | ||
|
|
||
| TestDataFactory.createLog(activityLogRepository, owner, record, | ||
| ActivityType.CASE_CREATED, "First log"); | ||
|
|
||
| TestDataFactory.createLog(activityLogRepository, owner, record, | ||
| ActivityType.UPDATED, "Second log"); | ||
|
|
||
| // Act & Assert | ||
| mockMvc.perform(get("/api/activity-logs/record/" + record.getId()) | ||
| .header("userId", owner.getId().toString())) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.length()").value(2)); | ||
| } | ||
|
|
||
| @Test | ||
| void should_allow_vet_in_same_clinic_to_see_logs() throws Exception { | ||
|
|
||
| Clinic clinic = TestDataFactory.createClinic(clinicRepository); | ||
|
|
||
| User owner = TestDataFactory.createOwner(userRepository, clinic); | ||
| User vet = new User( | ||
| "Vet", | ||
| "vet@test.com", | ||
| "password", | ||
| Role.VET, | ||
| clinic | ||
| ); | ||
| vet = userRepository.save(vet); | ||
|
|
||
| Pet pet = TestDataFactory.createPet(petRepository, owner); | ||
| MedicalRecord record = TestDataFactory.createRecord( | ||
| medicalRecordRepository, owner, clinic, pet | ||
| ); | ||
|
|
||
| TestDataFactory.createLog(activityLogRepository, owner, record, | ||
| ActivityType.CASE_CREATED, "log"); | ||
|
|
||
| mockMvc.perform(get("/api/activity-logs/record/" + record.getId()) | ||
| .header("userId", vet.getId().toString())) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.length()").value(1)); | ||
| } | ||
|
|
||
| @Test | ||
| void should_filter_out_logs_for_vet_in_other_clinic() throws Exception { | ||
|
|
||
| Clinic clinicA = TestDataFactory.createClinic(clinicRepository); | ||
| Clinic clinicB = TestDataFactory.createClinic(clinicRepository); | ||
|
|
||
| User owner = TestDataFactory.createOwner(userRepository, clinicA); | ||
|
|
||
| User vetOtherClinic = new User( | ||
| "Vet", | ||
| "vet2@test.com", | ||
| "password", | ||
| Role.VET, | ||
| clinicB | ||
| ); | ||
| vetOtherClinic = userRepository.save(vetOtherClinic); | ||
|
|
||
| Pet pet = TestDataFactory.createPet(petRepository, owner); | ||
| MedicalRecord record = TestDataFactory.createRecord( | ||
| medicalRecordRepository, owner, clinicA, pet | ||
| ); | ||
|
|
||
| TestDataFactory.createLog(activityLogRepository, owner, record, | ||
| ActivityType.CASE_CREATED, "log"); | ||
|
|
||
| mockMvc.perform(get("/api/activity-logs/record/" + record.getId()) | ||
| .header("userId", vetOtherClinic.getId().toString())) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.length()").value(0)); | ||
| } | ||
|
|
||
| @Test | ||
| void should_return_400_if_userId_missing() throws Exception { | ||
|
|
||
| mockMvc.perform(get("/api/activity-logs/record/" + UUID.randomUUID())) | ||
| .andExpect(status().isBadRequest()); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.