package at.gv.egiz.moazs; import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.egiz.moazs.client.TnvzHelper; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType; import at.gv.zustellung.msg.xsd.MetaData; import at.gv.zustellung.msg.xsd.SystemComponentType; import at.gv.zustellung.tnvz.xsd.MimeTypeList; import at.gv.zustellung.tnvz.xsd.QueryPersonResponse; 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 java.util.List; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Payload.payloadBuilder; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver.receiverBuilder; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender.senderBuilder; import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.persondata.CorporateBodyType.corporateBodyTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType.Value.valueBuilder; import static at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType.identificationTypeBuilder; import static at.gv.zustellung.msg.xsd.ErrorInfoType.errorInfoTypeBuilder; import static at.gv.zustellung.msg.xsd.MetaData.metaDataBuilder; import static at.gv.zustellung.msg.xsd.SystemComponentType.systemComponentTypeBuilder; import static at.gv.zustellung.tnvz.xsd.MimeTypeList.mimeTypeListBuilder; import static at.gv.zustellung.tnvz.xsd.PersonResultSuccessType.personResultSuccessTypeBuilder; import static at.gv.zustellung.tnvz.xsd.PersonResultType.Error.errorBuilder; import static at.gv.zustellung.tnvz.xsd.PersonResultType.personResultTypeBuilder; import static at.gv.zustellung.tnvz.xsd.QueryPersonResponse.QueryResultList.queryResultListBuilder; import static at.gv.zustellung.tnvz.xsd.QueryPersonResponse.queryPersonResponseBuilder; import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class TnvzHelperTest { private TnvzHelper helper; @Mock private TNVZServicePort port; private Mzs2MsgConverter converter; @Before public void setup() { converter = new Mzs2MsgConverter(); helper = new TnvzHelper(converter); } @Test public void acceptWildcardMimetype() { List acceptedTypes = List.of("*/*"); List attachedTypes = List.of("pdf", "xml", "html", "random/attachedtype"); var receiverId = identification("zbpk", "receiver-id-value"); var deliveryRequest = deliveryRequest(attachedTypes, receiverId); var success = tnvzSuccess(acceptedTypes, receiverId); when(port.queryPerson(any())).thenReturn(success); var actual = helper.performQueryPersonRequest(deliveryRequest, port); assertThat(actual).isEqualToComparingFieldByFieldRecursively(receiverId); } @Test(expected = MoaZSException.class) public void rejectWhenNotAddressable() { List attachedTypes = List.of("*/*"); var receiverId = identification("zbpk", "receiver-id-value"); var deliveryRequest = deliveryRequest(attachedTypes, receiverId); var error = tnvzError("400", "Person not found."); when(port.queryPerson(any())).thenReturn(error); helper.performQueryPersonRequest(deliveryRequest, port); } @Test(expected = MoaZSException.class) public void rejectMismatchedMimeTypes() { List acceptedTypes = List.of("xml"); List attachedTypes = List.of("pdf", "xml", "html", "random/attachedtype"); var receiverId = identification("zbpk", "receiver-id-value"); var deliveryRequest = deliveryRequest(attachedTypes, receiverId); var success = tnvzSuccess(acceptedTypes, receiverId); when(port.queryPerson(any())).thenReturn(success); helper.performQueryPersonRequest(deliveryRequest, port); } @Test public void acceptSpecificMimeType() { List acceptedTypes = List.of("pdf", "xml", "html"); List attachedTypes = List.of("pdf", "xml"); var receiverId = identification("zbpk", "receiver-id-value"); var deliveryRequest = deliveryRequest(attachedTypes, receiverId); var success = tnvzSuccess(acceptedTypes, receiverId); when(port.queryPerson(any())).thenReturn(success); var actual = helper.performQueryPersonRequest(deliveryRequest, port); assertThat(actual).isEqualToComparingFieldByFieldRecursively(receiverId); } private DeliveryRequestType deliveryRequest(List mzsMimeTypes, IdentificationType receiverId) { var corporateBody = corporateBodyTypeBuilder() .withFullName("Corporate Sender GmbH") .withIdentification(List.of(identification("zbpk", "deadbeef"))) .build(); var sender = senderBuilder() .withCorporateBody(corporateBody) .build(); var receiver = receiverBuilder() .withIdentification(receiverId) .build(); var origin = systemComponentTypeBuilder() .withParticipantID("ID") .build(); var metadata = metaDataBuilder() .withOrigin(origin) .build(); return deliveryRequestTypeBuilder() .withSender(sender) .withReceiver(receiver) .withPayload(payload(mzsMimeTypes)) .withMetaData(metadata) .build(); } private IdentificationType identification(String id, String value) { var receiverIdValue = valueBuilder() .withValue(value) .build(); return identificationTypeBuilder() .withId(id) .withValue(receiverIdValue) .build(); } private List payload(List mimeTypes) { return mimeTypes.stream() .map((mtype) -> payloadBuilder().withMIMEType(mtype).build()) .collect(toList()); } public QueryPersonResponse tnvzSuccess(List mimeTypes, IdentificationType receiverId) { var success = personResultSuccessTypeBuilder() .withMimeTypeList(setupMimeTypeList(mimeTypes)) .withIdentification(converter.convert(receiverId)) .build(); var personResult = personResultTypeBuilder() .withSuccess(success) .build(); var queryResultList = queryResultListBuilder() .withQueryResult(List.of(personResult)) .build(); return queryPersonResponseBuilder() .withQueryResultList(queryResultList) .build(); } private QueryPersonResponse tnvzError(String code, String text) { var info = errorInfoTypeBuilder() .withCode(code) .withText(text) .build(); var error = errorBuilder() .withErrorInfo(info) .build(); var result = personResultTypeBuilder() .withError(error) .build(); var queryResultList = queryResultListBuilder() .withQueryResult(List.of(result)) .build(); return queryPersonResponseBuilder() .withQueryResultList(queryResultList) .build(); } private MimeTypeList setupMimeTypeList(List mimeTypes) { return mimeTypeListBuilder().withMimeType(mimeTypes).build(); } }