package be.fgov.ehealth.technicalconnector.signature.transformers;

import be.fgov.ehealth.technicalconnector.test.tests.utils.LoggingUtils;
import be.fgov.ehealth.technicalconnector.test.tests.utils.XmlAsserter;
import lombok.RequiredArgsConstructor;
import org.apache.xml.security.signature.XMLSignatureByteInput;
import org.apache.xml.security.signature.XMLSignatureInput;
import org.apache.xml.security.signature.XMLSignatureStreamInput;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

@RequiredArgsConstructor
public class NippinOptionalDeflateTransformerTest {

    @BeforeClass
    public static void before() {
        LoggingUtils.bootstrap();
    }

    @Test
    public void testXMLSignatureStreamInput() throws Exception {
        byte[] compressed = Base64.getDecoder().decode(
                "xVZdb+I6EP0rVd5LnI9SqNyswkdbpAXSQHqv9qUyyZR4ldisnYR2f/1OSgsEuLrsw2qfUGbOGc/M8QymX17" +
                        "z7KICpbkUt4bVIsYFiFgmXCxvjWh+d9kxLnTBRMIyKeDWeANtfPGo0PYNjKcP48GdRay+AlaAv1plPGYFRgrhRw" +
                        "m6uMDgQt8aaVGsbkxzvV63clBxqXipW1XGMCwoEK0FmLlMczOWeS6FWV0bG+YNHnM+O3nBVMzK2ZGds" +
                        "8iVXmxPtndk9yzy+mecp1u6Y2xaMwat2RLGULCEFcyjs9lo4rW7xLbbzrVrdaj5bqFDUYBaKa5hUuYL" +
                        "UB5x265jk27HpeaRk5ong9fGIwk8GgLT+DuB9fOenZofdjqAFWAhoghQ/trwX0maR8ixXPCMF29zKTO" +
                        "P9pjm8fBHyVc5YjzaZwpmoCoeQ18m4Nk2sa4cah7aaaBkUsaF2FRnEQcFJIQ4dpuaTR8NygVWECjkYj" +
                        "zHbl0RxOwZqZ9lcs1EDE+zXn0kaRGENKx0VmIj4D3LjxB7lnevVIUvkhBWjKtN8g5xu50N8MhJzcPaz" +
                        "WZrRnoGsRTJPylAFqfI815YpoGaJzyIHjKVvYUgYM2ygBWpzOQShcv6KRNL2HH/B3cQKRKlZosMGrmd" +
                        "jHUSSR+YDjHZpeD1DXryg4dP9glPjZ4N/D1E/YUZoaIveHURh+fovWIOHXihcN8sucZ+4s1VsmLZ53i" +
                        "0UdjOldO2XQvFbTprNbbE3fD8/pTtc/dGZyDjcqMyboeb018DnEIP73v3kliXNqkn9sDZgPfRspTqzf" +
                        "OD4Ouo789H08nzYNqPxsPJvEneQhsBhq8FiLp1XjC5bxJ2rgbjjmcgWA6ez0SlGFs+61hKLPjZt5oBt" +
                        "shmvdH3ng5Jd7i4T79/I8n8MXqdPEZ3vSia9MLIKh6tFUki6z76Nx1GP1PeDOodfZ7scKBAx4qvNqvs" +
                        "QGjLvXbd6y6uCvf4EuwzG2pYTr1VDpxN+O8KXIc8U+AgHM764SioFf7Dwn7qWUmpdJwq/lL8FWFPt9Z" +
                        "sSjuWBa8+Hg31gj2/992zez+ezkdP77MVDoNp+KcHK/8oCT4n6690/7ixI+3HMa//x3e7d2vZvC1OPC" +
                        "PM89563i8=");

        XMLSignatureInput input = new XMLSignatureStreamInput(new ByteArrayInputStream(compressed));

        NippinOptionalDeflateTransformer transformer = new NippinOptionalDeflateTransformer();
        XMLSignatureInput result = transformer.enginePerformTransform(input, null, null, null, false);

        XmlAsserter.assertSimilar("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:eMOHMDF101CreateApplicationRequest xmlns=\"http://www.mercurius.vlaanderen.be/mohm/common/v7\" xmlns:ns2=\"http://www.mercurius.vlaanderen.be/mohm/df101/v3\" xmlns:ns3=\"http://www.mercurius.vlaanderen.be/vsb/common/v2\" xmlns:ns4=\"http://www.mercurius.vlaanderen.be/wzcmh/common/v3\"><ns2:MessageMetadata><SSIN>69022637418</SSIN><EnterpriseNumber>0464320984</EnterpriseNumber></ns2:MessageMetadata><ns2:CreateApplication><Reason>New_Application</Reason><DependentPerson><SSIN>69022637418</SSIN></DependentPerson><MobilityTool><BasicEquipment><CareServiceCode>220153</CareServiceCode><Productnumber>103101000326</Productnumber><PublicPrice>2232.50</PublicPrice><AllowanceVSB>2200.00</AllowanceVSB><Supplement>32.50</Supplement><SupportAndRepairCode>230498</SupportAndRepairCode></BasicEquipment></MobilityTool><IsSecondWheelchair>false</IsSecondWheelchair><IsEarlyRenewalPathologicalChange>false</IsEarlyRenewalPathologicalChange><IsEarlyRenewalUnusableMobilityTool>false</IsEarlyRenewalUnusableMobilityTool><HasRecognitionVAPH>false</HasRecognitionVAPH><HasSDA>false</HasSDA><IsProfessionalUse>false</IsProfessionalUse><Bandagist><ApprovalNumber>062008536241</ApprovalNumber></Bandagist><Enterprise><EnterpriseNumber>0464320984</EnterpriseNumber></Enterprise><ApplicationDocument><ns4:Document><ns4:DocumentDate>2019-01-20</ns4:DocumentDate><ns4:DocumentCategory>APPLICATION_DOCUMENT</ns4:DocumentCategory><ns4:DocumentExtension>PNG</ns4:DocumentExtension><ns4:DocumentFilename>Aanvraag_scooter_A1</ns4:DocumentFilename><ns4:Document>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ns4:Document></ns4:Document></ApplicationDocument><Prescription><ApprovalNumber>14744790004</ApprovalNumber><PrescriptionDate>2019-01-13</PrescriptionDate><PrescriptionDocument><ns4:Document><ns4:DocumentDate>2019-01-13</ns4:DocumentDate><ns4:DocumentCategory>PRESCRIPTION</ns4:DocumentCategory><ns4:DocumentExtension>PNG</ns4:DocumentExtension><ns4:DocumentFilename>scooter_voorschrift</ns4:DocumentFilename><ns4:Document>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ns4:Document></ns4:Document></PrescriptionDocument></Prescription><MotivationReport><ns4:Document><ns4:DocumentDate>2019-01-19</ns4:DocumentDate><ns4:DocumentCategory>MOTIVATIONREPORT</ns4:DocumentCategory><ns4:DocumentExtension>PNG</ns4:DocumentExtension><ns4:DocumentFilename>motivatie_scooter</ns4:DocumentFilename><ns4:Document>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ns4:Document></ns4:Document></MotivationReport><IsAccident>false</IsAccident></ns2:CreateApplication></ns2:eMOHMDF101CreateApplicationRequest>\n", new String(result.getBytes(), StandardCharsets.UTF_8));

    }


