TopicObserver.com

Trond Pettersen on Web Development and Topic Maps

Unit Testing Sun IdM XPress Rules with jUnit

Now that you know how to Execute XPress rules from Java I hope that the link to unit testing your XPress code (or perhaps mess?) is clear, and I would of course advise you to do so for every rule you write.

In order to test your XPress rules with jUnit, you can write normal unit tests that utilizes our XpressUtil.executeRule method. In addition, we have to use IdM’s SessionFactory to create a session to work with.

Here’s an example:

package com.mycorp.xpress;

import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import com.mycorp.util.XpressUtil;
import com.waveset.session.Session;
import com.waveset.session.SessionFactory;
import com.waveset.util.EncryptedData;
import com.waveset.util.WavesetException;

public class UserRuleLibraryTest {

	private Session session = null;

	private static final String username = "Configurator";
	private static final String password = "configurator";
        private static final String userAttributeRule = "MyCorp-UserRuleLibrary:getUserAttribute";

        @Before
	public void initializeSession() throws WavesetException {
		// You can of course move this somewhere else
		// ... and move away from hard coded usr/passwd.
		EncryptedData encPassword = new EncryptedData(password);
		session = SessionFactory.getSession(username, encPassword);
	}

	@Test
	public void getUserAttributeRuleReturnsCorrectValueForName() throws WavesetException {
		Object ruleResult = XpressUtil.executeRule( session, userAttributeRule, makeUserAttributeArgs() );
		assertEquals("Received unexpected fullname from rule", "Bushy George", ruleResult);
	}

        private Map<String, Object> makeUserAttributeArgs() {
                Map<String, Object> args = new HashMap<String, Object>();
		args.put("context", getSession());
		args.put("accountId", "georgeb");
		args.put("attrName", "fullname");
                return args;
        }

}

One thing to remember is that in order to run this test, you have to pass in the waveset.home to be used. If you are like me — and use eclipse, you can set this under e.g. “Run Configurations” → “VM arguments”. Here’s a copy of what arguments I use for most of my jUnit tests:

-Xms128m
-Xmx512m
-Dwaveset.home="C:\\code\\mycorp\\idm\\image\\idm"

Next up: Unit testing Sun IdM web pages.

Leave one

2 Responses

  1. ok, how to run it? i dont see a “main” to run. and i am not running the idm locally. Should i be? thanks

    Reply
  2. Run As -> jUnit Test.

    Reply

Leave a Reply

  • RSS @twitter

  • Tags

  • Topics

  • Recent Comments

  • Topic Map Feeds