/******************************************************************************* * Copyright 2019 Graz University of Technology * MOA ZS has been developed in a cooperation between EGIZ * and Graz University of Technology. * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * https://joinup.ec.europa.eu/news/understanding-eupl-v12 * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. *******************************************************************************/ 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.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.*; /** * @author Christof Rabensteiner * */ @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 backend; @Before public void setup() { backend = new DeliveryRequestBackend( repository, tnvzHelper, converter, clientFactory, verifier); } @Test public void executePipelineWithoutTnvzRequest() { var appDeliveryID = "no-tnvz-request"; var expectedStatus = setupMocks(appDeliveryID, false); backend.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())) .thenThrow(moaZSException("Not addressable", "400")); backend.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()); } @Test public void executePipelineWithTnvzRequest() { var appDeliveryID = "tnvz-request"; var expectedStatus = setupMocks(appDeliveryID, true); backend.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()); backend.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.createMsgClient(any())).thenReturn(msgClient); when(msgClient.delivery(msgRequest)).thenReturn(status); if (tnvzRequest) { when(clientFactory.createTnvzClient(any())).thenReturn(tnvzClient); when(tnvzHelper.performQueryPersonRequest(any(), any())).thenReturn(identification); } return status; } 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(); } }