/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
public class NFMUtilTest{
    static testMethod void testMonitoring() {
        NFMUtil.Monitoring Monitoring = new NFMUtil.Monitoring();
        NFMUtil.ControllerUtil();
    }
    
    static testMethod void testParseStr2Date() {
        Date rtn = NFMUtil.parseStr2Date(null);
        System.assertEquals(null, rtn);
        
        rtn = NFMUtil.parseStr2Date('2000');
        System.assertEquals(null, rtn);
        
        rtn = NFMUtil.parseStr2Date('2000123123');
        System.assertEquals(null, rtn);

        rtn = NFMUtil.parseStr2Date('2000AB13');
        System.assertEquals(null, rtn);

        rtn = NFMUtil.parseStr2Date('20001231');
        System.assertEquals(Date.newinstance(2000, 12, 31), rtn);

        rtn = NFMUtil.parseStr2Date('99991231');
        // System.assertEquals(Date.newinstance(4000, 12, 31), rtn);

        rtn = NFMUtil.parseStr2Date('40001231');
        // System.assertEquals(Date.newinstance(4000, 12, 31), rtn);

        rtn = NFMUtil.parseStr2Date('40010101');
        // System.assertEquals(Date.newinstance(4000, 12, 31), rtn);

        rtn = NFMUtil.parseStr2Date('00000000');
        System.assertEquals(null, rtn);

        rtn = NFMUtil.parseStr2Date('19000101');
        System.assertEquals(null, rtn);

        rtn = NFMUtil.parseStr2Date('18991231');
        System.assertEquals(null, rtn);

        rtn = NFMUtil.parseStr2Date('00000000', false);
        System.assertEquals(null, rtn);

        rtn = NFMUtil.parseStr2Date('19000101', false);
        // System.assertEquals(Date.newinstance(1900, 1, 1), rtn);

        rtn = NFMUtil.parseStr2Date('18991231', false);
        // System.assertEquals(Date.newinstance(1900, 1, 1), rtn);

        String rtn1 = NFMUtil.formatDate2StrDateTime(Date.today());
        // System.assertEquals('20211212000000', rtn1);
    }

    static testMethod void testFormatDate2Str() {
        String rtn = NFMUtil.formatDate2Str(null);
        System.assertEquals(null, rtn);

        rtn = NFMUtil.formatDate2Str(Date.newinstance(2000, 11, 22));
        System.assertEquals('20001122', rtn);

        rtn = NFMUtil.formatDate2Str(Date.newinstance(1900, 01, 02));
        System.assertEquals('19000102', rtn);

        rtn = NFMUtil.formatDate2Str(Date.newinstance(1900, 01, 01));
        System.assertEquals('19000101', rtn);

        rtn = NFMUtil.formatDate2Str(Date.newinstance(1899, 12, 31));
        System.assertEquals('19000101', rtn);

        rtn = NFMUtil.formatDate2Str(Date.newinstance(4000, 12, 30));
        System.assertEquals('40001230', rtn);

        rtn = NFMUtil.formatDate2Str(Date.newinstance(4000, 12, 31));
        System.assertEquals('99991231', rtn);

        rtn = NFMUtil.formatDate2Str(Date.newinstance(4001, 1, 1));
        System.assertEquals('99991231', rtn);
    }
    
    static testMethod void testGetMapValue() {
        BatchIF_Log__c iflog = new BatchIF_Log__c();

        Map<String, String> transferMap = new Map<String, String>();
        transferMap.put('ckey1', 'value1');

        String rtn = NFMUtil.getMapValue(null, null, null, iflog);
        System.assertEquals(null, rtn);
        
        rtn = NFMUtil.getMapValue(null, null, 'key', iflog);
        System.assertEquals('key', rtn);

        rtn = NFMUtil.getMapValue(transferMap, 'c', null, iflog);
        System.assertEquals(null, rtn);
        System.assertEquals(null, iflog.ErrorLog__c);

        rtn = NFMUtil.getMapValue(transferMap, 'c', 'key', iflog);
        System.assertEquals('key', rtn);
        System.assertEquals(true, iflog.ErrorLog__c.indexOf('Please') >= 0);

        rtn = NFMUtil.getMapValue(transferMap, 'c', 'key1', iflog);
        System.assertEquals('value1', rtn);

        rtn = NFMUtil.getMapValue(transferMap, '', 'ckey1', iflog);
        System.assertEquals('value1', rtn);

        rtn = NFMUtil.getMapValue(transferMap, null, 'ckey1', iflog);
        System.assertEquals('value1', rtn);
    }

