diff --git a/src/test/java/school/hei/haapi/integration/AttendanceIT.java b/src/test/java/school/hei/haapi/integration/AttendanceIT.java index 11f141ac10..092db44460 100644 --- a/src/test/java/school/hei/haapi/integration/AttendanceIT.java +++ b/src/test/java/school/hei/haapi/integration/AttendanceIT.java @@ -25,6 +25,7 @@ import school.hei.haapi.endpoint.rest.client.ApiException; import school.hei.haapi.endpoint.rest.model.*; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.MockUtils; import school.hei.haapi.integration.conf.TestUtils; import school.hei.haapi.model.User; import school.hei.haapi.repository.UserRepository; @@ -39,6 +40,7 @@ class AttendanceIT extends FacadeITMockedThirdParties { @Autowired CheckAttendanceTriggeredService checkAttendanceTriggeredService; @Autowired UserRepository userRepository; @Autowired UserService userService; + @Autowired MockUtils mockUtils; private ApiClient anApiClient(String token) { return TestUtils.anApiClient(token, localPort); @@ -215,7 +217,7 @@ void default_attendance_unchecked() throws ApiException { List events = api.crupdateEvents( List.of( - someCreatableEvent( + mockUtils.someCreatableEvent( COURSE, TEACHER1_ID, Instant.now(), Instant.now().plusSeconds(10))), null, null, diff --git a/src/test/java/school/hei/haapi/integration/EventIT.java b/src/test/java/school/hei/haapi/integration/EventIT.java index b12057bbb8..24f97f6f4a 100644 --- a/src/test/java/school/hei/haapi/integration/EventIT.java +++ b/src/test/java/school/hei/haapi/integration/EventIT.java @@ -37,8 +37,6 @@ import static school.hei.haapi.integration.conf.TestUtils.group1; import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; import static school.hei.haapi.integration.conf.TestUtils.setUpS3Service; -import static school.hei.haapi.integration.conf.TestUtils.someCreatableEvent; -import static school.hei.haapi.integration.conf.TestUtils.someCreatableEventByManager1; import static school.hei.haapi.integration.conf.TestUtils.student1AttendEvent2; import static school.hei.haapi.integration.conf.TestUtils.student1MissEvent1; import static school.hei.haapi.integration.conf.TestUtils.student2AttendEvent2; @@ -51,6 +49,7 @@ import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.testcontainers.junit.jupiter.Testcontainers; import school.hei.haapi.endpoint.rest.api.EventsApi; @@ -64,12 +63,14 @@ import school.hei.haapi.endpoint.rest.model.EventStats; import school.hei.haapi.endpoint.rest.model.UpdateEventParticipant; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.MockUtils; import school.hei.haapi.integration.conf.TestUtils; @Slf4j @Testcontainers @AutoConfigureMockMvc public class EventIT extends FacadeITMockedThirdParties { + @Autowired MockUtils mockUtils; private ApiClient anApiClient(String token) { return TestUtils.anApiClient(token, localPort); @@ -348,7 +349,16 @@ void delete_event_student_ko_and_manager_ko() throws ApiException { EventsApi managerApi = new EventsApi(anApiClient(MANAGER1_TOKEN)); List events = managerApi.crupdateEvents( - List.of(someCreatableEventByManager1(INTEGRATION)), MONDAY, 1, "09:00", "12:00"); + List.of( + mockUtils.someCreatableEvent( + INTEGRATION, + MANAGER_ID, + Instant.parse("2023-12-08T08:00:00.00Z"), + Instant.parse("2023-12-08T10:00:00.00Z"))), + MONDAY, + 1, + "09:00", + "12:00"); assertThrowsForbiddenException(() -> studentApi.deleteEventById(events.getFirst().getId())); @@ -380,7 +390,7 @@ void event_stats_are_exact() throws ApiException { List createdEvents = managerApi.crupdateEvents( List.of( - someCreatableEvent( + mockUtils.someCreatableEvent( COURSE, MANAGER_ID, Instant.now(), Instant.now().plus(Duration.of(4, HOURS)))), null, null, diff --git a/src/test/java/school/hei/haapi/integration/LateFeeVerifiedServiceIT.java b/src/test/java/school/hei/haapi/integration/LateFeeVerifiedServiceIT.java index 11f0534431..a4a3485a85 100644 --- a/src/test/java/school/hei/haapi/integration/LateFeeVerifiedServiceIT.java +++ b/src/test/java/school/hei/haapi/integration/LateFeeVerifiedServiceIT.java @@ -4,6 +4,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.verify; +import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD; import static school.hei.haapi.endpoint.rest.model.FeeTypeEnum.TUITION; import static school.hei.haapi.model.User.Role.STUDENT; import static school.hei.haapi.model.User.Status.ENABLED; @@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.annotation.DirtiesContext; import org.testcontainers.junit.jupiter.Testcontainers; import school.hei.haapi.endpoint.event.model.LateFeeVerified; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; @@ -26,7 +28,8 @@ @Testcontainers @AutoConfigureMockMvc -public class LateFeeVerifiedServiceIT extends FacadeITMockedThirdParties { +@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) +class LateFeeVerifiedServiceIT extends FacadeITMockedThirdParties { @Autowired private LateFeeVerifiedService subject; @Autowired private UserRepository userRepository; @MockBean private Mailer mailer; diff --git a/src/test/java/school/hei/haapi/integration/OrganizerIT.java b/src/test/java/school/hei/haapi/integration/OrganizerIT.java index 5bec10fe4d..94ea84d33e 100644 --- a/src/test/java/school/hei/haapi/integration/OrganizerIT.java +++ b/src/test/java/school/hei/haapi/integration/OrganizerIT.java @@ -17,7 +17,6 @@ import static school.hei.haapi.integration.conf.TestUtils.assertThrowsForbiddenException; import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; import static school.hei.haapi.integration.conf.TestUtils.setUpEventBridge; -import static school.hei.haapi.integration.conf.TestUtils.someCreatableEvent; import static school.hei.haapi.integration.conf.TestUtils.student1MissEvent1; import static school.hei.haapi.integration.conf.TestUtils.student3AttendEvent1; import static school.hei.haapi.integration.conf.TestUtils.uploadProfilePicture; @@ -47,6 +46,7 @@ import school.hei.haapi.endpoint.rest.model.EventType; import school.hei.haapi.endpoint.rest.model.Organizer; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.MockUtils; import school.hei.haapi.integration.conf.TestUtils; import software.amazon.awssdk.services.eventbridge.EventBridgeClient; @@ -56,6 +56,7 @@ public class OrganizerIT extends FacadeITMockedThirdParties { @MockBean private EventBridgeClient eventBridgeClientMock; @Autowired ObjectMapper objectMapper; + @Autowired MockUtils mockUtils; private ApiClient anApiClient(String token) { return TestUtils.anApiClient(token, localPort); @@ -148,7 +149,7 @@ void manipulate_events_ok() throws ApiException { EventsApi api = new EventsApi(organizerClient); CreateEvent createEvent = - someCreatableEvent( + mockUtils.someCreatableEvent( EventType.EXAM, ORGANIZER1_ID, Instant.now(), Instant.now().plus(1, HOURS)); List events = api.crupdateEvents(List.of(createEvent), null, null, null, null); diff --git a/src/test/java/school/hei/haapi/integration/PaginationIT.java b/src/test/java/school/hei/haapi/integration/PaginationIT.java index 161c71e93b..bf4825a693 100644 --- a/src/test/java/school/hei/haapi/integration/PaginationIT.java +++ b/src/test/java/school/hei/haapi/integration/PaginationIT.java @@ -20,6 +20,7 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.mock.mockito.MockBean; import org.testcontainers.junit.jupiter.Testcontainers; @@ -32,6 +33,7 @@ import school.hei.haapi.endpoint.rest.model.Payment; import school.hei.haapi.endpoint.rest.model.Student; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.MockUtils; import school.hei.haapi.integration.conf.TestUtils; import software.amazon.awssdk.services.eventbridge.EventBridgeClient; @@ -39,13 +41,14 @@ @AutoConfigureMockMvc class PaginationIT extends FacadeITMockedThirdParties { @MockBean private EventBridgeClient eventBridgeClientMock; + @Autowired private MockUtils mockUtils; private ApiClient anApiClient(String token) { return TestUtils.anApiClient(token, localPort); } @BeforeEach - public void setUp() throws ApiException { + void setUp() { setUpCognito(cognitoComponentMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); @@ -56,7 +59,7 @@ public void setUp() throws ApiException { private void someCreatableStudentList(int nbOfNewStudents) throws ApiException { List newStudents = new ArrayList<>(); for (int i = 0; i < nbOfNewStudents; i++) { - newStudents.add(StudentIT.someCreatableStudent()); + newStudents.add(mockUtils.someCreatableStudent()); } ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); diff --git a/src/test/java/school/hei/haapi/integration/PaymentIT.java b/src/test/java/school/hei/haapi/integration/PaymentIT.java index 598c06b630..2bbeea8eb3 100644 --- a/src/test/java/school/hei/haapi/integration/PaymentIT.java +++ b/src/test/java/school/hei/haapi/integration/PaymentIT.java @@ -8,7 +8,6 @@ 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.PAID; -import static school.hei.haapi.integration.StudentIT.someCreatableStudent; import static school.hei.haapi.integration.StudentIT.student1; import static school.hei.haapi.integration.conf.TestUtils.FEE1_ID; import static school.hei.haapi.integration.conf.TestUtils.FEE3_ID; @@ -53,6 +52,7 @@ import school.hei.haapi.endpoint.rest.model.Payment; import school.hei.haapi.endpoint.rest.model.Student; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.MockUtils; import school.hei.haapi.integration.conf.TestUtils; import software.amazon.awssdk.services.eventbridge.EventBridgeClient; @@ -61,6 +61,7 @@ class PaymentIT extends FacadeITMockedThirdParties { @Autowired EntityManager entityManager; @MockBean private EventBridgeClient eventBridgeClientMock; + @Autowired private MockUtils mockUtils; private ApiClient anApiClient(String token) { return TestUtils.anApiClient(token, localPort); @@ -278,7 +279,7 @@ void student_is_now_enabled_after_paying_fee() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); PayingApi payingApi = new PayingApi(manager1Client); UsersApi usersApi = new UsersApi(manager1Client); - CrupdateStudent subject = someCreatableStudent(); + CrupdateStudent subject = mockUtils.someCreatableStudent(); subject.setStatus(ENABLED); // Assert before all that the actual student is SUSPENDED ... @@ -365,7 +366,7 @@ void manager_write_with_some_bad_fields_ko() { } @Test - void manager_write_with_non_given_creation_datetime_ko() throws ApiException { + void manager_write_with_non_given_creation_datetime_ko() { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); PayingApi api = new PayingApi(manager1Client); @@ -376,7 +377,7 @@ void manager_write_with_non_given_creation_datetime_ko() throws ApiException { } @Test - void manager_write_with_creation_datetime_after_current_time_ko() throws ApiException { + void manager_write_with_creation_datetime_after_current_time_ko() { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); PayingApi api = new PayingApi(manager1Client); diff --git a/src/test/java/school/hei/haapi/integration/StudentIT.java b/src/test/java/school/hei/haapi/integration/StudentIT.java index 6927d431d8..a0f2dc8992 100644 --- a/src/test/java/school/hei/haapi/integration/StudentIT.java +++ b/src/test/java/school/hei/haapi/integration/StudentIT.java @@ -19,7 +19,6 @@ import static school.hei.haapi.endpoint.rest.model.Sex.M; import static school.hei.haapi.endpoint.rest.model.SpecializationField.COMMON_CORE; import static school.hei.haapi.endpoint.rest.model.SpecializationField.EL; -import static school.hei.haapi.endpoint.rest.model.SpecializationField.TN; import static school.hei.haapi.endpoint.rest.model.WorkStudyStatus.NOT_WORKING; import static school.hei.haapi.endpoint.rest.model.WorkStudyStatus.WORKING; import static school.hei.haapi.integration.GroupIT.updatedGroup3; @@ -49,21 +48,17 @@ 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; +import static school.hei.haapi.model.User.Role.STUDENT; import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.javafaker.Faker; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.net.URL; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Instant; import java.time.LocalDate; -import java.time.ZoneId; -import java.util.ArrayList; import java.util.List; import java.util.Objects; import org.junit.jupiter.api.BeforeEach; @@ -80,6 +75,7 @@ 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.endpoint.rest.mapper.UserMapper; import school.hei.haapi.endpoint.rest.model.Coordinates; import school.hei.haapi.endpoint.rest.model.CrupdateStudent; import school.hei.haapi.endpoint.rest.model.EnableStatus; @@ -88,7 +84,10 @@ import school.hei.haapi.endpoint.rest.model.Statistics; import school.hei.haapi.endpoint.rest.model.Student; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.MockUtils; import school.hei.haapi.integration.conf.TestUtils; +import school.hei.haapi.model.User; +import school.hei.haapi.service.UserService; import software.amazon.awssdk.services.eventbridge.EventBridgeClient; import software.amazon.awssdk.services.eventbridge.model.PutEventsRequest; import software.amazon.awssdk.services.eventbridge.model.PutEventsRequestEntry; @@ -102,75 +101,14 @@ public class StudentIT extends FacadeITMockedThirdParties { @MockBean private EventBridgeClient eventBridgeClientMock; @Autowired ObjectMapper objectMapper; + @Autowired UserService userService; + @Autowired UserMapper userMapper; + @Autowired MockUtils mockUtils; private ApiClient anApiClient(String token) { return TestUtils.anApiClient(token, localPort); } - File getFileFromResource(String resourceName) { - URL resource = this.getClass().getClassLoader().getResource(resourceName); - return new File(resource.getFile()); - } - - public static CrupdateStudent createStudent1() { - CrupdateStudent student = new CrupdateStudent(); - student.setId("student1_id"); - student.setFirstName("Ryan"); - student.setLastName("Andria"); - student.setEmail("test+ryan@hei.school"); - student.setRef("STD21001"); - student.setPhone("0322411123"); - student.setStatus(ENABLED); - student.setSex(M); - student.setBirthDate(LocalDate.parse("2000-01-01")); - student.setEntranceDatetime(Instant.parse("2021-11-08T08:25:24.00Z")); - student.setAddress("Adr 1"); - student.setNic(""); - student.setBirthPlace(""); - student.coordinates(coordinatesWithNullValues()); - return student; - } - - public static CrupdateStudent someUpdatableStudent() { - return createStudent1() - .address("Adr 999") - .sex(F) - .lastName("Other last") - .firstName("Other first") - .specializationField(TN) - .birthDate(LocalDate.parse("2000-01-03")); - } - - public static CrupdateStudent someCreatableStudent() { - CrupdateStudent student = new CrupdateStudent(); - Faker faker = new Faker(); - student.setId(null); - student.setFirstName(faker.name().firstName()); - student.setLastName(faker.name().lastName()); - student.setEmail("test+" + randomUUID() + "@hei.school"); - student.setRef("STD21" + (int) (Math.random() * 1_000_000)); - student.setPhone("03" + (int) (Math.random() * 1_000_000_000)); - student.setStatus(ENABLED); - student.setSex(Math.random() < 0.3 ? F : M); - Instant birthday = Instant.parse("1993-11-30T18:35:24.00Z"); - int ageOfEntrance = 14 + (int) (Math.random() * 20); - student.setBirthDate(birthday.atZone(ZoneId.systemDefault()).toLocalDate()); - student.setEntranceDatetime(birthday.plusSeconds(ageOfEntrance * 365L * 24L * 60L * 60L)); - student.setAddress(faker.address().fullAddress()); - student.specializationField(COMMON_CORE); - student.setCoordinates(coordinatesWithNullValues()); - - return student; - } - - static List someCreatableStudentList(int nbOfStudent) { - List studentList = new ArrayList<>(); - for (int i = 0; i < nbOfStudent; i++) { - studentList.add(someCreatableStudent()); - } - return studentList; - } - public static Student student1() { Student student = new Student(); student.setId("student1_id"); @@ -377,7 +315,7 @@ public static Student repeatingStudent2() { } @BeforeEach - public void setUp() { + void setUp() { setUpCognito(cognitoComponentMock); setUpEventBridge(eventBridgeClientMock); setUpS3Service(fileService, student1()); @@ -385,14 +323,14 @@ public void setUp() { @Test void manager_generate_group_students_ok() throws IOException, InterruptedException { - String STUDENTS_GROUP = "/groups/" + GROUP1_ID + "/students/raw"; + String studentsGroupPath = "/groups/" + GROUP1_ID + "/students/raw"; HttpClient httpClient = HttpClient.newBuilder().build(); String basePath = "http://localhost:" + localPort; HttpResponse response = httpClient.send( HttpRequest.newBuilder() - .uri(URI.create(basePath + STUDENTS_GROUP)) + .uri(URI.create(basePath + studentsGroupPath)) .GET() .header("Authorization", "Bearer " + MANAGER1_TOKEN) .build(), @@ -474,9 +412,8 @@ void manager_upload_profile_picture() throws IOException, InterruptedException { } @Test - @Disabled - // TODO: Same here - void student_update_other_profile_picture_ko() throws ApiException { + @Disabled("TODO: Same here") + void student_update_other_profile_picture_ko() { ApiClient student1Client = anApiClient(STUDENT1_TOKEN); UsersApi api = new UsersApi(student1Client); assertThrowsForbiddenException( @@ -484,9 +421,8 @@ void student_update_other_profile_picture_ko() throws ApiException { } @Test - @Disabled - // TODO: Check why this returns null while a Forbidden Exception is thrown - void student_update_own_ko() throws ApiException { + @Disabled("TODO: Check why this returns null while a Forbidden Exception is thrown") + void student_update_own_ko() { ApiClient student1Client = anApiClient(STUDENT1_TOKEN); UsersApi api = new UsersApi(student1Client); assertThrowsForbiddenException( @@ -608,7 +544,7 @@ void manager_read_ok() throws ApiException { } @Test - void manager_read_displayed_commitment_date() throws ApiException, InterruptedException { + void manager_read_displayed_commitment_date() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); @@ -758,98 +694,27 @@ void monitor_read_students_ok() throws ApiException { @Test void manager_write_update_ok() throws ApiException { - ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); - UsersApi api = new UsersApi(manager1Client); - List toCreate = - api.createOrUpdateStudents(List.of(someCreatableStudent(), someCreatableStudent()), null); - - Student created0 = toCreate.getFirst(); - CrupdateStudent toUpdate0 = - new CrupdateStudent() - .birthDate(created0.getBirthDate()) - .id(created0.getId()) - .entranceDatetime(created0.getEntranceDatetime()) - .phone(created0.getPhone()) - .nic(created0.getNic()) - .birthPlace(created0.getBirthPlace()) - .email(created0.getEmail()) - .address(created0.getAddress()) - .firstName(created0.getFirstName()) - .lastName(created0.getLastName()) - .sex(created0.getSex()) - .ref(created0.getRef()) - .coordinates(coordinatesWithNullValues()) - .specializationField(created0.getSpecializationField()) - .status(created0.getStatus()); - toUpdate0.setLastName("A new name zero"); - - Student created1 = toCreate.get(1); - CrupdateStudent toUpdate1 = - new CrupdateStudent() - .birthDate(created1.getBirthDate()) - .id(created1.getId()) - .entranceDatetime(created1.getEntranceDatetime()) - .phone(created1.getPhone()) - .nic(created1.getNic()) - .birthPlace(created1.getBirthPlace()) - .email(created1.getEmail()) - .address(created1.getAddress()) - .firstName(created1.getFirstName()) - .lastName(created1.getLastName()) - .sex(created1.getSex()) - .ref(created1.getRef()) - .coordinates(coordinatesWithNullValues()) - .specializationField(created1.getSpecializationField()) - .status(created1.getStatus()); - toUpdate1.setLastName("A new name one"); - - Student updated0 = - new Student() - .birthDate(toUpdate0.getBirthDate()) - .id(toUpdate0.getId()) - .entranceDatetime(toUpdate0.getEntranceDatetime()) - .phone(toUpdate0.getPhone()) - .nic(toUpdate0.getNic()) - .birthPlace(toUpdate0.getBirthPlace()) - .email(toUpdate0.getEmail()) - .address(toUpdate0.getAddress()) - .firstName(toUpdate0.getFirstName()) - .lastName("A new name zero") - .sex(toUpdate0.getSex()) - .ref(toUpdate0.getRef()) - .coordinates(coordinatesWithNullValues()) - .specializationField(toUpdate0.getSpecializationField()) - .workStudyStatus(NOT_WORKING) - .status(toUpdate0.getStatus()) - .groups(List.of()) - .isRepeatingYear(false); - - Student updated1 = - new Student() - .birthDate(toUpdate1.getBirthDate()) - .id(toUpdate1.getId()) - .entranceDatetime(toUpdate1.getEntranceDatetime()) - .phone(toUpdate1.getPhone()) - .nic(toUpdate1.getNic()) - .birthPlace(toUpdate1.getBirthPlace()) - .email(toUpdate1.getEmail()) - .address(toUpdate1.getAddress()) - .firstName(toUpdate1.getFirstName()) - .lastName("A new name one") - .sex(toUpdate1.getSex()) - .ref(toUpdate1.getRef()) - .specializationField(toUpdate1.getSpecializationField()) - .coordinates(coordinatesWithNullValues()) - .workStudyStatus(NOT_WORKING) - .status(toUpdate1.getStatus()) - .groups(List.of()) - .isRepeatingYear(false); - - List updated = api.createOrUpdateStudents(List.of(toUpdate0, toUpdate1), null); + ApiClient client = anApiClient(MANAGER1_TOKEN); + UsersApi api = new UsersApi(client); + + User user1 = mockUtils.createSomeUser(STUDENT, User.Status.ENABLED); + User user2 = mockUtils.createSomeUser(STUDENT, User.Status.ENABLED); + userService.saveAll(List.of(user1, user2)); + + Student student1 = userMapper.toRestStudent(user1); + Student student2 = userMapper.toRestStudent(user2); + + CrupdateStudent crupdateStudent1 = TestUtils.toCrupdateStudent(student1).firstName("Test"); + CrupdateStudent crupdateStudent2 = TestUtils.toCrupdateStudent(student2).address("Home"); + List updated = + api.createOrUpdateStudents(List.of(crupdateStudent1, crupdateStudent2), null); + + Student updated1 = student1.firstName("Test"); + Student updated2 = student2.address("Home"); assertEquals(2, updated.size()); - assertTrue(updated.contains(updated0)); assertTrue(updated.contains(updated1)); + assertTrue(updated.contains(updated2)); } @Test @@ -870,7 +735,7 @@ void manager_read_student_by_exclude_group_id() throws ApiException { void manager_write_update_rollback_on_event_error() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); - CrupdateStudent toCreate = someCreatableStudent(); + CrupdateStudent toCreate = mockUtils.someCreatableStudent(); reset(eventBridgeClientMock); when(eventBridgeClientMock.putEvents((PutEventsRequest) any())) .thenThrow(RuntimeException.class); @@ -889,8 +754,11 @@ void manager_write_update_rollback_on_event_error() throws ApiException { void manager_write_update_more_than_10_students_ko() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); - CrupdateStudent studentToCreate = someCreatableStudent(); - List listToCreate = someCreatableStudentList(11); + CrupdateStudent studentToCreate = mockUtils.someCreatableStudent(); + List listToCreate = + mockUtils.createSomeUsers(11, STUDENT, User.Status.ENABLED).stream() + .map(user -> TestUtils.toCrupdateStudent(userMapper.toRestStudent(user))) + .toList(); listToCreate.add(studentToCreate); assertThrowsApiException( @@ -904,35 +772,35 @@ void manager_write_update_more_than_10_students_ko() throws ApiException { } @Test - void manager_write_with_longitude_null_ko() throws ApiException { + void manager_write_with_coordinates_null_ko() { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); assertThrowsApiException( "{\"type\":\"400 BAD_REQUEST\",\"message\":\"Longitude is null, it must go hand in hand" + " with latitude\"}", - () -> - api.createOrUpdateStudents( - List.of( - someCreatableStudent() - .coordinates(new Coordinates().longitude(null).latitude(12.0))), - null)); - } - - @Test - void manager_write_with_latitude_null_ko() throws ApiException { - ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); - UsersApi api = new UsersApi(manager1Client); + () -> { + api.createOrUpdateStudents( + List.of( + TestUtils.toCrupdateStudent( + userMapper.toRestStudent( + mockUtils.createSomeUser(STUDENT, User.Status.ENABLED))) + .coordinates(new Coordinates().longitude(null).latitude(0.))), + null); + }); assertThrowsApiException( "{\"type\":\"400 BAD_REQUEST\",\"message\":\"Latitude is null, it must go hand in hand with" + " longitude\"}", - () -> - api.createOrUpdateStudents( - List.of( - someCreatableStudent() - .coordinates(new Coordinates().longitude(12.0).latitude(null))), - null)); + () -> { + api.createOrUpdateStudents( + List.of( + TestUtils.toCrupdateStudent( + userMapper.toRestStudent( + mockUtils.createSomeUser(STUDENT, User.Status.ENABLED))) + .coordinates(new Coordinates().longitude(0.).latitude(null))), + null); + }); } @Test @@ -949,7 +817,11 @@ void manager_write_update_triggers_userUpserted() throws ApiException { .build()); List created = - api.createOrUpdateStudents(List.of(someCreatableStudent(), someCreatableStudent()), null); + api.createOrUpdateStudents( + mockUtils.createSomeUsers(2, STUDENT, User.Status.ENABLED).stream() + .map(user -> TestUtils.toCrupdateStudent(userMapper.toRestStudent(user))) + .toList(), + null); ArgumentCaptor captor = ArgumentCaptor.forClass(PutEventsRequest.class); verify(eventBridgeClientMock, times(1)).putEvents(captor.capture()); @@ -1072,13 +944,13 @@ void crupdate_students_with_payment_frequency() throws ApiException { UsersApi usersApi = new UsersApi(apiClient); PayingApi payingApi = new PayingApi(apiClient); - CrupdateStudent creatableStudent1 = someCreatableStudent(); + CrupdateStudent creatableStudent1 = mockUtils.someCreatableStudent(); creatableStudent1.setPaymentFrequency(MONTHLY); - CrupdateStudent creatableStudent2 = someCreatableStudent(); + CrupdateStudent creatableStudent2 = mockUtils.someCreatableStudent(); creatableStudent2.setPaymentFrequency(YEARLY); - CrupdateStudent creatableStudent3 = someCreatableStudent(); + CrupdateStudent creatableStudent3 = mockUtils.someCreatableStudent(); creatableStudent3.setPaymentFrequency(null); List studentsCreated = @@ -1119,7 +991,7 @@ void student_update_self_ko() throws ApiException { UsersApi api = new UsersApi(student1Client); Student currentStudent1 = api.getStudentById(STUDENT1_ID); Student expectedStudent1AfterUpdate = randomizeStudentUpdatableValues(currentStudent1); - CrupdateStudent payload = toCrupdateStudent(expectedStudent1AfterUpdate); + CrupdateStudent payload = TestUtils.toCrupdateStudent(expectedStudent1AfterUpdate); assertThrowsForbiddenException(() -> api.updateStudent(STUDENT1_ID, payload)); } @@ -1143,25 +1015,6 @@ void manager_read_group_students_ok() throws ApiException { assertEquals(student1(), actualGroupStudentsByFirstName.getFirst()); } - private CrupdateStudent toCrupdateStudent(Student student) { - return new CrupdateStudent() - .id(student.getId()) - .birthDate(student.getBirthDate()) - .id(student.getId()) - .entranceDatetime(student.getEntranceDatetime()) - .phone(student.getPhone()) - .nic(student.getNic()) - .birthPlace(student.getBirthPlace()) - .email(student.getEmail()) - .address(student.getAddress()) - .firstName(student.getFirstName()) - .lastName(student.getLastName()) - .sex(student.getSex()) - .ref(student.getRef()) - .specializationField(student.getSpecializationField()) - .status(student.getStatus()); - } - private Student randomizeStudentUpdatableValues(Student student) { return new Student() .id(student.getId()) diff --git a/src/test/java/school/hei/haapi/integration/TeacherIT.java b/src/test/java/school/hei/haapi/integration/TeacherIT.java index 1d885691a4..9631ef2584 100644 --- a/src/test/java/school/hei/haapi/integration/TeacherIT.java +++ b/src/test/java/school/hei/haapi/integration/TeacherIT.java @@ -23,8 +23,6 @@ import static school.hei.haapi.integration.conf.TestUtils.setUpCognito; 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.someCreatableTeacher; -import static school.hei.haapi.integration.conf.TestUtils.someCreatableTeacherList; import static school.hei.haapi.integration.conf.TestUtils.teacher1; import static school.hei.haapi.integration.conf.TestUtils.teacher2; import static school.hei.haapi.integration.conf.TestUtils.uploadProfilePicture; @@ -51,11 +49,13 @@ 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.endpoint.rest.mapper.UserMapper; import school.hei.haapi.endpoint.rest.model.CrupdateTeacher; import school.hei.haapi.endpoint.rest.model.EnableStatus; import school.hei.haapi.endpoint.rest.model.Sex; import school.hei.haapi.endpoint.rest.model.Teacher; import school.hei.haapi.integration.conf.FacadeITMockedThirdParties; +import school.hei.haapi.integration.conf.MockUtils; import school.hei.haapi.integration.conf.TestUtils; import software.amazon.awssdk.services.eventbridge.EventBridgeClient; import software.amazon.awssdk.services.eventbridge.model.PutEventsRequest; @@ -65,6 +65,8 @@ class TeacherIT extends FacadeITMockedThirdParties { @MockBean private EventBridgeClient eventBridgeClientMock; @Autowired private ObjectMapper objectMapper; + @Autowired private MockUtils mockUtils; + @Autowired private UserMapper userMapper; private ApiClient anApiClient(String token) { return TestUtils.anApiClient(token, localPort); @@ -147,7 +149,7 @@ void manager_read_ok() throws ApiException { void manager_write_update_rollback_on_event_error() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); - CrupdateTeacher toCreate = someCreatableTeacher(); + CrupdateTeacher toCreate = mockUtils.someCreatableTeacher(); reset(eventBridgeClientMock); when(eventBridgeClientMock.putEvents((PutEventsRequest) any())) .thenThrow(RuntimeException.class); @@ -163,19 +165,16 @@ void manager_write_update_rollback_on_event_error() throws ApiException { @Test void manager_write_create_ok() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); - CrupdateTeacher toCreate = someCreatableTeacher(); - Teacher expected = expectedCreatedTeacher(); + CrupdateTeacher toCreate = mockUtils.someCreatableTeacher(); + Teacher expected = userMapper.toRestTeacher(userMapper.toDomain(toCreate)); UsersApi api = new UsersApi(manager1Client); List created = api.createOrUpdateTeachers(List.of(toCreate)); assertEquals(1, created.size()); - Teacher created0 = created.getFirst(); - assertTrue(isValidUUID(created0.getId())); - expected.setId(created0.getId()); - expected.setRef(toCreate.getRef()); - expected.setEmail(toCreate.getEmail()); - assertEquals(expected, created0); + Teacher firstTeacher = created.getFirst(); + assertTrue(isValidUUID(firstTeacher.getId())); + assertEquals(expected, firstTeacher.id(null)); } @Test @@ -183,8 +182,8 @@ void manager_write_create_ok() throws ApiException { void manager_write_update_more_than_10_teachers_ko() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); - CrupdateTeacher teacherToCreate = someCreatableTeacher(); - List listToCreate = someCreatableTeacherList(11); + CrupdateTeacher teacherToCreate = mockUtils.someCreatableTeacher(); + List listToCreate = mockUtils.someCreatableTeacherList(11); listToCreate.add(teacherToCreate); assertThrowsApiException( @@ -200,18 +199,13 @@ void manager_write_update_more_than_10_teachers_ko() throws ApiException { void manager_write_update_ok() throws ApiException { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); - CrupdateTeacher toUpdate = someCreatableTeacher(); + CrupdateTeacher toUpdate = mockUtils.someCreatableTeacher(); List created = api.createOrUpdateTeachers(List.of(toUpdate)); toUpdate.setId(created.getFirst().getId()); - Teacher expected = expectedCreatedTeacher(); - expected.setId(created.getFirst().getId()); - expected.setLastName("New last name"); - expected.setEmail(toUpdate.getEmail()); - expected.setRef(toUpdate.getRef()); - toUpdate.setLastName("New last name"); + Teacher expected = userMapper.toRestTeacher(userMapper.toDomain(toUpdate)); List updated = api.createOrUpdateTeachers(List.of(toUpdate)); @@ -224,14 +218,15 @@ void manager_write_update_with_some_bad_fields_ko() { ApiClient manager1Client = anApiClient(MANAGER1_TOKEN); UsersApi api = new UsersApi(manager1Client); CrupdateTeacher toCreate1 = - someCreatableTeacher() + mockUtils + .someCreatableTeacher() .firstName(null) .lastName(null) .email(null) .address(null) .phone(null) .ref(null); - CrupdateTeacher toCreate2 = someCreatableTeacher().email("bademail"); + CrupdateTeacher toCreate2 = mockUtils.someCreatableTeacher().email("bademail"); ApiException exception1 = assertThrows(ApiException.class, () -> api.createOrUpdateTeachers(List.of(toCreate1))); diff --git a/src/test/java/school/hei/haapi/integration/conf/MockUtils.java b/src/test/java/school/hei/haapi/integration/conf/MockUtils.java new file mode 100644 index 0000000000..18e52ff4bc --- /dev/null +++ b/src/test/java/school/hei/haapi/integration/conf/MockUtils.java @@ -0,0 +1,140 @@ +package school.hei.haapi.integration.conf; + +import static java.time.ZoneOffset.UTC; +import static java.time.temporal.ChronoUnit.YEARS; +import static java.util.UUID.randomUUID; +import static school.hei.haapi.endpoint.rest.model.EnableStatus.ENABLED; +import static school.hei.haapi.model.User.Role.STUDENT; + +import com.github.javafaker.Faker; +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; +import org.joda.time.DateTime; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import school.hei.haapi.endpoint.rest.mapper.UserMapper; +import school.hei.haapi.endpoint.rest.model.CreateEvent; +import school.hei.haapi.endpoint.rest.model.CrupdateStudent; +import school.hei.haapi.endpoint.rest.model.CrupdateTeacher; +import school.hei.haapi.endpoint.rest.model.EventType; +import school.hei.haapi.endpoint.rest.model.Group; +import school.hei.haapi.endpoint.rest.model.Sex; +import school.hei.haapi.model.User; + +@Component +public class MockUtils { + private final Faker faker; + @Autowired UserMapper userMapper; + + MockUtils() { + faker = new Faker(); + } + + public CreateEvent someCreatableEvent( + EventType eventType, String planerId, Instant beginDatetime, Instant endDatetime) { + return new CreateEvent() + .id("event" + randomUUID() + "_id") + .courseId(TestUtils.COURSE1_ID) + .beginDatetime(beginDatetime) + .endDatetime(endDatetime) + .description("Another event") + .eventType(eventType) + .plannerId(planerId) + .groups(List.of(TestUtils.createGroupIdentifier(TestUtils.group1()))); + } + + private Date createSomeEntranceDateTime(Date userBirthdate, int minimumAge) { + return faker + .date() + .between( + Date.from( + userBirthdate + .toInstant() + .atZone(ZoneOffset.UTC) + .toLocalDateTime() + .plusYears(minimumAge) + .toInstant(UTC)), + DateTime.now().toDate()); + } + + private Date createSomeBirthDateBetween(int minimumAge, int maximumAge) { + DateTime now = DateTime.now(); + return faker + .date() + .between(now.minusYears(maximumAge).toDate(), now.minusYears(minimumAge).toDate()); + } + + public List someCreatableTeacherList(int nbOfTeacher) { + List teacherList = new ArrayList<>(); + for (int i = 0; i < nbOfTeacher; i++) { + teacherList.add(someCreatableTeacher()); + } + return teacherList; + } + + public CrupdateTeacher someCreatableTeacher() { + var firstName = faker.name().firstName(); + Date birthDate = createSomeBirthDateBetween(20, 35); + return new CrupdateTeacher() + .firstName(firstName) + .lastName(faker.name().lastName()) + .email("%s%d@hei.school".formatted(firstName, faker.number().randomNumber(10, true))) + .ref("TCR" + faker.number().randomNumber(5, true)) + .phone(faker.phoneNumber().phoneNumber()) + .status(ENABLED) + .sex(Sex.valueOf(faker.options().option(Sex.M.getValue(), Sex.F.getValue()))) + .birthDate(birthDate.toInstant().atOffset(UTC).toLocalDate()) + .entranceDatetime(createSomeEntranceDateTime(birthDate, 20).toInstant()) + .coordinates(TestUtils.coordinatesWithNullValues()) + .address("Adr X"); + } + + public User createSomeUser(User.Role role, User.Status status) { + var userBirthdate = createSomeBirthDateBetween(18, 25); + return User.builder() + .firstName(faker.name().firstName()) + .lastName(faker.name().lastName()) + .email(faker.internet().emailAddress()) + .phone(faker.phoneNumber().phoneNumber()) + .latitude(faker.random().nextDouble()) + .longitude(faker.random().nextDouble()) + .address(faker.address().streetAddress()) + .sex(Math.random() >= 0.3 ? User.Sex.M : User.Sex.F) + .status(status) + .role(role) + .ref(role + "-" + faker.number().randomNumber(5, true)) + .nic(String.valueOf(faker.number().randomNumber(12, true))) + .birthDate(userBirthdate.toInstant().atOffset(UTC).toLocalDate()) + .entranceDatetime(createSomeEntranceDateTime(userBirthdate, 18).toInstant()) + .build(); + } + + public List createSomeUsers(int count, User.Role role, User.Status status) { + return IntStream.range(0, count).mapToObj(i -> createSomeUser(role, status)).toList(); + } + + public Group createSomeGroup() { + return new Group() + .name(faker.lorem().sentence(3)) + .ref("GRP" + faker.number().randomNumber(5, true)) + .creationDatetime(faker.date().past(4, TimeUnit.of(YEARS)).toInstant()); + } + + public List createSomeGroupList(int nbOfGroup) { + List groupList = new ArrayList<>(); + for (int i = 0; i < nbOfGroup; i++) { + groupList.add(createSomeGroup()); + } + return groupList; + } + + public CrupdateStudent someCreatableStudent() { + return TestUtils.toCrupdateStudent( + userMapper.toRestStudent(createSomeUser(STUDENT, User.Status.ENABLED))); + } +} 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 c74d46a5e2..2e82013a76 100644 --- a/src/test/java/school/hei/haapi/integration/conf/TestUtils.java +++ b/src/test/java/school/hei/haapi/integration/conf/TestUtils.java @@ -1,6 +1,5 @@ package school.hei.haapi.integration.conf; -import static java.util.UUID.randomUUID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; @@ -82,12 +81,11 @@ import school.hei.haapi.endpoint.rest.model.CrupdateGrade; import school.hei.haapi.endpoint.rest.model.CrupdateMonitor; import school.hei.haapi.endpoint.rest.model.CrupdatePromotion; +import school.hei.haapi.endpoint.rest.model.CrupdateStudent; import school.hei.haapi.endpoint.rest.model.CrupdateStudentFee; -import school.hei.haapi.endpoint.rest.model.CrupdateTeacher; import school.hei.haapi.endpoint.rest.model.Event; import school.hei.haapi.endpoint.rest.model.EventParticipant; import school.hei.haapi.endpoint.rest.model.EventStats; -import school.hei.haapi.endpoint.rest.model.EventType; import school.hei.haapi.endpoint.rest.model.ExamInfo; import school.hei.haapi.endpoint.rest.model.Fee; import school.hei.haapi.endpoint.rest.model.FeeFrequency; @@ -98,7 +96,6 @@ import school.hei.haapi.endpoint.rest.model.Letter; import school.hei.haapi.endpoint.rest.model.LetterUser; import school.hei.haapi.endpoint.rest.model.Manager; -import school.hei.haapi.endpoint.rest.model.Monitor; import school.hei.haapi.endpoint.rest.model.Observer; import school.hei.haapi.endpoint.rest.model.Promotion; import school.hei.haapi.endpoint.rest.model.Scope; @@ -117,7 +114,6 @@ import software.amazon.awssdk.services.eventbridge.model.PutEventsResponse; public class TestUtils { - public static final String STAFF_MEMBER1_ID = "staff1_id"; public static final String STUDENT1_ID = "student1_id"; public static final String STUDENT2_ID = "student2_id"; @@ -318,21 +314,6 @@ public static TransactionDetails psp2Verification() { .build(); } - public static CrupdateTeacher someCreatableTeacher() { - return new CrupdateTeacher() - .firstName("Some") - .lastName("User") - .email(randomUUID() + "@hei.school") - .ref("TCR21-" + randomUUID()) - .phone("0332511129") - .status(ENABLED) - .sex(Sex.M) - .birthDate(LocalDate.parse("2000-01-01")) - .entranceDatetime(Instant.parse("2021-11-08T08:25:24.00Z")) - .coordinates(coordinatesWithNullValues()) - .address("Adr X"); - } - public static CreateFee creatableFee1() { return new CreateFee() .type(TUITION) @@ -354,26 +335,6 @@ public static CrupdateStudentFee creatableStudentFee() { .dueDatetime(Instant.parse("2021-12-08T08:25:24.00Z")); } - public static CrupdateStudentFee updatableStudentFee() { - return new CrupdateStudentFee() - .id(FEE3_ID) - .studentId(STUDENT1_ID) - .type(TUITION) - .totalAmount(5000) - .category(UNKNOWN) - .frequency(FeeFrequency.UNKNOWN) - .comment("Updated comment") - .creationDatetime(Instant.parse("2022-12-08T08:25:24.00Z")) - .dueDatetime(Instant.parse("2021-12-09T08:25:24.00Z")); - } - - public static Group createGroup() { - return new Group() - .name("Collaborative work like GWSP") - .ref("created") - .creationDatetime(Instant.parse("2021-11-08T08:25:24.00Z")); - } - public static Course createCourse(String code) { return new Course().code(code).name("Collaborative work like GWSP").credits(12).totalHours(5); } @@ -426,22 +387,6 @@ public static CrupdateGrade createGrade() { return new CrupdateGrade().score(20.0); } - public static List someCreatableTeacherList(int nbOfTeacher) { - List teacherList = new ArrayList<>(); - for (int i = 0; i < nbOfTeacher; i++) { - teacherList.add(someCreatableTeacher()); - } - return teacherList; - } - - public static List someCreatableGroupList(int nbOfGroup) { - List groupList = new ArrayList<>(); - for (int i = 0; i < nbOfGroup; i++) { - groupList.add(createGroup()); - } - return groupList; - } - public static List someCreatableCourseList(int nbOfCourse) { List courseList = new ArrayList<>(); for (int i = 0; i < nbOfCourse; i++) { @@ -635,44 +580,6 @@ public static Teacher teacher4() { .coordinates(coordinatesWithNullValues()); } - public static Monitor monitor1() { - return new Monitor() - .id(MONITOR1_ID) - .firstName("Monitor") - .lastName("One") - .email("test+monitor@hei.school") - .ref("MTR21001") - .phone("0322411123") - .status(ENABLED) - .sex(Sex.M) - .birthDate(LocalDate.parse("2000-01-01")) - .entranceDatetime(Instant.parse("2021-11-08T08:25:24.00Z")) - .nic("") - .birthPlace("") - .address("Adr 1") - .coordinates(new Coordinates().longitude(-123.123).latitude(123.0)) - .highSchoolOrigin("Lycée Andohalo"); - } - - public static Monitor monitor2() { - return new Monitor() - .id(MONITOR2_ID) - .firstName("Monitor2") - .lastName("two") - .email("test+monitor2@hei.school") - .ref("MTR21002") - .phone("0322411123") - .status(ENABLED) - .sex(Sex.M) - .birthDate(LocalDate.parse("2000-02-02")) - .entranceDatetime(Instant.parse("2021-11-08T08:25:24.00Z")) - .nic("") - .birthPlace("") - .address("Adr 2") - .coordinates(new Coordinates().longitude(-123.123).latitude(123.0)) - .highSchoolOrigin("Lycée Andohalo"); - } - public static CrupdateMonitor monitor1Link(List studentRefs) { return new CrupdateMonitor() .id(MONITOR1_ID) @@ -977,26 +884,6 @@ public static StudentGrade studentGrade1() { return new StudentGrade().grade(grade1()).student(student1()); } - public static StudentGrade studentGrade2() { - return new StudentGrade().grade(grade2()); - } - - public static StudentGrade studentGrade3() { - return new StudentGrade().grade(grade3()); - } - - public static StudentGrade studentGrade4() { - return new StudentGrade().grade(grade4()); - } - - public static StudentGrade studentGrade5() { - return new StudentGrade().grade(grade5()); - } - - public static StudentGrade studentGrade6() { - return new StudentGrade().grade(grade6()); - } - public static StudentGrade studentGrade7() { return new StudentGrade().grade(grade7()).student(student3()); } @@ -1056,15 +943,6 @@ public static FeeTemplate feeTemplate2() { .type(TUITION); } - // public static ExamDetail examDetail1() { - // return new ExamDetail() - // .id(exam1().getId()) - // .title(exam1().getTitle()) - // .examinationDate(exam1().getExaminationDate()) - // .coefficient(exam1().getCoefficient()) - // .participants(List.of(studentGrade1(), studentGrade7())); - // } - public static AwardedCourseExam awardedCourseExam1() { return new AwardedCourseExam() .id(AWARDED_COURSE1_ID) @@ -1434,27 +1312,6 @@ public static CreateEvent createEventCourse1() { .groups(List.of(createGroupIdentifier(group1()))); } - public static CreateEvent someCreatableEventByManager1(EventType eventType) { - return someCreatableEvent( - eventType, - MANAGER_ID, - Instant.parse("2023-12-08T08:00:00.00Z"), - Instant.parse("2023-12-08T10:00:00.00Z")); - } - - public static CreateEvent someCreatableEvent( - EventType eventType, String planerId, Instant beginDatetime, Instant endDatetime) { - return new CreateEvent() - .id("event" + randomUUID() + "_id") - .courseId(COURSE1_ID) - .beginDatetime(beginDatetime) - .endDatetime(endDatetime) - .description("Another event") - .eventType(eventType) - .plannerId(planerId) - .groups(List.of(createGroupIdentifier(group1()))); - } - public static CreateFee createFeeForTest() { return new CreateFee() .comment("test") @@ -1737,4 +1594,24 @@ public static int anAvailableRandomPort() { throw new RuntimeException(e); } } + + public static CrupdateStudent toCrupdateStudent(Student student) { + return new CrupdateStudent() + .id(student.getId()) + .birthDate(student.getBirthDate()) + .id(student.getId()) + .entranceDatetime(student.getEntranceDatetime()) + .phone(student.getPhone()) + .nic(student.getNic()) + .birthPlace(student.getBirthPlace()) + .email(student.getEmail()) + .address(student.getAddress()) + .firstName(student.getFirstName()) + .lastName(student.getLastName()) + .sex(student.getSex()) + .ref(student.getRef()) + .specializationField(student.getSpecializationField()) + .coordinates(student.getCoordinates()) + .status(student.getStatus()); + } } diff --git a/src/test/java/school/hei/haapi/service/PaymentServiceTest.java b/src/test/java/school/hei/haapi/service/PaymentServiceTest.java index 63aa7edf60..1fba9bbcaf 100644 --- a/src/test/java/school/hei/haapi/service/PaymentServiceTest.java +++ b/src/test/java/school/hei/haapi/service/PaymentServiceTest.java @@ -8,7 +8,6 @@ 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.TestUtils.MANAGER1_TOKEN; @@ -26,8 +25,6 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; 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; @@ -40,6 +37,7 @@ 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.MockUtils; import school.hei.haapi.integration.conf.MockedThirdParties; import school.hei.haapi.integration.conf.TestUtils; import school.hei.haapi.model.Fee; @@ -51,12 +49,12 @@ @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; + @Autowired private MockUtils mockUtils; private String FEE7_ID = "fee7_id"; @BeforeEach @@ -72,7 +70,7 @@ void user_status_is_computed_after_paying_fee_by_mpbs() throws ApiException { UsersApi usersApi = new UsersApi(manager1Client); PayingApi payingApi = new PayingApi(manager1Client); - var correspondingCreateableStudent = someCreatableStudent(); + var correspondingCreateableStudent = mockUtils.someCreatableStudent(); var correspondingFee = payingApi.createStudentFees(STUDENT1_ID, List.of(creatableFee1())).getFirst(); var correspondingMpbs = diff --git a/src/test/java/school/hei/haapi/service/UserServiceTest.java b/src/test/java/school/hei/haapi/service/UserServiceTest.java index 6fb3c1b233..c131e3d3c1 100644 --- a/src/test/java/school/hei/haapi/service/UserServiceTest.java +++ b/src/test/java/school/hei/haapi/service/UserServiceTest.java @@ -12,6 +12,7 @@ import static school.hei.haapi.model.User.Role.STUDENT; import java.util.List; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -28,6 +29,7 @@ @Testcontainers @AutoConfigureMockMvc +@Slf4j class UserServiceTest extends FacadeITMockedThirdParties { @Autowired private UserService subject; @MockBean private EventBridgeClient eventBridgeClientMock; @@ -54,6 +56,9 @@ void dao_can_handle_null_value_in_params() { List students = subject.getByCriteria( STUDENT, null, null, null, new PageFromOne(1), new BoundedPageSize(15), null, null); + + // Todo: remove this log.info, test doesn't pass due to other test + log.info("students: {}", students); assertEquals(student1().getId(), students.getFirst().getId()); assertEquals(student2().getId(), students.get(1).getId()); } @@ -68,8 +73,4 @@ void disabled_user_enable_ko() { student1.setStatus(User.Status.ENABLED); }); } - - private ApiClient anApiClient(String token) { - return TestUtils.anApiClient(token, localPort); - } }