@isTest
private class NFM204RestTest {


    @testSetup
    static void makeTestRepair() {
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'Prd1';
        prd1.ProductCode            = 'Prd1';
        prd1.Repair_Product_Code__c = 'Prd1_RP';
        prd1.Name                   = 'Prd1';
        prd1.Manual_Entry__c        = false;
        prd1.Category2__c = '本体';
        prd1.Category3__c = '纤维镜';
        prd1.Category4__c = 'CV';
        prd1.Category5__c = '260SL系列';
        prd1.Asset_Model_No__c = '1001';
        insert prd1;

        // 产品注册证
        Product_Register__c prc = new Product_Register__c();
        prc.Name = 'SFDC测试用2018001';
        prc.MD5EncryptionKey__c = '53E322978D1B614830580290534EFB48';
        insert prc;

        Product_Register__c prc2 = new Product_Register__c();
        prc2.Name = 'FYL';
        prc2.MD5EncryptionKey__c = '071AD0CE5B2FA631EAFD086A814D461A';
        insert prc2;

        // 产品-注册证关系
        Product_Register_Link__c prl = new Product_Register_Link__c();
        prl.Product_Register__c = prc.id;
        prl.Product2__c = prd1.id;
        insert prl;

        // IF转送表
        BatchIF_Transfer__c trans1 = new BatchIF_Transfer__c();
        trans1.Table__c = 'Product2';
        trans1.Column__c = 'Product_Status__c';
        trans1.External_Value__c = '20';
        trans1.Internal_Value__c = '正常销售';
        insert trans1;

        BatchIF_Transfer__c trans2 = new BatchIF_Transfer__c();
        trans2.Table__c = 'Product_Register__c';
        trans2.Column__c = 'RegisterNoStatus__c';
        trans2.External_Value__c = '20';
        trans2.Internal_Value__c = '有效';
        insert trans2;
    }
	
    @isTest 
    static void test_method_one() {
        NFM204Rest.GeDatas GeDatas = new NFM204Rest.GeDatas();
        NFM204Rest.GeData GeData = new NFM204Rest.GeData();
        GeDatas.GeData = new NFM204Rest.GeData[]{GeData};

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        GeDatas.Monitoring = new NFMUtil.Monitoring();
        GeDatas.Monitoring.MessageGroupNumber = nowStr + '01';

        // 其余产品注册证
        GeData.MaterialNo = 'Prd1';
        GeData.MaterialStatus = '20';
        GeData.RegisterNo = 'SFDC测试用2018001'; 
        GeData.RegisterNoStatus = '20';
        GeData.ValidFrom = '20180522';
        GeData.ValidTo = '20220521';
        GeData.MaterialStatusType = null;
        GeData.PrdCompanyAddr = '1111111111111111111111111111111111111111111111111111111111111111111';
        GeData.PrdCompanyAddr += '2222222222222222222222222222222222222222222222222222222222222222222';
        GeData.PrdCompanyAddr += '3333333333333333333333333333333333333333333333333333333333333333333';
        GeData.PrdCompanyAddr += '4444444444444444444444444444444444444444444444444444444444444444444';

        NFMUtil.Monitoring Monitoring = GeDatas.Monitoring;
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM204', GeDatas.GeData);
        NFM204Rest.executefuture(rowData.Id);


        // 测试FYL产品注册证
        GeData.MaterialNo = 'Prd1';
        GeData.MaterialStatus = '20';
        GeData.RegisterNo = 'FYL'; 
        GeData.RegisterNoStatus = '50';
        GeData.ValidFrom = null;
        GeData.MaterialStatusType = null;
        GeData.PrdCompanyAddr = '1111111111111111111111111111111111111111111111111111111111111111111';
        GeData.PrdCompanyAddr += '2222222222222222222222222222222222222222222222222222222222222222222';
        GeData.PrdCompanyAddr += '3333333333333333333333333333333333333333333333333333333333333333333';
        GeData.PrdCompanyAddr += '4444444444444444444444444444444444444444444444444444444444444444444';


        rowData = NFMUtil.saveRowData(Monitoring, 'NFM204', GeDatas.GeData);
        NFM204Rest.executefuture(rowData.Id);

        // 测试异常
        GeData.MaterialNo = 'Prd1';
        GeData.MaterialStatus = '20';
        GeData.RegisterNo = 'SFDC测试用2018001'; 
        GeData.RegisterNoStatus = '50';
        GeData.ValidFrom = 'ZZZZ';
        GeData.MaterialStatusType = null;


        rowData = NFMUtil.saveRowData(Monitoring, 'NFM204', GeDatas.GeData);
        NFM204Rest.executefuture(rowData.Id);
        //System.Test.stopTest();

    }                                      
	