    static testMethod void testTrimLeft() {
        String rtn = NFMUtil.trimLeft('AAAA70000', 'A');
        System.assertEquals('70000', rtn);

        rtn = NFMUtil.trimLeft('000070000', '0');
        System.assertEquals('70000', rtn);

        rtn = NFMUtil.trimLeft(' 000070000', '0');
        System.assertEquals(' 000070000', rtn);

        rtn = NFMUtil.trimLeft('0 00070000', '0');
        System.assertEquals(' 00070000', rtn);

        rtn = NFMUtil.trimLeft('', '0');
        System.assertEquals('', rtn);

        rtn = NFMUtil.trimLeft('0 00070000', null);
        System.assertEquals(null, rtn);

        rtn = NFMUtil.trimLeft(null, '0');
        System.assertEquals(null, rtn);
    }

    static testMethod void testIsSandbox() {
        Organization currOrg = [Select IsSandbox from Organization limit 1];
        Boolean urlIsSandbox = NFMUtil.isSandbox();
        System.assertEquals(currOrg.IsSandbox, urlIsSandbox);
        if (currOrg.IsSandbox) {
            // System.assertEquals('owdc_test_2018', NFMUtil.CLIENT_CERT_NAME);
        } else {
            // System.assertEquals('owdc_test_2018', NFMUtil.CLIENT_CERT_NAME);
        }
    }
    static testMethod void testIsNew00() {
        
        NFMUtil nfutil = new NFMUtil();
        Date df = Date.valueOf('2018-12-19');
        nfmutil.formatDate2Str(null);
        nfmutil.formatDate2Str(df);
        nfmutil.formatDate2StrSpo(null);
        nfmutil.formatDate2StrSpo(df);
        String rowDataStr = 'abc';
        String endpoint00 = 'NFMUtil.NFM201_ENDPOINT';
        String endpoint01 = 'NFMUtil.NFM009_ENDPOINT';
        
        Test.setMock(HttpCalloutMock.class, new NFMHttpCalloutMock());
        nfmutil.sendToSpo(rowDataStr,endpoint00);
        String str1 = nfmutil.sendToSpoRet(rowDataStr,endpoint00);
        nfmutil.sendToSap(rowDataStr,endpoint01);
        nfmutil.sendToSapRet(rowDataStr,endpoint01);
        nfmutil.sendToAWS(rowDataStr,endpoint01);
        nfmutil.sendToETQ(rowDataStr,endpoint01);
        nfmutil.sendToSapStatusAndBody(rowDataStr,endpoint01);

    }
    static testMethod void testIsNew01() {
        String endusers = '12';
        NFMUtil.Monitoring Monitoring   = new NFMUtil.Monitoring();
        Monitoring.Text = '';
        NFMUtil.makeRowData(Monitoring, 'NFM001', endusers);
    }
    static testMethod void testIsNew02() {
        
        //NFMUtil.Monitoring Monitoring   = new NFMUtil.Monitoring();
        //Monitoring.Text = '';
        //CPL003Rest.GeDatas GeDatas = new CPL003Rest.GeDatas();
        //CPL003Rest.GeData GeData = new CPL003Rest.GeData();
        //GeDatas.Inventory = new CPL003Rest.GeData[]{GeData};
        //NFMUtil.saveRowData(Monitoring, 'CPL003', GeDatas.Inventory);

        String endusers = '12';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        NFMUtil.makeRowData(iflog, 'NFM001', endusers);
       
    }
    static testMethod void TestgetRowDataStr() {
        BatchIF_Log__c rowData = null;
        String endusers = '12';
        NFMUtil.Monitoring Monitoring   = new NFMUtil.Monitoring();
        Monitoring.Text = '';
        rowData = NFMUtil.makeRowData(Monitoring, 'NFM001', endusers);
        NFMUtil.getRowDataStr(rowData);
    }

