Search in sources :

Example 1 with Person

use of org.drools.scenariosimulation.backend.model.Person in project drools by kiegroup.

the class RuleScenarioRunnerHelperTest method createObject.

@Test
public void createObject() {
    Map<List<String>, Object> params = new HashMap<>();
    params.put(singletonList("firstName"), "TestName");
    params.put(singletonList("age"), 10);
    ValueWrapper<Object> initialInstance = runnerHelper.getDirectMapping(params);
    Object objectRaw = runnerHelper.createObject(initialInstance, Person.class.getCanonicalName(), params, this.getClass().getClassLoader());
    assertTrue(objectRaw instanceof Person);
    Person object = (Person) objectRaw;
    assertEquals(10, object.getAge());
    assertEquals("TestName", object.getFirstName());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 2 with Person

use of org.drools.scenariosimulation.backend.model.Person in project drools by kiegroup.

the class RuleScenarioRunnerHelperTest method executeScenario.

@Test
public void executeScenario() {
    ArgumentCaptor<Object> insertCaptor = ArgumentCaptor.forClass(Object.class);
    ScenarioRunnerData scenarioRunnerData = new ScenarioRunnerData();
    scenarioRunnerData.addBackground(new InstanceGiven(personFactIdentifier, new Person()));
    scenarioRunnerData.addBackground(new InstanceGiven(disputeFactIdentifier, new Dispute()));
    scenarioRunnerData.addGiven(new InstanceGiven(personFactIdentifier, new Person()));
    FactMappingValue factMappingValue = new FactMappingValue(personFactIdentifier, firstNameExpectedExpressionIdentifier, NAME);
    scenarioRunnerData.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(factMappingValue), false));
    scenarioRunnerData.addExpect(new ScenarioExpect(personFactIdentifier, singletonList(factMappingValue), true));
    int inputObjects = scenarioRunnerData.getBackgrounds().size() + scenarioRunnerData.getGivens().size();
    String ruleFlowGroup = "ruleFlowGroup";
    settings.setRuleFlowGroup(ruleFlowGroup);
    runnerHelper.executeScenario(kieContainerMock, scenarioRunnerData, expressionEvaluatorFactory, simulation.getScesimModelDescriptor(), settings);
    verify(ruleScenarioExecutableBuilderMock, times(1)).setActiveRuleFlowGroup(ruleFlowGroup);
    verify(ruleScenarioExecutableBuilderMock, times(inputObjects)).insert(insertCaptor.capture());
    for (Object value : insertCaptor.getAllValues()) {
        assertTrue(value instanceof Person || value instanceof Dispute);
    }
    verify(ruleScenarioExecutableBuilderMock, times(1)).addInternalCondition(eq(Person.class), any(), any());
    verify(ruleScenarioExecutableBuilderMock, times(1)).run();
    assertEquals(1, scenarioRunnerData.getResults().size());
    // test not rule error
    settings.setType(ScenarioSimulationModel.Type.DMN);
    assertThatThrownBy(() -> runnerHelper.executeScenario(kieContainerMock, scenarioRunnerData, expressionEvaluatorFactory, simulation.getScesimModelDescriptor(), settings)).isInstanceOf(ScenarioException.class).hasMessageStartingWith("Impossible to run");
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) Dispute(org.drools.scenariosimulation.backend.model.Dispute) ScenarioRunnerData(org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 3 with Person

use of org.drools.scenariosimulation.backend.model.Person in project drools by kiegroup.

the class RuleScenarioRunnerHelperTest method getScenarioResultsTest.

@Test
public void getScenarioResultsTest() {
    List<InstanceGiven> scenario1Inputs = runnerHelper.extractGivenValues(simulation.getScesimModelDescriptor(), scenario1.getUnmodifiableFactMappingValues(), classLoader, expressionEvaluatorFactory);
    List<ScenarioExpect> scenario1Outputs = runnerHelper.extractExpectedValues(scenario1.getUnmodifiableFactMappingValues());
    assertTrue(scenario1Inputs.size() > 0);
    InstanceGiven input1 = scenario1Inputs.get(0);
    scenario1Outputs = scenario1Outputs.stream().filter(elem -> elem.getFactIdentifier().equals(input1.getFactIdentifier())).collect(toList());
    List<ScenarioResult> scenario1Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario1Outputs, input1, expressionEvaluatorFactory);
    assertEquals(1, scenario1Results.size());
    assertEquals(FactMappingValueStatus.SUCCESS, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
    List<InstanceGiven> scenario2Inputs = runnerHelper.extractGivenValues(simulation.getScesimModelDescriptor(), scenario2.getUnmodifiableFactMappingValues(), classLoader, expressionEvaluatorFactory);
    List<ScenarioExpect> scenario2Outputs = runnerHelper.extractExpectedValues(scenario2.getUnmodifiableFactMappingValues());
    assertTrue(scenario2Inputs.size() > 0);
    InstanceGiven input2 = scenario2Inputs.get(0);
    scenario2Outputs = scenario2Outputs.stream().filter(elem -> elem.getFactIdentifier().equals(input2.getFactIdentifier())).collect(toList());
    List<ScenarioResult> scenario2Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario2Outputs, input2, expressionEvaluatorFactory);
    assertEquals(1, scenario2Results.size());
    assertEquals(FactMappingValueStatus.SUCCESS, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
    List<ScenarioExpect> newFact = singletonList(new ScenarioExpect(personFactIdentifier, emptyList(), true));
    List<ScenarioResult> scenario2NoResults = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), newFact, input2, expressionEvaluatorFactory);
    assertEquals(0, scenario2NoResults.size());
    Person person = new Person();
    person.setFirstName("ANOTHER STRING");
    InstanceGiven newInput = new InstanceGiven(personFactIdentifier, person);
    List<ScenarioResult> scenario3Results = runnerHelper.getScenarioResultsFromGivenFacts(simulation.getScesimModelDescriptor(), scenario1Outputs, newInput, expressionEvaluatorFactory);
    assertEquals(FactMappingValueStatus.FAILED_WITH_ERROR, scenario1Outputs.get(0).getExpectedResult().get(0).getStatus());
    assertEquals(1, scenario3Results.size());
    assertEquals(person.getFirstName(), scenario3Results.get(0).getResultValue().get());
    assertEquals("NAME", scenario3Results.get(0).getFactMappingValue().getRawValue());
}
Also used : ScenarioExpect(org.drools.scenariosimulation.backend.runner.model.ScenarioExpect) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 4 with Person

