#region Header

/*
 * Copyright (c) eHealth
 */

#endregion Header

/**
 * BaseTest regrouping all the function needed for the STSService test
 *
 * @author EHP
 */
namespace be.ehealth.technicalconnector.service.sts
{
    using be.ehealth.technicalconnector.beid;
    using be.ehealth.technicalconnector.config;
    using be.ehealth.technicalconnector.service.sts.domain;
    using be.ehealth.technicalconnector.service.sts.security;
    using be.ehealth.technicalconnector.service.sts.security.impl;
    using be.ehealth.technicalconnector.utils;
    using NUnit.Framework;
    using org.w3c.dom;
    using System;
    using System.Text;
    
    
   
    [TestFixture]
    public abstract class STSServiceBaseTest
    {

        private static  string XMLNS_SAML = "urn:oasis:names:tc:SAML:1.0:assertion";

        [SetUpAttribute]
        public static void init()
        {
            ConfigFactory.getConfigValidator().getConfig().setProperty("KEYSTORE_DIR", "\\P12\\acc\\");
        }

        private STSService service;

        [Test]
        public void renewTokenBasedOnEid()
        {
            var headerCred = BeIDCredential.getInstance("session", BeIDCredential.EID_AUTH_ALIAS);

            string keystoreloc = ConfigFactory.getConfigValidator().getConfig().getProperty("test.keystore.location");
            string alias = ConfigFactory.getConfigValidator().getConfig().getProperty("test.keystore.alias");
            string pwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.keystore.password");
            string pkpwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.keystore.privatekeypassword");

            var bodyCred = new KeyStoreCredential(keystoreloc, pwd, alias, pkpwd);
            string hok = Convert.ToBase64String(bodyCred.getCertificate().getEncoded());

            string persistToken = ConnectorIOUtils.convertStreamToString(ConnectorIOUtils.getResourceAsStream("/examples/sas/renew.eid.sas"));

            persistToken = persistToken.Replace("${user.inss}", BeIDFactory.getBeIDInfo("test", false).getIdentity().getNationalNumber());
            persistToken = persistToken.Replace("${hok}", hok);

            Element samlToken = ConnectorXmlUtils.toElement(Encoding.UTF8.GetBytes(persistToken));

            int expected = samlToken.getElementsByTagNameNS(XMLNS_SAML, "Attribute").getLength();


            for (int i = 0; i < 5; i++)
            {
                samlToken = service.renewToken(headerCred, bodyCred, samlToken, 24);
            }

            Assert.AreEqual(expected, samlToken.getElementsByTagNameNS(XMLNS_SAML, "Attribute").getLength());
        }

        [Test]
        public void issueEid()
        {
            var headerCred = BeIDCredential.getInstance("session", BeIDCredential.EID_AUTH_ALIAS);

            string keystoreloc = ConfigFactory.getConfigValidator().getConfig().getProperty("test.keystore.location");
            string alias = ConfigFactory.getConfigValidator().getConfig().getProperty("test.keystore.alias");
            string pwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.keystore.password");
            string pkpwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.keystore.privatekeypassword");

            KeyStoreCredential bodyCred = new KeyStoreCredential(keystoreloc, pwd, alias, pkpwd);

            string inss = BeIDFactory.getBeIDInfo("session", false)
                                        .getIdentity()
                                        .getNationalNumber();

            java.util.List designators = new java.util.ArrayList();
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:ehealth:1.0:certificateholder:person:ssin", "urn:be:fgov:identification-namespace"));
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:person:ssin", "urn:be:fgov:identification-namespace"));

            // create the list of SAML Attributes
            java.util.List attributes = new java.util.ArrayList();
            attributes.add(new SAMLAttribute("urn:be:fgov:person:ssin", "urn:be:fgov:identification-namespace", inss));
            attributes.add(new SAMLAttribute("urn:be:fgov:ehealth:1.0:certificateholder:person:ssin", "urn:be:fgov:identification-namespace", inss));

            service.getToken(headerCred, bodyCred, attributes, designators, getHolderOfKeyMethod(), 24);
        }

        [Test]
        public void IssueEidOnly()
        {
            Credential headerCred = BeIDCredential.getInstance("session", BeIDCredential.EID_AUTH_ALIAS);
            Credential bodyCred = headerCred;

            string inss = BeIDFactory.getBeIDInfo("session", false)
                                   .getIdentity()
                                   .getNationalNumber();

            java.util.List designators = new java.util.ArrayList();
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:ehealth:1.0:certificateholder:person:ssin", "urn:be:fgov:identification-namespace"));
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:person:ssin", "urn:be:fgov:identification-namespace"));

            // create the list of SAML Attributes
            java.util.List attributes = new java.util.ArrayList();
            attributes.add(new SAMLAttribute("urn:be:fgov:person:ssin", "urn:be:fgov:identification-namespace", inss));
            attributes.add(new SAMLAttribute("urn:be:fgov:ehealth:1.0:certificateholder:person:ssin", "urn:be:fgov:identification-namespace", inss));


            service.getToken(headerCred, bodyCred, attributes, designators, getHolderOfKeyMethod(), 24);
        }

