Skip to content

Commit

Permalink
Merge pull request #2160 from objectcomputing/chore-2155/remove-csv-f…
Browse files Browse the repository at this point in the history
…iles-when-done

Temp files now created that will be deleted after being served.
  • Loading branch information
mkimberlin authored Apr 3, 2024
2 parents 2b108e8 + 7c6dbb7 commit 5aab459
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,14 +40,14 @@ public File generateFile(MemberProfileReportQueryDTO queryDTO) {
List<MemberProfileRecord> filteredRecords = memberProfileReportRepository.findAllByMemberIds(memberIds, key);
memberRecords.addAll(filteredRecords);
}

return createCsv(memberRecords);

}

private File createCsv(List<MemberProfileRecord> 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(
Expand All @@ -69,8 +72,8 @@ private File createCsv(List<MemberProfileRecord> 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('"');
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class MemberProfileReportServicesImplTest {
@Mock
private MemberProfileReportRepository memberProfileReportRepository;

@Mock
private MemberProfileFileProvider memberProfileFileProvider;

@InjectMocks
private MemberProfileReportServicesImpl memberProfileReportServices;

Expand All @@ -48,6 +51,9 @@ void resetMocks() {
void testGenerateFileWithAllMemberProfiles() throws IOException {
List<MemberProfileRecord> 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);
Expand All @@ -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()));
Expand All @@ -85,6 +93,26 @@ void testGenerateFileWithSelectedMemberProfiles() throws IOException {
assertRecordEquals(expectedRecord, csvRecord1);
}

@Test
void testGenerateFileNotGenerated() throws IOException {
List<MemberProfileRecord> 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 RuntimeException());
// 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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,15 +18,19 @@
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 {

@Mock
private SkillRecordRepository skillRecordRepository;

@Mock
private SkillRecordFileProvider skillRecordFileProvider;

@InjectMocks
private SkillRecordServicesImpl skillRecordServices;

Expand All @@ -44,6 +45,7 @@ void resetMocks() {
}

@Test
@Order(1)
void testFileGeneration() throws IOException {
SkillRecord record1 = new SkillRecord();
record1.setName("Java");
Expand All @@ -53,7 +55,9 @@ void testFileGeneration() throws IOException {
record1.setCategoryName("Languages, Libraries, and Frameworks");

when(skillRecordRepository.findAll()).thenReturn(Collections.singletonList(record1));

File tmpFile = File.createTempFile("foobar",".csv");
tmpFile.deleteOnExit();
when(skillRecordFileProvider.provideFile()).thenReturn(tmpFile);
File file = skillRecordServices.generateFile();
assertNotNull(file);

Expand All @@ -77,4 +81,22 @@ void testFileGeneration() throws IOException {
assertEquals(record1.getCategoryName(), csvRecord.get("category_name"));
}

@Test
@Order(2)
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();
});
}

}

0 comments on commit 5aab459

Please sign in to comment.