From dd720e504ae955ec4b55ba1ef293775614c7c20e Mon Sep 17 00:00:00 2001 From: Emiliana Date: Fri, 2 Aug 2024 16:34:01 +0200 Subject: [PATCH] test: Add test case when success is false --- .../daam/services/AcceptTermsService.java | 16 +++++++++++-- .../daam/api/AcceptTermsTest.java | 14 +++++++++++ ...on => accept_terms_for_success_false.json} | 7 +++--- ...cept_terms_for_success_false.json.license} | 0 ...ess_false_application_not_submittable.json | 24 ------------------- ...e_application_not_submittable.json.license | 3 --- ...terms_success_false_unauthorized_user.json | 24 ------------------- ...ccess_false_unauthorized_user.json.license | 3 --- 8 files changed, 31 insertions(+), 60 deletions(-) rename src/test/resources/mappings/{accept_terms_success_false_application_not_found.json => accept_terms_for_success_false.json} (71%) rename src/test/resources/mappings/{accept_terms_success_false_application_not_found.json.license => accept_terms_for_success_false.json.license} (100%) delete mode 100644 src/test/resources/mappings/accept_terms_success_false_application_not_submittable.json delete mode 100644 src/test/resources/mappings/accept_terms_success_false_application_not_submittable.json.license delete mode 100644 src/test/resources/mappings/accept_terms_success_false_unauthorized_user.json delete mode 100644 src/test/resources/mappings/accept_terms_success_false_unauthorized_user.json.license diff --git a/src/main/java/io/github/genomicdatainfrastructure/daam/services/AcceptTermsService.java b/src/main/java/io/github/genomicdatainfrastructure/daam/services/AcceptTermsService.java index 883ced3..e930157 100644 --- a/src/main/java/io/github/genomicdatainfrastructure/daam/services/AcceptTermsService.java +++ b/src/main/java/io/github/genomicdatainfrastructure/daam/services/AcceptTermsService.java @@ -12,16 +12,22 @@ import io.github.genomicdatainfrastructure.daam.remote.rems.model.SuccessResponse; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import lombok.extern.java.Log; import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.rest.client.inject.RestClient; import java.util.List; import static java.util.Optional.ofNullable; +import static java.util.stream.Collectors.joining; +@Log @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 final String remsApiKey; private final RemsApplicationCommandApi remsApplicationCommandApi; private final RemsApiQueryGateway remsApiQueryGateway; @@ -50,11 +56,17 @@ public void acceptTerms(Long id, String userId, AcceptTermsCommand acceptTermsCo if (Boolean.FALSE.equals(response.getSuccess())) { var nonNullErrors = ofNullable(response.getErrors()).orElseGet(List::of); - List errorStrings = nonNullErrors.stream() + var concatenatedErrors = nonNullErrors.stream() .map(Object::toString) + .collect(joining(";")); + + log.warning(ACCEPT_TERMS_LOG.formatted(userId, id, concatenatedErrors)); + + var errorMessages = nonNullErrors.stream() + .map(it -> ERROR_MESSAGE.formatted(it)) .toList(); - throw new AcceptTermsException(id, errorStrings); + throw new AcceptTermsException(id, errorMessages); } } } diff --git a/src/test/java/io/github/genomicdatainfrastructure/daam/api/AcceptTermsTest.java b/src/test/java/io/github/genomicdatainfrastructure/daam/api/AcceptTermsTest.java index 444195a..f7193c5 100644 --- a/src/test/java/io/github/genomicdatainfrastructure/daam/api/AcceptTermsTest.java +++ b/src/test/java/io/github/genomicdatainfrastructure/daam/api/AcceptTermsTest.java @@ -66,4 +66,18 @@ void cannot_accept_terms_when_not_in_submittable_state() { .statusCode(428) .body("title", equalTo("Application Not In Correct State")); } + + @Test + void cannot_accept_terms_when_success_false() { + given() + .auth() + .oauth2(getAccessToken("alice")) + .contentType("application/json") + .body("{ \"application-id\": 44, \"accepted-licenses\": [1, 2] }") + .when() + .post("/api/v1/applications/44/accept-terms") + .then() + .statusCode(400) + .body("title", equalTo("Could not accept terms")); + } } diff --git a/src/test/resources/mappings/accept_terms_success_false_application_not_found.json b/src/test/resources/mappings/accept_terms_for_success_false.json similarity index 71% rename from src/test/resources/mappings/accept_terms_success_false_application_not_found.json rename to src/test/resources/mappings/accept_terms_for_success_false.json index 104cd37..32eec19 100644 --- a/src/test/resources/mappings/accept_terms_success_false_application_not_found.json +++ b/src/test/resources/mappings/accept_terms_for_success_false.json @@ -4,7 +4,7 @@ "url": "/api/applications/accept-licenses", "bodyPatterns": [ { - "matchesJsonPath": "$[?(@.application-id == 12345)]" + "matchesJsonPath": "$[?(@.application-id == 44)]" } ] }, @@ -15,9 +15,8 @@ }, "jsonBody": { "success": false, - "errors": [ - "Application not found" - ] + "errors": [], + "warnings": [] } }, "priority": 1 diff --git a/src/test/resources/mappings/accept_terms_success_false_application_not_found.json.license b/src/test/resources/mappings/accept_terms_for_success_false.json.license similarity index 100% rename from src/test/resources/mappings/accept_terms_success_false_application_not_found.json.license rename to src/test/resources/mappings/accept_terms_for_success_false.json.license diff --git a/src/test/resources/mappings/accept_terms_success_false_application_not_submittable.json b/src/test/resources/mappings/accept_terms_success_false_application_not_submittable.json deleted file mode 100644 index 68d54b7..0000000 --- a/src/test/resources/mappings/accept_terms_success_false_application_not_submittable.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "request": { - "method": "POST", - "url": "/api/applications/accept-licenses", - "bodyPatterns": [ - { - "matchesJsonPath": "$[?(@.application-id == 2)]" - } - ] - }, - "response": { - "status": 200, - "headers": { - "Content-Type": "application/json" - }, - "jsonBody": { - "success": false, - "errors": [ - "Application not in submittable state" - ] - } - }, - "priority": 1 -} \ No newline at end of file diff --git a/src/test/resources/mappings/accept_terms_success_false_application_not_submittable.json.license b/src/test/resources/mappings/accept_terms_success_false_application_not_submittable.json.license deleted file mode 100644 index c8d4da6..0000000 --- a/src/test/resources/mappings/accept_terms_success_false_application_not_submittable.json.license +++ /dev/null @@ -1,3 +0,0 @@ -SPDX-FileCopyrightText: 2024 PNED G.I.E. - -SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/src/test/resources/mappings/accept_terms_success_false_unauthorized_user.json b/src/test/resources/mappings/accept_terms_success_false_unauthorized_user.json deleted file mode 100644 index 31b5d91..0000000 --- a/src/test/resources/mappings/accept_terms_success_false_unauthorized_user.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "request": { - "method": "POST", - "url": "/api/applications/accept-licenses", - "bodyPatterns": [ - { - "matchesJsonPath": "$[?(@.application-id == 1)]" - } - ] - }, - "response": { - "status": 200, - "headers": { - "Content-Type": "application/json" - }, - "jsonBody": { - "success": false, - "errors": [ - "User not authorized to accept terms for this application" - ] - } - }, - "priority": 1 -} \ No newline at end of file diff --git a/src/test/resources/mappings/accept_terms_success_false_unauthorized_user.json.license b/src/test/resources/mappings/accept_terms_success_false_unauthorized_user.json.license deleted file mode 100644 index c8d4da6..0000000 --- a/src/test/resources/mappings/accept_terms_success_false_unauthorized_user.json.license +++ /dev/null @@ -1,3 +0,0 @@ -SPDX-FileCopyrightText: 2024 PNED G.I.E. - -SPDX-License-Identifier: Apache-2.0 \ No newline at end of file