Skip to content

Commit

Permalink
test: Add test case when success is false
Browse files Browse the repository at this point in the history
  • Loading branch information
emilianapali committed Aug 2, 2024
1 parent c4c947f commit dd720e5
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"url": "/api/applications/accept-licenses",
"bodyPatterns": [
{
"matchesJsonPath": "$[?(@.application-id == 12345)]"
"matchesJsonPath": "$[?(@.application-id == 44)]"
}
]
},
Expand All @@ -15,9 +15,8 @@
},
"jsonBody": {
"success": false,
"errors": [
"Application not found"
]
"errors": [],
"warnings": []
}
},
"priority": 1
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit dd720e5

Please sign in to comment.