use of eu.bcvsolutions.idm.core.api.dto.IdmScriptDto in project CzechIdMng by bcvsolutions.
the class ExecuteScriptTaskExecutorTest method testExecuteScriptDefault.
@Test
public void testExecuteScriptDefault() {
IdmScriptDto scriptDto = new IdmScriptDto();
scriptDto.setCode(TEST_SCRIPT_CODE + "_1");
scriptDto.setName(TEST_SCRIPT_CODE + "_1");
scriptDto.setCategory(IdmScriptCategory.DEFAULT);
StringBuilder builder = new StringBuilder();
builder.append("task.setCounter(0l);\n");
builder.append("task.setCount(10l);\n");
builder.append("for (int index = 0; index < 10; index++) {\n");
builder.append(" task.increaseCounter();\n");
builder.append(" task.updateState();\n");
builder.append("}\n");
scriptDto.setScript(builder.toString());
scriptDto = scriptService.save(scriptDto);
prepareAuthForTestScript(scriptDto);
Map<String, Object> parameters = new HashMap<>();
parameters.put("scriptCode", TEST_SCRIPT_CODE + "_1");
executeScriptEvaluator.init(parameters);
executeScriptEvaluator.process();
}
use of eu.bcvsolutions.idm.core.api.dto.IdmScriptDto in project CzechIdMng by bcvsolutions.
the class ExecuteScriptTaskExecutorTest method testExeciteScriptWithResult.
@Test
public void testExeciteScriptWithResult() {
// remove previous long running task
List<IdmLongRunningTaskDto> tasks = longRunningTaskService.find(null).getContent();
for (IdmLongRunningTaskDto task : tasks) {
longRunningTaskService.delete(task);
}
IdmScriptDto scriptDto = new IdmScriptDto();
scriptDto.setCode(TEST_SCRIPT_CODE + "_3");
scriptDto.setName(TEST_SCRIPT_CODE + "_3");
scriptDto.setCategory(IdmScriptCategory.SYSTEM);
StringBuilder builder = new StringBuilder();
builder.append("import eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto;\n");
builder.append("task.setCounter(0l);\n");
builder.append("task.setCount(5l);\n");
builder.append("for (int index = 0; index < 5; index++) {\n");
builder.append(" IdmIdentityDto dto = new IdmIdentityDto();\n");
builder.append(" dto.setUsername('test-execute-' + index);\n");
builder.append(" dto.setLastName('test-execute-' + index);\n");
builder.append(" dto.setFirstName('' + index);\n");
builder.append(" identityService.save(dto);\n");
builder.append(" task.increaseCounter();\n");
builder.append(" task.updateState();\n");
builder.append("}\n");
scriptDto.setScript(builder.toString());
scriptDto = scriptService.save(scriptDto);
prepareAuthForTestScript(scriptDto);
IdmScriptAuthorityDto authDto = new IdmScriptAuthorityDto();
authDto.setType(ScriptAuthorityType.SERVICE);
authDto.setClassName("eu.bcvsolutions.idm.core.model.service.impl.DefaultIdmIdentityService");
authDto.setScript(scriptDto.getId());
authDto.setService("identityService");
scriptAuthorityService.save(authDto);
authDto = new IdmScriptAuthorityDto();
authDto.setType(ScriptAuthorityType.CLASS_NAME);
authDto.setClassName("eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto");
authDto.setScript(scriptDto.getId());
scriptAuthorityService.save(authDto);
Map<String, Object> parameters = new HashMap<>();
parameters.put("scriptCode", TEST_SCRIPT_CODE + "_3");
taskExecutor.init(parameters);
LongRunningFutureTask<Boolean> futureTask = manager.execute(taskExecutor);
try {
assertEquals(Boolean.TRUE, futureTask.getFutureTask().get());
} catch (InterruptedException | ExecutionException e) {
fail(e.getMessage());
}
IdmLongRunningTaskDto longRunningTask = longRunningTaskService.get(taskExecutor.getLongRunningTaskId());
assertEquals(OperationState.EXECUTED, longRunningTask.getResult().getState());
assertEquals(5, longRunningTask.getCount().longValue());
assertEquals(5, longRunningTask.getCounter().longValue());
IdmIdentityFilter identityFilter = new IdmIdentityFilter();
identityFilter.setText("test-execute-");
List<IdmIdentityDto> identities = identityService.find(identityFilter, new PageRequest(0, 20, new Sort(Direction.ASC, IdmIdentity_.firstName.getName()))).getContent();
assertEquals(5, identities.size());
for (int index = 0; index < 5; index++) {
assertEquals(String.valueOf(index), identities.get(index).getFirstName());
}
}
use of eu.bcvsolutions.idm.core.api.dto.IdmScriptDto in project CzechIdMng by bcvsolutions.
the class ScriptEvaluatorTest method testCreateDuplScriptCode.
@Test(expected = DataIntegrityViolationException.class)
public void testCreateDuplScriptCode() {
String duplCode = "script_code";
//
IdmScriptDto script = new IdmScriptDto();
script.setCategory(IdmScriptCategory.DEFAULT);
script.setCode(duplCode);
script.setName("script_name_" + System.currentTimeMillis());
//
script = scriptService.saveInternal(script);
//
IdmScriptDto script2 = new IdmScriptDto();
script2.setCategory(IdmScriptCategory.DEFAULT);
script2.setCode(duplCode);
script2.setName("script_name_" + System.currentTimeMillis());
//
script2 = scriptService.saveInternal(script2);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmScriptDto in project CzechIdMng by bcvsolutions.
the class ScriptEvaluatorTest method testScriptSecurityExceptionWithCatchSecurityException.
@Test(expected = IdmSecurityException.class)
public void testScriptSecurityExceptionWithCatchSecurityException() {
String testString = "TEST-" + System.currentTimeMillis();
//
Triple<IdmScriptDto, IdmScriptDto, IdmScriptDto> scripts = createThreeScripts();
IdmScriptDto script1 = scripts.getFirst();
IdmScriptDto script2 = scripts.getSecond();
IdmScriptDto script3 = scripts.getThird();
//
StringBuilder script1Body = new StringBuilder();
script1Body.append("try {\n");
script1Body.append(this.createScriptThatCallAnother(script2, IdmScriptCategory.DEFAULT, null, false));
script1Body.append("} catch (SecurityException e) {\n");
script1Body.append("return \"" + testString + "\";\n");
script1Body.append("}\n");
script1Body.append("return null;\n");
//
script1.setScript(script1Body.toString());
script2.setScript(this.createScriptThatCallAnother(script3, IdmScriptCategory.DEFAULT, null, false));
script3.setScript("throw new SecurityException(\"" + testString + "\")");
//
script1 = this.scriptService.save(script1);
script2 = this.scriptService.save(script2);
script3 = this.scriptService.save(script3);
// must throw exception
defaultScriptEvaluator.evaluate(script1.getCode());
fail();
}
use of eu.bcvsolutions.idm.core.api.dto.IdmScriptDto in project CzechIdMng by bcvsolutions.
the class ScriptEvaluatorTest method testScriptExceptionWithCatch.
@Test
public void testScriptExceptionWithCatch() {
String testString = "TEST-" + System.currentTimeMillis();
//
Triple<IdmScriptDto, IdmScriptDto, IdmScriptDto> scripts = createThreeScripts();
IdmScriptDto script1 = scripts.getFirst();
IdmScriptDto script2 = scripts.getSecond();
IdmScriptDto script3 = scripts.getThird();
//
StringBuilder script1Body = new StringBuilder();
script1Body.append("try {\n");
script1Body.append(this.createScriptThatCallAnother(script2, IdmScriptCategory.DEFAULT, null, false));
script1Body.append("} catch (Throwable e) {\n");
script1Body.append("return \"" + testString + "\";\n");
script1Body.append("}\n");
script1Body.append("return null;\n");
//
script1.setScript(script1Body.toString());
script2.setScript(this.createScriptThatCallAnother(script3, IdmScriptCategory.DEFAULT, null, false));
script3.setScript("throw new Exception(\"" + testString + "\")");
//
script1 = this.scriptService.save(script1);
script2 = this.scriptService.save(script2);
script3 = this.scriptService.save(script3);
try {
Object result = defaultScriptEvaluator.evaluate(script1.getCode());
assertNotNull(result);
assertEquals(testString, result);
} catch (Throwable e) {
fail();
}
}
Aggregations