@isTest
private class SBG200RestTest {

    public static Account acc1;
    public static Account acc2;
    public static Account acc3;
    public static Account acc4;

    public static Contact core;
    public static License_Information__c licenseInformation;

    static void inittransfersData() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account acc1 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            PostCode__c = '123456',
            ManagementCode_Ext__c = '5111'
        );
        Account acc2 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            PostCode__c = '123456',
            ManagementCode_Ext__c = '5112'
        );
        Account acc3 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            CancelReason__c = 'test',
            PostCode__c = '123456',
            ManagementCode_Ext__c = '5003'
        );

        Account acc4 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            PostCode__c = '123456',
            ManagementCode_Ext__c = '300220'
        );

        insert new List<Account> {acc1, acc2, acc3,acc4};

        License_Information__c licenseInformation = new License_Information__c();
        licenseInformation.LicenseType__c = '辐射安全许可证' ;
		licenseInformation.BusinessLicense__c = 'TestNo20191216' ;
		licenseInformation.ValidFrom__c = Date.today().addDays(-22);
		licenseInformation.ValidTo__c = Date.today().addDays(22);
		licenseInformation.LicenseAndAccount__c = acc1.Id;
		licenseInformation.Name = 'test01'+'辐射安全许可证';

		insert licenseInformation;

        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Account';
        transfer.Column__c         = 'Dealer_Type__c';
        transfer.External_Value__c = '22';
        transfer.Internal_Value__c = 'Authorize dealer';
        transfers.add(transfer);

        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'License_Information__c';
        transfer.Column__c         = 'LicenseType__c';
        transfer.External_Value__c = '01';
        transfer.Internal_Value__c = '营业执照';
        transfers.add(transfer);

        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'License_Information__c';
        transfer.Column__c         = 'LicenseType__c';
        transfer.External_Value__c = '08';
        transfer.Internal_Value__c = '辐射安全许可证';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'CertificationDetails__c';
        transfer.Column__c         = 'ProdustionType__c';
        transfer.External_Value__c = 'D';
        transfer.Internal_Value__c = 'Delta';
        transfers.add(transfer);

        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'CertificationDetails__c';
        transfer.Column__c         = 'ProdustionType__c';
        transfer.External_Value__c = 'V';
        transfer.Internal_Value__c = 'Venta';
        transfers.add(transfer);

        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'CertificationDetails__c';
        transfer.Column__c         = 'ProdustionType__c';
        transfer.External_Value__c = 'DV';
        transfer.Internal_Value__c = 'Delta+Venta';
        transfers.add(transfer);

        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'CertificationDetails__c';
        transfer.Column__c         = 'ProdustionType__c';
        transfer.External_Value__c = 'F';
        transfer.Internal_Value__c = 'FOX-IQ';
        transfers.add(transfer);

        insert transfers;

        core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=acc1.id,ManagementCode_Ext__c = 'C100300X');
        insert core;

    }

    static testMethod void testMethod1() {
        inittransfersData();
        SBG200Rest.GeDatas GeDatas = new  SBG200Rest.GeDatas();
        SBG200Rest.GeData GeData = new  SBG200Rest.GeData();
        SBG200Rest.License_Info License_Info01 = new  SBG200Rest.License_Info();
        SBG200Rest.Detail_Info Detail_Info01 = new SBG200Rest.Detail_Info();
        SBG200Rest.Detail_Info Detail_Info05 = new SBG200Rest.Detail_Info();
        GeDatas.Partners = new  SBG200Rest.GeData[] {GeData};
        GeData.License_Info = new  SBG200Rest.License_Info[] {License_Info01};
        License_Info01.Detail_Info = new SBG200Rest.Detail_Info[] {Detail_Info01,Detail_Info05};
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';

        GeData.CompanyCode = '';
        GeData.IfControl = false;

        License_Info01.LicenseType = '08';
        License_Info01.BusLicenseNo = '20161101';
        License_Info01.ValidFrom = '20180522';
        License_Info01.ValidTo = null;
        Detail_Info01.ProName = 'test01';
        Detail_Info01.ProSerial = 'V';
        Detail_Info01.Quantity = '222';
        Detail_Info01.Category = '';

        Detail_Info05.ProName = 'test02';
        Detail_Info05.ProSerial = 'DV';
        Detail_Info05.Quantity = '222';
        Detail_Info05.Category = '';


        System.Test.startTest();
        NFMUtil.Monitoring Monitoring = GeDatas.Monitoring;
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'SBG200', GeDatas.Partners);
        SBG200Rest.main(rowData.Id);

        GeData.CompanyCode = '5111';
        rowData = NFMUtil.saveRowData(Monitoring, 'SBG200', GeDatas.Partners);
        SBG200Rest.main(rowData.Id);

        License_Info01.ValidTo = '20220522';
        rowData = NFMUtil.saveRowData(Monitoring, 'SBG200', GeDatas.Partners);
        SBG200Rest.main(rowData.Id);

        Detail_Info01.Category = '销售';
        Detail_Info05.Category = '使用';
        rowData = NFMUtil.saveRowData(Monitoring, 'SBG200', GeDatas.Partners);
        SBG200Rest.main(rowData.Id);

        License_Info01.LicenseType = '22';
        rowData = NFMUtil.saveRowData(Monitoring, 'SBG200', GeDatas.Partners);
        SBG200Rest.main(rowData.Id);

        License_Info01.LicenseType = '08';
        Detail_Info01.ProSerial = 'Z';
        rowData = NFMUtil.saveRowData(Monitoring, 'SBG200', GeDatas.Partners);
        SBG200Rest.main(rowData.Id);

        License_Info01.LicenseType = '08';
        Detail_Info01.ProSerial = 'F';
        Detail_Info01.ProName = '2018052201201805220120180522012018052201201805220120180522012018052201201805220120180522012018052201' +
        						'2018052201201805220120180522012018052201201805220120180522012018052201201805220120180522012018052201' +
        						'2018052201201805220120180522012018052201201805220120180522012018052201201805220120180522012018052201';
        rowData = NFMUtil.saveRowData(Monitoring, 'SBG200', GeDatas.Partners);
        SBG200Rest.main(rowData.Id);
        System.Test.stopTest();
    }

    @isTest
    static void testMethod4() {
        //测试 SBG200Rest.execute();
        Test.startTest();

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"SBG200","MessageGroupNumber":"20180001722190"},"Partners":[{"CompanyCode":"5111","IfControl":true,"License_Info":[{"Detail_Info":[{"ProName":"Test01","ProSerial":"D","Quantity":"22","Category":"销售"},{"ProName":"Test02","ProSerial":"V","Quantity":"222","Category":"销售"}],"LicenseType":"08","ValidTo":"20191118","ValidFrom":"20191104","BusLicenseNo":"磊洲凤科336号"},{"Detail_Info":null,"LicenseType":"01","ValidTo":"99991231","ValidFrom":"20171117","BusLicenseNo":"91510100LEIZHOU"}]}]}';
        req.requestURI = 'services/apexrest/SBG200/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response = res;

        SBG200Rest.execute();

        Test.stopTest();
    }
}