From ed4c7a2a21b55ed1ac56b3aa87a4b19de0cc6722 Mon Sep 17 00:00:00 2001 From: estradaw Date: Fri, 29 Mar 2024 11:23:39 -0500 Subject: [PATCH 1/2] Temp files now created that will be deleted after being served. --- .../csvreport/MemberProfileFileProvider.java | 21 +++++++++++++ .../MemberProfileReportServicesImpl.java | 15 ++++++---- .../skill_record/SkillRecordFileProvider.java | 19 ++++++++++++ .../skill_record/SkillRecordServicesImpl.java | 6 ++-- .../MemberProfileReportServicesImplTest.java | 30 ++++++++++++++++++- .../SkillRecordServicesImplTest.java | 24 ++++++++++++++- 6 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 server/src/main/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileFileProvider.java create mode 100644 server/src/main/java/com/objectcomputing/checkins/services/skill_record/SkillRecordFileProvider.java diff --git a/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileFileProvider.java b/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileFileProvider.java new file mode 100644 index 0000000000..981011c77c --- /dev/null +++ b/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileFileProvider.java @@ -0,0 +1,21 @@ +package com.objectcomputing.checkins.services.memberprofile.csvreport; + +import jakarta.inject.Singleton; + +import java.io.File; +import java.io.IOException; + +@Singleton +public class MemberProfileFileProvider { + + public File provideFile() { + try { + File csvFile = File.createTempFile("member_profiles", ".csv"); + csvFile.deleteOnExit(); + return csvFile; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImpl.java b/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImpl.java index 59fd261ff4..c62ff6bcca 100644 --- a/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImpl.java +++ b/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImpl.java @@ -18,12 +18,15 @@ public class MemberProfileReportServicesImpl implements MemberProfileReportServices { private final MemberProfileReportRepository memberProfileReportRepository; + private final MemberProfileFileProvider memberProfileFileProvider; @Value("${aes.key}") private String key; - public MemberProfileReportServicesImpl(MemberProfileReportRepository memberProfileReportRepository) { + public MemberProfileReportServicesImpl(MemberProfileReportRepository memberProfileReportRepository, + MemberProfileFileProvider memberProfileFileProvider) { this.memberProfileReportRepository = memberProfileReportRepository; + this.memberProfileFileProvider = memberProfileFileProvider; } @Override @@ -37,14 +40,14 @@ public File generateFile(MemberProfileReportQueryDTO queryDTO) { List filteredRecords = memberProfileReportRepository.findAllByMemberIds(memberIds, key); memberRecords.addAll(filteredRecords); } - return createCsv(memberRecords); + } private File createCsv(List memberRecords) { - File csvFile = new File("member_profiles.csv"); - CSVFormat csvFormat = getCsvFormat(); + File csvFile = memberProfileFileProvider.provideFile(); + CSVFormat csvFormat = getCsvFormat(); try (final CSVPrinter printer = new CSVPrinter(new FileWriter(csvFile, StandardCharsets.UTF_8), csvFormat)) { for (MemberProfileRecord record : memberRecords) { printer.printRecord( @@ -69,8 +72,8 @@ private File createCsv(List memberRecords) { } public static CSVFormat getCsvFormat() { - String[] headers = { "First Name", "Last Name", "Title", "Location", "Work Email", "Start Date", "Tenure", - "PDL Name", "PDL Email", "Supervisor Name", "Supervisor Email" }; + String[] headers = {"First Name", "Last Name", "Title", "Location", "Work Email", "Start Date", "Tenure", + "PDL Name", "PDL Email", "Supervisor Name", "Supervisor Email"}; return CSVFormat.DEFAULT .withHeader(headers) .withQuote('"'); diff --git a/server/src/main/java/com/objectcomputing/checkins/services/skill_record/SkillRecordFileProvider.java b/server/src/main/java/com/objectcomputing/checkins/services/skill_record/SkillRecordFileProvider.java new file mode 100644 index 0000000000..dae2640b9d --- /dev/null +++ b/server/src/main/java/com/objectcomputing/checkins/services/skill_record/SkillRecordFileProvider.java @@ -0,0 +1,19 @@ +package com.objectcomputing.checkins.services.skill_record; + +import jakarta.inject.Singleton; + +import java.io.File; +import java.io.IOException; + +@Singleton +public class SkillRecordFileProvider { + public File provideFile() { + try { + File csvFile = File.createTempFile("skill_records", ".csv"); + csvFile.deleteOnExit(); + return csvFile; + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/server/src/main/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImpl.java b/server/src/main/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImpl.java index 2279bc9942..eb69836ec7 100644 --- a/server/src/main/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImpl.java +++ b/server/src/main/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImpl.java @@ -14,9 +14,11 @@ public class SkillRecordServicesImpl implements SkillRecordServices { private final SkillRecordRepository skillRecordRepository; + private final SkillRecordFileProvider skillRecordFileProvider; - public SkillRecordServicesImpl(SkillRecordRepository skillRecordRepository) { + public SkillRecordServicesImpl(SkillRecordRepository skillRecordRepository, SkillRecordFileProvider skillRecordFileProvider) { this.skillRecordRepository = skillRecordRepository; + this.skillRecordFileProvider = skillRecordFileProvider; } @Override @@ -26,7 +28,7 @@ public File generateFile() { String[] headers = { "name", "description", "extraneous", "pending", "category_name" }; CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(headers).withQuote('"'); - File csvFile = new File("skill_records.csv"); + File csvFile = skillRecordFileProvider.provideFile(); try (final CSVPrinter printer = new CSVPrinter(new FileWriter(csvFile, StandardCharsets.UTF_8), csvFormat)) { for (SkillRecord record : skillRecords) { printer.printRecord(record.getName(), record.getDescription(), record.isExtraneous(), record.isPending(), record.getCategoryName()); diff --git a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java index ef447cb5b3..ccdb89fb36 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java @@ -31,6 +31,9 @@ class MemberProfileReportServicesImplTest { @Mock private MemberProfileReportRepository memberProfileReportRepository; + @Mock + private MemberProfileFileProvider memberProfileFileProvider; + @InjectMocks private MemberProfileReportServicesImpl memberProfileReportServices; @@ -48,6 +51,9 @@ void resetMocks() { void testGenerateFileWithAllMemberProfiles() throws IOException { List expectedRecords = createSampleRecords(); when(memberProfileReportRepository.findAll()).thenReturn(expectedRecords); + File tmpFile = File.createTempFile("member",".csv"); + tmpFile.deleteOnExit(); + when(memberProfileFileProvider.provideFile()).thenReturn(tmpFile); // Generate a file with all members File file = memberProfileReportServices.generateFile(null); @@ -71,7 +77,9 @@ void testGenerateFileWithSelectedMemberProfiles() throws IOException { when(memberProfileReportRepository .findAllByMemberIds(eq(List.of(expectedRecord.getId().toString())), any())) .thenReturn(List.of(expectedRecord)); - + File tmpFile = File.createTempFile("member",".csv"); + tmpFile.deleteOnExit(); + when(memberProfileFileProvider.provideFile()).thenReturn(tmpFile); // Generate a file with selected members MemberProfileReportQueryDTO dto = new MemberProfileReportQueryDTO(); dto.setMemberIds(List.of(expectedRecord.getId())); @@ -85,6 +93,26 @@ void testGenerateFileWithSelectedMemberProfiles() throws IOException { assertRecordEquals(expectedRecord, csvRecord1); } + @Test + void testGenerateFileNotGenerated() throws IOException { + List allRecords = createSampleRecords(); + MemberProfileRecord expectedRecord = allRecords.get(1); + when(memberProfileReportRepository + .findAllByMemberIds(eq(List.of(expectedRecord.getId().toString())), any())) + .thenReturn(List.of(expectedRecord)); + + when(memberProfileFileProvider.provideFile()).thenThrow(new IOException()); + // Generate a file with selected members + MemberProfileReportQueryDTO dto = new MemberProfileReportQueryDTO(); + dto.setMemberIds(List.of(expectedRecord.getId())); + + assertThrows(RuntimeException.class, () -> { + memberProfileReportServices.generateFile(dto); + }); + } + + + private static void assertRecordEquals(MemberProfileRecord record, CSVRecord csvRecord) { assertEquals(record.getFirstName(), csvRecord.get("First Name")); assertEquals(record.getLastName(), csvRecord.get("Last Name")); diff --git a/server/src/test/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImplTest.java index b4748f6c44..c2f465297c 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImplTest.java @@ -30,6 +30,9 @@ class SkillRecordServicesImplTest { @Mock private SkillRecordRepository skillRecordRepository; + @Mock + private SkillRecordFileProvider skillRecordFileProvider; + @InjectMocks private SkillRecordServicesImpl skillRecordServices; @@ -53,7 +56,9 @@ void testFileGeneration() throws IOException { record1.setCategoryName("Languages, Libraries, and Frameworks"); when(skillRecordRepository.findAll()).thenReturn(Collections.singletonList(record1)); - + File tmpFile = File.createTempFile("skills",".csv"); + tmpFile.deleteOnExit(); + when(skillRecordFileProvider.provideFile()).thenReturn(tmpFile); File file = skillRecordServices.generateFile(); assertNotNull(file); @@ -77,4 +82,21 @@ void testFileGeneration() throws IOException { assertEquals(record1.getCategoryName(), csvRecord.get("category_name")); } + @Test + void testNoFileGenerated() throws IOException { + SkillRecord record1 = new SkillRecord(); + record1.setName("Java"); + record1.setDescription("Various technical skills"); + record1.setExtraneous(true); + record1.setPending(true); + record1.setCategoryName("Languages, Libraries, and Frameworks"); + + when(skillRecordRepository.findAll()).thenReturn(Collections.singletonList(record1)); + + when(skillRecordFileProvider.provideFile()).thenThrow(new RuntimeException()); + assertThrows(RuntimeException.class, () -> { + skillRecordServices.generateFile(); + }); + } + } \ No newline at end of file From 7c6dbb7701589429a5b1712bd22098383508ef67 Mon Sep 17 00:00:00 2001 From: estradaw Date: Fri, 29 Mar 2024 13:17:14 -0500 Subject: [PATCH 2/2] Fix broken tests --- .../MemberProfileReportServicesImplTest.java | 2 +- .../skill_record/SkillRecordServicesImplTest.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java index ccdb89fb36..ce333533f5 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java @@ -101,7 +101,7 @@ void testGenerateFileNotGenerated() throws IOException { .findAllByMemberIds(eq(List.of(expectedRecord.getId().toString())), any())) .thenReturn(List.of(expectedRecord)); - when(memberProfileFileProvider.provideFile()).thenThrow(new IOException()); + when(memberProfileFileProvider.provideFile()).thenThrow(new RuntimeException()); // Generate a file with selected members MemberProfileReportQueryDTO dto = new MemberProfileReportQueryDTO(); dto.setMemberIds(List.of(expectedRecord.getId())); diff --git a/server/src/test/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImplTest.java index c2f465297c..134b69dca0 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/skill_record/SkillRecordServicesImplTest.java @@ -4,10 +4,7 @@ import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.*; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; @@ -21,9 +18,10 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.when; @MicronautTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(TestInstance.Lifecycle.PER_CLASS) class SkillRecordServicesImplTest { @@ -47,6 +45,7 @@ void resetMocks() { } @Test + @Order(1) void testFileGeneration() throws IOException { SkillRecord record1 = new SkillRecord(); record1.setName("Java"); @@ -56,7 +55,7 @@ void testFileGeneration() throws IOException { record1.setCategoryName("Languages, Libraries, and Frameworks"); when(skillRecordRepository.findAll()).thenReturn(Collections.singletonList(record1)); - File tmpFile = File.createTempFile("skills",".csv"); + File tmpFile = File.createTempFile("foobar",".csv"); tmpFile.deleteOnExit(); when(skillRecordFileProvider.provideFile()).thenReturn(tmpFile); File file = skillRecordServices.generateFile(); @@ -83,6 +82,7 @@ void testFileGeneration() throws IOException { } @Test + @Order(2) void testNoFileGenerated() throws IOException { SkillRecord record1 = new SkillRecord(); record1.setName("Java");