package at.gv.egiz.moazs; import at.gv.egiz.moazs.client.ClientFactory; import at.gv.egiz.moazs.client.TnvzHelper; import at.gv.egiz.moazs.backend.DeliveryRequestBackend; import at.gv.egiz.moazs.backend.SignatureVerifier; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.repository.InMemoryDeliveryRepository; import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.msg.xsd.App2ZusePort; import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; import at.gv.zustellung.msg.xsd.MetaData; import at.gv.zustellung.msg.xsd.persondata.IdentificationType; import at.gv.zustellung.tnvz.xsd.TNVZServicePort; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import static at.gv.egiz.moazs.MoaZSException.moaZSException; import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; import static at.gv.egiz.moazs.scheme.RequestStatusResponse.getResponseID; import static at.gv.zustellung.app2mzs.xsd.ClientType.clientTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Success.successBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; import static at.gv.zustellung.msg.xsd.MetaData.metaDataBuilder; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class DeliveryRequestBackendTest { private DeliveryRepository repository = new InMemoryDeliveryRepository(100); @Mock private TnvzHelper tnvzHelper; @Mock private ClientFactory clientFactory; @Mock private App2ZusePort msgClient; @Mock private TNVZServicePort tnvzClient; @Mock private Mzs2MsgConverter converter; @Mock private SignatureVerifier verifier; private DeliveryRequestBackend pipeline; @Before public void setup() { pipeline = new DeliveryRequestBackend( repository, tnvzHelper, converter, clientFactory, verifier); } @Test public void executePipelineWithoutTnvzRequest() { var appDeliveryID = "no-tnvz-request"; var expectedStatus = setupMocks(appDeliveryID, false); pipeline.accept(appDeliveryID); verifyZeroInteractions(tnvzHelper); var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); var actualStatus = (DeliveryRequestStatusType) response.getResponse(); assertThat(actualStatus).isEqualTo(expectedStatus); } @Test public void rejectDeliveryWhenReceiverIsNotAddressable() { var appDeliveryID = "not-addressable"; setupMocks(appDeliveryID, true); when(tnvzHelper.performQueryPersonRequest(any(), any(), any())) .thenThrow(buildMzsException("400", appDeliveryID)); pipeline.accept(appDeliveryID); var responseID = getResponseID(appDeliveryID); var response = repository.retrieveResponse(responseID).get(); var status = (DeliveryRequestStatusType) response.getResponse(); var actualCode = status.getError().getErrorInfo().getCode(); assertThat(actualCode).isEqualTo("400"); verifyZeroInteractions(converter); verify(tnvzHelper).performQueryPersonRequest(any(), any(), any()); } @Test public void executePipelineWithTnvzRequest() { var appDeliveryID = "tnvz-request"; var expectedStatus = setupMocks(appDeliveryID, true); pipeline.accept(appDeliveryID); var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); var actualStatus = (DeliveryRequestStatusType) response.getResponse(); assertThat(actualStatus).isEqualTo(expectedStatus); } @Test public void rejectInvalidSignature() { var appDeliveryID = "invalid-signature"; setupMocks(appDeliveryID, true); doThrow(moaZSException("Signature Invalid!")).when(verifier).accept(any()); pipeline.accept(appDeliveryID); var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); var status = (DeliveryRequestStatusType) response.getResponse(); var actualCode = status.getError().getErrorInfo().getCode(); assertThat(actualCode).isEqualTo(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID); } private DeliveryRequestStatusType setupMocks(String appDeliveryId, boolean tnvzRequest) { var mzsRequest = setupMzsRequest(appDeliveryId, tnvzRequest); var msgRequest = setupMsgRequest(appDeliveryId); var status = setupStatus(appDeliveryId); var signedStatus = new byte[0]; var identification = IdentificationType.identificationTypeBuilder().build(); repository.store(mzsRequest); repository.store(getResponseID(appDeliveryId), signedStatus); when(converter.convert(eq(mzsRequest) )).thenReturn(msgRequest); when(converter.convert(eq(mzsRequest), any())).thenReturn(msgRequest); when(clientFactory.create(any(), same(App2ZusePort.class))).thenReturn(msgClient); when(msgClient.delivery(msgRequest)).thenReturn(status); if (tnvzRequest) { when(clientFactory.create(any(), same(TNVZServicePort.class))).thenReturn(tnvzClient); when(tnvzHelper.performQueryPersonRequest(any(), any(), any())).thenReturn(identification); } return status; } private MoaZSException buildMzsException(String code, String appDeliveryID) { return moaZSExceptionBuilder("") .withErrorCode(code) .withAppDeliveryID(appDeliveryID) .build(); } private DeliveryRequestStatusType setupStatus(String appDeliveryId) { var success = successBuilder() .withAppDeliveryID(appDeliveryId) .build(); return deliveryRequestStatusTypeBuilder() .withSuccess(success) .build(); } private DeliveryRequestType setupMzsRequest(String appDeliveryId, boolean tnvzRequest) { var msgClient = clientTypeBuilder() .withURL("http://zuse") .build(); var config = configTypeBuilder() .withPerformQueryPersonRequest(tnvzRequest) .withMSGClient(msgClient) .build(); return deliveryRequestTypeBuilder() .withMetaData(setupMetaData(appDeliveryId)) .withConfig(config) .build(); } private at.gv.zustellung.msg.xsd.DeliveryRequestType setupMsgRequest(String appDeliveryId) { return at.gv.zustellung.msg.xsd.DeliveryRequestType.deliveryRequestTypeBuilder() .withMetaData(setupMetaData(appDeliveryId)) .build(); } private MetaData setupMetaData(String appDeliveryId) { return metaDataBuilder() .withAppDeliveryID(appDeliveryId) .build(); } }