Sunday, 18 November 2018

Testclass

/*
    Name               : AutomatedTestSetup_Controller_Test
    Apex class         : AutomatedTestSetup_Controller
    Description        : For Automated Test class execution process(SP-684-Automate Test Classes execution in Salesforce Orgs)
    Created By         : Krishna yerru
    Created Date       : 23-Oct-2018

********************************************************************************************************************************
    Change Version   Name              Date                    Task/Case#                 Description
********************************************************************************************************************************
 
********************************************************************************************************************************       
*/
@isTest
private class AutomatedTestSetup_Controller_Test {
    static Automated_Test_Run__c run;
    static Test_Runner_Job__c job;
    static list<Messaging.SingleEmailMessage> messages;
    static Messaging.SingleEmailMessage mail;
    
    static {
        Id ATERecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Execution').getRecordTypeId();
        Id ATEmilRecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Email Notification').getRecordTypeId();
        DateTime dt = System.now();
        DateTime future = dt.addMinutes(+5);
        run = new Automated_Test_Run__c(Name = 'Test Automated Run', Excluded_Classes__c = 'TestRunner, Foo',Schedule__c=future,Emails__c = 'test@test.com',RecordtypeID=ATERecordTypeId);
        insert run;
        
        job = new Test_Runner_Job__c(Name = 'Test', Automated_Test_Run__c = run.Id);
        insert job;
        
        runJobs();
    }
    
    static void runJobs() {
        messages = AutomatedTestSetup_Controller.emailUnprocessedJobStatus();
        mail = messages.size() > 0 ? messages[0] : null;
    }      

    public static testMethod void emailCreatedForAllPassing() {
        Test.startTest();
        System.assert(mail.getSubject().contains(run.Name), 'Run name not in subject');       
        set<string> emails = new set<string>();
        emails.addAll(mail.getToAddresses());
        System.assert(emails.contains(run.Emails__c), 'Run emails not in mail');        
        System.assert(!mail.getHtmlBody().contains('Test Failures'), 'Failures appear for passing tests');
        Test.Stoptest();
    }
    
    public static testMethod void emailCreatedForFailures() {
        list<ApexTestResult> results = new list<ApexTestResult>();
        Test.startTest();
        ApexTestResult result = new ApexTestResult(AsyncApexJobId = job.Id, Outcome = 'Fail', ApexClass = [select Id, Name from ApexClass limit 1], MethodName = 'TestMethod', Message  = 'Test Message', StackTrace = 'Test Stack Trace');      
        results.add(result);
        
        list<Test_Runner_Job__c> jobs = [Select t.Name, 
          t.Automated_Test_Run__r.Emails__c, 
          t.Automated_Test_Run__r.Email_on_Pass__c, 
          t.Automated_Test_Run__r.Name, 
          t.Automated_Test_Run__c 
          From Test_Runner_Job__c t where t.Id = :job.Id];
            mail = AutomatedTestSetup_Controller.emailMethodStatus(jobs, results)[0];             
        System.assert(mail.getHtmlBody().contains(result.Message), 'No message for failures');  
        Test.Stoptest();        
    }
    
    public static testMethod void emailOnPassUnchecked() {
        Test.startTest();
        run.Email_on_Pass__c = false;
        update run;        
        job = job.clone();
        insert job;        
        runJobs();        
        System.assertEquals(null, mail, 'Email created when Email on Pass unchecked');
        Test.stopTest();
    }
    
    public static testMethod void testRunnerResultsProcessesJobs() {
        Test.startTest();
        job = [select Processed__c from Test_Runner_Job__c where Id = :job.Id];
        System.assert(job.Processed__c, 'Job not marked processed');
        Test.stopTest();
    }
    
    //newly
    
