/**
 * 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 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);
    // }




}