Skip to content

Commit

Permalink
[SELC-5276] fix: Throw exception in of 404 for getUOFromRecipientCode
Browse files Browse the repository at this point in the history
  • Loading branch information
pierpaolodidato89 authored Jul 22, 2024
1 parent e795b41 commit 6d43cc2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ public Uni<Boolean> checkManager(OnboardingUserRequest onboardingUserRequest) {
}

public Uni<CustomError> checkRecipientCode(String recipientCode, String originId) {
return uoApi.findByUnicodeUsingGET1(recipientCode, null).onItem()
return onboardingUtils.getUoFromRecipientCode(recipientCode).onItem()
.transformToUni(uoResource ->
onboardingUtils.validationRecipientCode(originId, uoResource));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.exception.InvalidRequestException;
import it.pagopa.selfcare.onboarding.exception.OnboardingNotAllowedException;
import it.pagopa.selfcare.onboarding.exception.ResourceNotFoundException;
import it.pagopa.selfcare.onboarding.util.InstitutionPaSubunitType;
import it.pagopa.selfcare.product.entity.Product;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.openapi.quarkus.party_registry_proxy_json.api.UoApi;
import org.openapi.quarkus.party_registry_proxy_json.model.UOResource;
Expand Down Expand Up @@ -48,7 +50,8 @@ private Uni<Void> checkRecipientCode(Onboarding onboarding) {
&& InstitutionType.PA.equals(onboarding.getInstitution().getInstitutionType())
&& Objects.nonNull(onboarding.getBilling())
&& Objects.nonNull(onboarding.getBilling().getRecipientCode())) {
return uoApi.findByUnicodeUsingGET1(onboarding.getBilling().getRecipientCode(), null)
final String recipientCode = onboarding.getBilling().getRecipientCode();
return getUoFromRecipientCode(recipientCode)
.flatMap(uoResource -> validationRecipientCode(onboarding.getInstitution().getOriginId(), uoResource))
.onItem().transformToUni(customError -> {
if (Objects.nonNull(customError)) {
Expand All @@ -60,6 +63,17 @@ private Uni<Void> checkRecipientCode(Onboarding onboarding) {
return Uni.createFrom().nullItem();
}

public Uni<UOResource> getUoFromRecipientCode(String recipientCode) {
return uoApi.findByUnicodeUsingGET1(recipientCode, null)
.onFailure(WebApplicationException.class)
.recoverWithUni(ex -> ((WebApplicationException) ex).getResponse().getStatus() == 404
? Uni.createFrom().failure(new ResourceNotFoundException(
String.format(UO_NOT_FOUND.getMessage(),
recipientCode
)))
: Uni.createFrom().failure(ex));
}

public Uni<CustomError> validationRecipientCode(String originId, UOResource uoResource) {
if (!originId.equals(uoResource.getCodiceIpa())) {
return Uni.createFrom().item(DENIED_NO_ASSOCIATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import org.apache.http.HttpStatus;
import org.bson.Document;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.reactive.ClientWebApplicationException;
Expand Down Expand Up @@ -1624,6 +1625,36 @@ void checkRecipientCodeWithValidResponse() {
.assertItem(customError);
}

@Test
void checkRecipientCodeWithGenericError() {
final String recipientCode = "recipientCode";
final String originId = "originId";
RuntimeException exception = new RuntimeException("Generic error");
when(uoApi.findByUnicodeUsingGET1(eq(recipientCode), any()))
.thenReturn(Uni.createFrom().failure(exception));
UniAssertSubscriber<CustomError> subscriber = onboardingService
.checkRecipientCode(recipientCode, originId)
.subscribe()
.withSubscriber(UniAssertSubscriber.create());

subscriber.assertFailedWith(RuntimeException.class);
}

@Test
void checkRecipientCodeWithResourceNotFound() {
final String recipientCode = "recipientCode";
final String originId = "originId";
ClientWebApplicationException exception = new ClientWebApplicationException(HttpStatus.SC_NOT_FOUND);
when(uoApi.findByUnicodeUsingGET1(eq(recipientCode), any()))
.thenReturn(Uni.createFrom().failure(exception));
UniAssertSubscriber<CustomError> subscriber = onboardingService
.checkRecipientCode(recipientCode, originId)
.subscribe()
.withSubscriber(UniAssertSubscriber.create());

subscriber.assertFailedWith(ResourceNotFoundException.class);
}

@Nested
@TestProfile(OnboardingTestProfile.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down

0 comments on commit 6d43cc2

Please sign in to comment.