    static testMethod void testparseStr2DateTime() {
        Datetime rtn = NFMUtil.parseStr2DateTime('201812191104');
        rtn = NFMUtil.parseStr2DateTime('20181219','110401');
        rtn = NFMUtil.parseStr2DateTime('50001219','110401');
        rtn = NFMUtil.parseStr2DateTime('18001219','110401');
        //System.assertEquals(null, rtn);

        //rtn = NFMUtil.parseStr2DateTime(Date.newinstance(2000, 11, 22));
        //System.assertEquals('20001122', rtn);
    }


    static testMethod void testgetETQData() {
        Test.setMock(HttpCalloutMock.class, new NFMHttpCalloutMock());
        String rowData = '{"expires_in":"2032/12/21","access_token":"32defefe"}';
        String tempurl = NFMUtil.NFM402_ENDPOINT;
        NFMUtil.getETQData(rowData,tempurl);
    }

    static testMethod void testparseStr2TimeAndDateTimeDate() {
        NFMUtil.parseStr2Time('233233');
        NFMUtil.parseStr2Time('2233233');
        NFMUtil.parseStr2DateTimeDate('20201223');
        NFMUtil.formatDateTime2StrSprit(Date.today());
        NFMUtil.formatDateTime2StrSprit2(Date.today()); //20220419 lt add
    }

    static testMethod void testreceiveToken() {
        Test.setMock(HttpCalloutMock.class, new NFM501HttpCallMock());
        NFMUtil.receiveToken();
    }

    static testMethod void testgetQLMData501() {
        Test.setMock(HttpCalloutMock.class, new NFM501HttpCallMock());
        String rowData = '{"code":"0","data":{"cursorMark":"60d01dde42ec7ed48d3730d6","list1":[{"agentRelationName":["李蕾电"],"agentRelationWay":["12345678"],"agentUnit":["四川乾新招投标代理有限公司"],"areaCity":"惠州市","areaCountry":"","areaProvince":"广东省","biddingType":"0","bidingAcquireTime":"2021-01-13 00:00:00","bidingEndTime":"2021-01-13 00:00:00","budget":[{"amount":"250000.00","unit":"元"}],"infoFile":["http://cusdata.qianlima.com/vip/info/download/V2/eyJhbGciOiJIUzI1NiJ9.eyJpbmZvSWQiOiIyMjczMjgxOTAiLCJhcHBLZXkiOiIwNzBmMDBiZi02NGYxLTQ3MjAtYThkOC1iYmUxYWE5NzZkMjIiLCJhcHBTZWNyZXQiOiI2N0JCMkJBRkM4QUEwQkEwQ0FCQjM3Q0JGNTBFQzI5MiIsImZpbGVVcmwiOiI0QjY2Mzg2MzY4MzI0MTQyNzY2MjU5NEI0QTc0NEM1NzcxNkI2RjcyNkI1MTNEM0QifQ.3UTAGOde4plSKFKf_DV1sBWXJbdsz7zN8a1KZZys6bo"],"infoId":"227328190","infoPublishTime":"2021-06-21 09:41:26","infoQianlimaUrl":"http://www.qianlima.com/zb/detail/20210621_227328190.html","infoTitle":"皮肤镜图像处理工作站调研公告","infoType":"5","infoTypeSegment":"3","isElectronic":"0","keywords":"图像处理","openBidingTime":"2021-01-13 00:00:00","projectId":"38_99df2844cf784982acdc61d00d7a7dbb","target":{"targetDetails":[{"number1":"12","totalPrice":"2645000.00","price":"","name":"四川省雅安市芦山县人民医院抗疫特别国债购置高清胃肠镜采购项目","model":"","brand":""}]},"tenderBeginTime":"2021-01-13 00:00:00","tenderEndTime":"2021-01-13 00:00:00","winnerAmount":[{"amount":"1598000.00","unit":"元"}],"xmNumber":"CD-1624266167710","zhaoBiaoUnit":["惠州市第一人民医院","OCM","惠州市第一人民医院","惠州市第一人民医院","惠州市第一人民医院","惠州市第一人民医院"],"zhaoRelationName":["范梅红"],"zhaoRelationWay":["0752-2883625"],"zhongBiaoUnit":["成都宋庄创意科技有限公司","OCSM","成都宋庄创意科技有限公司","成都宋庄创意科技有限公司","成都宋庄创意科技有限公司","成都宋庄创意科技有限公司"],"zhongRelationName":["1234"],"zhongRelationWay":["1234567"]}]},"msg":"正确返回数据"}';
        String tempurl = NFMUtil.NFM501_ENDPOINT;
        NFMUtil.getQLMData(rowData,tempurl);
        NFMUtil.getFileData(tempurl, rowData);
    }

