Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: retrieve validation codes #124

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
package io.github.genomicdatainfrastructure.daam.api;

import io.github.genomicdatainfrastructure.daam.exceptions.AcceptTermsException;
import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;

import io.github.genomicdatainfrastructure.daam.model.ErrorResponse;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;

@Provider
public class AcceptTermsExceptionMapper implements ExceptionMapper<AcceptTermsException> {

Expand All @@ -22,7 +22,7 @@ public Response toResponse(AcceptTermsException exception) {
"Could not accept terms",
BAD_REQUEST.getStatusCode(),
exception.getMessage(),
exception.getErrorMessages()
exception.getWarnings()
);

return Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
package io.github.genomicdatainfrastructure.daam.api;

import io.github.genomicdatainfrastructure.daam.exceptions.ApplicationSubmissionException;
import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;

import io.github.genomicdatainfrastructure.daam.model.ErrorResponse;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import lombok.extern.java.Log;

import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;

@Log
@Provider
public class ApplicationSubmissionExceptionMapper implements
Expand All @@ -24,10 +24,9 @@ public Response toResponse(ApplicationSubmissionException exception) {
"Application could not be submitted",
BAD_REQUEST.getStatusCode(),
exception.getMessage(),
exception.getErrorMessages()
exception.getWarnings()
);
log.warning("Application could not be submitted: " + String.join(", ", exception
.getErrorMessages()));

return Response
.status(BAD_REQUEST)
.entity(errorResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

package io.github.genomicdatainfrastructure.daam.exceptions;

import io.github.genomicdatainfrastructure.daam.model.ValidationWarning;
import lombok.Getter;

import java.util.List;

import static java.util.Optional.ofNullable;

@Getter
public class AcceptTermsException extends RuntimeException {

private static final String MESSAGE = "Terms and Licenses of application %s could not be accepted, due to the following errors:";
private final List<String> errorMessages;
private static final String MESSAGE = "Terms and Licenses of application %s could not be accepted.";
private final transient List<ValidationWarning> warnings;

public AcceptTermsException(Long applicationId, List<String> errorMessages) {
public AcceptTermsException(Long applicationId, List<ValidationWarning> warnings) {
super(String.format(MESSAGE, applicationId));
this.errorMessages = errorMessages;
}

public List<String> getErrorMessages() {
return errorMessages;
this.warnings = ofNullable(warnings).orElseGet(List::of);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.genomicdatainfrastructure.daam.exceptions;

import io.github.genomicdatainfrastructure.daam.model.ValidationWarning;
import lombok.Getter;

import java.util.List;

import static java.util.Optional.ofNullable;

@Getter
public class ApplicationSubmissionException extends RuntimeException {

private static final String MESSAGE = "Application %s could not be submitted due to the following errors:";
private final List<String> errorMessages;
private static final String MESSAGE = "Application %s could not be submitted.";
private final transient List<ValidationWarning> warnings;

public ApplicationSubmissionException(Long applicationId, List<String> errorMessages) {
public ApplicationSubmissionException(Long applicationId, List<ValidationWarning> warnings) {
super(String.format(MESSAGE, applicationId));
this.errorMessages = errorMessages;
}

public List<String> getErrorMessages() {
return errorMessages;
this.warnings = ofNullable(warnings).orElseGet(List::of);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import io.github.genomicdatainfrastructure.daam.exceptions.AcceptTermsException;
import io.github.genomicdatainfrastructure.daam.gateways.RemsApiQueryGateway;
import io.github.genomicdatainfrastructure.daam.model.AcceptTermsCommand;
import io.github.genomicdatainfrastructure.daam.model.ValidationWarning;
import io.github.genomicdatainfrastructure.daam.remote.rems.api.RemsApplicationCommandApi;
import io.github.genomicdatainfrastructure.daam.remote.rems.model.AcceptLicensesCommand;
import io.github.genomicdatainfrastructure.daam.remote.rems.model.SuccessResponse;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.java.Log;
Expand All @@ -25,8 +25,7 @@
@ApplicationScoped
public class AcceptTermsService {

private static final String ERROR_MESSAGE = "Error: %s";
private static final String ACCEPT_TERMS_LOG = "Terms and Licenses of application %s could not be accepted, due to the following errors: %s";
private static final String ACCEPT_TERMS_LOG = "Terms and Licenses of application %s could not be accepted: %s";

private final String remsApiKey;
private final RemsApplicationCommandApi remsApplicationCommandApi;
Expand All @@ -46,11 +45,12 @@ public AcceptTermsService(
public void acceptTerms(Long id, String userId, AcceptTermsCommand acceptTermsCommand) {
remsApiQueryGateway.checkIfApplicationIsEditableByUser(id, userId);

AcceptLicensesCommand remoteAcceptLicensesCommand = new AcceptLicensesCommand();
remoteAcceptLicensesCommand.setApplicationId(id);
remoteAcceptLicensesCommand.setAcceptedLicenses(acceptTermsCommand.getAcceptedLicenses());
var remoteAcceptLicensesCommand = AcceptLicensesCommand.builder()
.applicationId(id)
.acceptedLicenses(acceptTermsCommand.getAcceptedLicenses())
.build();

SuccessResponse response = remsApplicationCommandApi.apiApplicationsAcceptLicensesPost(
var response = remsApplicationCommandApi.apiApplicationsAcceptLicensesPost(
remsApiKey, userId, remoteAcceptLicensesCommand);

if (Boolean.FALSE.equals(response.getSuccess())) {
Expand All @@ -62,11 +62,15 @@ public void acceptTerms(Long id, String userId, AcceptTermsCommand acceptTermsCo

log.warning(ACCEPT_TERMS_LOG.formatted(id, concatenatedErrors));

var errorMessages = nonNullErrors.stream()
.map(it -> ERROR_MESSAGE.formatted(it))
var warnings = nonNullErrors.stream()
.map(it -> ValidationWarning.builder()
.key(it.getType())
.formId(it.getFormId())
.fieldId(it.getFieldId())
.build())
.toList();

throw new AcceptTermsException(id, errorMessages);
throw new AcceptTermsException(id, warnings);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@

import io.github.genomicdatainfrastructure.daam.exceptions.ApplicationSubmissionException;
import io.github.genomicdatainfrastructure.daam.gateways.RemsApiQueryGateway;
import io.github.genomicdatainfrastructure.daam.model.ValidationWarning;
import io.github.genomicdatainfrastructure.daam.remote.rems.api.RemsApplicationCommandApi;
import io.github.genomicdatainfrastructure.daam.remote.rems.model.SubmitApplicationCommand;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.java.Log;

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.util.List;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;

@Log
@ApplicationScoped
public class SubmitApplicationService {

private static final String ERROR_MESSAGE = "Field %s %s";
private static final String SUBMISSION_LOG = "%s failed to submit application %s due to the following errors: %s";
private static final String SUBMISSION_LOG = "%s failed to submit application %s: %s";

private final String remsApiKey;
private final RemsApplicationCommandApi remsApplicationCommandApi;
Expand All @@ -41,14 +40,6 @@ public SubmitApplicationService(
this.gateway = remsApiQueryGateway;
}

private String formatError(String error) {
return switch (error) {
case "t.form.validation/required" -> "is required.";
case "t.form.validation/format_error" -> "has invalid format.";
default -> error;
};
}

public void submitApplication(Long id, String userId) {
gateway.checkIfApplicationIsEditableByUser(id, userId);

Expand All @@ -69,13 +60,15 @@ public void submitApplication(Long id, String userId) {

log.warning(SUBMISSION_LOG.formatted(userId, id, concatenatedErrors));

var errorMessages = nonNullErrors.stream()
.filter(it -> it.getFieldId() != null)
.filter(it -> it.getType() != null)
.map(it -> ERROR_MESSAGE.formatted(it.getFieldId(), formatError(it.getType())))
var warnings = nonNullErrors.stream()
.map(it -> ValidationWarning.builder()
.key(it.getType())
.formId(it.getFormId())
.fieldId(it.getFieldId())
.build())
.toList();

throw new ApplicationSubmissionException(id, errorMessages);
throw new ApplicationSubmissionException(id, warnings);
}
}
}
29 changes: 7 additions & 22 deletions src/main/openapi/daam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,6 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/CreateApplicationResponse"
"422":
description: Validation warnings
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/ValidationWarnings"
"404":
description: Catalogue Item not found
content:
Expand Down Expand Up @@ -976,27 +968,20 @@ components:
title:
type: string
title: dataset title
ValidationWarnings:
properties:
warnings:
type: array
title: validation warnings
items:
$ref: "#/components/schemas/ValidationWarning"
ValidationWarning:
properties:
key:
type: string
title: validation key
formId:
type: string
type: integer
format: int64
title: form id
fieldId:
title: field id
type: string
fieldValidationKey:
type: string
title: field validation key
required:
- key
AddedAttachment:
properties:
id:
Expand Down Expand Up @@ -1056,11 +1041,11 @@ components:
detail:
type: string
title: Error detail
errorMessages:
validationWarnings:
type: array
items:
type: string
description: Additional error messages related to the response
$ref: "#/components/schemas/ValidationWarning"
description: List of validation warnings, to be send to the frontend, where the translation will happen.
RetrieveGrantedDatasetIdentifiers:
type: object
properties:
Expand Down
8 changes: 4 additions & 4 deletions src/main/openapi/rems.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/SubmitApplicationResponse"
$ref: "#/components/schemas/SuccessApplicationResponse"
/api/applications/{application-id}:
get:
tags:
Expand Down Expand Up @@ -335,7 +335,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/SuccessResponse"
$ref: "#/components/schemas/SuccessApplicationResponse"
/api/entitlements:
get:
tags:
Expand Down Expand Up @@ -409,7 +409,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/SuccessResponse"
$ref: "#/components/schemas/SuccessApplicationResponse"
components:
schemas:
CatalogueItem:
Expand Down Expand Up @@ -1281,7 +1281,7 @@ components:
format: int64
required:
- application-id
SubmitApplicationResponse:
SuccessApplicationResponse:
type: object
properties:
success:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class AcceptTermsTestIT extends AcceptTermsTest {
public class AcceptTermsIT extends AcceptTermsTest {
}
Loading