        [Test]
        public void issueSVEid()
        {
            string keystoreloc = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.location");
            string alias = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.alias");
            string pwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.password");
            string pkpwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.privatekeypassword");
            var cred = new KeyStoreCredential(keystoreloc, pwd, alias, pkpwd);

            string inss = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.user");

            java.util.List designators = new java.util.ArrayList();
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:ehealth:1.0:certificateholder:person:ssin", "urn:be:fgov:identification-namespace"));
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:person:ssin", "urn:be:fgov:identification-namespace"));
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:person:ssin:ehealth:1.0:doctor:nihii11", "urn:be:fgov:certified-namespace"));
            // create the list of SAML Attributes
            java.util.List attributes = new java.util.ArrayList();
            attributes.add(new SAMLAttribute("urn:be:fgov:person:ssin", "urn:be:fgov:identification-namespace", inss));
            attributes.add(new SAMLAttribute("urn:be:fgov:ehealth:1.0:certificateholder:person:ssin", "urn:be:fgov:identification-namespace", inss));

            service.getToken(cred, cred, attributes, designators, "test", "urn:be:fgov:ehealth:connector:gui", inss, getSenderVouchesMethod(), 24);
        }

        [Test]
        public void issueTokenForMandator()
        {
            string keystoreloc = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.location");
            string alias = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.alias");
            string pwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.password");
            string pkpwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.privatekeypassword");

            var headerCred = new KeyStoreCredential(keystoreloc, pwd, alias, pkpwd);
            var bodyCred = headerCred;

            string cbe = "0809394427";

            java.util.List designators = new java.util.ArrayList();
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:ehealth:1.0:certificateholder:enterprise:cbe-number", "urn:be:fgov:identification-namespace"));
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:kbo-bce:organization:cbe-number", "urn:be:fgov:identification-namespace"));
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:ehealth:1.0:servicename:external", "urn:be:fgov:identification-namespace"));
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:kbo-bce:organization:cbe-number:ehealth:1.0:recognisedmandatary:boolean", "urn:be:fgov:certified-namespace:ehealth"));

            // create the list of SAML Attributes
            java.util.List attributes = new java.util.ArrayList();
            attributes.add(new SAMLAttribute("urn:be:fgov:ehealth:1.0:certificateholder:enterprise:cbe-number", "urn:be:fgov:identification-namespace", cbe));
            attributes.add(new SAMLAttribute("urn:be:fgov:kbo-bce:organization:cbe-number", "urn:be:fgov:identification-namespace", cbe));
            attributes.add(new SAMLAttribute("urn:be:fgov:ehealth:1.0:servicename:external", "urn:be:fgov:identification-namespace", "insurability"));


            service.getToken(headerCred, bodyCred, attributes, designators, getHolderOfKeyMethod(), 24);
        }

        [Test]
        public void renewToken()
        {
            string keystoreloc = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.location");
            string alias = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.alias");
            string pwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.password");
            string pkpwd = ConfigFactory.getConfigValidator().getConfig().getProperty("test.session.sv.keystore.privatekeypassword");

            var headerCred = new KeyStoreCredential(keystoreloc, pwd, alias, pkpwd);
            var bodyCred = headerCred;

            string cbe = "0809394427";

            java.util.List designators = new java.util.ArrayList();
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:ehealth:1.0:certificateholder:enterprise:cbe-number", "urn:be:fgov:identification-namespace"));
            designators.add(new SAMLAttributeDesignator("urn:be:fgov:kbo-bce:organization:cbe-number", "urn:be:fgov:identification-namespace"));

            // create the list of SAML Attributes
            java.util.List attributes = new java.util.ArrayList();
            attributes.add(new SAMLAttribute("urn:be:fgov:ehealth:1.0:certificateholder:enterprise:cbe-number", "urn:be:fgov:identification-namespace", cbe));
            attributes.add(new SAMLAttribute("urn:be:fgov:kbo-bce:organization:cbe-number", "urn:be:fgov:identification-namespace", cbe));

            Element token = service.getToken(headerCred, bodyCred, attributes, designators, getHolderOfKeyMethod(), 24);

            Assert.AreEqual(2, token.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", "Attribute")
                .getLength());

            for (int i = 0; i < 5; i++)
            {
                token = service.renewToken(headerCred, bodyCred, token, 24);
            }

            Assert.AreEqual(2, token.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", "Attribute")
                .getLength());
        }

        protected abstract string getHolderOfKeyMethod();

        protected abstract string getSenderVouchesMethod();

        /**
         * @param service the service to set
         */
        public void setService(STSService service) {
            this.service = service;
        }

    }
}