    static testMethod void testgetQLMData502() {
        Test.setMock(HttpCalloutMock.class, new NFM501HttpCallMock());
        String rowData = '{"code":"0","data":{"infoHtml":"<!DOCTYPE html><html>a</html>"},"msg":"正确返回数据"}';
        String tempurl = NFMUtil.NFM502_ENDPOINT;
        NFMUtil.getQLMData(rowData,tempurl);
        NFMUtil.getFileData(tempurl, rowData);
    }

    static testMethod void testZyh_1() {
        Date dt = Date.today();
        Datetime dtm = Datetime.now();
        String rtn = NFMUtil.formatDate2StrDateNewTime(dt);
        rtn = NFMUtil.formatDateTime2StrDateTime(dtm);
        Datetime parseStr2 = NFMUtil.parseStr2DateTimeDate('20230909090909');
        Datetime parseStr3 = NFMUtil.parseStr3DateTime('20230909');
        Datetime parseStr2dt = NFMUtil.parseStr2DateTime('20230909090909');
        Date parseStr2Date = NFMUtil.parseStr2Date('20230909');
        Date parseDateTimeStr2Date = NFMUtil.parseDateTimeStr2Date('20230909090909');
        String getSignMD5 = NFMUtil.getSignMD5();
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Log__c = '1213';
        iflog.retry_cnt__c = null;
        iflog.ErrorLog__c = '1';
        iflog.RowDataFlg__c = true;
        insert iflog;
        BatchIF_Log__c rowData = new BatchIF_Log__c();
        rowData.Log__c = '1213';
        rowData.retry_cnt__c = null;
        rowData.ErrorLog__c = '1';
        rowData.RowDataFlg__c = true;
        insert rowData;
        NFMUtil.againSendRequest(iflog, 'retry_cnt__c', rowData, 'Error');
        rowData = NFMUtil.QLMmakeRowData('String response', rowData);
        rowData = NFMUtil.LogAutoSend(rowData, null, 'String status');
        rowData = NFMUtil.LogAutoSend(rowData, null, 'String status', true);
        rowData = NFMUtil.updateRowData(rowData.Id, null);

        List<String> repairSoIdList = new List<String>();
        RepairSubOrder__c subRepair = new RepairSubOrder__c();
        subRepair.Name = 'test001';
        subRepair.Status__c = '待处理';
        insert subRepair;
        repairSoIdList.add(subRepair.Id);
        BatchIF_Log__c iflog613s = new BatchIF_Log__c();
        iflog613s.Type__c = 'NFM613S';
        iflog613s.Log__c = 'callout start\n';
        insert iflog613s;
        NFM613Controller.callout(iflog613s.Id, repairSoIdList);
        NFM613Controller.callout(null, repairSoIdList);


        Test.startTest();
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        String JsonMsg = '{"Monitoring":{"MessageGroupNumber":"1668147552","Receiver":"SFDC","Text":"ONLINE","TransmissionDateTime":"1668147552","NumberOfRecord":"1668147552","Sender":"ONLINE","Tag":"ONLINE","MessageType":"NFM624"},"GeData":[{"UpsertContacts":[{"UnifiedIContactID":"1064598998507061248","Type":"*****","ServicePlatformCode":"","RegSource":"智慧医疗","PlatformDisabledRepresentation":false,"OwnerId":"00510000003MkTbAAK","MobilePhone":"***********","LastName":"***","Isactive":"有效","IgnoreSameName":true,"id":"0039D00000LY5n5QAD","FirstName":"","errorMsg":"","ContactAddress":"","AWSDataId":"1064594026553933825","ApproveDate":"2023-01-16","AgentFlag":false,"AccountId":"0019D00000S0H1LQAV"}],"UpsertAccounts":[{"StateText":null,"StateMaster":null,"RecordTypeId":null,"PlatformCode":"2626","ParentId":null,"OwnerId":null,"OCMCategory":null,"Name":null,"id":"0019D00000S0H1LQAV","HospitalSource":null,"Hospital":"0019D00000S0GiKQAV","DepartmentName":null,"DepartmentClass":null,"CityMaster":null}],"SFDCCodes":["8104136","8104146"],"rowDataId":null,"reCallNfm624RequestId":"1064598998020521984","NFM624SecondaryProcessing":true,"nfm624RequestId":"","Managements":["2625","2626"],"logstr":"0116Lu的新医院普外科0116Lu的新医院 普外科 普外科","isError":0,"ContactMap":{"2614":{"UnifiedIContactID":"1064542048599670784","Type":"*****","ServicePlatformCode":"","RegSource":"智慧医疗","PlatformDisabledRepresentation":false,"OwnerId":null,"MobilePhone":"***********","LastName":"***","Isactive":"有效","IgnoreSameName":true,"id":"0039D00000LVKS9QAP","FirstName":"","errorMsg":"","ContactAddress":"","AWSDataId":"1064541177216237569","ApproveDate":"2023-01-16","AgentFlag":false,"AccountId":null}},"ContactId":"1064598998507061248"}]}';
        req.requestURI = 'services/apexrest/NFM624Rest2/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;
        NFM624Rest2.execute();
        Test.stopTest();
        // String sendTenInfo = NFMUtil.sendTenInfo('String token504', 'String jsonStr', 'String endpoint');
        // NFMUtil.response sendToPiAWS = NFMUtil.sendToPiAWS('String rowDataStr', 'String endpoint', 'String awsToken');
        // NFMUtil.response getAwsToken = NFMUtil.getAwsToken();
        // NFMUtil.response getAWSQLMData1 = NFMUtil.getAWSQLMData('String endpoint', 'String token');
        // NFMUtil.response getAWSQLMData2 = NFMUtil.getAWSQLMData('String endpoint', 'String jsonStr', 'String token');
    }


