Skip to content

Commit

Permalink
feat: put the level 3 error handling logic into the MagdaClientGiveEn…
Browse files Browse the repository at this point in the history
…rollmentHistoryService
  • Loading branch information
NielskoDesmetski committed Jul 16, 2024
1 parent 95e515f commit a990c40
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -32,21 +42,65 @@ 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))
.sources(Set.of(HO, INT))
.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));
}
}
}

0 comments on commit a990c40

Please sign in to comment.