Rule Tips/Tricks : AttributeGenerator Rule

Hi folks,
Developing rules in idn is somewhat difficult as there is no way to do trial and error which is quite basic stuff in any development, specially if you are new to rule development.
I will try to share few tips around attribute generator rule so you can test the generator logic without submitting the rule. Pasting the simple java class in which I have written the generator rule code.


import openconnector.Log;

import java.text.Normalizer;
import sailpoint.api.SailPointContext;
import sailpoint.object.Filter;
import sailpoint.object.Identity;
import sailpoint.object.QueryOptions;
import sailpoint.tools.GeneralException;



public class EmailGenerator {

	Identity identity=null;
	SailPointContext context=null;
	Log log=null;
	
	
	public  String normalizeInput(String inputString){
		//Decompose and remove diacritical marks, spaces, special characters and symbols
		if(inputString == null || inputString.isEmpty())
			return "";
		inputString = Normalizer.normalize(inputString, Normalizer.Form.NFD);
		inputString = inputString.replaceAll("[^a-zA-Z0-9]","");
		return inputString;
	}
	
	public String generateEmail(String firstName, String lastName, String midInitial , int iteration) {
		
		String email;
		  
		  switch ( iteration ) {
		    case 0:
		      email = firstName.substring(0, 1) + midInitial + lastName + "dev.com";
		     
		      break;
		    case 1: 
		      email = firstName + midInitial  + lastName+ "@dev.com";
		      
			  break;
		    default:
		    	
		      email = firstName + midInitial +  lastName + ( iteration - 1 ) + "@dev.com";
		      break;
		  }
		  if ( isUnique ( email ) ) {
			 
		    return email;
		    }
		  else if ( iteration < 100 ) {
			  
		    return generateEmail( firstName, midInitial, lastName, ( iteration + 1 ) );
		    }
		  else {
			  
			  return null;
		    }
		
		
	}
	
		
	public boolean isUnique(String email)  {

		
		QueryOptions ops = new QueryOptions();
		int numberFound = 0;

		Filter filter = Filter.ignoreCase( Filter.eq("email", email));
		ops.addFilter(filter);

		try {
			numberFound = context.countObjects(Identity.class, ops);

		} catch (GeneralException e) {
			// TODO Auto-generated catch block
			
			
		}

		
		return numberFound == 0;
	}
	/*
	 * If we haven't returned already, then we haven't found an existing mail.
	 */
	
	
	public String voidMain() {
		
		String firstName = normalizeInput((String)identity.getAttribute("firstname"));
		String lastName = normalizeInput((String)identity.getAttribute("lastname"));
		String middleName = normalizeInput((String)identity.getAttribute("middleName"));
		String midInitial="";
		
		if(middleName == null || middleName.isEmpty()) {
			
			
		}else {
			 midInitial=middleName.substring(0, 1);
			
		}
		
		
		
		String finalEmail=generateEmail(firstName, lastName, midInitial, 0).toLowerCase();
		
		
		return finalEmail;
	}

}

We cannot test the isUnique logic without rule actually running into idn because it’s checking against identities list. To simulate that we can simply use random function which will return true/false randomly. Check below code which will run and you can test your generation logic.






import openconnector.Log;


import java.text.Normalizer;
import sailpoint.api.SailPointContext;
import sailpoint.object.Filter;
import sailpoint.object.Identity;
import sailpoint.object.QueryOptions;
import sailpoint.tools.GeneralException;
import sailpoint.tools.Util;


public class EmailGeneratorTesting {

	Identity identity=null;
	SailPointContext context=null;
	Log log=null;
	
	
	public  String normalizeInput(String inputString){
		//Decompose and remove diacritical marks, spaces, special characters and symbols
		if(inputString == null || inputString.isEmpty())
			return "";
		inputString = Normalizer.normalize(inputString, Normalizer.Form.NFD);
		inputString = inputString.replaceAll("[^a-zA-Z0-9]","");
		return inputString;
	}
	
	public String generateEmail(String firstName, String lastName, String midInitial , int iteration) {
		
		String email;
		  
		  switch ( iteration ) {
		    case 0:
		      email = firstName.substring(0, 1) + midInitial + lastName + "@dev.com";
		      System.out.println("devsb Email Rule: case0 "+ email);
		      break;
		    case 1: 
		      email = firstName + midInitial  + lastName+ "@dev.com";
		      System.out.println("devsb Email Rule: case1 "+ email);
			  break;
		    default:
		    	System.out.println("devsb Email Rule: defaultcase "+iteration);
		      email = firstName + midInitial +  lastName + ( iteration - 1 ) + "@dev.com";
		      break;
		  }
		  if ( isUnique ( email ) ) {
			  System.out.println("devsb Email Rule: final email: "+email);
		    return email;
		    }
		  else if ( iteration < 100 ) {
			  System.out.println("devsb Email Rule: iteration "+iteration);
		    return generateEmail( firstName,lastName, midInitial,  ( iteration + 1 ) );
		    }
		  else {
			  System.out.println("devsb Email Rule: returning   null ");
			  return null;
		    }
		
		
	}
	
		
	public boolean isUnique(String email)  {
/*
		QueryOptions ops = new QueryOptions();
		int numberFound = 0;

		Filter filter = Filter.ignoreCase( Filter.eq("email", email));
		ops.addFilter(filter);

		try {
			numberFound = context.countObjects(Identity.class, ops);

		} catch (GeneralException e) {
			// TODO Auto-generated catch block
			System.out.println("devsb Email Rule: exception in running context search"+ e.getMessage());
			
		}

		System.out.println("devsb Email Rule: printing numberfound:" + numberFound);
		return numberFound == 0;
		*/
		return (Math.random() < 0.5);
	}
	/*
	 * If we haven't returned already, then we haven't found an existing mail.
	 */
	
	
	public static void main(String args[]) {
	
		String firstName = "Chirag";
		String lastName = "Patel";
		String middleName = null;
		String midInitial="";
		
		if(middleName == null || middleName.isEmpty()) {
			System.out.println("devsb Email Rule: empty middlename");
			
		}else {
			System.out.println("devsb Email Rule: printing middlename "+middleName);
			 midInitial=middleName.substring(0, 1);
			
		}
		
		System.out.println("devsb Email Rule: printing mid initial "+midInitial);
		
		
		EmailGeneratorTesting em=new EmailGeneratorTesting();
		System.out.println(em.generateEmail(firstName, lastName, midInitial, 0));
	}

}

Feel free to use this and let me know in comments if you have any questions. You can use similar setup to test your identity attribute rule also.

5 Likes

This is great, thanks for sharing @chirag.patel!

Thanks, Chirag- I wish we had more of this.

1 Like

Matt , I can write something similar for LCS rule or any other identity attribute rule if that helps.