use of org.drools.scenariosimulation.backend.model.Person in project drools by kiegroup.

the class RuleScenarioRunnerHelperTest method extractBackgroundValues.

@Test
public void extractBackgroundValues() {
    // TEST 0 - empty background
    Background emptyBackground = new Background();
    List<InstanceGiven> emptyBackgroundGivens = runnerHelper.extractBackgroundValues(emptyBackground, classLoader, expressionEvaluatorFactory);
    assertEquals(0, emptyBackgroundGivens.size());
    emptyBackground.addData();
    emptyBackgroundGivens = runnerHelper.extractBackgroundValues(emptyBackground, classLoader, expressionEvaluatorFactory);
    assertEquals(0, emptyBackgroundGivens.size());
    // TEST 1 - background correct
    List<InstanceGiven> backgroundGivens = runnerHelper.extractBackgroundValues(this.background, classLoader, expressionEvaluatorFactory);
    assertEquals(3, backgroundGivens.size());
    for (InstanceGiven backgroundGiven : backgroundGivens) {
        if (backgroundGiven.getFactIdentifier().equals(personFactIdentifier)) {
            assertEquals(personFactIdentifier, backgroundGiven.getFactIdentifier());
            Person person = (Person) backgroundGiven.getValue();
            assertEquals(NAME, person.getFirstName());
        } else if (backgroundGiven.getFactIdentifier().equals(disputeFactIdentifier)) {
            assertEquals(disputeFactIdentifier, backgroundGiven.getFactIdentifier());
            Dispute dispute = (Dispute) backgroundGiven.getValue();
            double parsedAmount = Double.parseDouble(AMOUNT);
            assertEquals(parsedAmount, dispute.getAmount(), 0.1);
        } else {
            fail();
        }
    }
    // TEST 2 - broken background
    String notValid = "notValid";
    FactMappingValue notValid1 = backgroundData1.addOrUpdateMappingValue(disputeFactIdentifier, amountGivenExpressionIdentifier, notValid);
    FactMappingValue notValid2 = backgroundData2.addOrUpdateMappingValue(disputeFactIdentifier, amountGivenExpressionIdentifier, notValid);
    assertThatThrownBy(() -> runnerHelper.extractBackgroundValues(this.background, classLoader, expressionEvaluatorFactory)).isInstanceOf(ScenarioException.class).hasMessage("Error in BACKGROUND data");
    assertEquals(FactMappingValueStatus.FAILED_WITH_EXCEPTION, notValid1.getStatus());
    assertTrue(notValid1.getExceptionMessage().startsWith("Impossible to parse"));
    assertEquals(FactMappingValueStatus.FAILED_WITH_EXCEPTION, notValid2.getStatus());
    assertTrue(notValid2.getExceptionMessage().startsWith("Impossible to parse"));
}
Also used : Background(org.drools.scenariosimulation.api.model.Background) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) InstanceGiven(org.drools.scenariosimulation.backend.runner.model.InstanceGiven) Dispute(org.drools.scenariosimulation.backend.model.Dispute) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Example 5 with Person

