/*
 * Copyright (c) Smals
 */
package be.ehealth.businessconnector.genericasync.mappers;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.unitils.UnitilsJUnit4TestClassRunner;
import org.unitils.reflectionassert.ReflectionAssert;
import org.unitils.reflectionassert.ReflectionComparatorMode;

import be.cin.mycarenet.esb.common.v2.CareProviderType;
import be.cin.mycarenet.esb.common.v2.IdType;
import be.cin.mycarenet.esb.common.v2.LicenseType;
import be.cin.mycarenet.esb.common.v2.NihiiType;
import be.cin.mycarenet.esb.common.v2.OrigineType;
import be.cin.mycarenet.esb.common.v2.PackageType;
import be.cin.mycarenet.esb.common.v2.ValueRefString;
import be.ehealth.business.mycarenetcommons.domain.CareProvider;
import be.ehealth.business.mycarenetcommons.domain.CommonInput;
import be.ehealth.business.mycarenetcommons.domain.Identification;
import be.ehealth.business.mycarenetcommons.domain.Nihii;
import be.ehealth.business.mycarenetcommons.domain.Origin;
import be.ehealth.technicalconnector.config.util.domain.PackageInfo;

/**
 * tests the mapping of the commonInput mycarenet object to the project specific common input jaxb object.
 * 
 * @author EHP
 * 
 */
@RunWith(UnitilsJUnit4TestClassRunner.class)
public class CommonInputMapperTest {

    private static final String SSIN = "ssin";

    private static final String NAME1 = "name1";

    private static final String SSIN_VALUE = "123456789";

    private static final String QUALITY = "quality";

    private static final String SITE_ID = "siteId";

    private static final String PACKAGE_NAME = "name";

    private static final String LICENSE_PASSWORD = "password";

    private static final String LICENSE_USERNAME = "userName";

    private static final String INPUTREFERENCE = "inputRefernce";

    private static CareProvider myCarenetCommons_careProvider;

    private static PackageInfo myCarenetCommons_packageInfo;


    @BeforeClass
    public static void init() {
        myCarenetCommons_careProvider = new CareProvider(new Nihii(QUALITY, SSIN_VALUE));
        myCarenetCommons_careProvider.setOrganization(new Identification(NAME1, null, SSIN, null));
        myCarenetCommons_careProvider.setPhysicalPerson(new Identification(NAME1, null, SSIN, null));
        myCarenetCommons_packageInfo = new PackageInfo(LICENSE_USERNAME, LICENSE_PASSWORD, PACKAGE_NAME);
    }

    @Test
    public void testMappingOrigin() throws Exception {
        OrigineType expected = new OrigineType();
        expected.setCareProvider(createCareProvider());
        expected.setPackage(createPackage(PACKAGE_NAME, LICENSE_PASSWORD, LICENSE_USERNAME));
        expected.setSiteID(createValueRefString(SITE_ID));

        Origin origin = new Origin(myCarenetCommons_packageInfo, SITE_ID, null);
        origin.setCareProvider(myCarenetCommons_careProvider);

        OrigineType actual = CommonInputMapper.mapOrigin(origin);

        ReflectionAssert.assertReflectionEquals(expected, actual, ReflectionComparatorMode.IGNORE_DEFAULTS);
    }

    @Test
    public void testMappingCommonInput() {
        Origin origin = new Origin(myCarenetCommons_packageInfo, myCarenetCommons_careProvider);
        CommonInput commonInput = new CommonInput(true, origin, INPUTREFERENCE);

        be.cin.mycarenet.esb.common.v2.CommonInput actual = CommonInputMapper.mapCommonInputType(commonInput);

        be.cin.mycarenet.esb.common.v2.CommonInput expected = new be.cin.mycarenet.esb.common.v2.CommonInput();
        expected.setInputReference(INPUTREFERENCE);
        expected.setNIPReference(null);
        OrigineType expectedOrigin = new OrigineType();
        PackageType expectedPackageType = createPackage(PACKAGE_NAME, LICENSE_PASSWORD, LICENSE_USERNAME);
        expectedOrigin.setPackage(expectedPackageType);
        CareProviderType expectedCareProvider = createCareProvider();
        expectedOrigin.setCareProvider(expectedCareProvider);
        expected.setOrigin(expectedOrigin);
        ReflectionAssert.assertReflectionEquals(expected, actual, ReflectionComparatorMode.IGNORE_DEFAULTS);
    }

    private PackageType createPackage(String packageName, String licensePassword, String licenceUsername) {
        PackageType expectedPackageType = new PackageType();
        ValueRefString packageNameValueRef = createValueRefString(packageName);
        expectedPackageType.setName(packageNameValueRef);
        LicenseType licenseType = new LicenseType();
        licenseType.setPassword(licensePassword);
        licenseType.setUsername(licenceUsername);
        expectedPackageType.setLicense(licenseType);
        return expectedPackageType;
    }

    private CareProviderType createCareProvider() {
        CareProviderType expectedCareProvider = new CareProviderType();
        expectedCareProvider.setNihii(createNihiiType(myCarenetCommons_careProvider.getNihii().getQuality(), myCarenetCommons_careProvider.getNihii().getValue()));
        IdType physicalPerson = new IdType();
        physicalPerson.setName(createValueRefString(myCarenetCommons_careProvider.getPhysicalPerson().getName()));
        physicalPerson.setSsin(createValueRefString(myCarenetCommons_careProvider.getPhysicalPerson().getSsin()));
        IdType organisation = physicalPerson;
        organisation.setName(createValueRefString(myCarenetCommons_careProvider.getOrganization().getName()));
        organisation.setSsin(createValueRefString(myCarenetCommons_careProvider.getOrganization().getSsin()));
        expectedCareProvider.setOrganization(organisation);
        expectedCareProvider.setPhysicalPerson(physicalPerson);
        return expectedCareProvider;
    }

    public ValueRefString createValueRefString(String value) {
        ValueRefString packageNameValueRef = new ValueRefString();
        packageNameValueRef.setValue(value);
        return packageNameValueRef;
    }

    private NihiiType createNihiiType(String quality, String value) {
        NihiiType nihii = new NihiiType();
        nihii.setQuality(quality);
        ValueRefString valueRefStr = new ValueRefString();
        valueRefStr.setValue(value);
        nihii.setValue(valueRefStr);
        return nihii;
    }

}
