-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/visa tests #32
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,12 @@ | ||
| package org.example.visacasemanagementsystem; | ||
|
|
||
| import org.example.visacasemanagementsystem.user.UserAuthorization; | ||
| import org.example.visacasemanagementsystem.user.entity.User; | ||
| import org.example.visacasemanagementsystem.user.repository.UserRepository; | ||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
|
|
||
| @SpringBootApplication | ||
| @EnableJpaAuditing | ||
| public class VisaCaseManagementSystemApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(VisaCaseManagementSystemApplication.class, args); | ||
| } | ||
|
|
||
| @Bean | ||
| public CommandLineRunner initData(UserRepository userRepository) { | ||
| return args -> { | ||
| // Skapa en vanlig användare (Applicant) | ||
| if (userRepository.findByEmail("user@test.com").isEmpty()) { | ||
| User user = new User(); | ||
| user.setFullName("USER"); | ||
| user.setEmail("user@test.com"); | ||
| user.setPassword("password"); // Notera: Du behöver sätta lösenord om din entitet kräver det | ||
| user.setUserAuthorization(UserAuthorization.USER); | ||
| userRepository.save(user); | ||
| System.out.println("Testanvändare skapad med ID: " + user.getId()); | ||
|
|
||
| User admin = new User(); | ||
| admin.setFullName("ADMIN"); | ||
| admin.setEmail("user@test.com2"); | ||
| admin.setPassword("password"); // Notera: Du behöver sätta lösenord om din entitet kräver det | ||
| admin.setUserAuthorization(UserAuthorization.ADMIN); | ||
| userRepository.save(admin); | ||
| System.out.println("Test-admin skapad med ID: " + admin.getId()); | ||
| } | ||
|
|
||
| // Skapa en admin (Handler) | ||
| if (userRepository.findByEmail("admin@test.com").isEmpty()) { | ||
| User sysadmin = new User(); | ||
| sysadmin.setFullName("SYSTEM ADMIN"); | ||
| sysadmin.setEmail("admin@test.com"); | ||
| sysadmin.setPassword("password"); | ||
| sysadmin.setUserAuthorization(UserAuthorization.SYSADMIN); | ||
| userRepository.save(sysadmin); | ||
| System.out.println("Test-sysadmin skapad med ID: " + sysadmin.getId()); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,47 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package org.example.visacasemanagementsystem.config; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.visacasemanagementsystem.user.UserAuthorization; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.visacasemanagementsystem.user.entity.User; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.example.visacasemanagementsystem.user.repository.UserRepository; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.boot.CommandLineRunner; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.context.annotation.Bean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.context.annotation.Configuration; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Configuration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class DataInitializer{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Bean | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public CommandLineRunner initData(UserRepository userRepository) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return args -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (userRepository.findByEmail("user@test.com").isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| User user = new User(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user.setFullName("USER"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user.setEmail("user@test.com"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user.setPassword("password"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user.setUserAuthorization(UserAuthorization.USER); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userRepository.save(user); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Testanvändare skapad med ID: " + user.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| User admin = new User(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| admin.setFullName("ADMIN"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| admin.setEmail("user@test.com2"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| admin.setPassword("password"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| admin.setUserAuthorization(UserAuthorization.ADMIN); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userRepository.save(admin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Test-admin skapad med ID: " + admin.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo in admin email and restructure initialization logic. Two issues:
🔧 Proposed fix if (userRepository.findByEmail("user@test.com").isEmpty()) {
User user = new User();
user.setFullName("USER");
user.setEmail("user@test.com");
user.setPassword("password");
user.setUserAuthorization(UserAuthorization.USER);
userRepository.save(user);
System.out.println("Testanvändare skapad med ID: " + user.getId());
+ }
+ if (userRepository.findByEmail("admin@test.com2").isEmpty()) {
User admin = new User();
admin.setFullName("ADMIN");
- admin.setEmail("user@test.com2");
+ admin.setEmail("admin@test.com2");
admin.setPassword("password");
admin.setUserAuthorization(UserAuthorization.ADMIN);
userRepository.save(admin);
System.out.println("Test-admin skapad med ID: " + admin.getId());
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (userRepository.findByEmail("admin@test.com").isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| User sysadmin = new User(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sysadmin.setFullName("SYSTEM ADMIN"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sysadmin.setEmail("admin@test.com"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sysadmin.setPassword("password"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sysadmin.setUserAuthorization(UserAuthorization.SYSADMIN); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userRepository.save(sysadmin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Test-sysadmin skapad med ID: " + sysadmin.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.example.visacasemanagementsystem.config; | ||
|
|
||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
|
|
||
| @Configuration | ||
| @EnableJpaAuditing | ||
| public class JpaConfig { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package org.example.visacasemanagementsystem.visa; | ||
|
|
||
| import org.example.visacasemanagementsystem.audit.AuditEventType; | ||
| import org.example.visacasemanagementsystem.audit.service.AuditService; | ||
| import org.example.visacasemanagementsystem.user.UserAuthorization; | ||
| import org.example.visacasemanagementsystem.user.entity.User; | ||
| import org.example.visacasemanagementsystem.user.repository.UserRepository; | ||
| import org.example.visacasemanagementsystem.visa.dto.CreateVisaDTO; | ||
| import org.example.visacasemanagementsystem.visa.dto.UpdateVisaDTO; | ||
| import org.example.visacasemanagementsystem.visa.entity.Visa; | ||
| import org.example.visacasemanagementsystem.visa.repository.VisaRepository; | ||
| import org.example.visacasemanagementsystem.visa.service.VisaService; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @SpringBootTest | ||
| @Transactional | ||
| @ActiveProfiles("test") | ||
| class VisaServiceIntegrationTest { | ||
|
|
||
| @Autowired | ||
| private VisaService visaService; | ||
| @Autowired | ||
| private VisaRepository visaRepository; | ||
| @Autowired | ||
| private UserRepository userRepository; | ||
|
|
||
| @Autowired | ||
| private AuditService auditService; | ||
|
|
||
| @Test | ||
| void applyForVisa_shouldSaveVisa_WhenDataIsValid() { | ||
| // Arrange | ||
| User user = createAndSaveValidUser(); | ||
|
|
||
| CreateVisaDTO dto = new CreateVisaDTO( | ||
| VisaType.STUDY, "Swedish", "PASS-INT-123", | ||
| LocalDate.now().plusDays(30), | ||
| user.getId() | ||
| ); | ||
|
|
||
| // Act | ||
| visaService.applyForVisa(dto, user.getId()); | ||
|
|
||
| // Assert | ||
| var savedVisas = visaRepository.findAll(); | ||
| assertThat(savedVisas).hasSize(1); | ||
| assertThat(savedVisas.get(0).getPassportNumber()).isEqualTo("PASS-INT-123"); | ||
| assertThat(auditService.findAll()).isNotEmpty(); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void updateVisa_shouldUpdateVisaAndResetStatus_WhenUserIsAuthorized() { | ||
| // Arrange | ||
| User user = createAndSaveValidUser(); | ||
|
|
||
| Visa visa = new Visa(); | ||
| visa.setApplicant(user); | ||
| visa.setVisaStatus(VisaStatus.INCOMPLETE); | ||
| visa.setPassportNumber("OLD-PASS"); | ||
| visa.setNationality("Swedish"); | ||
| visa.setVisaType(VisaType.STUDY); | ||
| visa.setTravelDate(LocalDate.now().plusMonths(2)); | ||
|
|
||
| visa.setStatusInformation("Please fix upload a clearer image of your passport"); | ||
| visa = visaRepository.save(visa); | ||
|
|
||
| UpdateVisaDTO dto = new UpdateVisaDTO( | ||
| visa.getId(), VisaType.TOURIST, VisaStatus.SUBMITTED, | ||
| "Swedish", "NEW-PASS-999", LocalDate.now().plusMonths(1), null | ||
| ); | ||
|
|
||
| // Act | ||
| visaService.updateVisa(visa.getId(), dto, user.getId()); | ||
|
|
||
| // Assert | ||
| Visa updatedVisa = visaRepository.findById(visa.getId()).get(); | ||
|
|
||
| assertThat(updatedVisa.getPassportNumber()).isEqualTo("NEW-PASS-999"); | ||
| assertThat(updatedVisa.getVisaType()).isEqualTo(VisaType.TOURIST); | ||
| assertThat(updatedVisa.getVisaStatus()).isEqualTo(VisaStatus.SUBMITTED); | ||
| assertThat(updatedVisa.getStatusInformation()).isNull(); | ||
|
|
||
| var logs = auditService.findAll(); | ||
|
|
||
| Visa finalVisa = visa; | ||
| assertThat(logs) | ||
| .as("Check that an update log exists in the audit DTO list") | ||
| .anyMatch(log -> | ||
| log.visaCaseId() != null && | ||
| log.visaCaseId().equals(finalVisa.getId()) && | ||
| log.auditEventType() == AuditEventType.UPDATED | ||
| ); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void assignHandler_shouldAssignAdminToVisa_AndChangeStatusToAssigned() { | ||
| // Arrange | ||
| User admin = new User(); | ||
| admin.setFullName("Test Admin"); | ||
| admin.setEmail("admin@test2.com"); | ||
| admin.setPassword("password123"); | ||
| admin.setUserAuthorization(UserAuthorization.ADMIN); | ||
| admin = userRepository.save(admin); | ||
|
|
||
| User applicant = createAndSaveValidUser(); | ||
|
|
||
| Visa visa = new Visa(); | ||
| visa.setApplicant(applicant); | ||
| visa.setVisaStatus(VisaStatus.INCOMPLETE); | ||
| visa.setNationality("Swedish"); | ||
| visa.setPassportNumber("PASS123"); | ||
| visa.setVisaType(VisaType.STUDY); | ||
| visa.setTravelDate(LocalDate.now().plusMonths(1)); | ||
| visa = visaRepository.save(visa); | ||
|
|
||
| // Act | ||
| visaService.assignHandler(visa.getId(), admin.getId()); | ||
|
|
||
| // Assert | ||
| Visa updatedVisa = visaRepository.findById(visa.getId()).orElseThrow(); | ||
|
|
||
| assertThat(updatedVisa.getHandler()).isNotNull(); | ||
| assertThat(updatedVisa.getHandler().getId()).isEqualTo(admin.getId()); | ||
| assertThat(updatedVisa.getVisaStatus()).isEqualTo(VisaStatus.ASSIGNED); | ||
|
|
||
| var logs = auditService.findAll(); | ||
| User finalAdmin = admin; | ||
| assertThat(logs).anyMatch(log -> | ||
| log.visaCaseId().equals(updatedVisa.getId()) && | ||
| log.userId().equals(finalAdmin.getId()) && | ||
| log.auditEventType() == AuditEventType.ASSIGNED && | ||
| log.description().contains("Test Admin") | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| private User createAndSaveValidUser() { | ||
| User user = new User(); | ||
| user.setFullName("Test User"); | ||
| user.setEmail(java.util.UUID.randomUUID() + "@test.com"); // Unik mail varje gång | ||
| user.setPassword("password"); | ||
| user.setUserAuthorization(UserAuthorization.USER); | ||
| return userRepository.save(user); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.