Skip to content

Commit b7e36d3

Browse files
authored
[DUOS-3056][risk=no] Log ES errors instead of failing (#2287)
1 parent aa04c66 commit b7e36d3

File tree

2 files changed

+96
-44
lines changed

2 files changed

+96
-44
lines changed

src/main/java/org/broadinstitute/consent/http/service/DatasetRegistrationService.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.cloud.storage.BlobId;
44
import jakarta.ws.rs.BadRequestException;
55
import jakarta.ws.rs.NotFoundException;
6+
import jakarta.ws.rs.core.Response;
67
import java.io.IOException;
78
import java.io.InputStream;
89
import java.sql.SQLException;
@@ -194,7 +195,13 @@ public List<Dataset> createDatasetsFromRegistration(
194195

195196
List<Dataset> datasets = datasetDAO.findDatasetsByIdList(createdDatasetIds);
196197
sendDatasetSubmittedEmails(datasets);
197-
elasticSearchService.indexDatasets(datasets);
198+
try (Response response = elasticSearchService.indexDatasets(datasets)) {
199+
if (response.getStatus() >= 400) {
200+
logWarn(String.format("Error indexing datasets from registration: %s", registration.getStudyName()));
201+
}
202+
} catch (Exception e) {
203+
logException(e);
204+
}
198205
return datasets;
199206
}
200207

@@ -266,7 +273,13 @@ public Dataset updateDataset(
266273
}
267274

268275
Dataset updatedDataset = datasetDAO.findDatasetById(datasetId);
269-
elasticSearchService.indexDataset(updatedDataset);
276+
try (Response response = elasticSearchService.indexDataset(dataset)) {
277+
if (response.getStatus() >= 400) {
278+
logWarn(String.format("Error indexing dataset update: %s", dataset.getName()));
279+
}
280+
} catch (Exception e) {
281+
logException(e);
282+
}
270283
return updatedDataset;
271284
}
272285

src/test/java/org/broadinstitute/consent/http/service/DatasetRegistrationServiceTest.java

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.broadinstitute.consent.http.service;
22

3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45
import static org.junit.jupiter.api.Assertions.assertFalse;
56
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -15,6 +16,7 @@
1516

1617
import com.google.cloud.storage.BlobId;
1718
import jakarta.ws.rs.NotFoundException;
19+
import jakarta.ws.rs.ServerErrorException;
1820
import jakarta.ws.rs.core.MediaType;
1921
import java.io.ByteArrayInputStream;
2022
import java.io.InputStream;
@@ -30,6 +32,7 @@
3032
import java.util.stream.Collectors;
3133
import java.util.stream.Stream;
3234
import org.apache.commons.lang3.RandomStringUtils;
35+
import org.apache.commons.lang3.RandomUtils;
3336
import org.broadinstitute.consent.http.cloudstore.GCSService;
3437
import org.broadinstitute.consent.http.db.DacDAO;
3538
import org.broadinstitute.consent.http.db.DatasetDAO;
@@ -57,14 +60,15 @@
5760
import org.broadinstitute.consent.http.util.gson.GsonUtil;
5861
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
5962
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
60-
import org.junit.jupiter.api.BeforeEach;
6163
import org.junit.jupiter.api.Test;
64+
import org.junit.jupiter.api.extension.ExtendWith;
6265
import org.mockito.ArgumentCaptor;
6366
import org.mockito.Captor;
6467
import org.mockito.Mock;
65-
import org.mockito.MockitoAnnotations;
68+
import org.mockito.junit.jupiter.MockitoExtension;
6669