use of org.drools.scenariosimulation.backend.model.Person in project drools by kiegroup.

the class ScenarioBeanUtilTest method navigateToObjectTest.

@Test
public void navigateToObjectTest() {
    Dispute dispute = new Dispute();
    Person creator = new Person();
    creator.setFirstName(FIRST_NAME);
    dispute.setCreator(creator);
    List<String> pathToProperty = Arrays.asList("creator", "firstName");
    ScenarioBeanWrapper<?> scenarioBeanWrapper = ScenarioBeanUtil.navigateToObject(dispute, pathToProperty, true);
    Object targetObject = scenarioBeanWrapper.getBean();
    assertEquals(targetObject, FIRST_NAME);
    assertNull(ScenarioBeanUtil.navigateToObject(null, Collections.emptyList()).getBean());
}
Also used : Dispute(org.drools.scenariosimulation.backend.model.Dispute) Person(org.drools.scenariosimulation.backend.model.Person) SubPerson(org.drools.scenariosimulation.backend.model.SubPerson) EnumTest(org.drools.scenariosimulation.backend.util.model.EnumTest) Test(org.junit.Test) RuleScenarioRunnerHelperTest(org.drools.scenariosimulation.backend.runner.RuleScenarioRunnerHelperTest)

Aggregations

Person (org.drools.scenariosimulation.backend.model.Person)7 Test (org.junit.Test)7 AbstractRuleCoverageTest (org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest)6 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)3 Dispute (org.drools.scenariosimulation.backend.model.Dispute)3 InstanceGiven (org.drools.scenariosimulation.backend.runner.model.InstanceGiven)3 ArrayList (java.util.ArrayList)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 ScenarioExpect (org.drools.scenariosimulation.backend.runner.model.ScenarioExpect)2 Map (java.util.Map)1 Background (org.drools.scenariosimulation.api.model.Background)1 SubPerson (org.drools.scenariosimulation.backend.model.SubPerson)1 RuleScenarioRunnerHelperTest (org.drools.scenariosimulation.backend.runner.RuleScenarioRunnerHelperTest)1 ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)1 ScenarioRunnerData (org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerData)1