@isTest
private class SSOpportunityBatch2Test {

    private static Product2 product = null;
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;

    static {
        product = new Product2( Name='テスト商品');
        insert product;
    }

    private static PricebookEntry buildPB(String input) {
        PricebookEntry entry = new PricebookEntry(Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = input;
        insert entry;
        return entry;
    }

    @isTest(SeeAllData=true)
    static void makeSqlTest() {
        // データを作って、データ移行の項目をテスト
        // テストデータ insertSSTest と同じするする
        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='出荷', CurrencyIsoCode='USD', CloseDate=Date.today());
        Opportunity opp2 = new Opportunity(Name='aiueo2', StageName='出荷', CurrencyIsoCode='CNY', CloseDate=Date.today());
        insert new Opportunity[] {opp1, opp2};
//        PricebookEntry entryUSD = buildPB('USD');       // opp1用
        PricebookEntry entryCNY = buildPB('CNY');       // opp2用
        OpportunityLineItem oli21 = new OpportunityLineItem(OpportunityId=opp2.Id, Name__c='oli21', Quantity=21, UnitPrice=100, PricebookEntryId=entryCNY.Id);
        OpportunityLineItem oli22 = new OpportunityLineItem(OpportunityId=opp2.Id, Name__c='oli22', Quantity=22, UnitPrice=200, PricebookEntryId=entryCNY.Id);
        insert new OpportunityLineItem[] {oli21, oli22};
        Id execBTId = Database.executeBatch(new SSOpportunityBatch2(new Id[] {opp1.Id}), 1);
        // ExceptionがなければOK

        // 念のため、再度 select sql の確認
        List<SS_Batch_Column_Mapping__c> settingList = SS_Batch_Column_Mapping__c.getall().values();
        for (SS_Batch_Column_Mapping__c setting : settingList) {
            String soql = SSOpportunityBatch2.makeSql(setting, new Set<String>());
            soql += ' limit 1';
            Database.query(soql);
            // ExceptionがなければOK
        }
    }

    @isTest
    static void updateOppTest() {
        // カスタム設定を定義
        SS_Batch_Column_Mapping__c oppMapping = new SS_Batch_Column_Mapping__c(Name = 'Opportunity', SS_TableName__c='SS_Prospect_5days_list__c');
        oppMapping.From_Column_001__c = 'Id';
        oppMapping.SS_Column_001__c = 'Opportunity.Name';
        oppMapping.From_Column_002__c = 'Id';                           // 重複してもエラーにはならない
        oppMapping.SS_Column_002__c = 'Opportunity.Name';
        oppMapping.From_Column_003__c = 'Forecast_last_month__c';
        oppMapping.SS_Column_003__c = 'Forecast_this_month__c';
        oppMapping.From_Column_004__c = 'Last_Shipping_Date_backup__c';
        oppMapping.SS_Column_004__c = 'Last_Shipping_Date__c';
        SS_Batch_Column_Mapping__c oliMapping = new SS_Batch_Column_Mapping__c(Name = 'OpportunityLineItem', SS_TableName__c='SS_Prospect_detail_product__c');
        insert new SS_Batch_Column_Mapping__c[] {oppMapping, oliMapping};
        // テストデータ
        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='引合', CloseDate=Date.today());
        Opportunity opp2 = new Opportunity(Name='aiueo2', StageName='引合', CloseDate=Date.today());
        insert new Opportunity[] {opp1, opp2};

        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new SSOpportunityBatch2(null), 5);
        System.Test.StopTest();
    }

    @isTest
    static void insertSSTest() {
        // カスタム設定を定義
        SS_Batch_Column_Mapping__c oppMapping = new SS_Batch_Column_Mapping__c(Name = 'Opportunity', SS_TableName__c='SS_Prospect_5days_list__c');
        oppMapping.From_Column_001__c = 'Name';
        oppMapping.SS_Column_001__c = 'Opportunity_Name__c';
        oppMapping.From_Column_002__c = 'Owner.Alias';
        oppMapping.SS_Column_002__c = 'Account_Name__c';
        oppMapping.From_Column_003__c = 'Forecast_last_month__c';
        oppMapping.SS_Column_003__c = 'Forecast_this_month__c';
        oppMapping.From_Column_004__c = 'Last_Shipping_Date_backup__c';
        oppMapping.SS_Column_004__c = 'Last_Shipping_Date__c';
        SS_Batch_Column_Mapping__c oliMapping = new SS_Batch_Column_Mapping__c(Name = 'OpportunityLineItem', SS_TableName__c='SS_Prospect_detail_product__c');
        oliMapping.From_Column_001__c = 'Name__c';
        oliMapping.SS_Column_001__c = 'Product_name__c';
        oliMapping.From_Column_002__c = 'Quantity';
        oliMapping.SS_Column_002__c = 'Number__c';
        insert new SS_Batch_Column_Mapping__c[] {oppMapping, oliMapping};

        // テストデータ
        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='引合', CurrencyIsoCode='USD', CloseDate=Date.today());
        Opportunity opp2 = new Opportunity(Name='aiueo2', StageName='引合', CurrencyIsoCode='CNY', CloseDate=Date.today());
        insert new Opportunity[] {opp1, opp2};
//        PricebookEntry entryUSD = buildPB('USD');       // opp1用
        PricebookEntry entryCNY = buildPB('CNY');       // opp2用
        OpportunityLineItem oli21 = new OpportunityLineItem(OpportunityId=opp2.Id, Name__c='oli21', Quantity=21, UnitPrice=100, PricebookEntryId=entryCNY.Id);
        OpportunityLineItem oli22 = new OpportunityLineItem(OpportunityId=opp2.Id, Name__c='oli22', Quantity=22, UnitPrice=200, PricebookEntryId=entryCNY.Id);
        insert new OpportunityLineItem[] {oli21, oli22};

        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new SSOpportunityBatch2(null), 5);
        System.Test.StopTest();
    }
}