function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TifTif 

Explore Custom Transaction Security Policies Compile Error: Missing '<EOF>'

Hi folks, 

I'm on the Transaction Security module and attempting the section "Explore Custom Transaction Security Policies". When I paste the test code into my BlockAndroidPolicyCondition Apex Class I'm getting the error below. Any suggestions why?


Error: Compile Error: Missing '<EOF>' at '@' at line 15 column 1

Code:
 
global class BlockAndroidPolicyCondition implements TxnSecurity.PolicyCondition {

 public boolean evaluate(TxnSecurity.Event e) {
LoginHistory eObj = [SELECT Platform FROM LoginHistory WHERE Id = :e.data.get('LoginHistoryId')];
  if (eObj != null) {  // Addition
    if(eObj.Platform.contains('Android') &&
       eObj.Platform.compareTo('Android 5') < 0) {
      return true;
    }
  } // Addition
 return false; 
}
 }
 
@isTest
public class TestBlockAndroid {
  public static testMethod void testIsAndroid() {

    /* Create a history object that has Platform = Android 4. */
    LoginHistory loginHistoryObj = new LoginHistory();
    loginHistoryObj.Platform = 'Android 4';

    /* Create a map for the event we’re going to build. */
    Map<String, String> eventData = new Map<String, String>();

    /* Insert the LoginHistoryId into the event data map. */
    insert loginHistoryObj;
    eventData.put('LoginHistoryId', loginHistoryObj.id);

    /* We’re not going to cause a real event in the org.
       Instead, we’re going to create a Transaction Security
       event object and “feed” it to the Policy Engine. */
    /* You can find more about TxnSecurity.Event in the 
       Apex Developer Guide. */
    TxnSecurity.Event e = new TxnSecurity.Event(
      '00Dxxx123123123', /* organizationId */
      '005xxx123123123', /* userId */
      'AuthSession', /* entityName */
      'Login', /* action */
      'LoginHistory', /* resourceType */
      '01pR00000009D2H', /* entityId */
      Datetime.newInstance(2016, 2, 15), /* timeStamp */
      eventData; ) /* data - Map with info about this event. */
        /* The only info in the map is the login history, and
           the only info in the login history is the Platform. */

    /* We are unit testing a PolicyCondition that triggers
       when a login is from Android OS version 5 or older. */
    BlockAndroidPolicyCondition condition =
      new BlockAndroidPolicyCondition();

    /* Assert that the condition is triggered by evaluating
       the event e. The Transaction Security PolicyCondition
       interface returns True if the policy is triggered. */
    System.assertEquals(true, condition.evaluate(e));
  }
}

@isTest
public class TestBlockAndroid {
  public static testMethod void testIsNotAndroid() {

    /* Create a history object that has Platform = Android 6. */
    LoginHistory loginHistoryObj = new LoginHistory();
    loginHistoryObj.Platform = 'Android 6';

    /* Create a map for the event we’re going to build. */
    Map<String, String> eventData = new Map<String, String>();

    /* Insert the LoginHistoryId into the event data map. */
    insert loginHistoryObj;
    eventData.put('LoginHistoryId', loginHistoryObj.id);

    /* We’re not going to cause a real event in the org.
       Instead, we’re going to create a Transaction Security
       event object and “feed” it to the Policy Engine. */
    /* You can find more about TxnSecurity.Event in the 
       Apex Developer Guide. */
    TxnSecurity.Event e = new TxnSecurity.Event(
      '00Dxxx123123123', /* organizationId */
      '005xxx123123123', /* userId */
      'AuthSession', /* entityName */
      'Login', /* action */
      'LoginHistory', /* resourceName */
      '01pR00000009D2H', /* entityId */
      Datetime.newInstance(2016, 2, 15), /* timeStamp */
      eventData; ) /* data - Map with info about this event. */
        /* The only info in the map is the login history, and
           the only info in the login history is the Platform. */

    /* We are unit testing a PolicyCondition that triggers
       when a login is from an Android OS version newer than 5. */
    BlockAndroidPolicyCondition condition =
      new BlockAndroidPolicyCondition();

    /* Assert that the condition is NOT triggered by evaluating
       the event e. The Transaction Security PolicyCondition
       interface returns False if the policy is not triggered. */
    System.assertEquals(false, condition.evaluate(e));
  }
}

 
Raj VakatiRaj Vakati
Use this code
 
@isTest(Seealldata=true)
public class TestBlockAndroid {
    public static testMethod void testIsAndroid() {
        System.Test.startTest() ;
        
        LoginHistory l = [ SELECT Platform FROM LoginHistory Limit 1];
        /* Create a map for the event we’re going to build. */
        Map<String, String> eventData = new Map<String, String>();
        eventData.put('LoginHistoryId', l.Id);
        
        TxnSecurity.Event e = new TxnSecurity.Event(
            UserInfo.getOrganizationId(), /* organizationId */
            UserInfo.getUserId(), /* userId */
            'AuthSession', /* entityName */
            'Login', /* action */
            'LoginHistory', /* resourceType */
            '01pR00000009D2H', /* entityId */
            Datetime.newInstance(2016, 2, 15), /* timeStamp */
            eventData); /* data - Map with info about this event. */
        
        BlockAndroidPolicyCondition condition =
            new BlockAndroidPolicyCondition();
        condition.evaluate(e);
        
        // System.assertEquals(true, condition.evaluate(e));
        System.Test.stopTest() ;
        
    }
}