-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in feature/MAGDASOLID-1797-lenient-magda-client (pull request …
…#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
Showing
6 changed files
with
224 additions
and
34 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
interfaces/src/main/java/be/vlaanderen/vip/magda/client/AbstractConnectorMagdaClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
interfaces/src/main/java/be/vlaanderen/vip/magda/client/LenientConnectorMagdaClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
interfaces/src/test/java/be/vlaanderen/vip/magda/client/LenientConnectorMagdaClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters