/******************************************************************************* * 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.TnvzHelper; import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.app2mzs.xsd.persondata.IdentificationType; import at.gv.zustellung.msg.xsd.ObjectFactory; import at.gv.zustellung.tnvz.xsd.MimeTypeList; import at.gv.zustellung.tnvz.xsd.MimeTypeListType; 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.MimeTypeListType.mimeTypeListTypeBuilder; 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; /** * @author Christof Rabensteiner * */ @RunWith(MockitoJUnitRunner.class) public class TnvzHelperTest { private static final ObjectFactory FACTORY = new ObjectFactory(); 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, true, 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, false, 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, false, 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, boolean allMimetypesAccepted, IdentificationType receiverId) { var successBuilder = personResultSuccessTypeBuilder() .withMimeTypeList(setupMimeTypeList(mimeTypes)) .withIdentification(converter.convert(receiverId)); if(allMimetypesAccepted) { successBuilder.withAllStandardMimeTypes(FACTORY.createIndicatorType()); } var personResult = personResultTypeBuilder() .withSuccess(successBuilder.build()) .build(); var queryResultList = queryResultListBuilder() .withQueryResult(List.of(personResult)) .build(); MimeTypeListType standardList = mimeTypeListTypeBuilder() .withMimeType(List.of()) .build(); return queryPersonResponseBuilder() .withQueryResultList(queryResultList) .withStandardMimeTypeList(standardList) .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(); } }