Search in sources :

Example 16 with Simulation

use of org.drools.scenariosimulation.api.model.Simulation in project drools by kiegroup.

the class DMNScenarioRunnerHelperTest method init.

@Before
public void init() {
    when(dmnScenarioExecutableBuilderMock.run()).thenReturn(mock(RequestContext.class));
    simulation = new Simulation();
    settings = new Settings();
    settings.setType(ScenarioSimulationModel.Type.DMN);
    settings.setDmnFilePath(DMN_FILE_PATH);
    personFactIdentifier = FactIdentifier.create("Fact 1", "Fact 1");
    firstNameGivenExpressionIdentifier = ExpressionIdentifier.create("First Name Given", FactMappingType.GIVEN);
    firstNameGivenFactMapping = simulation.getScesimModelDescriptor().addFactMapping(personFactIdentifier, firstNameGivenExpressionIdentifier);
    firstNameGivenFactMapping.addExpressionElement("Fact 1", String.class.getCanonicalName());
    firstNameGivenFactMapping.addExpressionElement("firstName", String.class.getCanonicalName());
    disputeFactIdentifier = FactIdentifier.create("Fact 2", "Fact 2");
    amountGivenExpressionIdentifier = ExpressionIdentifier.create("Amount Given", FactMappingType.GIVEN);
    amountNameGivenFactMapping = simulation.getScesimModelDescriptor().addFactMapping(disputeFactIdentifier, amountGivenExpressionIdentifier);
    amountNameGivenFactMapping.addExpressionElement("Fact 2", BigDecimal.class.getCanonicalName());
    amountNameGivenFactMapping.addExpressionElement("amount", BigDecimal.class.getCanonicalName());
    firstNameExpectedExpressionIdentifier = ExpressionIdentifier.create("First Name Expected", FactMappingType.EXPECT);
    firstNameExpectedFactMapping = simulation.getScesimModelDescriptor().addFactMapping(personFactIdentifier, firstNameExpectedExpressionIdentifier);
    firstNameExpectedFactMapping.addExpressionElement("Fact 1", String.class.getCanonicalName());
    firstNameExpectedFactMapping.addExpressionElement("firstName", String.class.getCanonicalName());
    amountExpectedExpressionIdentifier = ExpressionIdentifier.create("Amount Expected", FactMappingType.EXPECT);
    amountNameExpectedFactMapping = simulation.getScesimModelDescriptor().addFactMapping(disputeFactIdentifier, amountExpectedExpressionIdentifier);
    amountNameExpectedFactMapping.addExpressionElement("Fact 2", Double.class.getCanonicalName());
    amountNameExpectedFactMapping.addExpressionElement("amount", Double.class.getCanonicalName());
    scenario1 = simulation.addData();
    scenario1.setDescription(TEST_DESCRIPTION);
    scenario1.addMappingValue(personFactIdentifier, firstNameGivenExpressionIdentifier, FEEL_EXPRESSION_NAME);
    firstNameExpectedValue = scenario1.addMappingValue(personFactIdentifier, firstNameExpectedExpressionIdentifier, FEEL_EXPRESSION_NAME);
    scenario2 = simulation.addData();
    scenario2.setDescription(TEST_DESCRIPTION);
    scenario2.addMappingValue(personFactIdentifier, firstNameGivenExpressionIdentifier, FEEL_EXPRESSION_NAME);
    scenario2.addMappingValue(personFactIdentifier, firstNameExpectedExpressionIdentifier, FEEL_EXPRESSION_NAME);
    scenario2.addMappingValue(disputeFactIdentifier, amountGivenExpressionIdentifier, AMOUNT);
    amountNameExpectedFactMappingValue = scenario2.addMappingValue(disputeFactIdentifier, amountExpectedExpressionIdentifier, AMOUNT);
    when(requestContextMock.get(DMNScenarioExecutableBuilder.DMN_RESULT)).thenReturn(dmnResultMock);
    when(requestContextMock.get(DMNScenarioExecutableBuilder.DMN_MODEL)).thenReturn(dmnModelMock);
}
Also used : Simulation(org.drools.scenariosimulation.api.model.Simulation) RequestContext(org.kie.api.runtime.RequestContext) Mockito.anyString(org.mockito.Mockito.anyString) Settings(org.drools.scenariosimulation.api.model.Settings) BigDecimal(java.math.BigDecimal) Before(org.junit.Before)

Example 17 with Simulation

use of org.drools.scenariosimulation.api.model.Simulation in project drools-wb by kiegroup.

the class RULEScenarioValidationTest method validate.

