Skip to main content
Version: 8.2505.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.

Detailed example

For a complete unit test example have a look at the ExpressionsTest.java shipped in the example maven project togetherwith SDK. Setup is described here.

Quick examples

Example 1 testing expressions of the notes

@Test
public void testNoteVarContains() {
AuthRequestImpl req = new AuthRequestImpl(new Properties());
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());
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());
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;