    @Test
    public void testXMLSignatureByteInput() throws Exception {
        byte[] compressed = Base64.getDecoder().decode(
                "xVZdb+I6EP0rVd5LnI9SqNyswkdbpAXSQHqv9qUyyZR4ldisnYR2f/1OSgsEuLrsw2qfUGbOGc/M8QymX17" +
                        "z7KICpbkUt4bVIsYFiFgmXCxvjWh+d9kxLnTBRMIyKeDWeANtfPGo0PYNjKcP48GdRay+AlaAv1plPGYFRgrhRw" +
                        "m6uMDgQt8aaVGsbkxzvV63clBxqXipW1XGMCwoEK0FmLlMczOWeS6FWV0bG+YNHnM+O3nBVMzK2ZGds" +
                        "8iVXmxPtndk9yzy+mecp1u6Y2xaMwat2RLGULCEFcyjs9lo4rW7xLbbzrVrdaj5bqFDUYBaKa5hUuYL" +
                        "UB5x265jk27HpeaRk5ong9fGIwk8GgLT+DuB9fOenZofdjqAFWAhoghQ/trwX0maR8ixXPCMF29zKTO" +
                        "P9pjm8fBHyVc5YjzaZwpmoCoeQ18m4Nk2sa4cah7aaaBkUsaF2FRnEQcFJIQ4dpuaTR8NygVWECjkYj" +
                        "zHbl0RxOwZqZ9lcs1EDE+zXn0kaRGENKx0VmIj4D3LjxB7lnevVIUvkhBWjKtN8g5xu50N8MhJzcPaz" +
                        "WZrRnoGsRTJPylAFqfI815YpoGaJzyIHjKVvYUgYM2ygBWpzOQShcv6KRNL2HH/B3cQKRKlZosMGrmd" +
                        "jHUSSR+YDjHZpeD1DXryg4dP9glPjZ4N/D1E/YUZoaIveHURh+fovWIOHXihcN8sucZ+4s1VsmLZ53i" +
                        "0UdjOldO2XQvFbTprNbbE3fD8/pTtc/dGZyDjcqMyboeb018DnEIP73v3kliXNqkn9sDZgPfRspTqzf" +
                        "OD4Ouo789H08nzYNqPxsPJvEneQhsBhq8FiLp1XjC5bxJ2rgbjjmcgWA6ez0SlGFs+61hKLPjZt5oBt" +
                        "shmvdH3ng5Jd7i4T79/I8n8MXqdPEZ3vSia9MLIKh6tFUki6z76Nx1GP1PeDOodfZ7scKBAx4qvNqvs" +
                        "QGjLvXbd6y6uCvf4EuwzG2pYTr1VDpxN+O8KXIc8U+AgHM764SioFf7Dwn7qWUmpdJwq/lL8FWFPt9Z" +
                        "sSjuWBa8+Hg31gj2/992zez+ezkdP77MVDoNp+KcHK/8oCT4n6690/7ixI+3HMa//x3e7d2vZvC1OPC" +
                        "PM89563i8=");

        XMLSignatureInput input = new XMLSignatureByteInput(compressed);

        NippinOptionalDeflateTransformer transformer = new NippinOptionalDeflateTransformer();
        XMLSignatureInput result = transformer.enginePerformTransform(input, null, null, null, false);

        XmlAsserter.assertSimilar("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:eMOHMDF101CreateApplicationRequest xmlns=\"http://www.mercurius.vlaanderen.be/mohm/common/v7\" xmlns:ns2=\"http://www.mercurius.vlaanderen.be/mohm/df101/v3\" xmlns:ns3=\"http://www.mercurius.vlaanderen.be/vsb/common/v2\" xmlns:ns4=\"http://www.mercurius.vlaanderen.be/wzcmh/common/v3\"><ns2:MessageMetadata><SSIN>69022637418</SSIN><EnterpriseNumber>0464320984</EnterpriseNumber></ns2:MessageMetadata><ns2:CreateApplication><Reason>New_Application</Reason><DependentPerson><SSIN>69022637418</SSIN></DependentPerson><MobilityTool><BasicEquipment><CareServiceCode>220153</CareServiceCode><Productnumber>103101000326</Productnumber><PublicPrice>2232.50</PublicPrice><AllowanceVSB>2200.00</AllowanceVSB><Supplement>32.50</Supplement><SupportAndRepairCode>230498</SupportAndRepairCode></BasicEquipment></MobilityTool><IsSecondWheelchair>false</IsSecondWheelchair><IsEarlyRenewalPathologicalChange>false</IsEarlyRenewalPathologicalChange><IsEarlyRenewalUnusableMobilityTool>false</IsEarlyRenewalUnusableMobilityTool><HasRecognitionVAPH>false</HasRecognitionVAPH><HasSDA>false</HasSDA><IsProfessionalUse>false</IsProfessionalUse><Bandagist><ApprovalNumber>062008536241</ApprovalNumber></Bandagist><Enterprise><EnterpriseNumber>0464320984</EnterpriseNumber></Enterprise><ApplicationDocument><ns4:Document><ns4:DocumentDate>2019-01-20</ns4:DocumentDate><ns4:DocumentCategory>APPLICATION_DOCUMENT</ns4:DocumentCategory><ns4:DocumentExtension>PNG</ns4:DocumentExtension><ns4:DocumentFilename>Aanvraag_scooter_A1</ns4:DocumentFilename><ns4:Document>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ns4:Document></ns4:Document></ApplicationDocument><Prescription><ApprovalNumber>14744790004</ApprovalNumber><PrescriptionDate>2019-01-13</PrescriptionDate><PrescriptionDocument><ns4:Document><ns4:DocumentDate>2019-01-13</ns4:DocumentDate><ns4:DocumentCategory>PRESCRIPTION</ns4:DocumentCategory><ns4:DocumentExtension>PNG</ns4:DocumentExtension><ns4:DocumentFilename>scooter_voorschrift</ns4:DocumentFilename><ns4:Document>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ns4:Document></ns4:Document></PrescriptionDocument></Prescription><MotivationReport><ns4:Document><ns4:DocumentDate>2019-01-19</ns4:DocumentDate><ns4:DocumentCategory>MOTIVATIONREPORT</ns4:DocumentCategory><ns4:DocumentExtension>PNG</ns4:DocumentExtension><ns4:DocumentFilename>motivatie_scooter</ns4:DocumentFilename><ns4:Document>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</ns4:Document></ns4:Document></MotivationReport><IsAccident>false</IsAccident></ns2:CreateApplication></ns2:eMOHMDF101CreateApplicationRequest>\n", new String(result.getBytes(), StandardCharsets.UTF_8));

    }

    @Test(expected = IOException.class)
    public void testXMLSignatureStreamInput_WrongInput() throws Exception{
        XMLSignatureInput input = new XMLSignatureByteInput("dummy".getBytes());

        NippinOptionalDeflateTransformer transformer = new NippinOptionalDeflateTransformer();
        transformer.enginePerformTransform(input, null, null, null, false);

    }


}