-
Notifications
You must be signed in to change notification settings - Fork 131
chore: increase code reliability #911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: preprod
Are you sure you want to change the base?
Changes from all commits
97c277b
6bf73d0
81846b9
67bfde8
248a712
da107b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,10 +29,11 @@ public class EventMapper { | |
| public school.hei.haapi.model.Event toDomain(CreateEvent createEvent) { | ||
| List<GroupIdentifier> groupIdentifiers = Objects.requireNonNull(createEvent.getGroups()); | ||
| List<Group> groups = | ||
| groupService.getAllById(groupIdentifiers.stream().map(GroupIdentifier::getId).toList()); | ||
| groups.stream() | ||
| .map(group -> mapGroupColorFromGroupIdentifiers(group, groupIdentifiers)) | ||
| .toList(); | ||
| groupService | ||
| .getAllById(groupIdentifiers.stream().map(GroupIdentifier::getId).toList()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to collect toList if you're going to stream it again man
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, to ensure that you really get all given groups, you might need to check if getAllById throws NotFound if ID is not found in DB, otherwise, you'll need to check for the size, like given group list size must equal retrieved group size from db |
||
| .stream() | ||
| .map(group -> mapGroupColorFromGroupIdentifiers(group, groupIdentifiers)) | ||
| .toList(); | ||
| List<Group> mappedGroup = groupService.saveDomainGroup(groups); | ||
|
|
||
| return Event.builder() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| package school.hei.haapi.endpoint.rest.mapper; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
| import school.hei.haapi.endpoint.rest.model.CrupdateGrade; | ||
|
|
@@ -10,6 +9,7 @@ | |
| import school.hei.haapi.endpoint.rest.validator.GradeValidator; | ||
| import school.hei.haapi.model.Exam; | ||
| import school.hei.haapi.model.User; | ||
| import school.hei.haapi.model.exception.NotFoundException; | ||
| import school.hei.haapi.repository.GradeRepository; | ||
| import school.hei.haapi.service.ExamService; | ||
| import school.hei.haapi.service.GradeService; | ||
|
|
@@ -25,63 +25,41 @@ public class GradeMapper { | |
| private final GradeValidator validator; | ||
| private final GradeRepository gradeRepository; | ||
|
|
||
| // todo: to review all class | ||
| public school.hei.haapi.model.Grade toDomain(Grade grade) { | ||
| return school.hei.haapi.model.Grade.builder() | ||
| .score(grade.getScore()) | ||
| .creationDatetime(grade.getCreatedAt()) | ||
| .build(); | ||
| } | ||
|
|
||
| public Grade toRest(school.hei.haapi.model.Grade grade) { | ||
| return new Grade() | ||
| .id(grade.getId()) | ||
| .createdAt(grade.getCreationDatetime()) | ||
| .score(grade.getScore().doubleValue()) | ||
| .score(grade.getScore()) | ||
| .updateDate(grade.getCreationDatetime()); | ||
| } | ||
|
|
||
| public StudentGrade toRestStudentGrade(school.hei.haapi.model.Grade grade) { | ||
| if (grade == null) { | ||
| return null; | ||
| } | ||
| var getStudentGrade = new StudentGrade().grade(toRest(grade)); | ||
| getStudentGrade.setStudent(userMapper.toRestStudent(grade.getStudent())); | ||
|
|
||
| return getStudentGrade; | ||
| } | ||
|
|
||
| public StudentGrade toRestStudentExamGrade(User student, Exam exam) { | ||
| Optional<school.hei.haapi.model.Grade> optionalGrade = | ||
| exam.getGrades().stream() | ||
| .filter(grade -> grade.getStudent().getId().equals(student.getId())) | ||
| .findFirst(); | ||
| school.hei.haapi.model.Grade grade = optionalGrade.get(); | ||
| var getStudentGrade = new StudentGrade().grade(toRest(grade)); | ||
| getStudentGrade.setStudent(userMapper.toRestStudent(student)); | ||
| return getStudentGrade; | ||
| return new StudentGrade() | ||
| .grade( | ||
| toRest( | ||
| exam.getGrades().stream() | ||
| .filter(grade -> grade.getStudent().getId().equals(student.getId())) | ||
| .findFirst() | ||
| .orElseThrow( | ||
| () -> | ||
| new NotFoundException( | ||
| "Student %s have no grade for the exam %s" | ||
| .formatted(student.getId(), exam.getId()))))) | ||
|
Comment on lines
+47
to
+54
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm, this looks like it could be done with way more ease, |
||
| .student(userMapper.toRestStudent(student)); | ||
| } | ||
|
|
||
| // public ExamDetail toRestExamDetail(Exam exam, List<school.hei.haapi.model.Grade> grades) { | ||
| // return new ExamDetail() | ||
| // .id(exam.getId()) | ||
| // .coefficient(exam.getCoefficient()) | ||
| // .title(exam.getTitle()) | ||
| // .examinationDate(exam.getExaminationDate().atZone(ZoneId.systemDefault()).toInstant()) | ||
| // .participants( | ||
| // grades.stream().map(grade -> this.toRestStudentGrade(grade)).collect(toList())); | ||
| // } | ||
|
|
||
| public school.hei.haapi.model.Grade toDomain( | ||
| CrupdateGrade grade, String examId, String studentRef) { | ||
| validator.accept(grade); | ||
|
|
||
| Exam exam = examService.getExamById(examId); | ||
| double scoreFinal = 0.0; | ||
|
|
||
| if (exam.getCoefficient() > 0 && grade.getScore() != null && grade.getScore() >= 0) { | ||
| scoreFinal = grade.getScore() * exam.getCoefficient(); | ||
| } | ||
|
|
||
| school.hei.haapi.model.Grade resultGrade = | ||
| gradeRepository | ||
|
|
@@ -94,7 +72,7 @@ public school.hei.haapi.model.Grade toDomain( | |
| exam, userService.findByRef(studentRef)))) | ||
| .getFirst()); | ||
|
|
||
| resultGrade.setScore(scoreFinal); | ||
| resultGrade.setScore(grade.getScore() * exam.getCoefficient()); | ||
| return resultGrade; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,25 +95,22 @@ public static String getFormattedBucketKey(User user, FileType fileType, String | |
| } | ||
|
|
||
| /* Use the JDK HttpClient (since v11) class to do the download. */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did you choose HttpClient ? |
||
| public byte[] useHttpClientToGet(String presignedUrlString) { | ||
| public byte[] useHttpClientToGet(String presignedUrlString) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. method name leaks implementation details, className should be enough to guess that this class handles HTTP or in a more abstract way, Networking things, and method name could be httpGet |
||
| throws URISyntaxException, IOException, InterruptedException { | ||
| ByteArrayOutputStream byteArrayOutputStream = | ||
| new ByteArrayOutputStream(); // Capture the response body to a byte array. | ||
|
|
||
| HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(); | ||
| HttpClient httpClient = HttpClient.newHttpClient(); | ||
| try { | ||
| URL presignedUrl = new URL(presignedUrlString); | ||
| HttpResponse<InputStream> response = | ||
| httpClient.send( | ||
| requestBuilder.uri(presignedUrl.toURI()).GET().build(), | ||
| HttpResponse.BodyHandlers.ofInputStream()); | ||
| URL presignedUrl = new URL(presignedUrlString); | ||
| HttpResponse<InputStream> response = | ||
| httpClient.send( | ||
| requestBuilder.uri(presignedUrl.toURI()).GET().build(), | ||
| HttpResponse.BodyHandlers.ofInputStream()); | ||
|
|
||
| IoUtils.copy(response.body(), byteArrayOutputStream); | ||
| IoUtils.copy(response.body(), byteArrayOutputStream); | ||
|
|
||
| logger.info("HTTP response code is " + response.statusCode()); | ||
| } catch (URISyntaxException | InterruptedException | IOException e) { | ||
| logger.error(e.getMessage(), e); | ||
| } | ||
| logger.info("HTTP response code is " + response.statusCode()); | ||
| return byteArrayOutputStream.toByteArray(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package school.hei.haapi.unit.objectMapper; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyList; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import school.hei.haapi.endpoint.rest.mapper.GradeMapper; | ||
| import school.hei.haapi.endpoint.rest.model.CrupdateGrade; | ||
| import school.hei.haapi.model.Exam; | ||
| import school.hei.haapi.model.Grade; | ||
| import school.hei.haapi.model.User; | ||
| import school.hei.haapi.model.exception.NotFoundException; | ||
| import school.hei.haapi.repository.GradeRepository; | ||
| import school.hei.haapi.service.ExamService; | ||
| import school.hei.haapi.service.GradeService; | ||
|
|
||
| class GradeMapperTest { | ||
| GradeMapper subject; | ||
| ExamService examService; | ||
| GradeRepository gradeRepository; | ||
| GradeService gradeService; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| gradeService = mock(); | ||
| examService = mock(); | ||
| gradeRepository = mock(); | ||
|
Comment on lines
+33
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use mock(GradeService.class) for readability |
||
| subject = new GradeMapper(mock(), gradeService, examService, mock(), mock(), gradeRepository); | ||
| } | ||
|
|
||
| @Test | ||
| void combine_student_with_bad_exam_to_get_grade_ko() { | ||
| User student1 = new User(); | ||
| student1.setId("student1"); | ||
|
Comment on lines
+41
to
+42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. builder style for fewer lines |
||
| User student2 = new User(); | ||
| student2.setId("student2"); | ||
| Exam exam = new Exam(); | ||
| exam.setGrades(List.of(new Grade("", student2, new Exam(), null, null))); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exam.setGrades, and the given Grade is not even linked to the exam where we add it? |
||
|
|
||
| NotFoundException notFoundException = | ||
| assertThrows(NotFoundException.class, () -> subject.toRestStudentExamGrade(student1, exam)); | ||
| assertEquals( | ||
| "Student %s have no grade for the exam %s".formatted(student1.getId(), exam.getId()), | ||
| notFoundException.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void map_grade_to_grade_or_create_grade() { | ||
| when(examService.getExamById(any())).thenReturn(new Exam(null, 1, null, null, null, null)); | ||
| when(gradeRepository.getGradeByExamIdAndStudentRef(any(), any())).thenReturn(Optional.empty()); | ||
| Grade gradeSaved = new Grade("", new User(), new Exam(), 1., Instant.now()); | ||
| when(gradeService.crupdateParticipantGrade(anyList())).thenReturn(List.of(gradeSaved)); | ||
|
|
||
| Grade grade = subject.toDomain(new CrupdateGrade().score(gradeSaved.getScore()), "", ""); | ||
|
|
||
| assertEquals(gradeSaved, grade); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't you use UserService's method?