From c779976bee7c2100935a3be94288659e403ff557 Mon Sep 17 00:00:00 2001 From: nprentza Date: Mon, 10 Jul 2023 13:20:15 +0300 Subject: [PATCH] test setup --- .../ReliabilityFireAndAlarmTest.java | 108 ++++++++++++++++++ .../infinispan/ReliabilityTestBasics.java | 14 +++ .../org/test/domain/fireandalarm/Alarm.java | 6 + .../org/test/domain/fireandalarm/Fire.java | 21 ++++ .../org/test/domain/fireandalarm/Room.java | 26 +++++ .../test/domain/fireandalarm/Sprinkler.java | 35 ++++++ 6 files changed, 210 insertions(+) create mode 100644 drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityFireAndAlarmTest.java create mode 100644 drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Alarm.java create mode 100644 drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Fire.java create mode 100644 drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Room.java create mode 100644 drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Sprinkler.java diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityFireAndAlarmTest.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityFireAndAlarmTest.java new file mode 100644 index 000000000000..47bdb500efe2 --- /dev/null +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityFireAndAlarmTest.java @@ -0,0 +1,108 @@ +package org.drools.reliability.infinispan; + +import org.drools.core.ClassObjectFilter; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.kie.api.runtime.conf.PersistedSessionOption; +import org.kie.api.runtime.rule.FactHandle; +import org.test.domain.fireandalarm.Alarm; +import org.test.domain.fireandalarm.Fire; +import org.test.domain.fireandalarm.Room; +import org.test.domain.fireandalarm.Sprinkler; + +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ReliabilityFireAndAlarmTest extends ReliabilityTestBasics{ + private static final String FIRE_AND_ALARM = + "import " + Alarm.class.getCanonicalName() + ";" + + "import " + Fire.class.getCanonicalName() + ";" + + "import " + Sprinkler.class.getCanonicalName() + ";" + + "import " + Room.class.getCanonicalName() + ";" + + "global java.util.List results;" + + "rule 'When there is a fire turn on the sprinkler' when\n" + + " Fire($room : room) \n" + + " $sprinkler: Sprinkler( room == $room, on == false ) \n" + + "then\n" + + " modify($sprinkler) { setOn(true) }; \n" + + " System.out.println(\"Turn on the sprinkler for room\" + $room.getName()); \n" + + "end\n" + + "rule 'Raise the alarm when we have one or more firs' when\n" + + " exists Fire() \n" + + "then\n" + + " insert( new Alarm() );\n" + + " System.out.println(\"Raise the alarm\");\n" + + "end\n"+ + "rule 'Cancel the alarm when all the fires have gone' when \n" + + " not Fire() \n" + + " $alarm : Alarm() \n" + + "then\n" + + " delete ( $alarm ); \n" + + " System.out.println(\"Cancel the alarm\"); \n" + + "end\n" + + "rule 'Status output when things are ok' when\n" + + " not Alarm() \n" + + " not Sprinkler ( on == true ) \n" + + "then \n" + + " System.out.println(\"Everything is ok\"); \n" + + "end"; + + @ParameterizedTest + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") + void testNoFailover(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){ + createSession(FIRE_AND_ALARM, persistenceStrategy, safepointStrategy); + + // phase 1 + Room room1 = new Room("Room 1"); + insert(room1); + FactHandle fireFact1 = insert(new Fire(room1)); + fireAllRules(); + + // phase 2 + Sprinkler sprinkler1 = new Sprinkler(room1); + insert(sprinkler1); + fireAllRules(); + + assertThat(sprinkler1.isOn()).isTrue(); + + // phase 3 + delete(fireFact1); + fireAllRules(); + } + + @ParameterizedTest + @MethodSource("strategyProviderStoresOnlyWithExplicitSafepoints") + void testPhase1FailoverPhase2FailoverPhase3(PersistedSessionOption.PersistenceStrategy persistenceStrategy, PersistedSessionOption.SafepointStrategy safepointStrategy){ + createSession(FIRE_AND_ALARM, persistenceStrategy, safepointStrategy); + + // phase 1 + Room room1 = new Room("Room 1"); + insert(room1); + FactHandle fireFact1 = insert(new Fire(room1)); + fireAllRules(); + + //failover(); + //restoreSession(FIRE_AND_ALARM, persistenceStrategy,safepointStrategy); + + // phase 2 + Sprinkler sprinkler1 = new Sprinkler(room1); + FactHandle sprinklerFact2 = insert(sprinkler1); + fireAllRules(); + + sprinkler1 = (Sprinkler) sessions.get(0).getObjects(new ClassObjectFilter(Sprinkler.class)).stream().collect(Collectors.toList()).get(0); + + assertThat(sprinkler1.isOn()).isTrue(); + + failover(); + restoreSession(FIRE_AND_ALARM, persistenceStrategy,safepointStrategy); + + // phase 3 + Optional fhToDelete = getFactHandle(room1); + if (!fhToDelete.isEmpty()){ + delete(fhToDelete.get()); + } + fireAllRules(); + } +} diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java index d389bc79daa9..6ef711f7e91b 100644 --- a/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/drools/reliability/infinispan/ReliabilityTestBasics.java @@ -47,6 +47,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.test.domain.Person; +import org.test.domain.fireandalarm.Room; import java.util.ArrayList; import java.util.Collection; @@ -352,6 +353,19 @@ protected Optional getFactHandle(KieSession kieSession, Person perso .filter(p -> ( (Person) p.getObject()).getAge()==person.getAge() ).findFirst(); } + protected Optional getFactHandle(Room room){ + return getFactHandle(sessions.get(0), room); + } + + protected Optional getFactHandle(KieSession kieSession, Room room){ + return kieSession.getFactHandles() + .stream() + .filter(r -> r.getObject() instanceof Room) + .filter(r -> ( (Room) r.getObject()).getName().equals(room.getName()) ) + .findFirst(); + } + + private static class OptionsFilter { private final Option[] options; diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Alarm.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Alarm.java new file mode 100644 index 000000000000..e25215b97dc0 --- /dev/null +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Alarm.java @@ -0,0 +1,6 @@ +package org.test.domain.fireandalarm; + +import java.io.Serializable; + +public class Alarm implements Serializable { +} diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Fire.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Fire.java new file mode 100644 index 000000000000..5e10af965e2a --- /dev/null +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Fire.java @@ -0,0 +1,21 @@ +package org.test.domain.fireandalarm; + +import java.io.Serializable; + +public class Fire implements Serializable { + private Room room; + + public Fire() { } + + public Fire(Room room) { + this.room = room; + } + + public Room getRoom() { + return room; + } + + public void setRoom(Room room) { + this.room = room; + } +} diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Room.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Room.java new file mode 100644 index 000000000000..425be179275c --- /dev/null +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Room.java @@ -0,0 +1,26 @@ +package org.test.domain.fireandalarm; + +import java.io.Serializable; + +public class Room implements Serializable { + private String name; + + public Room() { } + + public Room(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } +} diff --git a/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Sprinkler.java b/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Sprinkler.java new file mode 100644 index 000000000000..610d7fd18828 --- /dev/null +++ b/drools-reliability/drools-reliability-infinispan/src/test/java/org/test/domain/fireandalarm/Sprinkler.java @@ -0,0 +1,35 @@ +package org.test.domain.fireandalarm; + +import java.io.Serializable; + +public class Sprinkler implements Serializable { + private Room room; + private boolean on = false; + + public Sprinkler() { } + + public Sprinkler(Room room) { + this.room = room; + } + + public Room getRoom() { + return room; + } + + public void setRoom(Room room) { + this.room = room; + } + + public boolean isOn() { + return on; + } + + public void setOn(boolean on) { + this.on = on; + } + + @Override + public String toString() { + return "Sprinkler for " + room; + } +}