/**
 * 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
private class ContentPreviewControllerTest {

    static testMethod void myUnitTest() {
        Document doc1 = new Document(
            Name = '20151111_testdoc1_test1',
            DeveloperName = 'testdoc1',
            // The FolderId lookup on Document is actually a polymorphic field that can lookup to a Folder or a User 
            FolderId = UserInfo.getUserId(),
            Body = EncodingUtil.base64Decode('test1')
        );
        Document doc2 = new Document(
            Name = '20151111_testdoc2_test2',
            DeveloperName = 'testdoc2',
            // The FolderId lookup on Document is actually a polymorphic field that can lookup to a Folder or a User 
            FolderId = UserInfo.getUserId(),
            Body = EncodingUtil.base64Decode('test2')
        );
        Document doc3 = new Document(
            Name = '20151111_testdoc3_test3',
            DeveloperName = 'testdoc3',
            // The FolderId lookup on Document is actually a polymorphic field that can lookup to a Folder or a User 
            FolderId = UserInfo.getUserId(),
            Body = EncodingUtil.base64Decode('test3')
        );
        insert new Document[] {doc1, doc2, doc3};
        
        RecordType rt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        Account hp1 = new Account(
            RecordTypeId = rt.Id,
            Name = 'test hp'
            //AnalysisContentId__c = doc1.Id + ',' + doc2.Id + ',' + doc3.Id + ',' + doc1.Id + ',' + doc2.Id + ',' + doc3.Id + ',' + doc3.Id
        );
        Account hp2 = new Account(
            RecordTypeId = rt.Id,
            Name = 'test hp2'
        );
        insert new Account[] { hp1, hp2 };
        
        Account2__c acc1 = [select Id from Account2__c where Account__c = :hp1.Id];
        acc1.AnalysisContentId__c = doc1.Id + ',' + doc2.Id + ',' + doc3.Id + ',' + doc1.Id + ',' + doc2.Id + ',' + doc3.Id + ',' + doc3.Id;
        acc1.AnalysisContentDate__c = Date.today();
        update acc1;
        //Account2__c acc12 = [select Id from Account2__c where Account__c = :hp2.Id];
        //acc2.AnalysisContentId__c = doc1.Id + ',' + doc2.Id + ',' + doc3.Id + ',' + doc1.Id + ',' + doc2.Id + ',' + doc3.Id + ',' + doc3.Id;
        //acc2.AnalysisContentDate__c = Date.today();
        
        PageReference page = new PageReference('/apex/ContentPreview?id=' + hp2.Id);
        System.Test.setCurrentPage(page);
        ContentPreviewController cpc = new ContentPreviewController();
        cpc.init();
       // System.assertEquals(true, cpc.noDoc);
        
        page = new PageReference('/apex/ContentPreview?id=' + hp1.Id);
        System.Test.setCurrentPage(page);
        cpc = new ContentPreviewController();
        cpc.init();
        // System.assertEquals(doc3.Id, cpc.xlsId);
        
        Map<String, Object> docMap = ContentPreviewController.getDocumentBody(doc1.Id);
        System.assertEquals('testdoc1_test1', docMap.get('name'));
        
        ContentPreviewController.saveComment('subject', 'comment', doc1.Id, hp1.Id);
        
        cpc.docId = doc1.Id;
        cpc.dummyReport.OwnerId = UserInfo.getUserId();
        cpc.sendDlRequest();
        List<BatchIF_Log__c> bList = [select Id, Log__c from BatchIF_Log__c where Type__c = 'DlRequest'];
        System.assertEquals(1, bList.size());
        System.assertEquals('test hp', bList[0].Log__c);
        
        cpc.sendRefRequest();
        bList = [select Id, Log__c from BatchIF_Log__c where Type__c = 'RefRequest' and Account__c = :hp1.Id];
        System.assertEquals(1, bList.size());
    }
}