  public static testMethod void TestATSrecords() {
         Id ATERecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Execution').getRecordTypeId();
        Id ATEmilRecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Email Notification').getRecordTypeId();
        DateTime dt = System.now();
        DateTime future = dt.addMinutes(+5);
        List<Automated_Test_Run__c> atsrecs=new List<Automated_Test_Run__c>();
        Automated_Test_Run__c  run1 = new Automated_Test_Run__c(Name = 'Test Automated Run', Excluded_Classes__c = 'TestRunner, Foo',Active__c = false,Emails__c = 'test@test.com',Schedule__c=future,RecordtypeID=ATERecordTypeId);
        atsrecs.add(run1);
        Automated_Test_Run__c run2 = new Automated_Test_Run__c(Name = 'Test Automated Run2',Emails__c = 'test@test.com',RecordtypeID=ATEmilRecordTypeId);
        atsrecs.add(run2);
        Test.startTest();
        insert atsrecs;     
        System.assert(atsrecs!=null);
        Test.stopTest();
    }

    
    
    
    static testMethod void testAutomatedprocess() {

        Id ATERecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Execution').getRecordTypeId();
        Id ATEmilRecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Email Notification').getRecordTypeId();
        List<Automated_Test_Run__c> ATSList= New List<Automated_Test_Run__c>();
        DateTime dt = System.now();
        DateTime future = dt.addMinutes(+5);
        
         
        Automated_Test_Run__c  run1 = new Automated_Test_Run__c(Name = 'Test Automated Run',Query__c='%Test%',Schedule_Name__c='AutoTestRunnerAdhoc254', Excluded_Classes__c = 'TestRunner, Foo',Active__c = false,Emails__c = 'test@test.com',Schedule__c=future,RecordtypeID=ATERecordTypeId);
        ATSList.add(run1);
        Automated_Test_Run__c run2 = new Automated_Test_Run__c(Name = 'Test Automated Run2',Excluded_Classes__c = 'TestRunner, Foo',Schedule_Name__c='AutoTestRunnerAdhoc254',Emails__c = 'test123@test.com',RecordtypeID=ATEmilRecordTypeId);
        ATSList.add(run2);
        
        /*Automated_Test_Run__c automated_test_run_Obj = new Automated_Test_Run__c(RecordTypeId = ATERecordTypeId,Active__c = false,Email_on_Pass__c = false, Emails__c = '18@test.com', Exclude_Managed_Packages__c = false, Excluded_Classes__c = '20', Query__c = 'Query__c828', Schedule__c = DateTime.now());
        ATSList.add(automated_test_run_Obj);
    
        Automated_Test_Run__c automated_test_run_Obj22 = new Automated_Test_Run__c(RecordTypeId = ATEmilRecordTypeId,Active__c = false,Email_on_Pass__c = false, Emails__c = '18', Exclude_Managed_Packages__c = false, Excluded_Classes__c = '20', Query__c = 'Query__c828', Schedule__c = DateTime.now());
        ATSList.add(automated_test_run_Obj22);
        */
        try{
        test.startTest();
        AutoTestRunner sh1 = new AutoTestRunner();      
        String sch = '0 0 23 * * ?';
        //system.schedule('AutoTestRunnerAdhoc254', sch, sh1);
        
        insert ATSList;
        List<Automated_Test_Run__c> ATSlist2=[SELECT Emails__c, Schedule_Name__c,RecordType.Name FROM Automated_Test_Run__c ORDER BY createdDate DESC LIMIT 1];
            system.debug('ATSlist* is'+ATSlist2);
            String scname =ATSlist2[0].Schedule_Name__c;
            system.debug('ATSlist job name is'+ATSlist2[0].Schedule_Name__c);
            ATSlist2[0].Schedule_Name__c='AutoTestRunnerAdhoc254';
            update ATSlist2;
        PageReference pageRef = Page.AutomatedTestSetup;
        Test.setCurrentPage(pageRef);
        AutomatedTestSetup_Controller controller = new AutomatedTestSetup_Controller();
        DateTime dt2 = System.now();
        DateTime future2 = dt.addMinutes(+5);
        Controller.scheduleJobs(future,'AutoTestRunnerAdhoc254');
        //Controller.scheduleJobs(dt,'AutoTestRunnerAdhoc258');
    
        AutomatedTestSetup_Controller.enqueueTests();
        Automated_Test_Run__c AtrRecord = new Automated_Test_Run__c();
        Controller.refreshHasScheduledJobs();
        Controller.getPieData();
        Controller.GetATSRunDeatils();
        Controller.Refresh();
        Controller.Calculatepercentage();
        Controller.ShowAbortbutton();
        Controller.EmailSendPending();
        Controller.email();
        
        
        Controller.CancelJobs();
        insert new Test_Runner_Job__c(Name = 'ParentJobid', Automated_Test_Run__c = run1.Id);
        Controller.AbortTestJobs();
        //Controller.AbortTestJobsEmailNotrequired();
        Controller.deleteCompletedJobs();
        Controller.getTestRunLink();
        test.StopTest(); 
        }catch(exception ex){
                
           
            }
}

public static testMethod void TestATSrecordinsertion() {
        Id ATERecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Execution').getRecordTypeId();
        Id ATEmilRecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Email Notification').getRecordTypeId();
        DateTime dt = System.now();
        DateTime future = dt.addMinutes(+5);
        Automated_Test_Run__c AtrData= new Automated_Test_Run__c();
        AtrData.Name = 'Test Automated Run';
        AtrData.Query__c = '%Test%';
        AtrData.Excluded_Classes__c = 'TestRunner, Foo';
        AtrData.Active__c = false; 
        AtrData.Emails__c= 'test@test.com';
        AtrData.Schedule__c = future;
        String ScheduleNameis='AutoTestRunnerAdhoc2588';
        AtrData.Schedule_Name__c= ScheduleNameis; 
        AtrData.RecordTypeId=ATERecordTypeId;
        test.startTest();           
        insert AtrData;
        system.assert(AtrData!=null);
        ApexPages.StandardController sc = new ApexPages.standardController(AtrData);
        AutomatedTestSetup_Controller e = new AutomatedTestSetup_Controller(sc);        
        AutomatedTestSetup_Controller sph = new AutomatedTestSetup_Controller (sc);
        PageReference pageRef = Page.AutomatedTestSetup;
        pageRef.getParameters().put('id', String.valueOf(AtrData.Id));
        Test.setCurrentPage(pageRef);
        sph.doInsert();
        test.StopTest();
}

public static testMethod void TestRecurenceATSrecordinsertion() {
        Id ATERecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Execution').getRecordTypeId();
        Id ATEmilRecordTypeId = Schema.SObjectType.Automated_Test_Run__c.getRecordTypeInfosByName().get('Automated Test Email Notification').getRecordTypeId();
        //DateTime dt = System.now();
        Automated_Test_Run__c AtrData= new Automated_Test_Run__c();
        AtrData.Name = 'Test Automated Run reccurence';
        String ScheduleNameis='AutoTestRunnerRecurrence258';
        AtrData.Query__c='%Text%';
        AtrData.Excluded_Classes__c='TestRunner, Foo';
        AtrData.Active__c = false; 
        //AtrData.Schedule__c=AtrRecord.Schedule__c;
        AtrData.Frequency__c='Monthly';
        AtrData.On_Every__c='8';
        Date Dateis=date.today();
        AtrData.Start_Date__c=Dateis;
        AtrData.End_Date__c=Dateis.addMonths(1);
        AtrData.Schedule_Name__c= ScheduleNameis; 
        AtrData.Preferred_Time__c='11:00 PM';
       
        AtrData.RecordTypeId=ATERecordTypeId;
        test.startTest(); 
        insert AtrData;
        
    
        system.assert(AtrData!=null);
        AutoTestRunnerRecurrence sh1 = new AutoTestRunnerRecurrence();      
        String sch = '0 0 23 * * ?';
        //system.schedule('AutoTestRunnerRecurrence2548', sch, sh1);
         AutomatedTestSetup_Controller controller = new AutomatedTestSetup_Controller();
        ApexPages.StandardController sc = new ApexPages.standardController(AtrData);
        AutomatedTestSetup_Controller e = new AutomatedTestSetup_Controller(sc);        
        AutomatedTestSetup_Controller sph = new AutomatedTestSetup_Controller (sc);
        PageReference pageRef = Page.AutomatedTestSetupRecurrence;
        pageRef.getParameters().put('id', String.valueOf(AtrData.Id));
        Test.setCurrentPage(pageRef);
        sph.RecuureceATSinsertandSchedule();
        
        List<Automated_Test_Run__c> ATSlist2=[SELECT Emails__c, Schedule_Name__c,RecordType.Name FROM Automated_Test_Run__c where Preferred_Time__c='11:00 PM' and  Frequency__c!=null and Name='Test Automated Run reccurence' ORDER BY createdDate DESC LIMIT 1];
            system.debug('ATSlist* is'+ATSlist2);
            String scname =ATSlist2[0].Schedule_Name__c;
            system.debug('ATSlist job name is'+ATSlist2[0].Schedule_Name__c);
            ATSlist2[0].Schedule_Name__c='AutoTestRunnerRecurrence';
            //update ATSlist2;
        
        AutomatedTestSetup_Controller.recurrenceenqueueTests();
        controller.click();
        controller.Adhocclick();
        controller.CancelRecurrenceJobs();
        controller.CancelAdhocJobs();
        test.StopTest();
}
/*
    public static testMethod void TestScheduleJobs() {
        Test.startTest();
        String CRON_EXP = '0 0 4 ? * SUN *';
        String jobId = System.schedule('AutoTestRunnerRecurrence2548',CRON_EXP,new AutoTestRunnerRecurrence());
        CronTrigger ct = [SELECT id, CronExpression, TimesTriggered,NextFireTime FROM CronTrigger WHERE id = :jobId];
        System.assertEquals(CRON_EXP, ct.CronExpression);
        System.assertEquals(0, ct.TimesTriggered);
        
        String jobId2 = System.schedule('AutoTestRunner285',CRON_EXP,new AutoTestRunner());
        CronTrigger ct2 = [SELECT id, CronExpression, TimesTriggered,NextFireTime FROM CronTrigger WHERE id = :jobId2];
        System.assertEquals(CRON_EXP, ct.CronExpression);
        System.assertEquals(0, ct.TimesTriggered);      
        Test.stopTest();  
    }*/ 
public static testMethod void Testing1() {
    Test.startTest();
    AutomatedTestSetup_Controller controller = new AutomatedTestSetup_Controller();
        controller.click();
        controller.Adhocclick();
        AutoTestRunnerRecurrence sh1 = new AutoTestRunnerRecurrence();      
        String sch = '0 0 23 * * ?';
        system.schedule('AutoTestRunnerRecurrence2548y', sch, sh1);
        AutoTestRunner sh2 = new AutoTestRunner();      
        
        system.schedule('AutoTestRunner548y', sch, sh2);
        controller.CancelRecurrenceJobs();
        controller.CancelAdhocJobs();
        test.StopTest();
}
}

No comments:

Post a Comment