Skip to content

Commit

Permalink
[frontend/backend] improve code
Browse files Browse the repository at this point in the history
Signed-off-by: Marine LM <[email protected]>
  • Loading branch information
MarineLeM committed Sep 25, 2024
1 parent fadc3c8 commit 5a2effb
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 109 deletions.
19 changes: 15 additions & 4 deletions openbas-api/src/main/java/io/openbas/service/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.openbas.rest.exception.ElementNotFoundException;
import io.openbas.rest.report.form.ReportInjectCommentInput;
import io.openbas.rest.report.form.ReportInput;
import jakarta.persistence.EntityManager;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,6 +24,7 @@
@Service
public class ReportService {
private final ReportRepository reportRepository;
private final EntityManager entityManager;

public Report report(@NotNull final UUID reportId) {
return this.reportRepository.findById(reportId).orElseThrow(ElementNotFoundException::new);
Expand Down Expand Up @@ -54,10 +56,8 @@ public Report updateReport(@NotNull final Report report, @NotNull final ReportIn
}

public List<ReportInjectComment> updateReportInjectComment(@NotNull final Report report, @NotNull final Inject inject, @NotNull final ReportInjectCommentInput input){
ReportInjectComment reportInjectComment = report.getReportInjectsComments().stream()
.filter(c -> c.getInject().getId().equals(input.getInjectId()))
.findFirst()
.orElse(null);
ReportInjectComment reportInjectComment = findReportInjectComment(report.getId(), inject.getId());

if (reportInjectComment != null) {
reportInjectComment.setComment(input.getComment());
} else {
Expand All @@ -71,6 +71,17 @@ public List<ReportInjectComment> updateReportInjectComment(@NotNull final Report
return report.getReportInjectsComments();
}

private ReportInjectComment findReportInjectComment(String reportId, String injectId) {
String jpql = "SELECT r FROM ReportInjectComment r WHERE r.report.id = :reportId AND r.inject.id = :injectId";

return this.entityManager.createQuery(jpql, ReportInjectComment.class)
.setParameter("reportId", UUID.fromString(reportId))
.setParameter("injectId", injectId)
.getResultStream()
.findFirst()
.orElse(null);
}

public void deleteReport(@NotBlank final UUID reportId) {
this.reportRepository.deleteById(reportId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.openbas.rest.report.form.ReportInformationInput;
import io.openbas.rest.report.form.ReportInjectCommentInput;
import io.openbas.rest.report.form.ReportInput;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand All @@ -16,9 +18,10 @@
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -28,13 +31,17 @@ public class ReportServiceTest {

@Mock
private ReportRepository reportRepository;
@Mock
private EntityManager entityManager;
@Mock
private TypedQuery<ReportInjectComment> reportInjectCommentQuery;

private ReportService reportService;

@BeforeEach
void before() {
// Injecting mocks into the controller
reportService = new ReportService(reportRepository);
reportService = new ReportService(reportRepository, entityManager);
}

@DisplayName("Test create a report")
Expand Down Expand Up @@ -99,7 +106,7 @@ void updateReport() throws Exception {
when(reportRepository.save(any(Report.class))).thenReturn(report);

// -- EXECUTE --
reportService.updateReport(report, reportInput);
reportService.updateReport(report, reportInput);

// -- ASSERT --
ArgumentCaptor<Report> reportCaptor = ArgumentCaptor.forClass(Report.class);
Expand All @@ -115,12 +122,14 @@ void updateReport() throws Exception {
class ReportInjectCommentTest {
@DisplayName("Test update existing report inject comment")
@Test
void updateExistingReportInjectComment() throws Exception {
void updateExistingReportInjectComment() {
// -- PREPARE --
Report report = new Report();
report.setName("test");
report.setId(UUID.randomUUID().toString());
Inject inject = new Inject();
inject.setId("fakeID123");

// add report inject comment
ReportInjectComment existingReportInjectComment = new ReportInjectComment();
existingReportInjectComment.setReport(report);
Expand All @@ -132,7 +141,11 @@ void updateExistingReportInjectComment() throws Exception {
commentInput.setInjectId(inject.getId());
commentInput.setComment("New comment");

when(reportRepository.save(any(Report.class))).thenReturn(report);
// Mock
when(entityManager.createQuery(anyString(), eq(ReportInjectComment.class))).thenReturn(reportInjectCommentQuery);
when(reportInjectCommentQuery.setParameter(eq("reportId"), eq(UUID.fromString(report.getId())))).thenReturn(reportInjectCommentQuery);
when(reportInjectCommentQuery.setParameter(eq("injectId"), eq(inject.getId()))).thenReturn(reportInjectCommentQuery);
when(reportInjectCommentQuery.getResultStream()).thenReturn(java.util.stream.Stream.of(existingReportInjectComment));

// -- EXECUTE --
reportService.updateReportInjectComment(report, inject, commentInput);
Expand All @@ -151,14 +164,20 @@ void addReportInjectComment() throws Exception {
// -- PREPARE --
Report report = new Report();
report.setName("test");
report.setId(UUID.randomUUID().toString());
Inject inject = new Inject();
inject.setId("fakeID123");

ReportInjectCommentInput commentInput = new ReportInjectCommentInput();
commentInput.setInjectId(inject.getId());
commentInput.setComment("New test comment");

// Mock
when(reportRepository.save(any(Report.class))).thenReturn(report);
when(entityManager.createQuery(anyString(), eq(ReportInjectComment.class))).thenReturn(reportInjectCommentQuery);
when(reportInjectCommentQuery.setParameter(eq("reportId"), eq(UUID.fromString(report.getId())))).thenReturn(reportInjectCommentQuery);
when(reportInjectCommentQuery.setParameter(eq("injectId"), eq(inject.getId()))).thenReturn(reportInjectCommentQuery);
when(reportInjectCommentQuery.getResultStream()).thenReturn(java.util.stream.Stream.of());

// -- EXECUTE --
reportService.updateReportInjectComment(report, inject, commentInput);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';
import { Paper, Typography } from '@mui/material';
import type { Exercise, ExpectationResultsByType, InjectResultDTO, Report, ReportInformation } from '../../../../../utils/api-types';
import type { Exercise, ExpectationResultsByType, InjectResultDTO, Report, ReportInformation, ReportInjectComment } from '../../../../../utils/api-types';
import { useAppDispatch } from '../../../../../utils/hooks';
import { useFormatter } from '../../../../../components/i18n';
import ReportInformationType from './ReportInformationType';
Expand Down Expand Up @@ -129,7 +129,7 @@ const ExerciseReportContent: React.FC<Props> = ({ report, exerciseId, canWrite =
initialInjectComments={report?.report_injects_comments}
injects={injects}
style={{ width: '100%', marginTop: 20 }}
onCommentSubmit={(value) => updateReportInjectCommentForExercise(exerciseId, report.report_id, value)}
onCommentSubmit={(value: ReportInjectComment) => updateReportInjectCommentForExercise(exerciseId, report.report_id, value)}
/>
)
}
Expand Down
Loading

0 comments on commit 5a2effb

Please sign in to comment.