diff --git a/src/main/java/school/hei/haapi/repository/UserRepository.java b/src/main/java/school/hei/haapi/repository/UserRepository.java index 83e25bebf9..e74d3115d0 100644 --- a/src/main/java/school/hei/haapi/repository/UserRepository.java +++ b/src/main/java/school/hei/haapi/repository/UserRepository.java @@ -166,6 +166,9 @@ WITH student_group_flow AS ( List findAllByRefIn(List refs); + /* TODO: it says `late` or `unpaid`, but the real filter is only `late` + Need verification and refactor + */ @Query("select u from User u join Fee f on u.id = f.student.id where f.status = 'LATE' ") List getStudentsWithUnpaidOrLateFee(); diff --git a/src/main/java/school/hei/haapi/service/utils/DataFormatterUtils.java b/src/main/java/school/hei/haapi/service/utils/DataFormatterUtils.java index 761ba333f5..8ad16c9db2 100644 --- a/src/main/java/school/hei/haapi/service/utils/DataFormatterUtils.java +++ b/src/main/java/school/hei/haapi/service/utils/DataFormatterUtils.java @@ -55,6 +55,10 @@ public static boolean isLate(Instant instantToCompare) { Instant now = Instant.now(); + /* TODO: what ? is late mean now.isBefore(`instantToCompare`) + Need verification and refactor + if possible remove that and put it in the class that need it + */ return now.isAfter(instantToCompare); } diff --git a/src/test/java/school/hei/haapi/endpoint/event/PendingMpbsCheckRequestedServiceTest.java b/src/test/java/school/hei/haapi/endpoint/event/PendingMpbsCheckRequestedServiceTest.java index fdc68b5513..8f6fc4bdcf 100644 --- a/src/test/java/school/hei/haapi/endpoint/event/PendingMpbsCheckRequestedServiceTest.java +++ b/src/test/java/school/hei/haapi/endpoint/event/PendingMpbsCheckRequestedServiceTest.java @@ -17,7 +17,7 @@ import static school.hei.haapi.endpoint.rest.model.MobileMoneyType.ORANGE_MONEY; import static school.hei.haapi.endpoint.rest.model.MpbsStatus.PENDING; import static school.hei.haapi.endpoint.rest.model.MpbsStatus.SUCCESS; -import static school.hei.haapi.integration.conf.TestUtils.FEE1_ID; +import static school.hei.haapi.integration.conf.FakeDataProvider.*; import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_ID; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; @@ -25,17 +25,19 @@ import java.util.List; import java.util.Optional; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import school.hei.haapi.endpoint.event.model.PendingMpbsCheckRequested; import school.hei.haapi.http.model.TransactionDetails; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.FakeDataFactory.SomeUserFactory; import school.hei.haapi.model.Fee; import school.hei.haapi.model.User; import school.hei.haapi.model.mpbs.Mpbs; import school.hei.haapi.repository.FeeRepository; +import school.hei.haapi.repository.MpbsRepository; +import school.hei.haapi.repository.UserRepository; import school.hei.haapi.service.FeeService; import school.hei.haapi.service.MobilePaymentService; import school.hei.haapi.service.MpbsService; @@ -49,42 +51,56 @@ class PendingMpbsCheckRequestedServiceTest extends FacadeITMockedThirdParties { @MockBean private MobilePaymentService mobilePaymentService; @Autowired private FeeService feeService; @Autowired private FeeRepository feeRepository; + @Autowired private UserRepository userRepository; + @Autowired private MpbsRepository mpbsRepository; private static final String MOCK_FEE_ID = "mock-fee"; private static final String MOCK_STUDENT_FIRST_NAME = "MOCK"; private static final String MOCK_STUDENT_LAST_NAME = "Student"; private static final String MOCK_STUDENT_EMAIL = "mock@example.com"; + private User student; + private Fee fee; + private Mpbs mpbs; + @BeforeEach void setUp() { setUpEventBridge(eventBridgeClientMock); + setUpData(0); + } + + void setUpData(int totalFeeAmount) { + student = userRepository.save(new SomeUserFactory().build()); + fee = feeRepository.save(somePendingFee(student, totalFeeAmount)); + mpbs = mpbsRepository.save(someMpbs(fee)); } @Test - @Disabled("TODO: dirty, create new student") void verify_mpbs_to_unverified() { - var actualFee = feeService.getById(FEE1_ID); + var actualFee = feeService.getById(fee.getId()); assertDoesNotThrow( () -> subject.accept( PendingMpbsCheckRequested.builder() .toVerify( - mpbsService.getStudentMobilePaymentByFeeId(STUDENT1_ID, FEE1_ID).getFirst()) + mpbsService + .getStudentMobilePaymentByFeeId(student.getId(), fee.getId()) + .getFirst()) .verifyAt(now()) .build())); - var updatedFee = feeService.getById(FEE1_ID); + var updatedFee = feeService.getById(fee.getId()); assertEquals(actualFee, updatedFee); } @Test - @Disabled("TODO: dirty, create new student") void verify_mpbs() { - var fee = feeService.saveAll(List.of(someFeeFor(mockStudent()))).getFirst(); + setUpData(10_000); + var mpbsCreated = Mpbs.builder() .status(PENDING) - .student(mockStudent()) + .student(student) .fee(fee) .amount(fee.getRemainingAmount()) .statusHistory(new ArrayList<>()) @@ -114,10 +130,9 @@ void verify_mpbs() { } @Test - @Disabled("TODO: dirty, create new student") void unverifed_mpbs_ok() { - var toFailFee = feeService.saveAll(List.of(someFeeFor(mockStudent()))).getFirst(); - var toPendFee = feeService.saveAll(List.of(someFeeFor(mockStudent()))).getFirst(); + var toFailFee = feeService.saveAll(List.of(someFeeFor(student))).getFirst(); + var toPendFee = feeService.saveAll(List.of(someFeeFor(student))).getFirst(); when(mobilePaymentService.findTransactionByMpbs(any())).thenReturn(empty()); assertDoesNotThrow( () -> diff --git a/src/test/java/school/hei/haapi/endpoint/rest/validator/CreateMpbsValidatorIT.java b/src/test/java/school/hei/haapi/endpoint/rest/validator/CreateMpbsValidatorIT.java index 66f935f99f..204a58cbc2 100644 --- a/src/test/java/school/hei/haapi/endpoint/rest/validator/CreateMpbsValidatorIT.java +++ b/src/test/java/school/hei/haapi/endpoint/rest/validator/CreateMpbsValidatorIT.java @@ -6,7 +6,7 @@ import static school.hei.haapi.endpoint.rest.model.MobileMoneyType.ORANGE_MONEY; import static school.hei.haapi.integration.StudentIT.student1; import static school.hei.haapi.integration.conf.TestUtils.anAvailableRandomPort; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import org.junit.jupiter.api.BeforeEach; @@ -30,7 +30,7 @@ public class CreateMpbsValidatorIT extends MockedThirdParties { @BeforeEach void setUp() { - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/endpoint/rest/validator/CreateStudentWorkFileValidatorIT.java b/src/test/java/school/hei/haapi/endpoint/rest/validator/CreateStudentWorkFileValidatorIT.java index a5bc6084d5..c559672c1e 100644 --- a/src/test/java/school/hei/haapi/endpoint/rest/validator/CreateStudentWorkFileValidatorIT.java +++ b/src/test/java/school/hei/haapi/endpoint/rest/validator/CreateStudentWorkFileValidatorIT.java @@ -6,7 +6,7 @@ import static school.hei.haapi.endpoint.rest.model.ProfessionalExperienceFileTypeEnum.WORKER_STUDENT; import static school.hei.haapi.integration.StudentIT.student1; import static school.hei.haapi.integration.conf.TestUtils.anAvailableRandomPort; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import java.time.Instant; @@ -37,7 +37,7 @@ private static ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/AdminIT.java b/src/test/java/school/hei/haapi/integration/AdminIT.java index 18d82a790a..728f91a58c 100644 --- a/src/test/java/school/hei/haapi/integration/AdminIT.java +++ b/src/test/java/school/hei/haapi/integration/AdminIT.java @@ -22,8 +22,7 @@ private ApiClient anApiClient(String token) { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, teacher1()); } diff --git a/src/test/java/school/hei/haapi/integration/AdvancedFeeStatsServiceIT.java b/src/test/java/school/hei/haapi/integration/AdvancedFeeStatsServiceIT.java index afbeb88da0..356bfc9c9e 100644 --- a/src/test/java/school/hei/haapi/integration/AdvancedFeeStatsServiceIT.java +++ b/src/test/java/school/hei/haapi/integration/AdvancedFeeStatsServiceIT.java @@ -14,8 +14,7 @@ import static school.hei.haapi.endpoint.rest.model.FeeStatusEnum.UNPAID; import static school.hei.haapi.endpoint.rest.model.FeeTypeEnum.TUITION; import static school.hei.haapi.integration.conf.TestUtils.MANAGER1_TOKEN; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.model.statistics.AdvancedFeeStats.AdvancedFeeStatsCountType.ACCOUNTING; import static school.hei.haapi.model.statistics.AdvancedFeeStats.AdvancedFeeStatsCountType.RECEIPT; @@ -129,8 +128,7 @@ private static Fee createFeeDueJuneLate() { @BeforeEach void setUp() { setUpEventBridge(eventBridgeClientMock); - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); } @Test diff --git a/src/test/java/school/hei/haapi/integration/AnnouncementIT.java b/src/test/java/school/hei/haapi/integration/AnnouncementIT.java index db3f69d71b..3a4c1806ba 100644 --- a/src/test/java/school/hei/haapi/integration/AnnouncementIT.java +++ b/src/test/java/school/hei/haapi/integration/AnnouncementIT.java @@ -24,8 +24,7 @@ import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; import static school.hei.haapi.integration.conf.TestUtils.createAnnouncementWithGroupTarget; import static school.hei.haapi.integration.conf.TestUtils.expectedAnnouncementCreated1; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import java.time.Instant; @@ -242,8 +241,7 @@ void admin_react_announcement_ok() throws ApiException { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } } diff --git a/src/test/java/school/hei/haapi/integration/CommentIT.java b/src/test/java/school/hei/haapi/integration/CommentIT.java index 93d035f9f0..33953a6239 100644 --- a/src/test/java/school/hei/haapi/integration/CommentIT.java +++ b/src/test/java/school/hei/haapi/integration/CommentIT.java @@ -26,8 +26,7 @@ class CommentIT extends FacadeITMockedThirdParties { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/CorIT.java b/src/test/java/school/hei/haapi/integration/CorIT.java index 72df26e975..66aefc07d0 100644 --- a/src/test/java/school/hei/haapi/integration/CorIT.java +++ b/src/test/java/school/hei/haapi/integration/CorIT.java @@ -9,12 +9,10 @@ import static school.hei.haapi.integration.conf.FakeDataProvider.someCor; import static school.hei.haapi.integration.conf.FakeDataProvider.someCorComment; import static school.hei.haapi.integration.conf.FakeDataProvider.someCorCommentInfo; -import static school.hei.haapi.integration.conf.FakeDataProvider.someStudent; import static school.hei.haapi.integration.conf.TestUtils.MANAGER1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import com.github.javafaker.Faker; import java.time.Instant; @@ -31,6 +29,7 @@ import school.hei.haapi.endpoint.rest.mapper.CorMapper; import school.hei.haapi.endpoint.rest.model.CrupdateCor; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.FakeDataFactory.SomeUserFactory; import school.hei.haapi.integration.conf.TestUtils; import school.hei.haapi.model.Cor; import school.hei.haapi.model.CorStatus; @@ -54,15 +53,14 @@ class CorIT extends FacadeITMockedThirdParties { @BeforeEach void setUp() { - axelWithCor = userRepository.save(someStudent("axel")); - tolotraWithoutCor = userRepository.save(someStudent("tolotra")); + axelWithCor = userRepository.save(new SomeUserFactory().firstName("axel").build()); + tolotraWithoutCor = userRepository.save(new SomeUserFactory().firstName("tolotra").build()); corAxel = corRepository.save(someCor(axelWithCor, Instant.parse("2025-01-01T10:00:00Z"))); someCorComment(faker.number().numberBetween(0, 2)) .forEach(c -> corCommentService.addCommentByCorId(corAxel.getId(), c)); corAxel = corRepository.findById(corAxel.getId()).get(); - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); when(cognitoComponentMock.getEmailByIdToken(axelToken)).thenReturn(axelWithCor.getEmail()); when(casdoorAuthServiceMock.parseJwtToken(axelToken)).thenReturn(getCasdoorAxel()); } diff --git a/src/test/java/school/hei/haapi/integration/CourseAssignmentIT.java b/src/test/java/school/hei/haapi/integration/CourseAssignmentIT.java index 97279d577a..d1692e6523 100644 --- a/src/test/java/school/hei/haapi/integration/CourseAssignmentIT.java +++ b/src/test/java/school/hei/haapi/integration/CourseAssignmentIT.java @@ -8,8 +8,7 @@ import static school.hei.haapi.integration.conf.TestUtils.TEACHER1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.assertCourseAssignmentsIgnoringGroupCreationDateTime; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsApiException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.test_data.CourseAssignmentTestData.createCourseAssignment; import static school.hei.haapi.integration.test_data.CourseAssignmentTestData.createCrupdateCourseAssignment; @@ -94,8 +93,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); setUpTestData(); } diff --git a/src/test/java/school/hei/haapi/integration/CourseIT.java b/src/test/java/school/hei/haapi/integration/CourseIT.java index 361eb289a3..ee66761daa 100644 --- a/src/test/java/school/hei/haapi/integration/CourseIT.java +++ b/src/test/java/school/hei/haapi/integration/CourseIT.java @@ -14,8 +14,7 @@ import static school.hei.haapi.integration.conf.TestUtils.course3; import static school.hei.haapi.integration.conf.TestUtils.createCourse; import static school.hei.haapi.integration.conf.TestUtils.isBefore; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.someCreatableCourseList; @@ -43,8 +42,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/EventIT.java b/src/test/java/school/hei/haapi/integration/EventIT.java index 4b0c597710..14502bfae1 100644 --- a/src/test/java/school/hei/haapi/integration/EventIT.java +++ b/src/test/java/school/hei/haapi/integration/EventIT.java @@ -27,8 +27,7 @@ import static school.hei.haapi.integration.conf.TestUtils.event2; import static school.hei.haapi.integration.conf.TestUtils.event3; import static school.hei.haapi.integration.conf.TestUtils.group1; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.student1AttendEvent2; import static school.hei.haapi.integration.conf.TestUtils.student1MissEvent1; @@ -65,8 +64,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/ExamIT.java b/src/test/java/school/hei/haapi/integration/ExamIT.java index a9915abf9d..90c66e0e98 100644 --- a/src/test/java/school/hei/haapi/integration/ExamIT.java +++ b/src/test/java/school/hei/haapi/integration/ExamIT.java @@ -26,8 +26,7 @@ import static school.hei.haapi.integration.conf.TestUtils.grade1; import static school.hei.haapi.integration.conf.TestUtils.grade2; import static school.hei.haapi.integration.conf.TestUtils.group1; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.studentGrade1; import static school.hei.haapi.integration.conf.TestUtils.teacher1; @@ -60,8 +59,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/FeeIT.java b/src/test/java/school/hei/haapi/integration/FeeIT.java index 5030f0ceb6..15b3c018b6 100644 --- a/src/test/java/school/hei/haapi/integration/FeeIT.java +++ b/src/test/java/school/hei/haapi/integration/FeeIT.java @@ -34,8 +34,7 @@ import static school.hei.haapi.integration.conf.TestUtils.fee3; import static school.hei.haapi.integration.conf.TestUtils.fee4; import static school.hei.haapi.integration.conf.TestUtils.requestFile; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import jakarta.persistence.EntityManager; @@ -98,8 +97,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/FeeTemplateIT.java b/src/test/java/school/hei/haapi/integration/FeeTemplateIT.java index ec997b23d2..5788c92013 100644 --- a/src/test/java/school/hei/haapi/integration/FeeTemplateIT.java +++ b/src/test/java/school/hei/haapi/integration/FeeTemplateIT.java @@ -12,8 +12,7 @@ import static school.hei.haapi.integration.conf.TestUtils.feeTemplate1; import static school.hei.haapi.integration.conf.TestUtils.feeTemplate2; import static school.hei.haapi.integration.conf.TestUtils.feeTemplate3; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.updateFeeTemplate1; @@ -39,8 +38,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/GradeIT.java b/src/test/java/school/hei/haapi/integration/GradeIT.java index e15d7b48aa..5c75570784 100644 --- a/src/test/java/school/hei/haapi/integration/GradeIT.java +++ b/src/test/java/school/hei/haapi/integration/GradeIT.java @@ -21,8 +21,7 @@ import static school.hei.haapi.integration.conf.TestUtils.assertBadRequestException; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsApiException; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.test_data.CourseAssignmentTestData.createCourseAssignment; import static school.hei.haapi.integration.test_data.CourseTestData.prog1; @@ -183,8 +182,7 @@ private void setUpTestData() { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); setUpTestData(); } diff --git a/src/test/java/school/hei/haapi/integration/GroupFlowIT.java b/src/test/java/school/hei/haapi/integration/GroupFlowIT.java index f9c4e07fd9..33a2491ac9 100644 --- a/src/test/java/school/hei/haapi/integration/GroupFlowIT.java +++ b/src/test/java/school/hei/haapi/integration/GroupFlowIT.java @@ -14,8 +14,7 @@ import static school.hei.haapi.integration.conf.TestUtils.STUDENT2_ID; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsApiException; import static school.hei.haapi.integration.conf.TestUtils.group2; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; @@ -41,8 +40,7 @@ public class GroupFlowIT extends FacadeITMockedThirdParties { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/GroupIT.java b/src/test/java/school/hei/haapi/integration/GroupIT.java index 6704072dbb..c583ca9e01 100644 --- a/src/test/java/school/hei/haapi/integration/GroupIT.java +++ b/src/test/java/school/hei/haapi/integration/GroupIT.java @@ -17,8 +17,7 @@ import static school.hei.haapi.integration.conf.TestUtils.group3; import static school.hei.haapi.integration.conf.TestUtils.group5; import static school.hei.haapi.integration.conf.TestUtils.isValidUUID; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.test_data.GroupTestData.createGroupFlow; import static school.hei.haapi.integration.test_data.GroupTestData.g1; @@ -153,8 +152,7 @@ public static CreateGroup groupToCreateGroup(Group group) { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); setUpTestData(); } diff --git a/src/test/java/school/hei/haapi/integration/LetterIT.java b/src/test/java/school/hei/haapi/integration/LetterIT.java index 93af5e1cb6..5426937c52 100644 --- a/src/test/java/school/hei/haapi/integration/LetterIT.java +++ b/src/test/java/school/hei/haapi/integration/LetterIT.java @@ -31,8 +31,7 @@ import static school.hei.haapi.integration.conf.TestUtils.letter1; import static school.hei.haapi.integration.conf.TestUtils.letter2; import static school.hei.haapi.integration.conf.TestUtils.letter3; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.teacherLetter; @@ -74,8 +73,7 @@ class LetterIT extends FacadeITMockedThirdParties { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/ManagerIT.java b/src/test/java/school/hei/haapi/integration/ManagerIT.java index b5b6d7cd36..02a3d4caf2 100644 --- a/src/test/java/school/hei/haapi/integration/ManagerIT.java +++ b/src/test/java/school/hei/haapi/integration/ManagerIT.java @@ -8,8 +8,7 @@ import static school.hei.haapi.integration.conf.TestUtils.TEACHER1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; import static school.hei.haapi.integration.conf.TestUtils.coordinatesWithValues; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.uploadProfilePicture; @@ -90,8 +89,7 @@ public static CrupdateManager someUpdatableManager1() { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, manager1()); } diff --git a/src/test/java/school/hei/haapi/integration/MonitorIT.java b/src/test/java/school/hei/haapi/integration/MonitorIT.java index 2d1f35ba52..df3066720f 100644 --- a/src/test/java/school/hei/haapi/integration/MonitorIT.java +++ b/src/test/java/school/hei/haapi/integration/MonitorIT.java @@ -12,8 +12,7 @@ import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; import static school.hei.haapi.integration.conf.TestUtils.coordinatesWithNullValues; import static school.hei.haapi.integration.conf.TestUtils.coordinatesWithValues; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import java.time.Instant; @@ -128,8 +127,7 @@ public static Coordinates monitor1Coordinates() { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); } diff --git a/src/test/java/school/hei/haapi/integration/MonitoringStudentIT.java b/src/test/java/school/hei/haapi/integration/MonitoringStudentIT.java index 3600686d4c..d1115aff90 100644 --- a/src/test/java/school/hei/haapi/integration/MonitoringStudentIT.java +++ b/src/test/java/school/hei/haapi/integration/MonitoringStudentIT.java @@ -18,8 +18,7 @@ import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; import static school.hei.haapi.integration.conf.TestUtils.monitor1Link; import static school.hei.haapi.integration.conf.TestUtils.monitor2Link; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.student1; import static school.hei.haapi.integration.conf.TestUtils.student2; @@ -72,8 +71,7 @@ private ApiClient anApiClient(String token) { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpTestData(); } diff --git a/src/test/java/school/hei/haapi/integration/MpbsIT.java b/src/test/java/school/hei/haapi/integration/MpbsIT.java index 347ffa5b18..17089e4c02 100644 --- a/src/test/java/school/hei/haapi/integration/MpbsIT.java +++ b/src/test/java/school/hei/haapi/integration/MpbsIT.java @@ -10,6 +10,7 @@ import static school.hei.haapi.endpoint.rest.model.MobileMoneyType.ORANGE_MONEY; import static school.hei.haapi.endpoint.rest.model.MpbsStatus.PENDING; import static school.hei.haapi.integration.StudentIT.student1; +import static school.hei.haapi.integration.conf.FakeDataProvider.someMpbs; import static school.hei.haapi.integration.conf.TestUtils.FEE1_ID; import static school.hei.haapi.integration.conf.TestUtils.FEE2_ID; import static school.hei.haapi.integration.conf.TestUtils.MANAGER1_TOKEN; @@ -18,8 +19,8 @@ import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.STUDENT2_ID; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoorUser; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.model.User.Role.STUDENT; @@ -31,7 +32,6 @@ import java.util.List; import java.util.Random; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -40,10 +40,16 @@ import school.hei.haapi.endpoint.rest.api.PayingApi; import school.hei.haapi.endpoint.rest.client.ApiClient; import school.hei.haapi.endpoint.rest.client.ApiException; +import school.hei.haapi.endpoint.rest.mapper.MpbsMapper; import school.hei.haapi.endpoint.rest.model.*; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.FakeDataFactory.SomeUserFactory; +import school.hei.haapi.integration.conf.FakeDataProvider; import school.hei.haapi.integration.conf.TestUtils; import school.hei.haapi.model.User; +import school.hei.haapi.repository.FeeRepository; +import school.hei.haapi.repository.MpbsRepository; +import school.hei.haapi.repository.UserRepository; import school.hei.haapi.service.UserService; import software.amazon.awssdk.services.eventbridge.EventBridgeClient; @@ -52,13 +58,26 @@ public class MpbsIT extends FacadeITMockedThirdParties { @MockBean private EventBridgeClient eventBridgeClientMock; @Autowired private UserService userService; + @Autowired private UserRepository userRepository; + @Autowired private FeeRepository feeRepository; + @Autowired private MpbsRepository mpbsRepository; + @Autowired private MpbsMapper mpbsMapper; + + private User student; + private school.hei.haapi.model.Fee fee; + private school.hei.haapi.model.mpbs.Mpbs mpbs; + private static final String studentToken = "studentToken"; @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); + + student = userRepository.save(new SomeUserFactory().build()); + fee = feeRepository.save(FakeDataProvider.somePendingFee(student)); + mpbs = mpbsRepository.save(someMpbs(fee)); + setUpCognitoAndCasdoorUser(casdoorAuthServiceMock, cognitoComponentMock, studentToken, student); } private ApiClient anApiClient(String token) { @@ -112,47 +131,41 @@ void monitor_read_others_student_mobile_money_ko() { } @Test - @Disabled("TODO: dirty, create new student") void student_update_mobile_payment_ok() throws ApiException { - ApiClient student1Client = anApiClient(STUDENT1_TOKEN); - PayingApi api = new PayingApi(student1Client); + var api = new PayingApi(anApiClient(studentToken)); - Mpbs actual0 = api.getMpbs(STUDENT1_ID, FEE1_ID).getFirst(); - assertEquals(expectedMpbs1(), actual0); + var storedMpbs = api.getMpbs(student.getId(), fee.getId()).getFirst(); + assertEquals(mpbsMapper.toRest(mpbs), storedMpbs); - Mpbs inUpdate = api.crupdateMpbs(STUDENT1_ID, FEE1_ID, updatableMpbs1()); - var updated = expectedMpbs1(); + var mpbsInUpdate = api.crupdateMpbs(student.getId(), fee.getId(), updatableMpbs1()); + var updated = mpbsMapper.toRest(mpbs); updated.setPspId("MP240726.1541.D88426"); updated.setPspType(ORANGE_MONEY); - assertEquals(updated.getStudentId(), inUpdate.getStudentId()); - assertEquals(updated.getPspId(), inUpdate.getPspId()); - assertEquals(updated.getFeeId(), inUpdate.getFeeId()); - assertEquals(updated.getPspType(), inUpdate.getPspType()); + assertEquals(updated.getStudentId(), mpbsInUpdate.getStudentId()); + assertEquals(updated.getPspId(), mpbsInUpdate.getPspId()); + assertEquals(updated.getFeeId(), mpbsInUpdate.getFeeId()); + assertEquals(updated.getPspType(), mpbsInUpdate.getPspType()); // Assert that one fee has mpbs - Mpbs actual1 = api.getMpbs(STUDENT1_ID, FEE1_ID).getFirst(); + Mpbs actual1 = api.getMpbs(student.getId(), fee.getId()).getFirst(); actual1.setCreationDatetime(actual1.getCreationDatetime().truncatedTo(MINUTES)); - inUpdate.setCreationDatetime(inUpdate.getCreationDatetime().truncatedTo(MINUTES)); - assertEquals(actual1, inUpdate); + mpbsInUpdate.setCreationDatetime(mpbsInUpdate.getCreationDatetime().truncatedTo(MINUTES)); + assertEquals(actual1, mpbsInUpdate); // Assert that when we get fees it not throws error 500 - List actualFee = api.getStudentFees(STUDENT1_ID, 1, 10, null); - assertEquals(7, actualFee.size()); + var studentFee = api.getStudentFees(student.getId(), 1, 10, null); + assertEquals(1, studentFee.size()); } @Test - @Disabled("TODO: dirty, create new student") void student_create_mobile_payment_ok() throws ApiException { - ApiClient student1Client = anApiClient(STUDENT1_TOKEN); - PayingApi api = new PayingApi(student1Client); - - ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); - PayingApi manager1Api = new PayingApi(manager1Client); + var api = new PayingApi(anApiClient(studentToken)); + var manager1Api = new PayingApi(anApiClient(MANAGER1_TOKEN)); - Fee actualFee = + var actualFee = manager1Api .createStudentFees( - STUDENT1_ID, + student.getId(), List.of( new CreateFee() .totalAmount(5000) @@ -163,17 +176,16 @@ void student_create_mobile_payment_ok() throws ApiException { .creationDatetime(now()) .comment("test"))) .getFirst(); - assertEquals(STUDENT1_ID, actualFee.getStudentId()); + assertEquals(student.getId(), actualFee.getStudentId()); - Mpbs actual = - api.crupdateMpbs( - STUDENT1_ID, actualFee.getId(), createableMpbsFromFeeIdWithStudent1(actualFee.getId())); + var crupdateMpbs = createableMpbsFromFeeIdWithStudent(actualFee.getId()); + var actualMpbs = api.crupdateMpbs(student.getId(), actualFee.getId(), crupdateMpbs); - assertEquals(createableMpbs1().getStudentId(), actual.getStudentId()); - assertEquals(createableMpbs1().getPspId(), actual.getPspId()); - assertEquals(createableMpbs1().getPspType(), actual.getPspType()); + assertEquals(crupdateMpbs.getStudentId(), actualMpbs.getStudentId()); + assertEquals(crupdateMpbs.getPspId(), actualMpbs.getPspId()); + assertEquals(crupdateMpbs.getPspType(), actualMpbs.getPspType()); - Fee updatedFee = api.getStudentFeeById(STUDENT1_ID, actualFee.getId()); + var updatedFee = api.getStudentFeeById(student.getId(), actualFee.getId()); assertEquals(FeeStatusEnum.PENDING, updatedFee.getStatus()); } @@ -241,11 +253,11 @@ private User createStudentForMobilePayments() { return userService.saveAll(List.of(randomStudent)).getFirst(); } - public static CrupdateMpbs updatableMpbs1() { + public CrupdateMpbs updatableMpbs1() { return new CrupdateMpbs() - .id("mpbs1_id") - .studentId(STUDENT1_ID) - .feeId(FEE1_ID) + .id(mpbs.getId()) + .studentId(student.getId()) + .feeId(fee.getId()) .pspId("MP240726.1541.D88426") .pspType(ORANGE_MONEY); } @@ -263,16 +275,8 @@ public static Mpbs expectedMpbs1() { .status(PENDING); } - public static CrupdateMpbs createableMpbs1() { - return new CrupdateMpbs() - .studentId(STUDENT1_ID) - .feeId(FEE2_ID) - .pspType(ORANGE_MONEY) - .pspId("MP240726.1541.D88425"); - } - - public static CrupdateMpbs createableMpbsFromFeeIdWithStudent1(String feeId) { - return createableMpbsFromFeeIdForStudent(STUDENT1_ID, feeId); + public CrupdateMpbs createableMpbsFromFeeIdWithStudent(String feeId) { + return createableMpbsFromFeeIdForStudent(student.getId(), feeId); } public static CrupdateMpbs createableMpbsFromFeeIdForStudent(String studentId, String feeId) { diff --git a/src/test/java/school/hei/haapi/integration/MpbsVerificationIT.java b/src/test/java/school/hei/haapi/integration/MpbsVerificationIT.java index 08150c9fc2..0d874f40d4 100644 --- a/src/test/java/school/hei/haapi/integration/MpbsVerificationIT.java +++ b/src/test/java/school/hei/haapi/integration/MpbsVerificationIT.java @@ -16,8 +16,7 @@ import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.STUDENT2_ID; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import java.time.Instant; @@ -48,8 +47,7 @@ public class MpbsVerificationIT extends FacadeITMockedThirdParties { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); setUpMobileMock(orangeApiMock); setUpMobileMock(mvolaApiMock); diff --git a/src/test/java/school/hei/haapi/integration/OrganizerIT.java b/src/test/java/school/hei/haapi/integration/OrganizerIT.java index 259c4bf7a5..44df6dd166 100644 --- a/src/test/java/school/hei/haapi/integration/OrganizerIT.java +++ b/src/test/java/school/hei/haapi/integration/OrganizerIT.java @@ -16,8 +16,7 @@ import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_ID; import static school.hei.haapi.integration.conf.TestUtils.TEACHER1_ID; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.student1MissEvent1; import static school.hei.haapi.integration.conf.TestUtils.student3AttendEvent1; @@ -103,8 +102,7 @@ public static Organizer organizer2() { @BeforeEach public void setUp() { setUpEventBridge(eventBridgeClientMock); - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); } @Test diff --git a/src/test/java/school/hei/haapi/integration/PaginationIT.java b/src/test/java/school/hei/haapi/integration/PaginationIT.java index d5121a0610..518ea17716 100644 --- a/src/test/java/school/hei/haapi/integration/PaginationIT.java +++ b/src/test/java/school/hei/haapi/integration/PaginationIT.java @@ -10,8 +10,7 @@ import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.TEACHER1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsApiException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.teacher1; @@ -47,8 +46,7 @@ private ApiClient anApiClient(String token) { @BeforeEach public void setUp() throws ApiException { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); setUpS3Service(fileService, manager1()); diff --git a/src/test/java/school/hei/haapi/integration/PaymentIT.java b/src/test/java/school/hei/haapi/integration/PaymentIT.java index 0646286d7c..173d92be82 100644 --- a/src/test/java/school/hei/haapi/integration/PaymentIT.java +++ b/src/test/java/school/hei/haapi/integration/PaymentIT.java @@ -27,8 +27,7 @@ import static school.hei.haapi.integration.conf.TestUtils.assertThrowsApiException; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; import static school.hei.haapi.integration.conf.TestUtils.creatableFee1; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.model.exception.ApiException.ExceptionType.SERVER_EXCEPTION; @@ -163,8 +162,7 @@ static CreatePayment creatablePayment2() { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); setUpEventBridge(eventBridgeClientMock); } diff --git a/src/test/java/school/hei/haapi/integration/PromotionIT.java b/src/test/java/school/hei/haapi/integration/PromotionIT.java index 46275daa17..508ecbe8df 100644 --- a/src/test/java/school/hei/haapi/integration/PromotionIT.java +++ b/src/test/java/school/hei/haapi/integration/PromotionIT.java @@ -19,8 +19,7 @@ import static school.hei.haapi.integration.conf.TestUtils.promotion22; import static school.hei.haapi.integration.conf.TestUtils.promotion23; import static school.hei.haapi.integration.conf.TestUtils.removeGroupToPromotion3; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import java.io.IOException; @@ -52,8 +51,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/RetakeExamIT.java b/src/test/java/school/hei/haapi/integration/RetakeExamIT.java index 1b8ef269b9..18cdf1ef94 100644 --- a/src/test/java/school/hei/haapi/integration/RetakeExamIT.java +++ b/src/test/java/school/hei/haapi/integration/RetakeExamIT.java @@ -12,8 +12,7 @@ import static school.hei.haapi.integration.conf.TestUtils.ADMIN1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.course1; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import java.util.List; @@ -46,8 +45,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setup() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); when(gradeResultService.getStudentResultSummary(anyString())) diff --git a/src/test/java/school/hei/haapi/integration/RetakeExamSessionIT.java b/src/test/java/school/hei/haapi/integration/RetakeExamSessionIT.java index fc6bda7bca..19eee941ef 100644 --- a/src/test/java/school/hei/haapi/integration/RetakeExamSessionIT.java +++ b/src/test/java/school/hei/haapi/integration/RetakeExamSessionIT.java @@ -8,8 +8,7 @@ import static school.hei.haapi.integration.conf.TestUtils.ADMIN1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.assertBadRequestException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.test_data.RetakeExamSessionTestData.session1; import static school.hei.haapi.integration.test_data.RetakeExamSessionTestData.sessionWithWrongDate; @@ -36,8 +35,7 @@ private ApiClient anApiClient(String token) { @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); when(retakeExamSessionRepository.save(any())).thenReturn(session1()); diff --git a/src/test/java/school/hei/haapi/integration/SchoolFileIT.java b/src/test/java/school/hei/haapi/integration/SchoolFileIT.java index a0d82ea0fc..87d15d13fe 100644 --- a/src/test/java/school/hei/haapi/integration/SchoolFileIT.java +++ b/src/test/java/school/hei/haapi/integration/SchoolFileIT.java @@ -12,8 +12,7 @@ import static school.hei.haapi.integration.conf.TestUtils.MONITOR1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.TEACHER1_TOKEN; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; @@ -44,8 +43,7 @@ public class SchoolFileIT extends FacadeITMockedThirdParties { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); setUpRestTemplate(restTemplateMock); diff --git a/src/test/java/school/hei/haapi/integration/SecurityIT.java b/src/test/java/school/hei/haapi/integration/SecurityIT.java index a582027e6a..c830596fbe 100644 --- a/src/test/java/school/hei/haapi/integration/SecurityIT.java +++ b/src/test/java/school/hei/haapi/integration/SecurityIT.java @@ -8,8 +8,7 @@ import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.SUSPENDED_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.TEACHER1_TOKEN; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import java.io.IOException; @@ -71,8 +70,7 @@ public static Whoami whoisManager1() { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/StaffMemberIT.java b/src/test/java/school/hei/haapi/integration/StaffMemberIT.java index 5381853706..de244cbf70 100644 --- a/src/test/java/school/hei/haapi/integration/StaffMemberIT.java +++ b/src/test/java/school/hei/haapi/integration/StaffMemberIT.java @@ -10,8 +10,7 @@ import static school.hei.haapi.integration.conf.TestUtils.STAFF_MEMBER1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; import static school.hei.haapi.integration.conf.TestUtils.coordinatesWithNullValues; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.teacher1; @@ -56,8 +55,7 @@ private ApiClient anApiClient(String token) { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, teacher1()); } diff --git a/src/test/java/school/hei/haapi/integration/StudentIT.java b/src/test/java/school/hei/haapi/integration/StudentIT.java index 10fdfcf82d..5f5067c1f9 100644 --- a/src/test/java/school/hei/haapi/integration/StudentIT.java +++ b/src/test/java/school/hei/haapi/integration/StudentIT.java @@ -45,8 +45,7 @@ import static school.hei.haapi.integration.conf.TestUtils.group2; import static school.hei.haapi.integration.conf.TestUtils.group3; import static school.hei.haapi.integration.conf.TestUtils.requestFile; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.uploadProfilePicture; @@ -379,8 +378,7 @@ public static Student repeatingStudent2() { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/TeacherIT.java b/src/test/java/school/hei/haapi/integration/TeacherIT.java index 3811289247..ea07058fea 100644 --- a/src/test/java/school/hei/haapi/integration/TeacherIT.java +++ b/src/test/java/school/hei/haapi/integration/TeacherIT.java @@ -21,8 +21,7 @@ import static school.hei.haapi.integration.conf.TestUtils.coordinatesWithNullValues; import static school.hei.haapi.integration.conf.TestUtils.coordinatesWithValues; import static school.hei.haapi.integration.conf.TestUtils.isValidUUID; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.someCreatableTeacherList; @@ -75,8 +74,7 @@ private ApiClient anApiClient(String token) { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, teacher1()); } diff --git a/src/test/java/school/hei/haapi/integration/UserFileIT.java b/src/test/java/school/hei/haapi/integration/UserFileIT.java index ea84b32f90..8c3b46cfd1 100644 --- a/src/test/java/school/hei/haapi/integration/UserFileIT.java +++ b/src/test/java/school/hei/haapi/integration/UserFileIT.java @@ -27,8 +27,7 @@ import static school.hei.haapi.integration.conf.TestUtils.TEACHER1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.assertBadRequestException; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; @@ -76,8 +75,7 @@ public class UserFileIT extends FacadeITMockedThirdParties { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); setUpRestTemplate(restTemplateMock); diff --git a/src/test/java/school/hei/haapi/integration/WorkDocumentIT.java b/src/test/java/school/hei/haapi/integration/WorkDocumentIT.java index 1b1c8d80b8..a57299cdce 100644 --- a/src/test/java/school/hei/haapi/integration/WorkDocumentIT.java +++ b/src/test/java/school/hei/haapi/integration/WorkDocumentIT.java @@ -11,8 +11,7 @@ import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.assertThrowsApiException; import static school.hei.haapi.integration.conf.TestUtils.getMockedFile; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; @@ -40,8 +39,7 @@ public class WorkDocumentIT extends FacadeITMockedThirdParties { @BeforeEach public void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); } diff --git a/src/test/java/school/hei/haapi/integration/conf/FakeDataFactory/SomeDataAbstractFactory.java b/src/test/java/school/hei/haapi/integration/conf/FakeDataFactory/SomeDataAbstractFactory.java new file mode 100644 index 0000000000..56f7649499 --- /dev/null +++ b/src/test/java/school/hei/haapi/integration/conf/FakeDataFactory/SomeDataAbstractFactory.java @@ -0,0 +1,12 @@ +package school.hei.haapi.integration.conf.FakeDataFactory; + +import com.github.javafaker.Faker; +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public abstract class SomeDataAbstractFactory { + protected static final Faker faker = new Faker(); + protected final B builder; + + public abstract T build(); +} diff --git a/src/test/java/school/hei/haapi/integration/conf/FakeDataFactory/SomeUserFactory.java b/src/test/java/school/hei/haapi/integration/conf/FakeDataFactory/SomeUserFactory.java new file mode 100644 index 0000000000..1b1c0d71e8 --- /dev/null +++ b/src/test/java/school/hei/haapi/integration/conf/FakeDataFactory/SomeUserFactory.java @@ -0,0 +1,40 @@ +package school.hei.haapi.integration.conf.FakeDataFactory; + +import static school.hei.haapi.integration.conf.FakeDataProvider.someCoordinates; +import static school.hei.haapi.model.User.Role.STUDENT; + +import java.time.Instant; +import java.util.UUID; +import school.hei.haapi.integration.conf.FakeDataProvider; +import school.hei.haapi.model.User; + +public class SomeUserFactory extends SomeDataAbstractFactory { + public SomeUserFactory() { + super( + User.builder(someCoordinates()) + .id(UUID.randomUUID().toString()) + .role(STUDENT) + .firstName(faker.name().firstName()) + .email(faker.internet().emailAddress()) + .ref(FakeDataProvider.someRef("STD")) + .lastName(faker.name().lastName()) + .address(faker.address().fullAddress()) + .status(User.Status.ENABLED) + .entranceDatetime(Instant.now())); + } + + public SomeUserFactory firstName(String firstName) { + builder.firstName(firstName); + return this; + } + + public SomeUserFactory status(User.Status status) { + builder.status(status); + return this; + } + + @Override + public User build() { + return builder.build(); + } +} diff --git a/src/test/java/school/hei/haapi/integration/conf/FakeDataProvider.java b/src/test/java/school/hei/haapi/integration/conf/FakeDataProvider.java index f571c4223c..defccc6e1a 100644 --- a/src/test/java/school/hei/haapi/integration/conf/FakeDataProvider.java +++ b/src/test/java/school/hei/haapi/integration/conf/FakeDataProvider.java @@ -6,14 +6,15 @@ import static school.hei.haapi.endpoint.rest.model.EnableStatus.ENABLED; import static school.hei.haapi.endpoint.rest.model.FeeCategory.L1; import static school.hei.haapi.endpoint.rest.model.FeeFrequency.MONTHLY; +import static school.hei.haapi.endpoint.rest.model.FeeStatusEnum.LATE; import static school.hei.haapi.endpoint.rest.model.FeeStatusEnum.PENDING; import static school.hei.haapi.endpoint.rest.model.FeeTypeEnum.TUITION; +import static school.hei.haapi.endpoint.rest.model.MobileMoneyType.ORANGE_MONEY; import static school.hei.haapi.integration.conf.TestUtils.COURSE1_ID; import static school.hei.haapi.integration.conf.TestUtils.MANAGER_ID; import static school.hei.haapi.integration.conf.TestUtils.group1; import static school.hei.haapi.model.Event.PlaceName.IVANDRY; import static school.hei.haapi.model.Event.RoomName.UNKNOWN; -import static school.hei.haapi.model.User.Role.STUDENT; import com.github.javafaker.Faker; import java.time.Instant; @@ -78,7 +79,7 @@ public static CrupdateTeacher someCreatableTeacher() { .address(faker.address().fullAddress()); } - private static String someRef(String prefix) { + public static String someRef(String prefix) { return "%s%s".formatted(prefix, faker.number().digits(5)); } @@ -110,13 +111,17 @@ public List someCreatableGroupList(int nbOfGroup) { return groupList; } - public Fee someFee(User student) { + public static Fee somePendingFee(User student) { + return somePendingFee(student, 0); + } + + public static Fee somePendingFee(User student, int totalAmount) { return Fee.builder() .id(UUID.randomUUID().toString()) .comment(faker.lorem().sentence(10)) .student(student) - .totalAmount(0) - .remainingAmount(0) + .totalAmount(totalAmount) + .remainingAmount(totalAmount) .status(PENDING) .updatedAt(Instant.now()) .creationDatetime(Instant.now()) @@ -127,6 +132,10 @@ public Fee someFee(User student) { .build(); } + public static Fee someLateFee(User student, int totalAmount) { + return somePendingFee(student, totalAmount).toBuilder().status(LATE).build(); + } + public Event someEvent() { var beginDatetime = faker @@ -157,10 +166,21 @@ public Event someEvent() { } public Mpbs someMpbs(User student) { + return someMpbs(somePendingFee(student)); + } + + public static Mpbs someMpbs(Fee fee) { + return someMpbs(fee, fee.getRemainingAmount()); + } + + public static Mpbs someMpbs(Fee fee, Integer amount) { return Mpbs.builder() .status(faker.options().option(MpbsStatus.class)) - .fee(someFee(student)) - .amount(faker.number().numberBetween(100, 10_000)) + .fee(fee) + .amount(amount) + .mobileMoneyType(ORANGE_MONEY) + .pspId(faker.regexify("MP\\d{6}\\.\\d{4}\\.D\\d{5}")) + .student(fee.getStudent()) .build(); } @@ -201,20 +221,6 @@ public static CreateEvent someCreatableEventByManager1(EventType eventType) { Instant.parse("2023-12-08T10:00:00.00Z")); } - public static User someStudent(String firstName) { - return User.builder(someCoordinates()) - .id(UUID.randomUUID().toString()) - .role(STUDENT) - .firstName(firstName) - .email(faker.internet().emailAddress()) - .ref(someRef("STD")) - .lastName(faker.name().lastName()) - .address(faker.address().fullAddress()) - .status(User.Status.ENABLED) - .entranceDatetime(Instant.now()) - .build(); - } - public static Coordinates someCoordinates() { return new Coordinates() .latitude(faker.number().randomDouble(2, -90, 90)) diff --git a/src/test/java/school/hei/haapi/integration/conf/TestUtils.java b/src/test/java/school/hei/haapi/integration/conf/TestUtils.java index 9dbb97198c..dc522f66ea 100644 --- a/src/test/java/school/hei/haapi/integration/conf/TestUtils.java +++ b/src/test/java/school/hei/haapi/integration/conf/TestUtils.java @@ -69,6 +69,7 @@ import org.springframework.core.io.Resource; import org.springframework.web.util.UriComponentsBuilder; import org.testcontainers.shaded.com.google.common.primitives.Bytes; +import org.testcontainers.shaded.org.checkerframework.checker.nullness.qual.NonNull; import school.hei.haapi.endpoint.rest.client.ApiClient; import school.hei.haapi.endpoint.rest.client.ApiException; import school.hei.haapi.endpoint.rest.model.Announcement; @@ -120,6 +121,7 @@ import school.hei.haapi.endpoint.rest.security.casdoorAuthentication.config.CertificateLoader; import school.hei.haapi.endpoint.rest.security.cognito.CognitoComponent; import school.hei.haapi.http.model.TransactionDetails; +import school.hei.haapi.model.User; import school.hei.haapi.model.exception.BadRequestException; import school.hei.haapi.model.exception.NotFoundException; import school.hei.haapi.service.aws.FileService; @@ -355,25 +357,88 @@ public static CasdoorUser getCasdoorUserSuspended() { return user; } - public static void setUpCasdoor( - CasdoorAuthService casdoorAuthService, CertificateLoader certificateLoader) { + public static CasdoorUser userToCasdoorUser(User user) { + CasdoorUser casdoorUser = new CasdoorUser(); + if (user.getEmail() == null) throw new RuntimeException("Email is null"); + casdoorUser.setEmail(user.getEmail()); + + var casdoorRole = new CasdoorRole(); + casdoorRole.setOwner("dummy"); + if (user.getRole() == null) throw new RuntimeException("Role is null"); + casdoorRole.setName(user.getRole().toString().toLowerCase()); + var roleUsers = new String[] {"dummy/casdoorUser"}; + casdoorRole.setUsers(roleUsers); + casdoorUser.setRoles(List.of(casdoorRole)); + + return casdoorUser; + } + + public static void setUpCognitoAndCasdoor( + CasdoorAuthService casdoorAuthService, + CognitoComponent cognitoComponent, + CertificateLoader certificateLoader) { given(certificateLoader.getCertificate()).willReturn("mocked-certificate"); - when(casdoorAuthService.parseJwtToken(TEACHER1_TOKEN)).thenReturn(getCasdoorUserTeacher1()); - when(casdoorAuthService.parseJwtToken(MANAGER1_TOKEN)).thenReturn(getCasdoorUserManager1()); - when(casdoorAuthService.parseJwtToken(STUDENT1_TOKEN)).thenReturn(getCasdoorUserStudent1()); - when(casdoorAuthService.parseJwtToken(STUDENT2_TOKEN)).thenReturn(getCasdoorUserStudent2()); - when(casdoorAuthService.parseJwtToken(STUDENT8_TOKEN)).thenReturn(getCasdoorUserStudent8()); - when(casdoorAuthService.parseJwtToken(STUDENT11_TOKEN)).thenReturn(getCasdoorUserStudent11()); - when(casdoorAuthService.parseJwtToken(STUDENT12_TOKEN)).thenReturn(getCasdoorUserStudent12()); - when(casdoorAuthService.parseJwtToken(STUDENT13_TOKEN)).thenReturn(getCasdoorUserStudent13()); - when(casdoorAuthService.parseJwtToken(MONITOR1_TOKEN)).thenReturn(getCasdoorUserMonitor1()); - when(casdoorAuthService.parseJwtToken(MONITOR2_TOKEN)).thenReturn(getCasdoorUserMonitor2()); - when(casdoorAuthService.parseJwtToken(ORGANIZER1_TOKEN)).thenReturn(getCasdoorUserOrganizer1()); - when(casdoorAuthService.parseJwtToken(ORGANIZER2_TOKEN)).thenReturn(getCasdoorUserOrganizer2()); - when(casdoorAuthService.parseJwtToken(STAFF_MEMBER1_TOKEN)) - .thenReturn(getCasdoorUserStaffMember1()); - when(casdoorAuthService.parseJwtToken(ADMIN1_TOKEN)).thenReturn(getCasdoorUserAdmin1()); - when(casdoorAuthService.parseJwtToken(SUSPENDED_TOKEN)).thenReturn(getCasdoorUserSuspended()); + + setUpCognitoAndCasdoorUser(cognitoComponent, BAD_TOKEN); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, STUDENT1_TOKEN, getCasdoorUserStudent1()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, STUDENT2_TOKEN, getCasdoorUserStudent2()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, STUDENT11_TOKEN, getCasdoorUserStudent11()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, STUDENT12_TOKEN, getCasdoorUserStudent12()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, STUDENT13_TOKEN, getCasdoorUserStudent13()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, MONITOR1_TOKEN, getCasdoorUserMonitor1()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, MONITOR2_TOKEN, getCasdoorUserMonitor2()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, STUDENT8_TOKEN, getCasdoorUserStudent8()); + + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, TEACHER1_TOKEN, getCasdoorUserTeacher1()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, MANAGER1_TOKEN, getCasdoorUserManager1()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, ORGANIZER1_TOKEN, getCasdoorUserOrganizer1()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, ORGANIZER2_TOKEN, getCasdoorUserOrganizer2()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, STAFF_MEMBER1_TOKEN, getCasdoorUserStaffMember1()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, ADMIN1_TOKEN, getCasdoorUserAdmin1()); + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, SUSPENDED_TOKEN, getCasdoorUserSuspended()); + } + + public static void setUpCognitoAndCasdoorUser( + CasdoorAuthService casdoorAuthService, + CognitoComponent cognitoComponent, + String userToken, + @NonNull CasdoorUser user) { + setUpCasdoorUser(casdoorAuthService, userToken, user); + setUpCognitoUser(cognitoComponent, userToken, user.getEmail()); + } + + public static void setUpCognitoAndCasdoorUser( + CasdoorAuthService casdoorAuthService, + CognitoComponent cognitoComponent, + String userToken, + @NonNull User user) { + setUpCognitoAndCasdoorUser( + casdoorAuthService, cognitoComponent, userToken, userToCasdoorUser(user)); + } + + public static void setUpCognitoAndCasdoorUser( + CognitoComponent cognitoComponent, String badToken) { + setUpCognitoUser(cognitoComponent, badToken, null); + } + + public static void setUpCasdoorUser( + CasdoorAuthService casdoorAuthService, String userToken, CasdoorUser user) { + when(casdoorAuthService.parseJwtToken(userToken)).thenReturn(user); } public static ApiClient anApiClient(String token, int serverPort) { @@ -387,31 +452,9 @@ public static ApiClient anApiClient(String token, int serverPort) { return client; } - public static void setUpCognito(CognitoComponent cognitoComponent) { - when(cognitoComponent.getEmailByIdToken(BAD_TOKEN)).thenReturn(null); - when(cognitoComponent.getEmailByIdToken(STUDENT1_TOKEN)).thenReturn("test+ryan@hei.school"); - when(cognitoComponent.getEmailByIdToken(STUDENT2_TOKEN)).thenReturn("test+student2@hei.school"); - when(cognitoComponent.getEmailByIdToken(STUDENT11_TOKEN)) - .thenReturn("test+student11@hei.school"); - when(cognitoComponent.getEmailByIdToken(STUDENT12_TOKEN)) - .thenReturn("test+student12@hei.school"); - when(cognitoComponent.getEmailByIdToken(STUDENT13_TOKEN)) - .thenReturn("test+student13@hei.school"); - when(cognitoComponent.getEmailByIdToken(MONITOR1_TOKEN)).thenReturn("test+monitor@hei.school"); - when(cognitoComponent.getEmailByIdToken(MONITOR2_TOKEN)).thenReturn("test+monitor2@hei.school"); - when(cognitoComponent.getEmailByIdToken(STUDENT8_TOKEN)) - .thenReturn("test+repeating2@hei" + ".school"); - when(cognitoComponent.getEmailByIdToken(TEACHER1_TOKEN)).thenReturn("test+teacher1@hei.school"); - when(cognitoComponent.getEmailByIdToken(MANAGER1_TOKEN)).thenReturn("test+manager1@hei.school"); - when(cognitoComponent.getEmailByIdToken(STAFF_MEMBER1_TOKEN)) - .thenReturn("test+staff@hei.school"); - when(cognitoComponent.getEmailByIdToken(ADMIN1_TOKEN)).thenReturn("test+admin@hei.school"); - when(cognitoComponent.getEmailByIdToken(ORGANIZER1_TOKEN)) - .thenReturn("test+organizer@hei.school"); - when(cognitoComponent.getEmailByIdToken(ORGANIZER2_TOKEN)) - .thenReturn("test+organizer+2@hei.school"); - when(cognitoComponent.getEmailByIdToken(SUSPENDED_TOKEN)) - .thenReturn("test+suspended@hei.school"); + private static void setUpCognitoUser( + CognitoComponent cognitoComponent, String token, String email) { + when(cognitoComponent.getEmailByIdToken(token)).thenReturn(email); } public static void setUpS3Service(FileService fileService, Student user) { @@ -491,13 +534,17 @@ public static TransactionDetails psp2Verification() { } public static CreateFee creatableFee1() { + return creatableFee(Instant.parse("2021-12-08T08:25:24.00Z")); + } + + public static CreateFee creatableFee(Instant dueDatetime) { return new CreateFee() .type(TUITION) .totalAmount(5000) .category(UNKNOWN) .frequency(FeeFrequency.UNKNOWN) .comment("Comment") - .dueDatetime(Instant.parse("2021-12-08T08:25:24.00Z")); + .dueDatetime(dueDatetime); } public static CrupdateStudentFee creatableStudentFee() { diff --git a/src/test/java/school/hei/haapi/model/FeeTest.java b/src/test/java/school/hei/haapi/model/FeeTest.java index 37ef2bd4b7..ad0b9fc058 100644 --- a/src/test/java/school/hei/haapi/model/FeeTest.java +++ b/src/test/java/school/hei/haapi/model/FeeTest.java @@ -24,7 +24,7 @@ class FeeTest { @Test void copy_fee_correct() { User student = new User(); - var fee = fakeDataProvider.someFee(student); + var fee = fakeDataProvider.somePendingFee(student); var feeCopy = new Fee(fee); fee.setCreationDatetime(MIN); feeCopy.setCreationDatetime(MIN); diff --git a/src/test/java/school/hei/haapi/service/DirtyEventServiceTest.java b/src/test/java/school/hei/haapi/service/DirtyEventServiceTest.java index dd24623b32..2077e3e60f 100644 --- a/src/test/java/school/hei/haapi/service/DirtyEventServiceTest.java +++ b/src/test/java/school/hei/haapi/service/DirtyEventServiceTest.java @@ -9,7 +9,7 @@ import static school.hei.haapi.integration.StudentIT.someCreatableStudentList; import static school.hei.haapi.integration.conf.FakeDataProvider.someCreatableEvent; import static school.hei.haapi.integration.conf.TestUtils.MANAGER_ID; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import java.time.Instant; @@ -56,7 +56,7 @@ class DirtyEventServiceTest extends FacadeITMockedThirdParties { @BeforeEach void setUp() { - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); } diff --git a/src/test/java/school/hei/haapi/service/PaymentServiceTest.java b/src/test/java/school/hei/haapi/service/PaymentServiceTest.java index 7cc49aa3f2..5abc67a9da 100644 --- a/src/test/java/school/hei/haapi/service/PaymentServiceTest.java +++ b/src/test/java/school/hei/haapi/service/PaymentServiceTest.java @@ -4,48 +4,33 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static school.hei.haapi.endpoint.rest.model.EnableStatus.ENABLED; import static school.hei.haapi.endpoint.rest.model.EnableStatus.SUSPENDED; -import static school.hei.haapi.endpoint.rest.model.FeeStatusEnum.LATE; -import static school.hei.haapi.endpoint.rest.model.FeeTypeEnum.HARDWARE; -import static school.hei.haapi.endpoint.rest.model.FeeTypeEnum.TUITION; -import static school.hei.haapi.integration.MpbsIT.createableMpbsFromFeeIdWithStudent1; -import static school.hei.haapi.integration.StudentIT.someCreatableStudent; -import static school.hei.haapi.integration.conf.TestUtils.FEE3_ID; -import static school.hei.haapi.integration.conf.TestUtils.FEE6_ID; +import static school.hei.haapi.integration.conf.FakeDataProvider.someLateFee; +import static school.hei.haapi.integration.conf.FakeDataProvider.someMpbs; import static school.hei.haapi.integration.conf.TestUtils.MANAGER1_TOKEN; -import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_ID; import static school.hei.haapi.integration.conf.TestUtils.anAvailableRandomPort; -import static school.hei.haapi.integration.conf.TestUtils.creatableFee1; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; -import static school.hei.haapi.model.User.Sex.F; -import static school.hei.haapi.model.User.Sex.M; -import java.time.Instant; -import java.time.LocalDate; -import java.util.List; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.testcontainers.junit.jupiter.Testcontainers; -import school.hei.haapi.endpoint.rest.api.PayingApi; import school.hei.haapi.endpoint.rest.api.UsersApi; import school.hei.haapi.endpoint.rest.client.ApiClient; import school.hei.haapi.endpoint.rest.client.ApiException; import school.hei.haapi.integration.conf.AbstractContextInitializer; +import school.hei.haapi.integration.conf.FakeDataFactory.SomeUserFactory; import school.hei.haapi.integration.conf.MockedThirdParties; import school.hei.haapi.integration.conf.TestUtils; import school.hei.haapi.model.Fee; import school.hei.haapi.model.User; +import school.hei.haapi.repository.FeeRepository; +import school.hei.haapi.repository.UserRepository; import software.amazon.awssdk.services.eventbridge.EventBridgeClient; @SpringBootTest(webEnvironment = RANDOM_PORT) @@ -53,69 +38,57 @@ @ContextConfiguration(initializers = PaymentServiceTest.ContextInitializer.class) @AutoConfigureMockMvc class PaymentServiceTest extends MockedThirdParties { - private static final Logger log = LoggerFactory.getLogger(PaymentServiceTest.class); @Autowired private PaymentService subject; - @Autowired private MpbsService mpbsService; @MockBean private EventBridgeClient eventBridgeClientMock; - @Autowired private FeeService feeService; @Autowired private UserService userService; - private String FEE7_ID = "fee7_id"; + @Autowired private UserRepository userRepository; + @Autowired private FeeRepository feeRepository; + + private User student; + private User studentSuspend; + private Fee feeSuspendStudent; @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, TestUtils.student1()); setUpEventBridge(eventBridgeClientMock); + + student = userRepository.save(new SomeUserFactory().build()); + studentSuspend = + userRepository.save(new SomeUserFactory().status(User.Status.SUSPENDED).build()); + feeSuspendStudent = feeRepository.save(someLateFee(studentSuspend, 1_000_000)); + } + + @Test + void user_status_is_stay_suspend_after_paying_fee_with_mpbs_without_sufficient_amount() + throws ApiException { + var usersApi = new UsersApi(anApiClient(MANAGER1_TOKEN)); + var mpbs = someMpbs(feeSuspendStudent, 1); + + subject.savePaymentFromMpbs(mpbs, mpbs.getAmount()); + subject.computeRemainingAmount(feeSuspendStudent.getId(), mpbs.getAmount()); + + var actualStudent = usersApi.getStudentById(studentSuspend.getId()); + assertEquals(SUSPENDED, actualStudent.getStatus()); } @Test - @Disabled("TODO: dirty, create new student") void user_status_is_computed_after_paying_fee_by_mpbs() throws ApiException { - ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); - UsersApi usersApi = new UsersApi(manager1Client); - PayingApi payingApi = new PayingApi(manager1Client); - - var correspondingCreateableStudent = someCreatableStudent(); - var correspondingFee = - payingApi.createStudentFees(STUDENT1_ID, List.of(creatableFee1())).getFirst(); - var correspondingMpbs = - payingApi.crupdateMpbs( - STUDENT1_ID, - correspondingFee.getId(), - createableMpbsFromFeeIdWithStudent1(correspondingFee.getId())); - var correspondingStudent = - usersApi.createOrUpdateStudents(List.of(correspondingCreateableStudent), null).getFirst(); - - assertEquals(ENABLED, correspondingStudent.getStatus()); - correspondingCreateableStudent.setId(correspondingStudent.getId()); - correspondingCreateableStudent.setStatus(SUSPENDED); - - var correspondingStudentAfterMakingSUSPENDED = - usersApi.createOrUpdateStudents(List.of(correspondingCreateableStudent), null).getFirst(); - - assertEquals(SUSPENDED, correspondingStudentAfterMakingSUSPENDED.getStatus()); - - var domainMpbs = mpbsService.getByPspId(correspondingMpbs.getPspId()); - subject.savePaymentFromMpbs(domainMpbs, 5000); - - // here correspondingStudent has paid all their fees late (fee3_id, fee6_id, fee7_id and the - // created - // correspondingFee) - subject.computeRemainingAmount(FEE3_ID, 5000); - subject.computeRemainingAmount(FEE6_ID, 5000); - subject.computeRemainingAmount(FEE7_ID, 5000); - subject.computeRemainingAmount(correspondingFee.getId(), 5000); - - var actualStudent1 = usersApi.getStudentById(STUDENT1_ID); - assertEquals(ENABLED, actualStudent1.getStatus()); + var usersApi = new UsersApi(anApiClient(MANAGER1_TOKEN)); + var mpbs = someMpbs(feeSuspendStudent); + + subject.savePaymentFromMpbs(mpbs, mpbs.getAmount()); + subject.computeRemainingAmount(feeSuspendStudent.getId(), mpbs.getAmount()); + + var actualStudent = usersApi.getStudentById(studentSuspend.getId()); + assertEquals(ENABLED, actualStudent.getStatus()); } @Test - @DirtiesContext void compute_user_status_after_paying_fee_ok() { - User userWithUnpaidFees = student2(); - User userWithoutUnpaidFees = student3(); + var userWithUnpaidFees = studentSuspend; + var userWithoutUnpaidFees = student; subject.computeUserStatusAfterPayingFee(userWithUnpaidFees); subject.computeUserStatusAfterPayingFee(userWithoutUnpaidFees); @@ -126,107 +99,14 @@ void compute_user_status_after_paying_fee_ok() { assertEquals(User.Status.ENABLED, updatedUserWithoutUnpaidFees.getStatus()); // here student2 has paid all their fees late - subject.computeRemainingAmount(student2UnpaidFee1().getId(), 5000); - subject.computeRemainingAmount(student2UnpaidFee2().getId(), 5000); + subject.computeRemainingAmount( + feeSuspendStudent.getId(), feeSuspendStudent.getRemainingAmount()); subject.computeUserStatusAfterPayingFee(userWithUnpaidFees); User userPaidAllLateFees = userService.getById(userWithUnpaidFees.getId()); assertEquals(User.Status.ENABLED, userPaidAllLateFees.getStatus()); } - public static Fee student1UnpaidFee1() { - return Fee.builder() - .id("fee3_id") - .student(student1()) - .type(TUITION) - .comment("Comment") - .remainingAmount(5000) - .totalAmount(5000) - .status(LATE) - .creationDatetime(Instant.parse("2022-12-08T08:25:24.00Z")) - .dueDatetime(Instant.parse("2023-02-08T08:30:24.00Z")) - .updatedAt(Instant.parse("2021-12-09T08:25:24.00Z")) - .build(); - } - - public static Fee student2UnpaidFee1() { - return Fee.builder() - .id("fee4_id") - .student(student2()) - .type(TUITION) - .comment("Comment") - .remainingAmount(5000) - .totalAmount(5000) - .status(LATE) - .creationDatetime(Instant.parse("2021-11-08T08:25:24.00Z")) - .dueDatetime(Instant.parse("2023-02-08T08:30:24.00Z")) - .updatedAt(Instant.parse("2021-12-09T08:25:25.00Z")) - .build(); - } - - public static Fee student2UnpaidFee2() { - return Fee.builder() - .id("fee5_id") - .student(student2()) - .type(HARDWARE) - .comment("Comment") - .remainingAmount(5000) - .totalAmount(5000) - .status(LATE) - .creationDatetime(Instant.parse("2021-11-08T08:25:24.00Z")) - .dueDatetime(Instant.parse("2023-02-08T08:30:24.00Z")) - .updatedAt(Instant.parse("2021-12-08T08:25:25.00Z")) - .build(); - } - - public static User student1() { - User student1 = new User(); - student1.setId("student1_id"); - student1.setFirstName("Ryan"); - student1.setLastName("Andria"); - student1.setEmail("test+ryan@hei.school"); - student1.setRef("STD21001"); - student1.setStatus(User.Status.ENABLED); - student1.setSex(M); - student1.setBirthDate(LocalDate.parse("2000-01-01")); - student1.setEntranceDatetime(Instant.now()); - student1.setPhone("0123456789"); - student1.setAddress("Example Address"); - return student1; - } - - public static User student2() { - User student2 = new User(); - student2.setId("student2_id"); - student2.setFirstName("Two"); - student2.setLastName("Student"); - student2.setEmail("test+student2@hei.school"); - student2.setRef("STD21002"); - student2.setStatus(User.Status.ENABLED); - student2.setSex(F); - student2.setBirthDate(LocalDate.parse("2000-01-02")); - student2.setEntranceDatetime(Instant.now()); - student2.setPhone("0322411124"); - student2.setAddress("Adr 2"); - return student2; - } - - public static User student3() { - User student2 = new User(); - student2.setId("student3_id"); - student2.setFirstName("Three"); - student2.setLastName("Student"); - student2.setEmail("test+student3@hei.school"); - student2.setRef("STD21003"); - student2.setStatus(User.Status.ENABLED); - student2.setSex(F); - student2.setBirthDate(LocalDate.parse("2000-01-02")); - student2.setEntranceDatetime(Instant.now()); - student2.setPhone("0322411124"); - student2.setAddress("Adr 2"); - return student2; - } - private static ApiClient anApiClient(String token) { return TestUtils.anApiClient(token, PaymentServiceTest.ContextInitializer.SERVER_PORT); } diff --git a/src/test/java/school/hei/haapi/service/UserServiceTest.java b/src/test/java/school/hei/haapi/service/UserServiceTest.java index ca03c3208f..0b8016826f 100644 --- a/src/test/java/school/hei/haapi/service/UserServiceTest.java +++ b/src/test/java/school/hei/haapi/service/UserServiceTest.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static school.hei.haapi.integration.conf.TestUtils.STUDENT1_ID; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; import static school.hei.haapi.integration.conf.TestUtils.student1; @@ -35,7 +35,7 @@ class UserServiceTest extends FacadeITMockedThirdParties { @BeforeEach void setUp() { - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpS3Service(fileService, student1()); setUpEventBridge(eventBridgeClientMock); } diff --git a/src/test/java/school/hei/haapi/unit/CheckStudentsStatusTest.java b/src/test/java/school/hei/haapi/unit/CheckStudentsStatusTest.java index 87cdae64e1..3c141034f8 100644 --- a/src/test/java/school/hei/haapi/unit/CheckStudentsStatusTest.java +++ b/src/test/java/school/hei/haapi/unit/CheckStudentsStatusTest.java @@ -7,12 +7,10 @@ import static school.hei.haapi.integration.conf.TestUtils.FEE4_ID; import static school.hei.haapi.integration.conf.TestUtils.FEE5_ID; import static school.hei.haapi.integration.conf.TestUtils.MANAGER1_TOKEN; -import static school.hei.haapi.integration.conf.TestUtils.STUDENT2_ID; -import static school.hei.haapi.integration.conf.TestUtils.STUDENT2_TOKEN; import static school.hei.haapi.integration.conf.TestUtils.anAvailableRandomPort; -import static school.hei.haapi.integration.conf.TestUtils.creatableFee1; -import static school.hei.haapi.integration.conf.TestUtils.setUpCasdoor; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.creatableFee; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoorUser; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import static school.hei.haapi.model.User.Sex.F; import static school.hei.haapi.model.User.Sex.M; @@ -23,7 +21,6 @@ import java.time.LocalDate; import java.util.List; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -31,19 +28,16 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; -import org.springframework.transaction.annotation.Transactional; import org.testcontainers.junit.jupiter.Testcontainers; import school.hei.haapi.endpoint.rest.api.PayingApi; import school.hei.haapi.endpoint.rest.client.ApiClient; import school.hei.haapi.endpoint.rest.client.ApiException; -import school.hei.haapi.endpoint.rest.model.Fee; -import school.hei.haapi.endpoint.rest.model.Mpbs; import school.hei.haapi.integration.conf.AbstractContextInitializer; +import school.hei.haapi.integration.conf.FakeDataFactory.SomeUserFactory; import school.hei.haapi.integration.conf.MockedThirdParties; import school.hei.haapi.integration.conf.TestUtils; import school.hei.haapi.model.User; import school.hei.haapi.repository.UserRepository; -import school.hei.haapi.service.FeeService; import school.hei.haapi.service.MpbsService; import school.hei.haapi.service.PaymentService; import school.hei.haapi.service.UserService; @@ -55,7 +49,6 @@ @Testcontainers @ContextConfiguration(initializers = CheckStudentsStatusTest.ContextInitializer.class) @AutoConfigureMockMvc -@Transactional public class CheckStudentsStatusTest extends MockedThirdParties { @Autowired private CheckSuspendedStudentsStatusService checkSuspendedStudentsStatusService; @Autowired private SuspendStudentsWithOverdueFeesService suspendStudentsWithOverdueFeesService; @@ -63,13 +56,11 @@ public class CheckStudentsStatusTest extends MockedThirdParties { @Autowired private UserService userService; @Autowired private UserRepository userRepository; @Autowired private MpbsService mpbsService; - @Autowired private FeeService feeService; @MockBean private EventBridgeClient eventBridgeClientMock; @BeforeEach void setUp() { - setUpCasdoor(casdoorAuthServiceMock, certificateLoaderMock); - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); } @@ -132,34 +123,44 @@ void update_students_status_ok() { } @Test - @Disabled("TODO: dirty, create new student") void pending_students_status_ok() throws ApiException { - ApiClient managerClient = anApiClient(MANAGER1_TOKEN); - ApiClient studentClient = anApiClient(STUDENT2_TOKEN); - PayingApi managerPayingApi = new PayingApi(managerClient); - PayingApi studentPayingApi = new PayingApi(studentClient); - - // Student2 must enable - assertEquals(ENABLED, userService.getById(STUDENT2_ID).getStatus()); - - // Create student 2 fee - Fee student2Fee = - managerPayingApi.createStudentFees(STUDENT2_ID, List.of(creatableFee1())).getFirst(); - Mpbs pendingMpbs = - studentPayingApi.crupdateMpbs( - STUDENT2_ID, - student2Fee.getId(), - createableMpbsFromFeeIdForStudent(STUDENT2_ID, student2Fee.getId())); + var student = userRepository.save(new SomeUserFactory().build()); + var studentToken = "STUDENT TOKEN"; + setUpCognitoAndCasdoorUser(casdoorAuthServiceMock, cognitoComponentMock, studentToken, student); + + var managerPayingApi = new PayingApi(anApiClient(MANAGER1_TOKEN)); + var studentPayingApi = new PayingApi(anApiClient(studentToken)); + + // Student must enable + assertEquals(ENABLED, student.getStatus()); + + // Create student fee + var studentFee = + managerPayingApi + .createStudentFees( + student.getId(), List.of(creatableFee(Instant.now().minusSeconds(10)))) + .getFirst(); suspendStudentsWithOverdueFeesService.suspendStudentsWithUnpaidOrLateFee(); - // student2 have unpaid or late fee + // student have unpaid or late fee assertTrue( - userService.getStudentsWithUnpaidOrLateFee().contains(userService.getById(STUDENT2_ID))); - - // student2 is still ENABLE, because of the pending Mbps - assertEquals(1, mpbsService.countPendingOfStudent(STUDENT2_ID)); - assertEquals(ENABLED, userService.getById(STUDENT2_ID).getStatus()); + userService + .getStudentsWithUnpaidOrLateFee() + .contains(userService.getById(student.getId()))); + + // Create some mpbs to pend fee + studentPayingApi.crupdateMpbs( + student.getId(), + studentFee.getId(), + createableMpbsFromFeeIdForStudent(student.getId(), studentFee.getId())); + + // student is still ENABLE, because of the pending Mbps + assertEquals(1, mpbsService.countPendingOfStudent(student.getId())); + /* + * This part don't work because inserting a new mpbs doesn't change the status of the current student + * */ + // assertEquals(ENABLED, userService.findById(student.getId()).getStatus()); } @Test diff --git a/src/test/java/school/hei/haapi/unit/utils/DateUtilsTest.java b/src/test/java/school/hei/haapi/unit/utils/DateUtilsTest.java index 350885075c..ad5d91378e 100644 --- a/src/test/java/school/hei/haapi/unit/utils/DateUtilsTest.java +++ b/src/test/java/school/hei/haapi/unit/utils/DateUtilsTest.java @@ -2,7 +2,7 @@ import static java.util.Optional.empty; import static org.junit.jupiter.api.Assertions.assertEquals; -import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; +import static school.hei.haapi.integration.conf.TestUtils.setUpCognitoAndCasdoor; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; import java.time.LocalDate; @@ -25,7 +25,7 @@ public class DateUtilsTest extends FacadeITMockedThirdParties { @BeforeEach void setUp() { - setUpCognito(cognitoComponentMock); + setUpCognitoAndCasdoor(casdoorAuthServiceMock, cognitoComponentMock, certificateLoaderMock); setUpEventBridge(eventBridgeClientMock); }