@Test
public void validate() {
    RULEScenarioValidation validation = new RULEScenarioValidation();
    // Test 0 - skip empty or not GIVEN/EXPECT columns
    Simulation test0 = new Simulation();
    test0.getScesimModelDescriptor().addFactMapping(FactIdentifier.DESCRIPTION, ExpressionIdentifier.create(VALUE, FactMappingType.OTHER));
    test0.getScesimModelDescriptor().addFactMapping(FactIdentifier.EMPTY, ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
    List<FactMappingValidationError> errorsTest0 = validation.validate(test0, settingsLocal, kieContainerMock);
    checkResult(errorsTest0);
    // Test 1 - simple type
    Simulation test1 = new Simulation();
    test1.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", int.class.getCanonicalName()), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
    List<FactMappingValidationError> errorsTest1 = validation.validate(test1, settingsLocal, kieContainerMock);
    checkResult(errorsTest1);
    FactMapping mySimpleType = test1.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", "notValidClass"), ExpressionIdentifier.create(VALUE, FactMappingType.GIVEN));
    mySimpleType.addExpressionElement("notValidClass", "notValidClass");
    errorsTest1 = validation.validate(test1, settingsLocal, kieContainerMock);
    checkResult(errorsTest1, new ExpectedError("Impossible to load class notValidClass"));
    // Test 2 - nested field
    Simulation test2 = new Simulation();
    // nameFM is valid
    FactIdentifier myFactIdentifier = FactIdentifier.create("mySimpleType", SampleBean.class.getCanonicalName());
    FactMapping nameFM = test2.getScesimModelDescriptor().addFactMapping(myFactIdentifier, ExpressionIdentifier.create("name", FactMappingType.GIVEN));
    nameFM.addExpressionElement("SampleBean", String.class.getCanonicalName());
    nameFM.addExpressionElement("name", String.class.getCanonicalName());
    // parentFM is valid
    FactMapping parentFM = test2.getScesimModelDescriptor().addFactMapping(myFactIdentifier, ExpressionIdentifier.create("parent", FactMappingType.EXPECT));
    parentFM.addExpressionElement("SampleBean", SampleBean.class.getCanonicalName());
    parentFM.addExpressionElement("parent", SampleBean.class.getCanonicalName());
    List<FactMappingValidationError> errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock);
    checkResult(errorsTest2);
    // parentFM is not valid anymore
    parentFM.addExpressionElement("notExisting", String.class.getCanonicalName());
    errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field with name 'notExisting' in class org.drools.workbench.screens.scenariosimulation.backend.server.SampleBean"));
    // nameWrongTypeFM has a wrong type
    FactMapping nameWrongTypeFM = test2.getScesimModelDescriptor().addFactMapping(myFactIdentifier, ExpressionIdentifier.create("parent2", FactMappingType.EXPECT));
    nameWrongTypeFM.addExpressionElement("SampleBean", Integer.class.getCanonicalName());
    nameWrongTypeFM.addExpressionElement("name", Integer.class.getCanonicalName());
    errorsTest2 = validation.validate(test2, settingsLocal, kieContainerMock);
    checkResult(errorsTest2, new ExpectedError("Impossible to find field with name 'notExisting' in class org.drools.workbench.screens.scenariosimulation.backend.server.SampleBean"), new ExpectedError(ScenarioSimulationI18nServerMessage.SCENARIO_VALIDATION_FIELD_CHANGED_ERROR, Arrays.asList("java.lang.Integer", "java.lang.String")));
    // Test 3 - list
    Simulation test3 = new Simulation();
    // topLevelListFM is valid
    FactMapping topLevelListFM = test3.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("mySimpleType", List.class.getCanonicalName()), ExpressionIdentifier.create("name", FactMappingType.GIVEN));
    topLevelListFM.addExpressionElement("List", List.class.getCanonicalName());
    topLevelListFM.setGenericTypes(Collections.singletonList(String.class.getCanonicalName()));
    // addressesFM is valid
    FactMapping addressesFM = test3.getScesimModelDescriptor().addFactMapping(myFactIdentifier, ExpressionIdentifier.create("addresses", FactMappingType.EXPECT));
    addressesFM.addExpressionElement("SampleBean", List.class.getCanonicalName());
    addressesFM.addExpressionElement("addresses", List.class.getCanonicalName());
    addressesFM.setGenericTypes(Collections.singletonList(String.class.getCanonicalName()));
    List<FactMappingValidationError> errorsTest3 = validation.validate(test3, settingsLocal, kieContainerMock);
    checkResult(errorsTest3);
}
Also used : FactMappingValidationError(org.drools.workbench.screens.scenariosimulation.model.FactMappingValidationError) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) List(java.util.List) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Test(org.junit.Test)

Example 18 with Simulation

use of org.drools.scenariosimulation.api.model.Simulation in project drools-wb by kiegroup.

the class ScenarioRunnerServiceImplTest method makeScenarioSimulationModel.

private ScenarioSimulationModel makeScenarioSimulationModel(boolean toSkip) {
    Simulation simulation = new Simulation();
    Settings settings = new Settings();
    settings.setType(Type.RULE);
    settings.setSkipFromBuild(toSkip);
    ScenarioSimulationModel scenarioSimulationModel = new ScenarioSimulationModel();
    scenarioSimulationModel.setSimulation(simulation);
    scenarioSimulationModel.setSettings(settings);
    scenarioSimulationModel.setBackground(new Background());
    return scenarioSimulationModel;
}
Also used : Simulation(org.drools.scenariosimulation.api.model.Simulation) Background(org.drools.scenariosimulation.api.model.Background) Settings(org.drools.scenariosimulation.api.model.Settings) ScenarioSimulationModel(org.drools.scenariosimulation.api.model.ScenarioSimulationModel)

