0

Apex class

Public class BlockIEPolicyCondition implements TxnSecurity.PolicyCondition {
    public boolean evaluate(TxnSecurity.Event e) {
        List<User> users = [SELECT ProfileId FROM User WHERE Id = :e.UserId];
       // if (users == null || users.size() == 0){
           // return false;
       // }
        String profileId = (String)users.get(0).get('ProfileId');
        List<Profile> profiles = [SELECT Name FROM Profile WHERE Id = :profileId];
       if (profiles == null || profiles.size() == 0 ||users == null || users.size() == 0){
            return false;
        } 
        String profileName = (String)profiles.get(0).get('Name');
        List<LoginHistory> eObjList = [SELECT Browser FROM LoginHistory WHERE Id = :e.data.get('LoginHistoryId') Limit 1];
        string profileName1 = '"' +profileName + '"';
        String P=system.label.BlockIEPolicyProfile;
        Boolean ismatch = p.contains(profileName1);
        if(ismatch && eObjList.size()>0 && eObjList[0].Browser.contains('IE'))
            return true;
       else 
            return false;
    }
}

Test class

@isTest
public class BlockIEPolicyCondition_Test 
{
    static list<User> userList=new list<User>();
    public static void setupTestData() {
        User use1=SRVS_CreateTestDataFactory.createUser('Service Integration Profile');
        use1.FederationIdentifier='9098';
         use1.External_ID__c='9098';
        use1.Dell_NTUserID__c='A_T';
        use1.File_Source__c='AMS - Admin';       
        use1.Organization__c='dell';
        use1.Service_User_Type__c='Business';
        insert use1;
        User use2=SRVS_CreateTestDataFactory.createUser('System Read Only');
        use2.FederationIdentifier='4567';
         use2.External_ID__c='4567';
        use2.Dell_NTUserID__c='B_T';
        use2.File_Source__c='AMS - Admin';       
        use2.Organization__c='dell';
        use2.Service_User_Type__c='Business';
        insert use2;
        userList.add(use2);
        userList.add(use1);
    }
    @isTest
    public static  void testIsNotIE() 
    { 
        User use3=SRVS_CreateTestDataFactory.createUser('service Agent');
        use3.FederationIdentifier='9876';
         use3.External_ID__c='9876';
        use3.Dell_NTUserID__c='C_T';
        use3.File_Source__c='AMS - Admin';       
        use3.Organization__c='dell';
        use3.Service_User_Type__c='Business';
        insert use3;
        system.runAs(use3)
        {
            Test.startTest(); 

            LoginHistory loginHistoryObj = new LoginHistory();
            Map<String, String> eventData = new Map<String, String>();
            TxnSecurity.Event e = new TxnSecurity.Event(
                UserInfo.getOrganizationId(),
                UserInfo.getUserId(), 
                'AuthSession', 
                'Login',
                'LoginHistory',
                '01pR00000009D2H', 
                Datetime.newInstance(2018, 6, 7),eventdata); 
            BlockIEPolicyCondition condition = new BlockIEPolicyCondition();
            boolean b= condition.evaluate(e);
            Test.stopTest(); 
        }
        System.assertEquals(0, userList.size());
    }
}
0
2

Code coverage is measured by determining the number of statements in the class compared with which statements are executed in various tests. The coverage is then number of unique statements executed across the tests / number of statements in the class * 100 as a percentage.

Code coverage in your class can only be affected by variations in code flow and any exceptions being thrown. Ignoring exceptions that leaves:

  1. whether "evaluate" is called in your tests
  2. whether the expression "(profiles == null || profiles.size() == 0 ||users == null || users.size() == 0)" is evaluated to both true and false in different tests, in order to ensure that the return statement is reached or the flow continues
  3. whether the expression "ismatch && eObjList.size()>0 && eObjList[0].Browser.contains('IE')" is evaluated to both true and false in different tests, in order to ensure that both return statements are reached.

On that latter point, I would remove the if statement and simply do:

return ismatch && eObjList.size()>0 && eObjList[0].Browser.contains('IE');

That way the one line of code will be reached regardless as to whether the expression evaluates true or false.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.