Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-drools-6136] Migrate drools tests to JUnit5 - #8 #6179

Merged
merged 12 commits into from
Dec 4, 2024
10 changes: 5 additions & 5 deletions drools-ecj/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
<artifactId>ecj</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.kie.memorycompiler.CompilationResult;
import org.kie.memorycompiler.resources.MemoryResourceReader;
import org.kie.memorycompiler.resources.MemoryResourceStore;
import org.junit.Test;

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

Expand Down
6 changes: 5 additions & 1 deletion drools-persistence/drools-persistence-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.drools.persistence.api.TransactionManagerFactory;
import org.drools.persistence.jta.JtaTransactionManager;
import org.drools.persistence.jta.JtaTransactionManagerFactory;
import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.kie.api.runtime.Environment;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -34,7 +34,7 @@ public class TransactionManagerFactoryTest {
TransactionManagerFactory transactionManagerFactory =
getTransactionManagerFactory();

@After
@AfterEach
public void cleanup() {
System.clearProperty("org.kie.txm.factory.class");
transactionManagerFactory.resetInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.drools.core.impl.RuleBaseFactory;
import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.drools.kiesession.rulebase.KnowledgeBaseFactory;
import org.drools.mvel.CommonTestMethodBase;
import org.drools.mvel.compiler.Cheese;
import org.drools.persistence.util.DroolsPersistenceUtil;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.kie.api.KieBase;
import org.kie.api.command.Command;
import org.kie.api.io.ResourceType;
Expand All @@ -59,29 +57,18 @@
import static org.drools.persistence.util.DroolsPersistenceUtil.cleanUp;
import static org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment;

@RunWith(Parameterized.class)
public class MoreBatchExecutionPersistenceTest extends CommonTestMethodBase {
public class MoreBatchExecutionPersistenceTest {


private KieSession ksession = null;

private Map<String, Object> context;
private boolean locking;

@Parameters(name="{0}")
public static Collection<Object[]> persistence() {
Object[][] locking = new Object[][] {
{ OPTIMISTIC_LOCKING },
{ PESSIMISTIC_LOCKING }
};
return Arrays.asList(locking);
public static Stream<String> parameters() {
return Stream.of(OPTIMISTIC_LOCKING, PESSIMISTIC_LOCKING);
};

public MoreBatchExecutionPersistenceTest(String locking) {
this.locking = PESSIMISTIC_LOCKING.equals(locking);
}

@After
@AfterEach
public void cleanUpPersistence() throws Exception {
disposeKSession();
cleanUp(context);
Expand All @@ -95,29 +82,30 @@ public void disposeKSession() {
}
}

protected StatefulKnowledgeSession createKnowledgeSession(KieBase kbase) {
private StatefulKnowledgeSession createKnowledgeSession(String locking, KieBase kbase) {
if( context == null ) {
context = DroolsPersistenceUtil.setupWithPoolingDataSource(DROOLS_PERSISTENCE_UNIT_NAME);
}
KieSessionConfiguration ksconf = RuleBaseFactory.newKnowledgeSessionConfiguration();
Environment env = createEnvironment(context);
if( this.locking ) {
if(PESSIMISTIC_LOCKING.equals(locking)) {
env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
}
return JPAKnowledgeService.newStatefulKnowledgeSession(kbase, ksconf, env);

}

@Test
public void testFireAllRules() {
@ParameterizedTest(name="{0}")
@MethodSource("parameters")
public void testFireAllRules(String locking) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("org/drools/mvel/integrationtests/drl/test_ImportFunctions.drl"), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ksession = createKnowledgeSession(kbase);
ksession = createKnowledgeSession(locking, kbase);

final Cheese cheese = new Cheese("stilton", 15);
ksession.insert(cheese);
Expand All @@ -143,16 +131,17 @@ public void testFireAllRules() {
assertThat(list.get(3)).isEqualTo("rule4");
}

@Test
public void testQuery() {
@ParameterizedTest(name="{0}")
@MethodSource("parameters")
public void testQuery(String locking) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("org/drools/mvel/integrationtests/simple_query_test.drl"), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ksession = createKnowledgeSession(kbase);
ksession = createKnowledgeSession(locking, kbase);

ksession.insert( new Cheese( "stinky", 5 ) );
ksession.insert( new Cheese( "smelly", 7 ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@
package org.drools.persistence.command;

import static org.assertj.core.api.Assertions.assertThat;
import static org.drools.persistence.util.DroolsPersistenceUtil.OPTIMISTIC_LOCKING;
import static org.drools.persistence.util.DroolsPersistenceUtil.PESSIMISTIC_LOCKING;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.drools.core.impl.RuleBaseFactory;
import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.drools.kiesession.rulebase.KnowledgeBaseFactory;
import org.drools.mvel.CommonTestMethodBase;
import org.drools.mvel.compiler.command.SimpleBatchExecutionTest;
import org.drools.persistence.util.DroolsPersistenceUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.kie.api.KieBase;
import org.kie.api.command.Command;
import org.kie.api.io.ResourceType;
Expand All @@ -54,39 +52,28 @@
import org.kie.internal.persistence.jpa.JPAKnowledgeService;
import org.kie.internal.runtime.StatefulKnowledgeSession;

@RunWith(Parameterized.class)
public class SimpleBatchExecutionPersistenceTest extends CommonTestMethodBase {
public class SimpleBatchExecutionPersistenceTest {

private Map<String, Object> context;
private boolean locking;

@Parameters(name="{0}")
public static Collection<Object[]> persistence() {
Object[][] locking = new Object[][] {
{ DroolsPersistenceUtil.OPTIMISTIC_LOCKING },
{ DroolsPersistenceUtil.PESSIMISTIC_LOCKING }
};
return Arrays.asList(locking);
public static Stream<String> parameters() {
return Stream.of(OPTIMISTIC_LOCKING, PESSIMISTIC_LOCKING);
};

public SimpleBatchExecutionPersistenceTest(String locking) {
this.locking = DroolsPersistenceUtil.PESSIMISTIC_LOCKING.equals(locking);
};

@After
@AfterEach
public void cleanUpPersistence() throws Exception {
disposeKSession();
DroolsPersistenceUtil.cleanUp(context);
context = null;
}

protected StatefulKnowledgeSession createKnowledgeSession(KieBase kbase) {
private StatefulKnowledgeSession createKnowledgeSession(String locking, KieBase kbase) {
if( context == null ) {
context = DroolsPersistenceUtil.setupWithPoolingDataSource(DroolsPersistenceUtil.DROOLS_PERSISTENCE_UNIT_NAME);
}
KieSessionConfiguration ksconf = RuleBaseFactory.newKnowledgeSessionConfiguration();
Environment env = DroolsPersistenceUtil.createEnvironment(context);
if( this.locking ) {
if(PESSIMISTIC_LOCKING.equals(locking)) {
env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
}
return JPAKnowledgeService.newStatefulKnowledgeSession(kbase, ksconf, env);
Expand All @@ -102,29 +89,29 @@ protected StatefulKnowledgeSession createKnowledgeSession(KieBase kbase) {
+ " then\n"
+ "end\n";

@Before
public void createKSession() throws Exception {
private void createKSession(String locking) throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newByteArrayResource(ruleString.getBytes()), ResourceType.DRL );
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
assertThat(kbuilder.hasErrors()).isFalse();
kbase.addPackages( kbuilder.getKnowledgePackages() );

ksession = createKnowledgeSession(kbase);
ksession = createKnowledgeSession(locking, kbase);
}

@After
@AfterEach
public void disposeKSession() throws Exception {
if( ksession != null ) {
ksession.dispose();
ksession = null;
}
}

@Test
@ParameterizedTest(name="{0}")
@MethodSource("parameters")
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertObjectCommand() throws Exception {

public void testInsertObjectCommand(String locking) throws Exception {
createKSession(locking);
String expected_1 = "expected_1";
String expected_2 = "expected_2";

Expand Down Expand Up @@ -152,10 +139,11 @@ public void testInsertObjectCommand() throws Exception {
assertThat(expectedList.isEmpty()).as("Retrieved object list did not contain expected objects.").isTrue();
}

@Test
@ParameterizedTest(name="{0}")
@MethodSource("parameters")
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertElementsCommand() throws Exception {

public void testInsertElementsCommand(String locking) throws Exception {
createKSession(locking);
String expected_1 = "expected_1";
String expected_2 = "expected_2";
Object [] expectedArr = {expected_1, expected_2};
Expand All @@ -181,10 +169,11 @@ public void testInsertElementsCommand() throws Exception {
assertThat(expectedList.isEmpty()).as("Retrieved object list did not contain expected objects.").isTrue();
}

@Test
@ParameterizedTest(name="{0}")
@MethodSource("parameters")
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testSetGlobalCommand() throws Exception {

public void testSetGlobalCommand(String locking) throws Exception {
createKSession(locking);
ksession.insert(new Integer(5));
ksession.insert(new Integer(7));
ksession.fireAllRules();
Expand All @@ -201,10 +190,11 @@ public void testSetGlobalCommand() throws Exception {
assertThat(global).isEqualTo("France");
}

@Test
@ParameterizedTest(name="{0}")
@MethodSource("parameters")
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetGlobalCommand() throws Exception {

public void testGetGlobalCommand(String locking) throws Exception {
createKSession(locking);
ksession.insert(new Integer(5));
ksession.insert(new Integer(7));
ksession.fireAllRules();
Expand All @@ -221,10 +211,11 @@ public void testGetGlobalCommand() throws Exception {
assertThat(global).as("Retrieved global is not equal to 'France'.").isEqualTo("France");
}

@Test
@ParameterizedTest(name="{0}")
@MethodSource("parameters")
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectCommand() throws Exception {

public void testGetObjectCommand(String locking) throws Exception {
createKSession(locking);
String expected_1 = "expected_1";
String expected_2 = "expected_2";
FactHandle handle_1 = ksession.insert( expected_1 );
Expand All @@ -247,10 +238,11 @@ public void testGetObjectCommand() throws Exception {
assertThat(result.getValue("out_2")).isEqualTo(expected_2);
}

@Test
@ParameterizedTest(name="{0}")
@MethodSource("parameters")
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectsCommand() throws Exception {

public void testGetObjectsCommand(String locking) throws Exception {
createKSession(locking);
String expected_1 = "expected_1";
String expected_2 = "expected_2";
FactHandle handle_1 = ksession.insert( expected_1 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
import org.drools.core.impl.EnvironmentFactory;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.kie.api.runtime.Environment;
import org.kie.api.runtime.EnvironmentName;

public class JtaTransactionManagerFactoryTest {

@BeforeClass
@BeforeAll
public static void setupOnce() throws NamingException {
InitialContext initContext = new InitialContext();
initContext.rebind("java:comp/UserTransaction", com.arjuna.ats.jta.UserTransaction.userTransaction());
Expand Down
Loading
Loading