Example 19 with Simulation

use of org.drools.scenariosimulation.api.model.Simulation in project drools-wb by kiegroup.

the class ScenarioSimulationServiceImplTest method runScenario.

@Test
public void runScenario() throws Exception {
    doReturn("test userMock").when(userMock).getIdentifier();
    final Path path = mock(Path.class);
    Simulation simulation = new Simulation();
    Settings settings = new Settings();
    Background background = new Background();
    service.runScenario(path, simulation.getScesimModelDescriptor(), simulation.getScenarioWithIndex(), settings, background);
    verify(scenarioRunnerServiceMock).runTest("test userMock", path, simulation.getScesimModelDescriptor(), simulation.getScenarioWithIndex(), settings, background);
}
Also used : Path(org.uberfire.backend.vfs.Path) Simulation(org.drools.scenariosimulation.api.model.Simulation) Background(org.drools.scenariosimulation.api.model.Background) Settings(org.drools.scenariosimulation.api.model.Settings) Test(org.junit.Test)

Example 20 with Simulation

use of org.drools.scenariosimulation.api.model.Simulation in project drools-wb by kiegroup.

the class ScenarioValidationServiceTest method validateSimulationStructure.

@Test
public void validateSimulationStructure() {
    Simulation simulation = new Simulation();
    Settings settings = new Settings();
    ScenarioValidationService scenarioValidationServiceSpy = spy(new ScenarioValidationService() {

        @Override
        protected List<FactMappingValidationError> validateDMN(Simulation simulation, Settings settings, KieContainer kieContainer) {
            return Collections.emptyList();
        }

        @Override
        protected List<FactMappingValidationError> validateRULE(Simulation simulation, Settings settings, KieContainer kieContainer) {
            return Collections.emptyList();
        }

        @Override
        protected KieContainer getKieContainer(Path path) {
            return kieContainerMock;
        }
    });
    settings.setType(ScenarioSimulationModel.Type.DMN);
    scenarioValidationServiceSpy.validateSimulationStructure(simulation, settings, pathMock);
    verify(scenarioValidationServiceSpy, never()).validateDMN(eq(simulation), eq(settings), eq(kieContainerMock));
    verify(scenarioValidationServiceSpy, never()).validateRULE(eq(simulation), eq(settings), eq(kieContainerMock));
    reset(scenarioValidationServiceSpy);
    FactMapping sampleFactMapping = simulation.getScesimModelDescriptor().addFactMapping(FactIdentifier.create("sample", String.class.getCanonicalName()), ExpressionIdentifier.create("sample", FactMappingType.GIVEN));
    sampleFactMapping.addExpressionElement("sample", String.class.getCanonicalName());
    settings.setType(ScenarioSimulationModel.Type.DMN);
    scenarioValidationServiceSpy.validateSimulationStructure(simulation, settings, pathMock);
    verify(scenarioValidationServiceSpy, timeout(1)).validateDMN(eq(simulation), eq(settings), eq(kieContainerMock));
    reset(scenarioValidationServiceSpy);
    settings.setType(ScenarioSimulationModel.Type.RULE);
    scenarioValidationServiceSpy.validateSimulationStructure(simulation, settings, pathMock);
    verify(scenarioValidationServiceSpy, timeout(1)).validateRULE(eq(simulation), eq(settings), eq(kieContainerMock));
    settings.setType(null);
    assertThatThrownBy(() -> scenarioValidationServiceSpy.validateSimulationStructure(simulation, settings, pathMock)).isInstanceOf(IllegalArgumentException.class);
}
Also used : Path(org.uberfire.backend.vfs.Path) FactMapping(org.drools.scenariosimulation.api.model.FactMapping) Simulation(org.drools.scenariosimulation.api.model.Simulation) List(java.util.List) Settings(org.drools.scenariosimulation.api.model.Settings) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Simulation (org.drools.scenariosimulation.api.model.Simulation)30 Settings (org.drools.scenariosimulation.api.model.Settings)13 Test (org.junit.Test)13 FactMapping (org.drools.scenariosimulation.api.model.FactMapping)12 Background (org.drools.scenariosimulation.api.model.Background)11 ScesimModelDescriptor (org.drools.scenariosimulation.api.model.ScesimModelDescriptor)11 ScenarioSimulationModel (org.drools.scenariosimulation.api.model.ScenarioSimulationModel)9 Path (org.uberfire.backend.vfs.Path)9 List (java.util.List)8 Before (org.junit.Before)7 Scenario (org.drools.scenariosimulation.api.model.Scenario)6 ScenarioWithIndex (org.drools.scenariosimulation.api.model.ScenarioWithIndex)6 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 AbstractScesimModel (org.drools.scenariosimulation.api.model.AbstractScesimModel)4 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)4 Inject (javax.inject.Inject)3 FactModelTuple (org.drools.workbench.screens.scenariosimulation.model.typedescriptor.FactModelTuple)3 Matchers.anyString (org.mockito.Matchers.anyString)3