diff --git a/interfaces/src/main/java/be/vlaanderen/vip/magda/client/domain/giveenrollmenthistory/MagdaClientGiveEnrollmentHistoryService.java b/interfaces/src/main/java/be/vlaanderen/vip/magda/client/domain/giveenrollmenthistory/MagdaClientGiveEnrollmentHistoryService.java index b3b0137b..d28f7f30 100644 --- a/interfaces/src/main/java/be/vlaanderen/vip/magda/client/domain/giveenrollmenthistory/MagdaClientGiveEnrollmentHistoryService.java +++ b/interfaces/src/main/java/be/vlaanderen/vip/magda/client/domain/giveenrollmenthistory/MagdaClientGiveEnrollmentHistoryService.java @@ -2,17 +2,20 @@ import be.vlaanderen.vip.magda.client.MagdaClient; import be.vlaanderen.vip.magda.client.MagdaClientException; +import be.vlaanderen.vip.magda.client.MagdaResponse; import be.vlaanderen.vip.magda.client.diensten.GeefHistoriekInschrijvingRequest; +import be.vlaanderen.vip.magda.exception.UitzonderingenSectionInResponseException; +import be.vlaanderen.vip.magda.legallogging.model.UitzonderingType; public class MagdaClientGiveEnrollmentHistoryService implements GiveEnrollmentHistoryService { - private final MagdaClient service; + private final MagdaClient client; private final MagdaResponseEnrollmentHistoryAdapter adapter; public MagdaClientGiveEnrollmentHistoryService( - MagdaClient service, + MagdaClient client, MagdaResponseEnrollmentHistoryAdapter adapter) { - this.service = service; + this.client = client; this.adapter = adapter; } @@ -23,6 +26,18 @@ public MagdaClientGiveEnrollmentHistoryService( @Override public EnrollmentHistory getEnrollmentHistory(GeefHistoriekInschrijvingRequest request) throws MagdaClientException { - return adapter.adapt(service.send(request)); + var responseWrapper = client.send(request); + + validateResponse(responseWrapper.getResponse(), request); // XXX test + + return adapter.adapt(responseWrapper); + } + + private void validateResponse(MagdaResponse response, GeefHistoriekInschrijvingRequest request) throws MagdaClientException { + if(response.getResponseUitzonderingEntries().stream().anyMatch(x -> + x.getUitzonderingType().equals(UitzonderingType.FOUT) && + !"30101".equals(x.getIdentification()))) { + throw new MagdaClientException("Level 3 exception occurred while calling magda service", new UitzonderingenSectionInResponseException(request.getSubject(), response.getResponseUitzonderingEntries(), request.getCorrelationId(), response.getRequestId())); + } } } diff --git a/interfaces/src/test/java/be/vlaanderen/vip/magda/client/domain/giveenrollmenthistory/MagdaClientGiveEnrollmentHistoryServiceTest.java b/interfaces/src/test/java/be/vlaanderen/vip/magda/client/domain/giveenrollmenthistory/MagdaClientGiveEnrollmentHistoryServiceTest.java index 1394b561..9f91f3f8 100644 --- a/interfaces/src/test/java/be/vlaanderen/vip/magda/client/domain/giveenrollmenthistory/MagdaClientGiveEnrollmentHistoryServiceTest.java +++ b/interfaces/src/test/java/be/vlaanderen/vip/magda/client/domain/giveenrollmenthistory/MagdaClientGiveEnrollmentHistoryServiceTest.java @@ -2,7 +2,11 @@ import be.vlaanderen.vip.magda.client.MagdaClient; import be.vlaanderen.vip.magda.client.MagdaClientException; +import be.vlaanderen.vip.magda.client.MagdaResponse; +import be.vlaanderen.vip.magda.client.MagdaResponseWrapper; import be.vlaanderen.vip.magda.client.diensten.GeefHistoriekInschrijvingRequest; +import be.vlaanderen.vip.magda.legallogging.model.UitzonderingEntry; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -11,12 +15,18 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDate; +import java.util.List; import java.util.Set; import java.util.UUID; import static be.vlaanderen.vip.magda.client.diensten.EducationEnrollmentSource.HO; import static be.vlaanderen.vip.magda.client.diensten.EducationEnrollmentSource.INT; +import static be.vlaanderen.vip.magda.legallogging.model.UitzonderingType.FOUT; +import static be.vlaanderen.vip.magda.legallogging.model.UitzonderingType.WAARSCHUWING; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class MagdaClientGiveEnrollmentHistoryServiceTest { @@ -32,10 +42,11 @@ class MagdaClientGiveEnrollmentHistoryServiceTest { @Nested class GetEnrollmentHistory { - @Test - void callsMagdaService() throws MagdaClientException { + private GeefHistoriekInschrijvingRequest request; - var request = GeefHistoriekInschrijvingRequest.builder() + @BeforeEach + void setup() { + request = GeefHistoriekInschrijvingRequest.builder() .insz("insz") .startDate(LocalDate.of(2024, 1, 1)) .endDate(LocalDate.of(2025, 1, 1)) @@ -43,10 +54,53 @@ void callsMagdaService() throws MagdaClientException { .build(); request.setCorrelationId(UUID.fromString("6469cd5e-e8ed-43f7-a91e-48fdfbb76e0f")); + } + + @Test + void callsMagdaService() throws MagdaClientException { + when(magdaClient.send(request)).thenReturn(new MagdaResponseWrapper(MagdaResponse.builder() + .responseUitzonderingEntries(List.of()) + .build())); service.getEnrollmentHistory(request); verify(magdaClient).send(request); } + + @Test + void throwsNoException_ifContainsAnyNonErrorLevel3Uitzondering() throws MagdaClientException { + when(magdaClient.send(request)).thenReturn(new MagdaResponseWrapper(MagdaResponse.builder() + .responseUitzonderingEntries(List.of(UitzonderingEntry.builder() + .identification("30101") + .uitzonderingType(WAARSCHUWING) + .build())) + .build())); + + assertDoesNotThrow(() -> service.getEnrollmentHistory(request)); + } + + @Test + void throwsNoException_ifContainsLevel3Error30101() throws MagdaClientException { + when(magdaClient.send(request)).thenReturn(new MagdaResponseWrapper(MagdaResponse.builder() + .responseUitzonderingEntries(List.of(UitzonderingEntry.builder() + .identification("30101") + .uitzonderingType(FOUT) + .build())) + .build())); + + assertDoesNotThrow(() -> service.getEnrollmentHistory(request)); + } + + @Test + void throwsException_ifContainsOtherLevel3Error() throws MagdaClientException { + when(magdaClient.send(request)).thenReturn(new MagdaResponseWrapper(MagdaResponse.builder() + .responseUitzonderingEntries(List.of(UitzonderingEntry.builder() + .identification("12345") + .uitzonderingType(FOUT) + .build())) + .build())); + + assertThrows(MagdaClientException.class, () -> service.getEnrollmentHistory(request)); + } } } \ No newline at end of file