Skip to main content
Version: 8.2411.x.x RR

Testing configration expressions

Writing EL expressions in the auth state configurations can get complex and challenging. Such expressions can be validated with unit tests, using only a limited part of the testing framework.

Example 1 testing expressions of the notes

@Test
public void testNoteVarContains() {
AuthRequestImpl req = new AuthRequestImpl(new Properties(), "NO_CLIENT_ID", "NO_DOMAIN", "NO_ESAUTH_ID");
AuthResponseImpl res = new AuthResponseImpl();
res.setNote("var", "value");

String str = "#{StringUtils.contains(notes['var'], 'val')}";
String txt = AuthUtil.substituteVariables(req, res, str);
assertEquals("true", txt);
}

@Test
public void testNoteConcat() {
AuthRequestImpl req = new AuthRequestImpl(new Properties(), "NO_CLIENT_ID", "NO_DOMAIN", "NO_ESAUTH_ID");
AuthResponseImpl res = new AuthResponseImpl();
res.setNote("first","one");
res.setNote("second","two");
res.setNote("third","three");
res.setNote("forth","four");

String str = "${StringUtils.join(Arrays.asList(notes.first,notes.second,notes.third,notes.forth).toArray())}";
String txt = AuthUtil.substituteVariables(req,res,str);
assertEquals("onetwothreefour",txt);
}

Example 2 testing expressions with sessions

@Test
public void testSessionVarContains() {
AuthRequestImpl req = new AuthRequestImpl(new Properties(), "NO_CLIENT_ID", "NO_DOMAIN", "NO_ESAUTH_ID");
MockSession ms = new MockSession();
ms.setAttribute("var", "value");
req.setSession(ms);
AuthResponseImpl res = new AuthResponseImpl();

String str = "#{StringUtils.contains(sess['var'], 'val')}";
String txt = AuthUtil.substituteVariables(req, res, str);
assertEquals("true", txt);
}

Imports

The example tests above are using the following classes:

  • ch.nevis.esauth.auth.engine.AuthResponseImpl;
  • ch.nevis.esauth.auth.engine.AuthUtil;
  • ch.nevis.esauth.test.auth.engine.AuthRequestImpl;
  • ch.nevis.esauth.test.auth.engine.MockSession;