using System;
using be.ehealth.businessconnector.mycarenet.agreementasync.v2.session;
using NUnit.Framework;
using be.fgov.ehealth.technicalconnector.tests.utils;
using ikvm.extensions;
using be.fgov.ehealth.technicalconnector.tests.session;
using be.ehealth.technicalconnector.utils;
using be.ehealth.technicalconnector.enumeration;
using be.ehealth.businessconnector.genericasync.domain;
using be.ehealth.business.mycarenetdomaincommons.domain;
using be.cin.mycarenet._1_0.carenet.types;
using be.cin.encrypted;
using be.cin.nip.async.generic;

namespace be.ehealth.businessconnector.mycarenet.agreementasyncv2.session
{
	
	public abstract class AbstractBaseIntegrationTest {

	    protected void getEAgreementResponse(String inputReference) {
            byte[] bytes = ConnectorIOUtils.getResourceAsByteArray(".\\examples\\mycarenet\\eagreementasyncv2\\request\\eAgreementRequest.xml");
            ProcessedPostResponse processedPostResponse = AgreementSessionServiceFactory.getAgreementService().getEAgreementRequest(bytes, "200", new InputReference(inputReference), "2.0");
            ProcessedGetResponse response = (ProcessedGetResponse) AgreementSessionServiceFactory.getAgreementService().getEAgreementResponse(GetRequest.newBuilder().withDefaults().build());

            Assert.AreEqual("urn:nip:tack:result:major:success", processedPostResponse.getPostResponse().getReturn().getResultMajor(), "Post response should have success result");
        	Assert.IsFalse(response.getMsgResponses().isEmpty());
            ProcessedMsgResponse processedMsgResponse = (ProcessedMsgResponse)response.getMsgResponses().get(0);
            Assert.AreEqual(0, processedMsgResponse.getSignatureVerificationResult().getErrors().size(), "SignatureVerificationResult should contain no error");
            MsgResponse msgResponse = (MsgResponse) processedMsgResponse.getMsgResponse();
            Assert.IsNotNull(processedMsgResponse.getMsgResponse().getXadesT(), "Missing xades");
            XmlAsserter.assertSimilar(ConnectorXmlUtils.toObject(processedMsgResponse.getSignedData(), typeof(EncryptedKnownContent)), processedMsgResponse.getRawDecryptedBlob());
            byte[] businessContent = (byte[]) ((ProcessedMsgResponse) response.getMsgResponses().get(0)).getBusinessResponse();
            String stringContent = ConnectorIOUtils.toString(businessContent, Charset.UTF_8);
            XmlAsserter.assertSimilar(ConnectorIOUtils.getResourceAsString(".\\examples\\mycarenet\\eagreementasyncv2\\expected\\eAgreementResponse.xml"), stringContent);

            AgreementSessionServiceFactory.getAgreementService().confirmAllMessages(response);
        }
	}
}
