Skip to content

Commit

Permalink
Merged in feature/MAGDASOLID-1797-lenient-magda-client (pull request …
Browse files Browse the repository at this point in the history
…#247)

feat: add a more lenient version of the ConnectorMagdaClient that leaves level 3 uitzondering handling up to the user

Approved-by: Laurens Debackere
  • Loading branch information
NielskoDesmetski committed Jul 16, 2024
2 parents a43461a + a990c40 commit 46d515b
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package be.vlaanderen.vip.magda.client;

import be.vlaanderen.vip.magda.exception.ServerException;

import java.util.UUID;

public abstract class AbstractConnectorMagdaClient implements MagdaClient {

private final MagdaConnector connector;

protected AbstractConnectorMagdaClient(
MagdaConnector connector) {
this.connector = connector;
}

@Override
public MagdaResponseWrapper send(MagdaRequest request) throws MagdaClientException {
return send(request, UUID.randomUUID());
}

@Override
public MagdaResponseWrapper send(MagdaRequest request, UUID requestId) throws MagdaClientException {
try {
var response = connector.send(request, requestId);

validateMagdaResponse(response, request);

return new MagdaResponseWrapper(response);
}
catch (ServerException e) {
throw new MagdaClientException("Error occurred while sending magda request", e);
}
}

protected abstract void validateMagdaResponse(MagdaResponse response, MagdaRequest request) throws MagdaClientException;
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
package be.vlaanderen.vip.magda.client;

import be.vlaanderen.vip.magda.exception.ServerException;
import be.vlaanderen.vip.magda.exception.UitzonderingenSectionInResponseException;
import be.vlaanderen.vip.magda.legallogging.model.UitzonderingEntry;
import be.vlaanderen.vip.magda.legallogging.model.UitzonderingType;

import java.util.List;
import java.util.UUID;

public class ConnectorMagdaClient implements MagdaClient {
private MagdaConnector connector;

public ConnectorMagdaClient(
MagdaConnector connector) {
this.connector = connector;
}
/**
* A MagdaConnector-based client which handles both level 2 and level 3 uitzondingen in the response.
* Level 3 uitzondingen are handled in an opinionated manner whereby an exception is thrown if at least one of them is of type "FOUT".
*/
public class ConnectorMagdaClient extends AbstractConnectorMagdaClient {

@Override
public MagdaResponseWrapper send(MagdaRequest request) throws MagdaClientException {
return send(request, UUID.randomUUID());
public ConnectorMagdaClient(MagdaConnector connector) {
super(connector);
}

@Override
public MagdaResponseWrapper send(MagdaRequest request, UUID requestId) throws MagdaClientException {
try {
var response = connector.send(request, requestId);

validateMagdaResponse(response, request);

return new MagdaResponseWrapper(response);
}
catch (ServerException e) {
throw new MagdaClientException("Error occurred while sending magda request", e);
}
}

private void validateMagdaResponse(MagdaResponse response, MagdaRequest request) throws MagdaClientException {
protected void validateMagdaResponse(MagdaResponse response, MagdaRequest request) throws MagdaClientException {
if(!response.getUitzonderingEntries().isEmpty()) {
throw new MagdaClientException("Level 2 exception occurred while calling magda service", new UitzonderingenSectionInResponseException(request.getSubject(), response.getUitzonderingEntries(), request.getCorrelationId(), response.getRequestId()));
}
Expand All @@ -47,5 +29,4 @@ private void validateMagdaResponse(MagdaResponse response, MagdaRequest request)
private boolean haveAtLeastOneFout(List<UitzonderingEntry> entries) {
return entries.stream().anyMatch(uitzonderingEntry -> uitzonderingEntry.getUitzonderingType().equals(UitzonderingType.FOUT));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package be.vlaanderen.vip.magda.client;

import be.vlaanderen.vip.magda.exception.UitzonderingenSectionInResponseException;

/**
* A MagdaConnector-based client which handles level 2 uitzondingen in the response.
*/
public class LenientConnectorMagdaClient extends AbstractConnectorMagdaClient {

public LenientConnectorMagdaClient(MagdaConnector connector) {
super(connector);
}

@Override
protected void validateMagdaResponse(MagdaResponse response, MagdaRequest request) throws MagdaClientException {
if(!response.getUitzonderingEntries().isEmpty()) {
throw new MagdaClientException("Level 2 exception occurred while calling magda service", new UitzonderingenSectionInResponseException(request.getSubject(), response.getUitzonderingEntries(), request.getCorrelationId(), response.getRequestId()));
}
}
}
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
@@ -0,0 +1,84 @@
package be.vlaanderen.vip.magda.client;

import be.vlaanderen.vip.magda.exception.ServerException;
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;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class LenientConnectorMagdaClientTest {
@Mock private MagdaConnector connector;

@InjectMocks
private LenientConnectorMagdaClient service;

@Nested
class Send {
private static final UUID REQUEST_ID = UUID.fromString("64fb1939-0ca7-432b-b7f4-3b53f7fc3789");

@Mock private MagdaRequest request;

@Mock private MagdaResponse response;

private final List<UitzonderingEntry> level2errors = new ArrayList<>();
private final List<UitzonderingEntry> level3errors = new ArrayList<>();

@BeforeEach
void setup() {
lenient().when(response.getUitzonderingEntries()).thenReturn(level2errors);
lenient().when(response.getResponseUitzonderingEntries()).thenReturn(level3errors);
lenient().when(response.getRequestId()).thenReturn(REQUEST_ID);
}

@Test
void returnsMagdaResponse_whenResponseContainsNoErrors() throws MagdaClientException {
when(connector.send(request, REQUEST_ID)).thenReturn(response);

var result = service.send(request, REQUEST_ID);

assertThat(result.getResponse(), is(equalTo(response)));
}

@Test
void returnsMagdaResponse_whenResponseContainsLevel3Errors() throws MagdaClientException {
when(connector.send(request, REQUEST_ID)).thenReturn(response);
level3errors.add(mock(UitzonderingEntry.class));

var result = service.send(request, REQUEST_ID);

assertThat(result.getResponse(), is(equalTo(response)));
}

@Test
void throwsException_whenSendFails() {
when(connector.send(request, REQUEST_ID)).thenThrow(ServerException.class);

assertThrows(MagdaClientException.class,
() -> service.send(request, REQUEST_ID));
}

@Test
void throwsException_whenResponseContainsLevel2Errors() {
when(connector.send(request, REQUEST_ID)).thenReturn(response);
level2errors.add(mock(UitzonderingEntry.class));

assertThrows(MagdaClientException.class,
() -> service.send(request, REQUEST_ID));
}
}
}
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 46d515b

Please sign in to comment.