Skip to content

Commit

Permalink
test: add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
admy7 committed Mar 21, 2024
1 parent b3f5fda commit 215f8a9
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 PNED G.I.E.
//
// SPDX-License-Identifier: Apache-2.0

package io.github.genomicdatainfrastructure.daam.api;

import static io.github.genomicdatainfrastructure.daam.security.PostAuthenticationFilter.USER_ID_CLAIM;
Expand Down Expand Up @@ -30,14 +31,12 @@ public List<ListedApplication> listApplicationsV1() {
}

@Override
public RetrievedApplication retrieveApplicationV1(String id) {
var principal = (OidcJwtCallerPrincipal) identity.getPrincipal();
String userId = principal.getClaim(USER_ID_CLAIM);
return retrieveApplicationService.retrieveApplication(Long.parseLong(id), userId);
public RetrievedApplication retrieveApplicationV1(Long id) {
return retrieveApplicationService.retrieveApplication(id);
}

@Override
public File retrieveAttachmentFromApplicationV1(String id, String attachmentId) {
public File retrieveAttachmentFromApplicationV1(Long id, Long attachmentId) {
throw new UnsupportedOperationException(
"Unimplemented method 'retrieveAttachmentFromApplicationV1'"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2024 PNED G.I.E.
//
// SPDX-License-Identifier: Apache-2.0

package io.github.genomicdatainfrastructure.daam.exceptions;

public class ApplicationNotFoundException extends RuntimeException {

private static final String MESSAGE = "Application %s not found";

public ApplicationNotFoundException(Long applicationId) {
super(MESSAGE.formatted(applicationId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2024 PNED G.I.E.
//
// SPDX-License-Identifier: Apache-2.0

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

@Provider
public class ApplicationNotFoundExceptionMapper implements ExceptionMapper<ApplicationNotFoundException> {

@Override
public Response toResponse(ApplicationNotFoundException exception) {
ErrorResponse errorResponse = new ErrorResponse(
"Application Not Found",
Response.Status.NOT_FOUND.getStatusCode(),
exception.getMessage()
);

return Response
.status(Response.Status.NOT_FOUND)
.entity(errorResponse)
.type(MediaType.APPLICATION_JSON)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public static RetrievedApplication toRetrievedApplication(Application remsApplic
.builder()
.workflow(toRetrievedApplicationWorkflow(remsApplication.getApplicationWorkflow()))
.externalId(remsApplication.getApplicationExternalId())
.id(remsApplication.getApplicationId().toString())
.id(remsApplication.getApplicationId())
.applicant(toRetrievedApplicationApplicant(remsApplication.getApplicationApplicant()))
.members(toRetrievedApplicationMembers(remsApplication.getApplicationMembers()))
.datasets(toRetrievedApplicationDatasets(remsApplication.getApplicationResources()))
.acceptedLicenses(toRetrievedApplicationAcceptedLicences(remsApplication.getApplicationAcceptedLicenses()))
.forms(toRetrievedApplicationForms(remsApplication.getApplicationForms()))
.invitedMembers(toRetrievedApplicationInvitedMembers(remsApplication.getApplicationInvitedMembers()))
.description(remsApplication.getApplicationDescription())
Expand All @@ -39,7 +38,7 @@ public static RetrievedApplication toRetrievedApplication(Application remsApplic
}

private static RetrievedApplicationWorkflow toRetrievedApplicationWorkflow(Response10953Workflow remsWorkflow) {
return new RetrievedApplicationWorkflow(remsWorkflow.getWorkflowId().toString(),
return new RetrievedApplicationWorkflow(remsWorkflow.getWorkflowId(),
remsWorkflow.getWorkflowType());
}

Expand All @@ -62,24 +61,16 @@ private static List<RetrievedApplicationMember> toRetrievedApplicationMembers(Se
private static List<RetrievedApplicationDataset> toRetrievedApplicationDatasets(List<V2Resource> remsResources) {
return remsResources
.stream()
.map(resource -> new RetrievedApplicationDataset(resource.getCatalogueItemId().toString(),
.map(resource -> new RetrievedApplicationDataset(resource.getCatalogueItemId(),
toLabelObject(resource.getCatalogueItemTitle()),
toLabelObject(resource.getCatalogueItemInfourl())))
.toList();
}

private static List<RetrievedAcceptedLicense> toRetrievedApplicationAcceptedLicences(Map<String, Set<Long>> remsAcceptedLicenses) {
return remsAcceptedLicenses
.entrySet()
.stream()
.flatMap(entry -> entry.getValue().stream().map(licenseId -> new RetrievedAcceptedLicense(entry.getKey(), licenseId.toString())))
.toList();
}

private static List<RetrievedApplicationForm> toRetrievedApplicationForms(List<Form> remsApplicationForms) {
return remsApplicationForms
.stream()
.map(form -> new RetrievedApplicationForm(form.getFormId().toString(),
.map(form -> new RetrievedApplicationForm(form.getFormId(),
form.getFormInternalName(),
toLabelObject(form.getFormExternalTitle()),
form.getFormFields()
Expand Down Expand Up @@ -118,7 +109,7 @@ private static List<RetrievedApplicationEvent> toRetrievedApplicationEvents(List
return remsApplicationEvents
.stream()
.map(event -> new RetrievedApplicationEvent(
event.getEventActor(),
event.getEventActorAttributes().getUserid(),
event.getEventTime(),
event.getEventType()))
.toList();
Expand All @@ -127,7 +118,7 @@ private static List<RetrievedApplicationEvent> toRetrievedApplicationEvents(List
private static List<RetrievedApplicationAttachment> toRetrievedApplicationAttachments(List<ApplicationAttachment> remsApplicationAttachments) {
return remsApplicationAttachments
.stream()
.map(attachment -> new RetrievedApplicationAttachment(attachment.getAttachmentId().toString(),
.map(attachment -> new RetrievedApplicationAttachment(attachment.getAttachmentId(),
attachment.getAttachmentFilename().toString(),
attachment.getAttachmentType()))
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 PNED G.I.E.
//
// SPDX-License-Identifier: Apache-2.0

package io.github.genomicdatainfrastructure.daam.services;

import io.github.genomicdatainfrastructure.daam.model.ListedApplication;
Expand Down Expand Up @@ -35,7 +36,7 @@ public List<ListedApplication> listApplications(String userId) {

public ListedApplication parse(ApplicationOverview applicationOverview) {
return ListedApplication.builder()
.id(applicationOverview.getApplicationId().toString())
.id(applicationOverview.getApplicationId())
.title(applicationOverview.getApplicationExternalId())
.currentState(applicationOverview.getApplicationState())
.stateChangedAt(applicationOverview.getApplicationLastActivity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,51 @@
// SPDX-License-Identifier: Apache-2.0

package io.github.genomicdatainfrastructure.daam.services;
import io.github.genomicdatainfrastructure.daam.exceptions.ApplicationNotFoundException;
import io.github.genomicdatainfrastructure.daam.model.RemsApplicationMapper;
import io.github.genomicdatainfrastructure.daam.model.RetrievedApplication;
import io.github.genomicdatainfrastructure.daam.remote.rems.api.RemsApplicationsApi;
import io.quarkus.oidc.runtime.OidcJwtCallerPrincipal;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import static io.github.genomicdatainfrastructure.daam.security.PostAuthenticationFilter.USER_ID_CLAIM;


@ApplicationScoped
public class RetrieveApplicationService {
private final String remsApiKey;
private final RemsApplicationsApi applicationsApi;
private final SecurityIdentity identity;

@Inject
public RetrieveApplicationService(
@ConfigProperty(name = "quarkus.rest-client.rems_yaml.api-key") String remsApiKey,
SecurityIdentity identity,
@RestClient RemsApplicationsApi applicationsApi
) {
this.remsApiKey = remsApiKey;
this.applicationsApi = applicationsApi;
this.identity = identity;
}

public RetrievedApplication retrieveApplication(Long applicationId, String userId) {
return RemsApplicationMapper.toRetrievedApplication(applicationsApi.apiApplicationsApplicationIdGet(applicationId, remsApiKey, userId));
public RetrievedApplication retrieveApplication(Long applicationId) {
var principal = (OidcJwtCallerPrincipal) identity.getPrincipal();
String userId = principal.getClaim(USER_ID_CLAIM);

try {
return RemsApplicationMapper
.toRetrievedApplication(applicationsApi.apiApplicationsApplicationIdGet(applicationId, remsApiKey, userId));
}
catch (WebApplicationException e) {
if (e.getResponse().getStatus() == 404) {
throw new ApplicationNotFoundException(applicationId);
}
throw e;
}
}
}
Loading

0 comments on commit 215f8a9

Please sign in to comment.