    static testMethod void testZyh_2(){
        Campaign cam = new Campaign();
        cam.Name = 'test campaign';
        cam.StartDate = Date.today().addDays(15);
        cam.EndDate = Date.today().addDays(18);
        cam.Name2__c = '1234';
        cam.Status = '申请中';
        cam.Mailflg_after45__c = true;
        cam.Mailflg_cancel__c = true;
        cam.Mailflg_before15__c = true;
        cam.Mailflg_before7__c = true;
        cam.Mailflg_after3__c = true;
        cam.HostName__c = '1';
        cam.cooperatorCompany__c = '1';
        insert cam;
        List<String> qisIdList = new List<String>();
        qisIdList.add(cam.Id);
        if (qisIdList.size() > 0) {
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM622';
            iflog.Log__c  = 'callout start\n';
            insert iflog;           
            NFM622Controller.callout(iflog.Id, qisIdList);
        }
    }
    // static testMethod void testgetFileData503() {
    //     Test.setMock(HttpCalloutMock.class, new NFM501HttpCallMock());
    //     String rowData = '{"expires_in":"2032/12/21","access_token":"32defefe"}';
    //     String tempurl = NFMUtil.NFM501_ENDPOINT;
    //     NFMUtil.getFileData(rowData,tempurl);
    // }




}