    @isTest static void test_method_two() {
        Test.startTest();
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        // Product2 prd1 = new Product2();
        // prd1.ProductCode_Ext__c     = 'Prd1';
        // prd1.ProductCode            = 'Prd1';
        // prd1.Repair_Product_Code__c = 'Prd1_RP';
        // prd1.Name                   = 'Prd1';
        // prd1.Manual_Entry__c        = false;
        // prd1.Category2__c = '本体';
        // prd1.Category3__c = '纤维镜';
        // prd1.Category4__c = 'CV';
        // prd1.Category5__c = '260SL系列';
        // prd1.Asset_Model_No__c = '1001';
        // insert prd1;
        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"NFM204","MessageGroupNumber":"20180001722190"},"GeData":[{"Z3PLAuthorizedNo":"","Z3PLAuthorized":"","YXQX":"1年","ValidTo":null,"ValidFrom":null,"Unit":null,"TransCondition":null,"StroageCondition":null,"RegisterNoStatus":"50","RegisterNoClass_Old":"","RegisterNoClass_New":"","RegisterNo":"FYL","ProductName":"SSC6024 : Sof-Curl 输尿管支架管 6Fr.X 24cm","PrdValidTo":null,"PrdValidFrom":null,"PrdCompanyLicense":"","PrdCompanyAddr":"","Other3":null,"Other2":null,"Other1":null,"Model":"SSC6024 ","MedPrdClass":"","MaterialStatusType":null,"MaterialStatus":"10","MaterialNo":"SSC6024","MaterialGroup":null,"BusinessScope":"","AuthorizedCompany_SH":"","AuthorizedCompany":"","ApprovedDate_SH":null,"ApprovedDate":null}]}';
        req.requestURI = 'services/apexrest/NFM204/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM204Rest.execute();

        Test.stopTest();
    }

    @isTest static void test_method_three() {
        Test.startTest();
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        // Product2 prd1 = new Product2();
        // prd1.ProductCode_Ext__c     = 'Prd1';
        // prd1.ProductCode            = 'Prd1';
        // prd1.Repair_Product_Code__c = 'Prd1_RP';
        // prd1.Name                   = 'Prd1';
        // prd1.Manual_Entry__c        = false;
        // prd1.Category2__c = '本体';
        // prd1.Category3__c = '纤维镜';
        // prd1.Category4__c = 'CV';
        // prd1.Category5__c = '260SL系列';
        // prd1.Asset_Model_No__c = '1001';
        // insert prd1;
        String JsonMsg = '{"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"NFM204","MessageGroupNumber":"20180001722190"},"GeData":[{"Z3PLAuthorizedNo":null,"Z3PLAuthorized":null,"YXQX":null,"ValidTo":null,"ValidFrom":null,"Unit":null,"TransCondition":null,"STRU_COM":null,"StroageCondition":null,"RegisterNoStatus":null,"RegisterNoClass_Old":null,"RegisterNoClass_New":null,"RegisterNo":null,"REG_Name":null,"REG_ADD":null,"ProductName":"70138002 : 平滑刀头","PROD_ADD":null,"PreDiscontinuedDatetime":"20240522","PrdValidTo":null,"PrdValidFrom":null,"PrdCompanyLicense":null,"PrdCompanyAddr":null,"Other3":null,"Other2":null,"Other1":null,"Model":"70138002","MedPrdClass":null,"MaterialStatusType":"1","MaterialStatus":"30","MaterialNo":"Prd1","MaterialGroup":null,"IsPrediscontinued":"1","ClinicalCode":"","BusinessScope":null,"AuthorizedCompany_SH":null,"AuthorizedCompany":null,"ApprovedDate_SH":null,"ApprovedDate":null,"APP_RSLT_SH":null,"APP_RSLT_OSH":null,"APP_RSLT_GZ":null,"APP_RSLT_BJ":null,"APP_NAME_SH":null,"APP_NAME_OSH":null,"APP_NAME_GZ":null,"APP_NAME_BJ":null,"APP_DATE_SH":null,"APP_DATE_OSH":null,"APP_DATE_GZ":null,"APP_DATE_BJ":null,"ADOPT_RANGE":null}]}';
        req.requestURI = 'services/apexrest/NFM204/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM204Rest.execute();

        Test.stopTest();
    }
	
}