Search in sources :

Example 11 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class BusinessRuleUnitIT method testRemovingOtherVariableFromAutoGeneratedRuleUnit.

@Test
public void testRemovingOtherVariableFromAutoGeneratedRuleUnit() throws Exception {
    Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
    resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/ExampleGenerated.bpmn"));
    resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/Generated.drl"));
    Application app = generateCode(resourcesTypeMap);
    Process<? extends Model> process = app.get(Processes.class).processById("ruletask.ExampleGenerated");
    HashMap<String, Object> map = new HashMap<>();
    map.put("singleString", "hello");
    map.put("singlePerson", new Person("John", 60));
    Model model = process.createModel();
    model.fromMap(map);
    ProcessInstance<? extends Model> instance = process.createInstance(model);
    Model variables = instance.variables();
    Map<String, Object> result = variables.toMap();
    assertNull(result.get("emptyString"));
    assertNull(result.get("emptyPerson"));
    instance.start();
    result = instance.variables().toMap();
    Person john = new Person("John", 60);
    john.setAdult(true);
    assertEquals(john, result.get("singlePerson"));
    assertNull(result.get("singleString"));
}
Also used : HashMap(java.util.HashMap) Processes(org.kie.kogito.process.Processes) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class BusinessRuleUnitIT method outputMappingNullCollection.

@Test
@DisplayName("Should throw an exception when a null collection variable is mapped as output of a datasource")
public void outputMappingNullCollection() throws Exception {
    Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
    resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/ExampleP.bpmn"));
    resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/Example.drl"));
    Application app = generateCode(resourcesTypeMap);
    Process<? extends Model> process = app.get(Processes.class).processById("ruletask.ExampleP");
    HashMap<String, Object> map = new HashMap<>();
    map.put("singleString", "hello");
    map.put("singlePerson", new Person("Yoko", 86));
    map.put("manyPersons", asList(new Person("Paul", 77), new Person("Ringo", 79)));
    Model model = process.createModel();
    model.fromMap(map);
    ProcessInstance<? extends Model> instance = process.createInstance(model);
    Model variables = instance.variables();
    Map<String, Object> result = variables.toMap();
    assertNull(result.get("emptyString"));
    assertNull(result.get("emptyPerson"));
    assertNull(result.get("emptyList"));
    instance.start();
    assertThat(instance.status()).isEqualTo(ProcessInstance.STATE_ERROR);
    assertThat(instance.error().get().errorMessage()).contains("Null collection variable used as an output variable");
}
Also used : HashMap(java.util.HashMap) Processes(org.kie.kogito.process.Processes) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 13 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class BusinessRuleUnitIT method inputMappingNullCollection.

@Test
@DisplayName("Should throw an exception when a null collection variable is mapped as input of a datasource")
public void inputMappingNullCollection() throws Exception {
    Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
    resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/ExampleP.bpmn"));
    resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/Example.drl"));
    Application app = generateCode(resourcesTypeMap);
    Process<? extends Model> process = app.get(Processes.class).processById("ruletask.ExampleP");
    HashMap<String, Object> map = new HashMap<>();
    map.put("singleString", "hello");
    map.put("singlePerson", new Person("Yoko", 86));
    map.put("manyPersons", null);
    Model model = process.createModel();
    model.fromMap(map);
    ProcessInstance<? extends Model> instance = process.createInstance(model);
    Model variables = instance.variables();
    Map<String, Object> result = variables.toMap();
    assertNull(result.get("emptyString"));
    assertNull(result.get("emptyPerson"));
    assertNull(result.get("emptyList"));
    instance.start();
    assertThat(instance.status()).isEqualTo(ProcessInstance.STATE_ERROR);
    assertThat(instance.error().get().errorMessage()).contains("The input collection variable of a data source cannot be null");
}
Also used : HashMap(java.util.HashMap) Processes(org.kie.kogito.process.Processes) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 14 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class BusinessRuleUnitIT method ioMappingAutoGeneratedRuleUnit.

