Skip to content

Commit

Permalink
[incubator-kie-issues#1771] Fixing BooleanEvalHelper to avoid class c…
Browse files Browse the repository at this point in the history
…ast exceptions
  • Loading branch information
Gabriele-Cardosi committed Jan 23, 2025
1 parent 2e8d384 commit 6d310fa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand Down Expand Up @@ -205,15 +205,13 @@ public static boolean isEqualsStringCompare(Object value, Object itemFromList) {
* @return
*/
public static Boolean getBooleanOrDialectDefault(Object rawReturn, FEELDialect feelDialect) {
if (feelDialect.equals(FEELDialect.BFEEL)) {
if (rawReturn instanceof Boolean bool) {
return bool;
} else {
return false;
}
} else {
return (Boolean) rawReturn;
Boolean toReturn = null;
if (rawReturn instanceof Boolean bool) {
toReturn = bool;
} else if (feelDialect.equals(FEELDialect.BFEEL)) {
toReturn = false;
}
return toReturn;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand Down Expand Up @@ -58,7 +58,12 @@ private static Collection<Object[]> data() {
{ "false and false or true", Boolean.TRUE , null},
{ "false and (false or true)", Boolean.FALSE , null},
{ "true or false and false", Boolean.TRUE , null},
{ "(true or false) and false", Boolean.FALSE , null}
{ "(true or false) and false", Boolean.FALSE , null},
//
{ "123 and false", Boolean.FALSE , null},
{ "\"true\" and false", Boolean.FALSE , null},
{ "123 or true", Boolean.TRUE , null},
{ "\"true\" or true", Boolean.TRUE , null}
};
return addAdditionalParameters(cases, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.mockito.Mockito;

import static org.assertj.core.api.Assertions.assertThat;
import static org.kie.dmn.feel.util.BooleanEvalHelper.getBooleanOrDialectDefault;
import static org.kie.dmn.feel.util.BooleanEvalHelper.getFalseOrDialectDefault;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;

Expand All @@ -50,6 +52,38 @@ void numericValuesComparative() {
assertThat(BooleanEvalHelper.compare(BigInteger.valueOf(1), 2.3, FEELDialect.FEEL,(l, r) -> l.compareTo(r) == 0)).isFalse();
}

@Test
void getBooleanOrDialectDefaultFEEL() {
assertThat(getBooleanOrDialectDefault(false, FEELDialect.FEEL)).isEqualTo(Boolean.FALSE);
assertThat(getBooleanOrDialectDefault(true, FEELDialect.FEEL)).isEqualTo(Boolean.TRUE);
assertThat(getBooleanOrDialectDefault("true", FEELDialect.FEEL)).isNull();
assertThat(getBooleanOrDialectDefault(null, FEELDialect.FEEL)).isNull();
}

@Test
void getBooleanOrDialectDefaultBFEEL() {
assertThat(getBooleanOrDialectDefault(false, FEELDialect.BFEEL)).isEqualTo(Boolean.FALSE);
assertThat(getBooleanOrDialectDefault(true, FEELDialect.BFEEL)).isEqualTo(Boolean.TRUE);
assertThat(getBooleanOrDialectDefault("true", FEELDialect.BFEEL)).isEqualTo(Boolean.FALSE);
assertThat(getBooleanOrDialectDefault(null, FEELDialect.BFEEL)).isEqualTo(Boolean.FALSE);
}

@Test
void getFalseOrDialectDefaultFEEL() {
assertThat(getFalseOrDialectDefault(false, FEELDialect.FEEL)).isEqualTo(Boolean.FALSE);
assertThat(getFalseOrDialectDefault(true, FEELDialect.FEEL)).isNull();
assertThat(getFalseOrDialectDefault("true", FEELDialect.FEEL)).isNull();
assertThat(getFalseOrDialectDefault(null, FEELDialect.FEEL)).isNull();
}

@Test
void getFalseOrDialectDefaultBFEEL() {
assertThat(getFalseOrDialectDefault(false, FEELDialect.BFEEL)).isEqualTo(Boolean.FALSE);
assertThat(getFalseOrDialectDefault(true, FEELDialect.BFEEL)).isEqualTo(Boolean.FALSE);
assertThat(getFalseOrDialectDefault("true", FEELDialect.BFEEL)).isEqualTo(Boolean.FALSE);
assertThat(getFalseOrDialectDefault(null, FEELDialect.BFEEL)).isEqualTo(Boolean.FALSE);
}

@Test
void isEqualsSCWithStringValue() {
assertThat(BooleanEvalHelper.isEqualsStringCompare("", "")).isTrue();
Expand Down

0 comments on commit 6d310fa

Please sign in to comment.