Skip to content

Commit

Permalink
[DROOLS-7488] tests - update rule action method in DRL (#5381)
Browse files Browse the repository at this point in the history
* tests with update method in drl

* getFactFactHandleForPerson chaged to getFactHandle

* fail test if Optional FactHandle is empty
  • Loading branch information
nprentza authored Jul 12, 2023
1 parent ac7544a commit df0acd8
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void deleteAfterFailover_shouldNotMatch(PersistedSessionOption.PersistenceStrate
restoreSession(BASIC_RULE, persistenceStrategy, safepointStrategy);
clearResults();

Optional<FactHandle> getFactHandleForPerson = getFactHandleForPerson(pMike);
Optional<FactHandle> getFactHandleForPerson = getFactHandle(pMike);
if (!getFactHandleForPerson.isEmpty()){
delete(getFactHandleForPerson.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,11 @@ protected Optional<Person> getPersonByName(KieSession kieSession, String name) {
.filter(p -> p.getName().equals(name) ).findFirst();
}

protected Optional<FactHandle> getFactHandleForPerson(Person person){
return getFactHandleForPerson(sessions.get(0), person);
protected Optional<FactHandle> getFactHandle(Person person){
return getFactHandle(sessions.get(0), person);
}

protected Optional<FactHandle> getFactHandleForPerson(KieSession kieSession, Person person){
protected Optional<FactHandle> getFactHandle(KieSession kieSession, Person person){
return kieSession.getFactHandles()
.stream()
.filter(p -> p.getObject() instanceof Person)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@

package org.drools.reliability.infinispan;

import org.test.domain.Person;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.conf.PersistedSessionOption;
import org.kie.api.runtime.rule.FactHandle;
import org.test.domain.Person;

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -33,7 +37,7 @@ class ReliabilityUpdateInDrlTest extends ReliabilityTestBasics {
" $s: String()\n" +
" $p: Person( getName().startsWith($s), getAge()>17 )\n" +
"then\n" +
" results.add( $p.getAge() );\n" +
" results.add( $p.getName() );\n" +
"end\n" +
"rule Birthday when\n" +
" $a: Integer()\n" +
Expand All @@ -44,37 +48,213 @@ class ReliabilityUpdateInDrlTest extends ReliabilityTestBasics {
"end";

@ParameterizedTest
@MethodSource("strategyProviderStoresOnly")
void updateInRHS_insertFireFailoverFire_shouldMatchUpdatesFromFirstSession(PersistedSessionOption.PersistenceStrategy strategy){
@MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints")
void insertFireUpdateFailoverFire_shouldMatchUpdatesFromFirstSession(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){

createSession(RULE_UPDATE, strategy);
createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);

insert("M");
insertMatchingPerson("Mike",22);
insertNonMatchingPerson("Eleven", 17);
insert(17); // person with age=17 will change to 18 (17+1)

assertThat(fireAllRules()).isEqualTo(2); // person with name that starts with M and has age>17 will be added to the results list
assertThat(getResults()).containsExactlyInAnyOrder(22);
assertThat(getResults()).containsExactlyInAnyOrder("Mike");

failover();

restoreSession(RULE_UPDATE, strategy);
restoreSession(RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults();

insert("E"); // NonMatchingPerson will match rule X

assertThat(fireAllRules()).isEqualTo(1);
assertThat(getResults()).containsExactlyInAnyOrder(18);
assertThat(getResults()).containsExactlyInAnyOrder("Eleven");

failover();

restoreSession(RULE_UPDATE, strategy);
restoreSession(RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults();

assertThat(fireAllRules()).isZero();
assertThat(getResults()).isEmpty();
}

@ParameterizedTest
@MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints")
void insertFailoverInsertFailoverInsertFire_shouldRecoverAndMatchRules(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){
createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);

insert("M");
insertMatchingPerson("Mike",22);
insertNonMatchingPerson("Eleven", 17);

failover();

restoreSession(RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults();

insert(17);

failover();

restoreSession(RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults();

insert("E");

assertThat(fireAllRules()).isEqualTo(3);
assertThat(getResults()).containsExactlyInAnyOrder("Mike","Eleven");
}

@ParameterizedTest
@MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints")
void insertFailoverDeleteFailoverInsert_shouldRecoverAndMatchRules(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){
createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);

insert("M");
insert("E");
insertMatchingPerson("Mike",22);
Person pEleven = new Person("Eleven", 16);
insert(pEleven);

failover();

restoreSession(RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults();

Optional<FactHandle> getFactHandleForPerson = getFactHandle(pEleven);
assertThat(getFactHandleForPerson.isEmpty()).isFalse();
delete(getFactHandleForPerson.get());

failover();

restoreSession(RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults();

insert(16);
insertMatchingPerson("Eleven",16);

assertThat(fireAllRules()).isEqualTo(2);
assertThat(getResults()).containsExactlyInAnyOrder("Mike");
}

@ParameterizedTest
@MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints")
void multipleKieSession_insertFireUpdateInsertFailoverFire_shouldMatchUpdatesAfterFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy) {
KieSession session1 = createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);
insert(session1, "M");
insertMatchingPerson(session1, "Mike", 16); // rule X match

KieSession session2 = createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);
insertMatchingPerson(session2, "Eleven", 18); // no rule match

assertThat(fireAllRules(session1)).isEqualTo(0);
assertThat(fireAllRules(session2)).isEqualTo(0);

insert(session1, 16); // rule Birthday match (session 1)
insert(session2, "E");

failover();
session1 = restoreSession(session1.getIdentifier(), RULE_UPDATE, persistenceStrategy, safepointStrategy);
clearResults(session1);
session2 = restoreSession(session2.getIdentifier(), RULE_UPDATE, persistenceStrategy, safepointStrategy);
clearResults(session2);

assertThat(fireAllRules(session1)).isEqualTo(1);
assertThat(getResults(session1)).isEmpty();

assertThat(fireAllRules(session2)).isEqualTo(1);
assertThat(getResults(session2)).containsExactlyInAnyOrder("Eleven");

insert(session1, 17); // rule Birthday match (session 1)
insert(session2, 17); // rule Birthday will not match (session 2)

failover();
session1 = restoreSession(session1.getIdentifier(), RULE_UPDATE, persistenceStrategy, safepointStrategy);
clearResults(session1);
session2 = restoreSession(session2.getIdentifier(), RULE_UPDATE, persistenceStrategy, safepointStrategy);
clearResults(session2);

assertThat(fireAllRules(session1)).isEqualTo(2);
assertThat(getResults(session1)).containsExactlyInAnyOrder("Mike");

assertThat(fireAllRules(session2)).isEqualTo(0);
}

@ParameterizedTest
@MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints")
void multipleKieSession_insertFailoverInsertFailoverInsertFire_shouldRecoverAndMatchRules(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){
KieSession session1 = createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);
KieSession session2 = createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);

insert(session1,"M");
insertMatchingPerson(session1,"Mike",22);
insertNonMatchingPerson(session2,"Eleven", 17);

failover();

session1 = restoreSession(session1.getIdentifier(), RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults(session1);
session2 = restoreSession(session2.getIdentifier(), RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults(session2);

insert(session2,17);

failover();

session1 = restoreSession(session1.getIdentifier(), RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults(session1);
session2 = restoreSession(session2.getIdentifier(), RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults(session2);

insert(session2,"E");

assertThat(fireAllRules(session1)).isEqualTo(1);
assertThat(getResults(session1)).containsExactlyInAnyOrder("Mike");

assertThat(fireAllRules(session2)).isEqualTo(2);
assertThat(getResults(session2)).containsExactlyInAnyOrder("Eleven");
}

@ParameterizedTest
@MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints")
void multiplKieSession_insertFailoverDeleteFailoverInsert_shouldRecoverAndMatchRules(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){
KieSession session1 = createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);
KieSession session2 = createSession(RULE_UPDATE, persistenceStrategy, safepointStrategy);

insert(session1,"M");
insert(session2,"E");
insertMatchingPerson(session1,"Mike",22);
Person pEleven = new Person("Eleven", 16);
insert(session2, pEleven);

failover();

session1 = restoreSession(session1.getIdentifier(), RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults(session1);
session2 = restoreSession(session2.getIdentifier(), RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults(session2);

Optional<FactHandle> getFactHandleForPerson = getFactHandle(session2, pEleven);
assertThat(getFactHandleForPerson.isEmpty()).isFalse();
delete(session2, getFactHandleForPerson.get());

failover();

session1 = restoreSession(session1.getIdentifier(), RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults(session1);
session2 = restoreSession(session2.getIdentifier(), RULE_UPDATE, persistenceStrategy,safepointStrategy);
clearResults(session2);

insert(session2,16);
insertMatchingPerson(session2,"Eleven",16);

assertThat(fireAllRules(session1)).isEqualTo(1);
assertThat(getResults(session1)).containsExactlyInAnyOrder("Mike");
assertThat(fireAllRules(session2)).isEqualTo(1);
assertThat(getResults(session2)).isEmpty();
}

}

0 comments on commit df0acd8

Please sign in to comment.