@Test
public void ioMappingAutoGeneratedRuleUnit() throws Exception {
    Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
    resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/ExampleGenerated.bpmn"));
    resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/Generated.drl"));
    Application app = generateCode(resourcesTypeMap);
    Process<? extends Model> process = app.get(Processes.class).processById("ruletask.ExampleGenerated");
    HashMap<String, Object> map = new HashMap<>();
    map.put("singleString", "hello");
    map.put("singlePerson", new Person("Yoko", 86));
    map.put("manyPersons", asList(new Person("Paul", 77), new Person("Ringo", 79)));
    map.put("emptyList", new ArrayList<>());
    Model model = process.createModel();
    model.fromMap(map);
    ProcessInstance<? extends Model> instance = process.createInstance(model);
    Model variables = instance.variables();
    Map<String, Object> result = variables.toMap();
    assertNull(result.get("emptyString"));
    assertNull(result.get("emptyPerson"));
    instance.start();
    result = instance.variables().toMap();
    Person yoko = new Person("Yoko", 86);
    yoko.setAdult(true);
    assertEquals(yoko, result.get("singlePerson"));
}
Also used : HashMap(java.util.HashMap) Processes(org.kie.kogito.process.Processes) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with Person

use of org.kie.kogito.codegen.data.Person in project kogito-runtimes by kiegroup.

the class BusinessRuleTaskIT method testBasicBusinessRuleTaskControlledByUnitOfWork.

@Test
public void testBasicBusinessRuleTaskControlledByUnitOfWork() throws Exception {
    Map<AbstractCodegenIT.TYPE, List<String>> resourcesTypeMap = new HashMap<>();
    resourcesTypeMap.put(TYPE.PROCESS, Collections.singletonList("ruletask/BusinessRuleTask.bpmn2"));
    resourcesTypeMap.put(TYPE.RULES, Collections.singletonList("ruletask/BusinessRuleTask.drl"));
    Application app = generateCode(resourcesTypeMap);
    assertThat(app).isNotNull();
    final List<String> startedProcesses = new ArrayList<>();
    // add custom event listener that collects data
    app.config().get(ProcessConfig.class).processEventListeners().listeners().add(new DefaultKogitoProcessEventListener() {

        @Override
        public void beforeProcessStarted(ProcessStartedEvent event) {
            startedProcesses.add(((KogitoProcessInstance) event.getProcessInstance()).getStringId());
        }
    });
    UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
    uow.start();
    Process<? extends Model> p = app.get(Processes.class).processById("BusinessRuleTask");
    Model m = p.createModel();
    m.fromMap(Collections.singletonMap("person", new Person("john", 25)));
    ProcessInstance<?> processInstance = p.createInstance(m);
    processInstance.start();
    assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
    Model result = (Model) processInstance.variables();
    assertThat(result.toMap()).hasSize(1).containsKey("person");
    assertThat(result.toMap().get("person")).isNotNull().hasFieldOrPropertyWithValue("adult", true);
    // since the unit of work has not been finished yet not listeners where invoked
    assertThat(startedProcesses).hasSize(0);
    uow.end();
    // after unit of work has been ended listeners are invoked
    assertThat(startedProcesses).hasSize(1);
}
Also used : UnitOfWork(org.kie.kogito.uow.UnitOfWork) ProcessConfig(org.kie.kogito.process.ProcessConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultKogitoProcessEventListener(org.kie.kogito.internal.process.event.DefaultKogitoProcessEventListener) Processes(org.kie.kogito.process.Processes) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) Model(org.kie.kogito.Model) ArrayList(java.util.ArrayList) List(java.util.List) Application(org.kie.kogito.Application) Person(org.kie.kogito.codegen.data.Person) Test(org.junit.jupiter.api.Test)

Aggregations

Person (org.kie.kogito.codegen.data.Person)32 Application (org.kie.kogito.Application)30 HashMap (java.util.HashMap)20 Test (org.junit.jupiter.api.Test)20 Model (org.kie.kogito.Model)20 Processes (org.kie.kogito.process.Processes)20 ArrayList (java.util.ArrayList)19 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 List (java.util.List)17 Arrays.asList (java.util.Arrays.asList)10 RuleUnits (org.kie.kogito.rules.RuleUnits)10 AdultUnit (org.kie.kogito.codegen.unit.AdultUnit)9 EnumSource (org.junit.jupiter.params.provider.EnumSource)8 DataHandle (org.kie.kogito.rules.DataHandle)5 UnitOfWork (org.kie.kogito.uow.UnitOfWork)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 ProcessInstanceDataEvent (org.kie.kogito.event.process.ProcessInstanceDataEvent)3 WorkItem (org.kie.kogito.process.WorkItem)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2