Skip to content

Commit 241a238

Browse files
Merge pull request #1325 from ORCID/handleUnexpectedErrorForProcessingCsvReports
tweaked error handling to catch any exception processing csv reports
2 parents 58f7fe1 + f4ce6cd commit 241a238

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

assertion-service/src/main/java/org/orcid/memberportal/service/assertion/services/CsvReportService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public class CsvReportService {
5050

5151
@Autowired
5252
private StoredFileService storedFileService;
53-
53+
5454
@Autowired
5555
private MailService mailService;
56-
56+
5757
@Autowired
5858
private MessageSource messageSource;
59-
59+
6060
public void storeCsvReportRequest(String userId, String filename, String type) {
6161
Instant now = Instant.now();
6262
CsvReport csvReport = new CsvReport();
@@ -67,14 +67,14 @@ public void storeCsvReportRequest(String userId, String filename, String type) {
6767
csvReport.setOriginalFilename(filename);
6868
csvReportRepository.save(csvReport);
6969
}
70-
70+
7171
public void processCsvReports() {
7272
LOG.info("Processing pending CSV reports");
7373
List<CsvReport> reports = csvReportRepository.findAllUnprocessed();
7474
reports.forEach(r -> {
7575
try {
7676
processCsvReportRequest(r);
77-
} catch (IOException e) {
77+
} catch (Exception e) {
7878
LOG.warn("Failed to generate CSV report of type {} for user {}", r.getReportType(), r.getOwnerId(), e);
7979
StringWriter sw = new StringWriter();
8080
PrintWriter pw = new PrintWriter(sw);
@@ -91,7 +91,7 @@ private void processCsvReportRequest(CsvReport csvReport) throws IOException {
9191
AssertionServiceUser user = userService.getUserById(csvReport.getOwnerId());
9292
String salesforceId = user.getSalesforceId();
9393
Locale locale = LocaleUtils.getLocale(user.getLangKey());
94-
94+
9595
String subject = null;
9696
String content = null;
9797
String report = null;
@@ -120,7 +120,7 @@ private void processCsvReportRequest(CsvReport csvReport) throws IOException {
120120
File reportFile = new File(storedFile.getFileLocation());
121121
mailService.sendCsvReportMail(reportFile, user, subject, content);
122122
LOG.info("Report sent to {}", user.getEmail());
123-
123+
124124
storedFileService.markAsProcessed(storedFile);
125125
}
126126

assertion-service/src/test/java/org/orcid/memberportal/service/assertion/services/CsvReportServiceTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CsvReportServiceTest {
4848

4949
@Mock
5050
private MailService mailService;
51-
51+
5252
@Mock
5353
private MessageSource messageSource;
5454

@@ -91,7 +91,7 @@ void testProcessCsvReports() throws IOException {
9191
Mockito.verify(mailService).sendCsvReportMail(Mockito.any(File.class), Mockito.any(AssertionServiceUser.class), Mockito.eq("report subject"), Mockito.eq("report content"));
9292
Mockito.verify(storedFileService, Mockito.times(3)).markAsProcessed(Mockito.any(StoredFile.class));
9393
}
94-
94+
9595
@Test
9696
void testProcessCsvReportsWithError() throws IOException {
9797
Mockito.when(csvReportRepository.findAllUnprocessed()).thenReturn(Arrays.asList(getCsvReport(CsvReport.PERMISSION_LINKS_TYPE)));
@@ -104,13 +104,32 @@ void testProcessCsvReportsWithError() throws IOException {
104104
Mockito.verify(mailService, Mockito.never()).sendCsvReportMail(Mockito.any(File.class), Mockito.any(AssertionServiceUser.class), Mockito.eq("links subject"), Mockito.eq("links content"));
105105
Mockito.verify(storedFileService, Mockito.never()).markAsProcessed(Mockito.any(StoredFile.class));
106106
Mockito.verify(csvReportRepository).save(csvReportCaptor.capture());
107-
107+
108108
CsvReport updated = csvReportCaptor.getValue();
109109
assertThat(updated.getError()).isNotNull();
110110
assertThat(updated.getError()).contains("IOException");
111111
assertThat(updated.getError()).contains("some error");
112112
}
113113

114+
@Test
115+
void testProcessCsvReportsWithUnexpectedError() throws IOException {
116+
Mockito.when(csvReportRepository.findAllUnprocessed()).thenReturn(Arrays.asList(getCsvReport(CsvReport.PERMISSION_LINKS_TYPE)));
117+
Mockito.when(userService.getUserById(Mockito.eq("user"))).thenReturn(getDummyUser());
118+
Mockito.when(permissionLinksCsvWriter.writeCsv(Mockito.eq("salesforce"))).thenThrow(new IllegalArgumentException("some error"));
119+
120+
csvReportService.processCsvReports();
121+
122+
Mockito.verify(permissionLinksCsvWriter).writeCsv(Mockito.eq("salesforce"));
123+
Mockito.verify(mailService, Mockito.never()).sendCsvReportMail(Mockito.any(File.class), Mockito.any(AssertionServiceUser.class), Mockito.eq("links subject"), Mockito.eq("links content"));
124+
Mockito.verify(storedFileService, Mockito.never()).markAsProcessed(Mockito.any(StoredFile.class));
125+
Mockito.verify(csvReportRepository).save(csvReportCaptor.capture());
126+
127+
CsvReport updated = csvReportCaptor.getValue();
128+
assertThat(updated.getError()).isNotNull();
129+
assertThat(updated.getError()).contains("IllegalArgumentException");
130+
assertThat(updated.getError()).contains("some error");
131+
}
132+
114133
@Test
115134
void testStoreCsvReportRequest() {
116135
Mockito.when(csvReportRepository.save(Mockito.any(CsvReport.class))).thenReturn(new CsvReport());

0 commit comments

Comments
 (0)