67-
public class DatasetRegistrationServiceTest {
70+
@ExtendWith(MockitoExtension.class)
71+
class DatasetRegistrationServiceTest {
6872

6973
private DatasetRegistrationService datasetRegistrationService;
7074

@@ -89,11 +93,6 @@ public class DatasetRegistrationServiceTest {
8993
@Mock
9094
private EmailService emailService;
9195

92-
@BeforeEach
93-
public void setUp() {
94-
MockitoAnnotations.openMocks(this);
95-
}
96-
9796
private void initService() {
9897
datasetRegistrationService = new DatasetRegistrationService(datasetDAO, dacDAO,
9998
datasetServiceDAO, gcsService, elasticSearchService, studyDAO, emailService);
@@ -108,15 +107,26 @@ private void initService() {
108107

109108
// ------------------------ test multiple dataset insert ----------------------------------- //
110109
@Test
111-
public void testInsertCompleteDatasetRegistration() throws Exception {
110+
void testInsertCompleteDatasetRegistration() throws Exception {
112111
User user = mock();
113112
DatasetRegistrationSchemaV1 schema = createRandomCompleteDatasetRegistration(user);
114113

114+
FormDataContentDisposition content = FormDataContentDisposition
115+
.name("file")
116+
.fileName("sharing_plan.txt")
117+
.build();
118+
119+
InputStream is = new ByteArrayInputStream("HelloWorld".getBytes(StandardCharsets.UTF_8));
120+
FormDataBodyPart bodyPart = mock();
121+
when(bodyPart.getMediaType()).thenReturn(MediaType.TEXT_PLAIN_TYPE);
122+
when(bodyPart.getContentDisposition()).thenReturn(content);
123+
when(bodyPart.getValueAs(any())).thenReturn(is);
124+
115125
initService();
116126

117127
Map<String, FormDataBodyPart> files = Map.of("alternativeDataSharingPlan",
118-
createFormDataBodyPart(), "consentGroups[0].nihInstitutionalCertificationFile",
119-
createFormDataBodyPart(), "otherUnused", createFormDataBodyPart());
128+
bodyPart, "consentGroups[0].nihInstitutionalCertificationFile",
129+
bodyPart, "otherUnused", bodyPart);
120130
when(gcsService.storeDocument(any(), any(), any())).thenReturn(BlobId.of("asdf", "hjkl"),
121131
BlobId.of("qwer", "tyuio"));
122132
when(dacDAO.findById(any())).thenReturn(new Dac());
@@ -237,7 +247,7 @@ public void testInsertCompleteDatasetRegistration() throws Exception {
237247

238248
// inserts only required fields to ensure that null fields are ok
239249
@Test
240-
public void testInsertMinimumDatasetRegistration() throws Exception {
250+
void testInsertMinimumDatasetRegistration() throws Exception {
241251
User user = mock();
242252
DatasetRegistrationSchemaV1 schema = createRandomMinimumDatasetRegistration(user);
243253

@@ -288,7 +298,7 @@ public void testInsertMinimumDatasetRegistration() throws Exception {
288298
}
289299

290300
@Test
291-
public void testDatasetCreateRegistrationEmails() throws Exception {
301+
void testDatasetCreateRegistrationEmails() throws Exception {
292302
User user = mock();
293303
DatasetRegistrationSchemaV1 schema = createRandomCompleteDatasetRegistration(user);
294304

@@ -301,7 +311,7 @@ public void testDatasetCreateRegistrationEmails() throws Exception {
301311
}
302312

303313
@Test
304-
public void testStudyUpdateNewDatasetEmails() throws Exception {
314+
void testStudyUpdateNewDatasetEmails() throws Exception {
305315
User user = mock();
306316
DatasetRegistrationSchemaV1 schema = createRandomCompleteDatasetRegistration(user);
307317
Study study = mock();
@@ -318,7 +328,7 @@ public void testStudyUpdateNewDatasetEmails() throws Exception {
318328
}
319329

320330
@Test
321-
public void testSendDatasetSubmittedEmailsExistingChairs() throws Exception {
331+
void testSendDatasetSubmittedEmailsExistingChairs() throws Exception {
322332
User user = new User();
323333
user.setRoles(List.of(new UserRole(UserRoles.CHAIRPERSON.getRoleId(), UserRoles.CHAIRPERSON.getRoleName())));
324334
Dac dac = mock();
@@ -334,7 +344,7 @@ public void testSendDatasetSubmittedEmailsExistingChairs() throws Exception {
334344
}
335345

336346
@Test
337-
public void testSendDatasetSubmittedEmailsNoChairs() throws Exception {
347+
void testSendDatasetSubmittedEmailsNoChairs() throws Exception {
338348
Dac dac = mock();
339349
Dataset dataset = new Dataset();
340350
dataset.setDacId(1);
@@ -348,7 +358,7 @@ public void testSendDatasetSubmittedEmailsNoChairs() throws Exception {
348358
}
349359

350360
@Test
351-
public void testCreatedDatasetsFromUpdatedStudy() {
361+
void testCreatedDatasetsFromUpdatedStudy() {
352362
Study study = mock();
353363
Set<Dataset> allDatasets = Stream.of(1, 2, 3, 4, 5).map((i) -> {
354364
Dataset dataset = new Dataset();
@@ -373,7 +383,7 @@ public void testCreatedDatasetsFromUpdatedStudy() {
373383
}
374384

375385
@Test
376-
public void testCreatedDatasetsFromUpdatedStudyNoDatasets() {
386+
void testCreatedDatasetsFromUpdatedStudyNoDatasets() {
377387
Study study = mock();
378388
List<DatasetUpdate> updatedDatasets = null;
379389
initService();
@@ -384,7 +394,7 @@ public void testCreatedDatasetsFromUpdatedStudyNoDatasets() {
384394
}
385395

386396
@Test
387-
public void testInsertAccessManagement() throws Exception {
397+
void testInsertAccessManagement() throws Exception {
388398
User user = mock();
389399
DatasetRegistrationSchemaV1 schema = createAccessManagementRegistrationNoDacId(user);
390400

@@ -406,16 +416,27 @@ public void testInsertAccessManagement() throws Exception {
406416

407417
// test inset multiple consent groups
408418
@Test
409-
public void testInsertMultipleDatasetRegistration() throws Exception {
419+
void testInsertMultipleDatasetRegistration() throws Exception {
410420
User user = mock();
411421
DatasetRegistrationSchemaV1 schema = createRandomMultipleDatasetRegistration(user);
412422

423+
FormDataContentDisposition content = FormDataContentDisposition
424+
.name("file")
425+
.fileName("sharing_plan.txt")
426+
.build();
427+
428+
InputStream is = new ByteArrayInputStream("HelloWorld".getBytes(StandardCharsets.UTF_8));
429+
FormDataBodyPart bodyPart = mock();
430+
when(bodyPart.getMediaType()).thenReturn(MediaType.TEXT_PLAIN_TYPE);
431+
when(bodyPart.getContentDisposition()).thenReturn(content);
432+
when(bodyPart.getValueAs(any())).thenReturn(is);
433+
413434
initService();
414435

415436
when(dacDAO.findById(any())).thenReturn(new Dac());
416437
Map<String, FormDataBodyPart> files = Map.of("alternativeDataSharingPlan",
417-
createFormDataBodyPart(), "consentGroups[0].nihInstitutionalCertificationFile",
418-
createFormDataBodyPart(), "otherUnused", createFormDataBodyPart());
438+
bodyPart, "consentGroups[0].nihInstitutionalCertificationFile",
439+
bodyPart, "otherUnused", bodyPart);
419440
when(gcsService.storeDocument(any(), any(), any())).thenReturn(BlobId.of("asdf", "hjkl"),
420441
BlobId.of("qwer", "tyuio"));
421442

@@ -494,7 +515,7 @@ public void testInsertMultipleDatasetRegistration() throws Exception {
494515
}
495516

496517
@Test
497-
public void testRegistrationErrorsOnInvalidDacId() throws Exception {
518+
void testRegistrationErrorsOnInvalidDacId() throws Exception {
498519

499520
User user = mock();
500521
DatasetRegistrationSchemaV1 schema = createRandomMinimumDatasetRegistration(user);
@@ -508,7 +529,40 @@ public void testRegistrationErrorsOnInvalidDacId() throws Exception {
508529
}
509530

510531
@Test
511-
public void testExtractStudyProperty() {
532+
void testRegistrationSucceedsWithESError() throws Exception {
533+
User user = mock();
534+
DatasetRegistrationSchemaV1 schema = createRandomMinimumDatasetRegistration(user);
535+
when(dacDAO.findById(any())).thenReturn(new Dac());
536+
when(elasticSearchService.indexDatasets(any())).thenThrow(new ServerErrorException("Timeout connecting to [elasticsearch]", 500));
537+
initService();
538+
assertDoesNotThrow(() -> {
539+
datasetRegistrationService.createDatasetsFromRegistration(schema, user, Map.of());
540+
}, "Registration Error");
541+
}
542+
543+
@Test
544+
void testUpdateDatasetSucceedsWithESError() {
545+
User user = mock();
546+
Dac dac = new Dac();
547+
dac.setDacId(RandomUtils.nextInt(1, 100));
548+
Dataset dataset = new Dataset();
549+
dataset.setDataSetId(RandomUtils.nextInt(1, 100));
550+
dataset.setDacId(dac.getDacId());
551+
String name = RandomStringUtils.randomAlphabetic(10);
552+
org.broadinstitute.consent.http.models.DatasetUpdate update = new org.broadinstitute.consent.http.models.DatasetUpdate(
553+
name,
554+
dac.getDacId(),
555+
List.of());
556+
when(datasetDAO.findDatasetById(any())).thenReturn(dataset);
557+
558+
initService();
559+
assertDoesNotThrow(() -> {
560+
datasetRegistrationService.updateDataset(dataset.getDataSetId(), user, update, Map.of());
561+
}, "Update Error");
562+
}
563+
564+
@Test
565+
void testExtractStudyProperty() {
512566
DatasetRegistrationService.StudyPropertyExtractor extractor = new DatasetRegistrationService.StudyPropertyExtractor(
513567
RandomStringUtils.randomAlphabetic(10),
514568
PropertyType.String,
@@ -533,7 +587,7 @@ public void testExtractStudyProperty() {
533587
}
534588

535589
@Test
536-
public void testExtractDatasetProperty() {
590+
void testExtractDatasetProperty() {
537591
DatasetRegistrationService.DatasetPropertyExtractor extractor = new DatasetRegistrationService.DatasetPropertyExtractor(
538592
RandomStringUtils.randomAlphabetic(10),
539593
RandomStringUtils.randomAlphabetic(10),
@@ -561,7 +615,7 @@ public void testExtractDatasetProperty() {
561615

562616

563617
@Test
564-
public void testExtractStudyPropertyTyped() {
618+
void testExtractStudyPropertyTyped() {
565619
DatasetRegistrationService.StudyPropertyExtractor extractor = new DatasetRegistrationService.StudyPropertyExtractor(
566620
RandomStringUtils.randomAlphabetic(10),
567621
PropertyType.Json,
@@ -583,7 +637,7 @@ public void testExtractStudyPropertyTyped() {
583637
}
584638

585639
@Test
586-
public void testExtractDatasetPropertyTyped() {
640+
void testExtractDatasetPropertyTyped() {
587641
DatasetRegistrationService.DatasetPropertyExtractor extractor = new DatasetRegistrationService.DatasetPropertyExtractor(
588642
RandomStringUtils.randomAlphabetic(10),
589643
RandomStringUtils.randomAlphabetic(10),
@@ -643,21 +697,6 @@ private void assertContainsStudyProperty(Collection<StudyProperty> props, String
643697
assertEquals(value, prop.get().getValue());
644698
}
645699

646-
647-
private FormDataBodyPart createFormDataBodyPart() {
648-
FormDataContentDisposition content = FormDataContentDisposition
649-
.name("file")
650-
.fileName("sharing_plan.txt")
651-
.build();
652-
653-
InputStream is = new ByteArrayInputStream("HelloWorld".getBytes(StandardCharsets.UTF_8));
654-
FormDataBodyPart bodyPart = mock();
655-
when(bodyPart.getMediaType()).thenReturn(MediaType.TEXT_PLAIN_TYPE);
656-
when(bodyPart.getContentDisposition()).thenReturn(content);
657-
when(bodyPart.getValueAs(any())).thenReturn(is);
658-
return bodyPart;
659-
}
660-
661700
private DatasetRegistrationSchemaV1 createRandomMinimumDatasetRegistration(User user) {
662701
DatasetRegistrationSchemaV1 schemaV1 = new DatasetRegistrationSchemaV1();
663702
schemaV1.setStudyName(RandomStringUtils.randomAlphabetic(10));

0 commit comments

Comments
 (0)