AuditExportService.java

1
package com.edtech.service;
2
3
import com.edtech.model.AuditLog;
4
import com.edtech.model.Document;
5
import com.edtech.repository.AuditLogRepository;
6
import com.edtech.repository.DocumentRepository;
7
import com.edtech.repository.ProjectMemberRepository;
8
import java.util.List;
9
import java.util.Locale;
10
import java.util.UUID;
11
import org.springframework.stereotype.Service;
12
13
/** Service responsavel por exportar trilhas de auditoria de documentos. */
14
@Service
15
public class AuditExportService {
16
17
  private static final String CSV_FORMAT = "csv";
18
  private static final String DOCUMENT_RESOURCE_TYPE = "Document";
19
20
  private final AuditLogRepository auditLogRepository;
21
  private final DocumentRepository documentRepository;
22
  private final ProjectMemberRepository projectMemberRepository;
23
  private final AuditTrailCsvExporter csvExporter;
24
25
  /** Cria o servico com os repositorios e exportador necessarios. */
26
  public AuditExportService(
27
      AuditLogRepository auditLogRepository,
28
      DocumentRepository documentRepository,
29
      ProjectMemberRepository projectMemberRepository,
30
      AuditTrailCsvExporter csvExporter) {
31
    this.auditLogRepository = auditLogRepository;
32
    this.documentRepository = documentRepository;
33
    this.projectMemberRepository = projectMemberRepository;
34
    this.csvExporter = csvExporter;
35
  }
36
37
  /**
38
   * Exporta os logs de auditoria relacionados a um documento.
39
   *
40
   * @param documentId identificador do documento.
41
   * @param userId identificador do usuario que solicitou a exportacao.
42
   * @param format formato solicitado para exportacao.
43
   * @return conteudo do arquivo exportado em bytes.
44
   */
45
  public byte[] exportDocumentAuditTrail(UUID documentId, UUID userId, String format) {
46
    String normalizedFormat = normalizeFormat(format);
47 1 1. exportDocumentAuditTrail : negated conditional → KILLED
    if (!CSV_FORMAT.equals(normalizedFormat)) {
48
      throw new IllegalArgumentException("Formato invalido. Use: csv");
49
    }
50
51
    Document document =
52
        documentRepository
53
            .findById(documentId)
54 1 1. lambda$exportDocumentAuditTrail$0 : replaced return value with null for com/edtech/service/AuditExportService::lambda$exportDocumentAuditTrail$0 → KILLED
            .orElseThrow(() -> new RuntimeException("Document not found"));
55
56
    projectMemberRepository
57
        .findByProjectIdAndUserId(document.getProject().getId(), userId)
58
        .orElseThrow(
59 1 1. lambda$exportDocumentAuditTrail$1 : replaced return value with null for com/edtech/service/AuditExportService::lambda$exportDocumentAuditTrail$1 → KILLED
            () -> new RuntimeException("Access denied: You are not a member of this project"));
60
61
    List<AuditLog> logs =
62
        auditLogRepository.findByResourceTypeAndResourceIdOrderByCreatedAtAsc(
63
            DOCUMENT_RESOURCE_TYPE, documentId);
64
65 1 1. exportDocumentAuditTrail : replaced return value with null for com/edtech/service/AuditExportService::exportDocumentAuditTrail → KILLED
    return csvExporter.export(logs);
66
  }
67
68
  public String buildFilename(UUID documentId, String format) {
69 1 1. buildFilename : replaced return value with "" for com/edtech/service/AuditExportService::buildFilename → KILLED
    return "audit-trail-" + documentId + "." + normalizeFormat(format);
70
  }
71
72
  private String normalizeFormat(String format) {
73 2 1. normalizeFormat : negated conditional → KILLED
2. normalizeFormat : negated conditional → KILLED
    if (format == null || format.isBlank()) {
74 1 1. normalizeFormat : replaced return value with "" for com/edtech/service/AuditExportService::normalizeFormat → KILLED
      return CSV_FORMAT;
75
    }
76 1 1. normalizeFormat : replaced return value with "" for com/edtech/service/AuditExportService::normalizeFormat → KILLED
    return format.toLowerCase(Locale.ROOT);
77
  }
78
}

Mutations

47

1.1
Location : exportDocumentAuditTrail
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:exportDocumentAuditTrail_WithInvalidFormat_ThrowsBeforeRepositoryAccess()]
negated conditional → KILLED

54

1.1
Location : lambda$exportDocumentAuditTrail$0
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:exportDocumentAuditTrail_WhenDocumentDoesNotExist_Throws()]
replaced return value with null for com/edtech/service/AuditExportService::lambda$exportDocumentAuditTrail$0 → KILLED

59

1.1
Location : lambda$exportDocumentAuditTrail$1
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:exportDocumentAuditTrail_WhenUserIsNotProjectMember_Throws()]
replaced return value with null for com/edtech/service/AuditExportService::lambda$exportDocumentAuditTrail$1 → KILLED

65

1.1
Location : exportDocumentAuditTrail
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:exportDocumentAuditTrail_WithNullFormat_UsesCsvFormat()]
replaced return value with null for com/edtech/service/AuditExportService::exportDocumentAuditTrail → KILLED

69

1.1
Location : buildFilename
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:buildFilename_NormalizesBlankAndUppercaseFormats()]
replaced return value with "" for com/edtech/service/AuditExportService::buildFilename → KILLED

73

1.1
Location : normalizeFormat
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:buildFilename_NormalizesBlankAndUppercaseFormats()]
negated conditional → KILLED

2.2
Location : normalizeFormat
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:exportDocumentAuditTrail_WithInvalidFormat_ThrowsBeforeRepositoryAccess()]
negated conditional → KILLED

74

1.1
Location : normalizeFormat
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:buildFilename_NormalizesBlankAndUppercaseFormats()]
replaced return value with "" for com/edtech/service/AuditExportService::normalizeFormat → KILLED

76

1.1
Location : normalizeFormat
Killed by : com.edtech.service.AuditExportServiceTest.[engine:junit-jupiter]/[class:com.edtech.service.AuditExportServiceTest]/[method:buildFilename_NormalizesBlankAndUppercaseFormats()]
replaced return value with "" for com/edtech/service/AuditExportService::normalizeFormat → KILLED

Active mutators

Tests examined


Report generated by PIT 1.16.1