Skip to content

Commit

Permalink
[BI-1910] Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timparsons committed Nov 3, 2023
1 parent f817c04 commit 0bad43c
Show file tree
Hide file tree
Showing 13 changed files with 1,176 additions and 79 deletions.
5 changes: 4 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ AWS_REGION=<aws region>
AWS_ACCESS_KEY_ID=<access key>
AWS_SECRET_KEY=<secret>
AWS_GENO_BUCKET=<s3 bucket for genotypic data uploads>
AWS_S3_ENDPOINT=<s3 endpoint, default https://s3.us-east-1.amazonaws.com>
AWS_S3_ENDPOINT=<s3 endpoint, default https://s3.us-east-1.amazonaws.com>

BRAPI_VENDOR_SUBMISSION_ENABLED=false #can a submission be sent to a vendor via BrAPI
BRAPI_VENDOR_CHECK_FREQUENCY=1d #how often to check for vendor updates for sample submissions
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class SampleSubmissionController {
private final Gson gson;

@Inject
public SampleSubmissionController(@Property(name = "brapi.vendors.submission-enabled") boolean brapiSubmissionEnabled, SampleSubmissionService sampleSubmissionService, ProgramService programService, SecurityService securityService, UserService userService) {
public SampleSubmissionController(@Property(name = "brapi.vendor-submission-enabled") boolean brapiSubmissionEnabled, SampleSubmissionService sampleSubmissionService, ProgramService programService, SecurityService securityService, UserService userService) {
this.brapiSubmissionEnabled = brapiSubmissionEnabled;
this.sampleSubmissionService = sampleSubmissionService;
this.programService = programService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public class SampleSubmissionImport implements BrAPIImport {
private String tissue;

@ImportFieldType(type = ImportFieldTypeEnum.TEXT)
@ImportFieldMetadata(id = "comment", name = Columns.COMMENTS, description = "Generic comments about this sample for the vendor")
private String comment;
@ImportFieldMetadata(id = "comments", name = Columns.COMMENTS, description = "Generic comments about this sample for the vendor")
private String comments;

@ImportFieldType(type= ImportFieldTypeEnum.TEXT, collectTime = ImportCollectTimeEnum.UPLOAD)
@ImportMappingRequired
Expand All @@ -99,7 +99,7 @@ public static final class Columns {
public static final String ORGANISM = "Organism";
public static final String SPECIES = "Species";
public static final String GERMPLASM_NAME = "Germplasm Name";
public static final String GERMPLASM_GID = "Germplasm GID";
public static final String GERMPLASM_GID = "GID";
public static final String TISSUE = "Tissue";
public static final String COMMENTS = "Comments";
public static final String OBS_UNIT_ID = "ObsUnitID";
Expand Down Expand Up @@ -146,7 +146,7 @@ public BrAPISample constructBrAPISample(boolean commit, Program program, User us
.row(row)
.column(Integer.valueOf(column))
.tissueType(tissue)
.sampleDescription(comment);
.sampleDescription(comments);

if (ou != null) {
brAPISample
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ private void processFile(List<BrAPIImport> finalBrAPIImportList, Table data, Pro
progress.setUpdatedBy(actingUser.getId());
importDAO.update(upload);
}catch (ValidatorException e) {
log.info("Validation errors", e);
log.info("Validation errors: \n" + e);
ImportProgress progress = upload.getProgress();
progress.setStatuscode((short) HttpStatus.UNPROCESSABLE_ENTITY.getCode());
progress.setMessage("Multiple Errors");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Prototype;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.server.exceptions.InternalServerException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.brapi.client.v2.model.exceptions.ApiException;
Expand Down Expand Up @@ -73,10 +74,6 @@ public class SampleSubmissionProcessor implements Processor {
private final BrAPIGermplasmDAO germplasmDAO;
private final BrAPIObservationUnitDAO observationUnitDAO;
private final SampleSubmissionService sampleSubmissionService;
private final DSLContext dsl;
private final BrAPIPlateDAO plateDAO;
private final BrAPISampleDAO sampleDAO;

private SampleSubmission submission;
private Map<String, BrAPIGermplasm> germplasmByGID = new HashMap<>();
private Map<String, BrAPIGermplasm> germplasmByDbId = new HashMap<>();
Expand All @@ -88,17 +85,11 @@ public class SampleSubmissionProcessor implements Processor {
public SampleSubmissionProcessor(@Property(name = "brapi.server.reference-source") String referenceSource,
BrAPIGermplasmDAO germplasmDAO,
BrAPIObservationUnitDAO observationUnitDAO,
SampleSubmissionService sampleSubmissionService,
DSLContext dsl,
BrAPIPlateDAO plateDAO,
BrAPISampleDAO sampleDAO) {
SampleSubmissionService sampleSubmissionService) {
this.referenceSource = referenceSource;
this.germplasmDAO = germplasmDAO;
this.observationUnitDAO = observationUnitDAO;
this.sampleSubmissionService = sampleSubmissionService;
this.dsl = dsl;
this.plateDAO = plateDAO;
this.sampleDAO = sampleDAO;
}

@Override
Expand Down Expand Up @@ -260,15 +251,21 @@ public void validateDependencies(Map<Integer, PendingImport> mappedBrAPIImport)

@Override
public void postBrapiData(Map<Integer, PendingImport> mappedBrAPIImport, Program program, ImportUpload upload) throws ValidatorException {
dsl.transaction(() -> {
List<BrAPIPlate> platesToSave = plateById.values().stream().map(PendingImportObject::getBrAPIObject).collect(Collectors.toList());
List<BrAPISample> samplesToSave = mappedBrAPIImport.values().stream().map(row -> row.getSample().getBrAPIObject()).collect(Collectors.toList());
List<BrAPIPlate> platesToSave = plateById.values().stream().map(PendingImportObject::getBrAPIObject).collect(Collectors.toList());
List<BrAPISample> samplesToSave = mappedBrAPIImport.values().stream().map(row -> row.getSample().getBrAPIObject()).collect(Collectors.toList());

submission.setPlates(platesToSave);
submission.setSamples(samplesToSave);
submission.setPlates(platesToSave);
submission.setSamples(samplesToSave);

try {
sampleSubmissionService.createSubmission(submission, program, upload);
});
} catch (ApiException e) {
log.error("Error saving sample submission import: " + Utilities.generateApiExceptionLogMessage(e), e);
throw new InternalServerException("Error saving sample submission import", e);
} catch (Exception e) {
log.error("Error saving sample submission import", e);
throw new InternalServerException(e.getMessage(), e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ public class SampleSubmissionService {
private static final String VENDOR_NOT_SUBMITTED_STATUS = "NOT SUBMITTED";
private static final String VENDOR_SUBMITTED_STATUS = "SUBMITTED";
private final String referenceSource;
private String dartBrapiUrl;
private String dartClientId;
private String dartToken;
private Duration requestTimeout;
private final String dartBrapiUrl;
private final String dartClientId;
private final String dartToken;
private final Duration requestTimeout;
private final boolean brapiSubmissionEnabled;

private final SampleSubmissionDAO submissionDAO;
private final BrAPIPlateDAO plateDAO;
Expand All @@ -85,6 +86,7 @@ public SampleSubmissionService(@Property(name = "brapi.server.reference-source")
@Property(name = "brapi.vendors.dart.client-id") String dartClientId,
@Property(name = "brapi.vendors.dart.token") String dartToken,
@Value(value = "${brapi.read-timeout:5m}") Duration requestTimeout,
@Property(name = "brapi.vendor-submission-enabled") boolean brapiSubmissionEnabled,
SampleSubmissionDAO submissionDAO,
BrAPIPlateDAO plateDAO,
BrAPISampleDAO sampleDAO,
Expand All @@ -96,6 +98,7 @@ public SampleSubmissionService(@Property(name = "brapi.server.reference-source")
this.dartClientId = dartClientId;
this.dartToken = dartToken;
this.requestTimeout = requestTimeout;
this.brapiSubmissionEnabled = brapiSubmissionEnabled;
this.submissionDAO = submissionDAO;
this.plateDAO = plateDAO;
this.sampleDAO = sampleDAO;
Expand All @@ -106,6 +109,9 @@ public SampleSubmissionService(@Property(name = "brapi.server.reference-source")

@Scheduled(fixedDelay = "${brapi.vendor-check-frequency}", initialDelay = "10s")
void checkSubmissionStatuses() {
if(!brapiSubmissionEnabled) {
return;
}
log.trace("checking vendor order statuses");
List<SampleSubmission> submittedAndNotCompleted = submissionDAO.getSubmittedAndNotComplete();
log.trace(submittedAndNotCompleted.size() + " orders to check");
Expand All @@ -127,15 +133,17 @@ public SampleSubmission createSubmission(SampleSubmission submission, Program pr
submission.setUpdatedByUser(upload.getUpdatedByUser());
submission.setUpdatedBy(upload.getUpdatedBy());

submissionDAO.insert(submission);
dsl.transaction(() -> {
submissionDAO.insert(submission);

List<BrAPIPlate> savedPlates = plateDAO.createPlates(program, submission.getPlates(), upload);
submission.setPlates(savedPlates);
Map<String, String> plateNameToDbId = savedPlates.stream().collect(Collectors.toMap(BrAPIPlate::getPlateName, BrAPIPlate::getPlateDbId));
List<BrAPIPlate> savedPlates = plateDAO.createPlates(program, submission.getPlates(), upload);
submission.setPlates(savedPlates);
Map<String, String> plateNameToDbId = savedPlates.stream().collect(Collectors.toMap(BrAPIPlate::getPlateName, BrAPIPlate::getPlateDbId));

List<BrAPISample> samplesToSave = submission.getSamples().stream().map(sample -> sample.plateDbId(plateNameToDbId.get(sample.getPlateName()))).collect(Collectors.toList());
List<BrAPISample> savedSamples = sampleDAO.createSamples(program, samplesToSave, upload);
submission.setSamples(savedSamples);
List<BrAPISample> samplesToSave = submission.getSamples().stream().map(sample -> sample.plateDbId(plateNameToDbId.get(sample.getPlateName()))).collect(Collectors.toList());
List<BrAPISample> savedSamples = sampleDAO.createSamples(program, samplesToSave, upload);
submission.setSamples(savedSamples);
});

return submission;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ brapi:
pheno-url: ${brapi.server.default-url}
geno-url: ${brapi.server.default-url}
reference-source: ${BRAPI_REFERENCE_SOURCE:breedinginsight.org}
vendor-submission-enabled: ${BRAPI_VENDOR_SUBMISSION_ENABLED:false}
vendor-check-frequency: ${BRAPI_VENDOR_CHECK_FREQUENCY:1d}
vendors:
submission-enabled: ${BRAPI_VENDOR_SUBMISSION_ENABLED:false}
dart:
url: ${DART_VENDOR_URL:`https://test-server.brapi.org`}
client-id: ${DART_CLIENT_ID:potato-salad}
Expand All @@ -171,7 +172,6 @@ brapi:
search:
wait-time: 1000
post-group-size: ${POST_CHUNK_SIZE:1000}
vendor-check-frequency: ${VENDOR_CHECK_FREQUENCY:30s}

email:
relay-server:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ $$
{
"id": "de7fed3a-ec44-4139-83cb-c773e09237bd",
"value": {
"fileFieldName": "Comment"
"fileFieldName": "Comments"
},
"objectId": "comment"
"objectId": "comments"
}
]', '[]',
false,
Expand Down
Loading

0 comments on commit 0bad43c

Please sign in to comment.