Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-5329 Review externalizing lambda failure case
  3. DROOLS-5346

Enhance unit test to detect non-externalized lambda widely

XMLWordPrintable

    • Icon: Sub-task Sub-task
    • Resolution: Done
    • Icon: Major Major
    • 7.38.0.Final
    • 7.37.0.Final
    • executable model
    • None
    • 2020 Week 19-21 (from May 4), 2020 Week 22-24 (from May 25), 2020 Week 25-27 (from Jun 15), 2020 Week 28-30 (from Jul 6)
    • 2
    • NEW
    • NEW

      Currently "lambda is not externalized" issue is not widely detected in unit tests. We need to write a specific assertion like this:

          @Test
          public void testExternalizeBindingVariableLambda() throws Exception {
              String str =
                      "package defaultpkg;\n" +
                      "import " + Person.class.getCanonicalName() + ";" +
                      "global java.util.List list;\n" +
                      "rule R when\n" +
                      "  $p : Person($n : name == \"Mario\")\n" +
                      "then\n" +
                      "  list.add($n);\n" +
                      "end";
      
              KieModuleModel kieModuleModel = KieServices.get().newKieModuleModel();
              kieModuleModel.setConfigurationProperty("drools.externaliseCanonicalModelLambda", Boolean.TRUE.toString());
      
              KieSession ksession = getKieSession(kieModuleModel, str );
              final List<String> list = new ArrayList<>();
              ksession.setGlobal("list", list);
      
              if (testRunType == RUN_TYPE.FLOW_DSL || testRunType == RUN_TYPE.PATTERN_DSL) {
                  RuleImpl rule = (RuleImpl)ksession.getKieBase().getRule("defaultpkg", "R");
                  Pattern pattern = (Pattern)rule.getLhs().getChildren().get(0);
                  Declaration declaration = pattern.getDeclarations().get("$n");
                  LambdaReadAccessor lambdaReadAccessor = (LambdaReadAccessor)declaration.getExtractor();
                  Field field = LambdaReadAccessor.class.getDeclaredField("lambda");
                  field.setAccessible(true);
                  Function1.Impl function1 = (Function1.Impl)field.get(lambdaReadAccessor);
                  Object lambda = function1.getLambda();
                  assertThat(lambda.getClass().getName(), containsString("LambdaExtractor")); // materialized Lambda
              }
      
              Person me = new Person( "Mario", 40 );
              ksession.insert( me );
              ksession.fireAllRules();
      
              Assertions.assertThat(list).containsExactlyInAnyOrder("Mario");
          }
      

      This JIRA is to enhance unit tests to assert it widely.

              rhn-support-tkobayas Toshiya Kobayashi
              rhn-support-tkobayas Toshiya Kobayashi
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: