Skip to content

Commit

Permalink
NO ISSUE: Migrate tests in drools traits to JUnit5. (#6122)
Browse files Browse the repository at this point in the history
* First steps

* More small cleanups

* More cleanups...

* More cleanups...

* More cleanups...

* More cleanups...

* More cleanups, progressing

* Fixed size assertions

* Fixed instanceof in tests

* Fixed equals in assertj

* Cleaned up isTrue assertions

* Proper use of assertions on exceptions

* Removed last bits of JUnit5 from tests
  • Loading branch information
pibizza authored Oct 11, 2024
1 parent faa1d6e commit 23fa0a3
Show file tree
Hide file tree
Showing 16 changed files with 2,455 additions and 2,884 deletions.
6 changes: 3 additions & 3 deletions drools-traits/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.drools.traits;

import org.drools.kiesession.agenda.DefaultAgenda;
import org.junit.jupiter.api.Test;
import org.drools.core.reteoo.ReteDumper;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.event.rule.DebugAgendaEventListener;
import org.kie.api.io.ResourceType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.drools.kiesession.rulebase.KnowledgeBaseFactory;
import org.drools.traits.core.base.evaluators.IsAEvaluatorDefinition;
import org.junit.BeforeClass;
import org.junit.jupiter.api.BeforeAll;
import org.kie.api.KieBase;
import org.kie.api.io.ResourceType;
import org.kie.internal.builder.KnowledgeBuilder;
Expand All @@ -33,7 +33,7 @@

public class CommonTraitTest {

@BeforeClass
@BeforeAll
public static void beforeClass() {
System.setProperty(EvaluatorOption.PROPERTY_NAME + "isA", IsAEvaluatorDefinition.class.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@
package org.drools.traits.compiler.factmodel.traits;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.drools.base.factmodel.traits.Trait;
import org.drools.base.factmodel.traits.Traitable;
import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.drools.kiesession.rulebase.KnowledgeBaseFactory;
import org.drools.traits.compiler.CommonTraitTest;
import org.drools.traits.core.factmodel.LogicalTypeInconsistencyException;
import org.drools.traits.core.factmodel.TraitFactoryImpl;
import org.drools.traits.core.factmodel.VirtualPropertyMode;
import org.drools.traits.core.util.StandaloneTraitFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.kie.api.KieBase;
import org.kie.api.builder.Message;
import org.kie.api.definition.type.PropertyReactive;
Expand All @@ -47,41 +45,26 @@
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.drools.traits.compiler.factmodel.traits.TraitTestUtils.createStandaloneTraitFactory;

@RunWith(Parameterized.class)
public class LegacyTraitTest extends CommonTraitTest {

private static final Logger LOGGER = LoggerFactory.getLogger(LegacyTraitTest.class);

public VirtualPropertyMode mode;

@Parameterized.Parameters
public static Collection modes() {
return Arrays.asList( new VirtualPropertyMode[][]
{
{ VirtualPropertyMode.MAP },
{ VirtualPropertyMode.TRIPLES }
} );
}

public LegacyTraitTest( VirtualPropertyMode m ) {
this.mode = m;
public static Collection<VirtualPropertyMode> modes() {
return List.of(VirtualPropertyMode.MAP, VirtualPropertyMode.TRIPLES);
}



private KieSession getSessionFromString( String drl ) {
private KieSession getSessionFromString(String drl) {
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add( ResourceFactory.newByteArrayResource( drl.getBytes() ),
ResourceType.DRL );
knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()),
ResourceType.DRL);
if (knowledgeBuilder.hasErrors()) {
throw new RuntimeException( knowledgeBuilder.getErrors().toString() );
throw new RuntimeException(knowledgeBuilder.getErrors().toString());
}

InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages( knowledgeBuilder.getKnowledgePackages() );
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());

KieSession session = kbase.newKieSession();
return session;
Expand Down Expand Up @@ -149,8 +132,9 @@ public void setPers(Pers pers) {
}


@Test
public void traitWithPojoInterface() {
@ParameterizedTest
@MethodSource("modes")
public void traitWithPojoInterface(VirtualPropertyMode mode) {
String source = "package org.drools.compiler.test;\n" +
"import " + LegacyTraitTest.Procedure.class.getCanonicalName() + ";\n" +
"import " + LegacyTraitTest.class.getCanonicalName() + ";\n" +
Expand All @@ -160,7 +144,7 @@ public void traitWithPojoInterface() {
// enhanced so that declaration is not needed
// "declare ProcedureImpl end " +
"declare trait ExtendedProcedure " +
" @role( event )" +
" @role(event)" +
"end " +

// Surgery must be declared as trait, since it does not extend Thing
Expand All @@ -174,54 +158,54 @@ public void traitWithPojoInterface() {
"when " +
" $p : ExtendedProcedure() " +
"then " +
" don( $p, Surgery.class ); " +
" don($p, Surgery.class); " +
"end " +

"rule 'Test 1' " +
"dialect 'mvel' " +
"when " +
" $s1 : ExtendedProcedure( $subject : subject ) " +
" $s2 : ExtendedProcedure( subject == $subject ) " +
" $s1 : ExtendedProcedure($subject : subject) " +
" $s2 : ExtendedProcedure(subject == $subject) " +
"then " +
"end " +

"rule 'Test 2' " +
"dialect 'mvel' " +
"when " +
" $s1 : ExtendedProcedure( $subject : subject.name ) " +
" $s2 : ExtendedProcedure( subject.name == $subject ) " +
" $s1 : ExtendedProcedure($subject : subject.name) " +
" $s2 : ExtendedProcedure(subject.name == $subject) " +
"then " +
"end " +

"rule 'Test 3' " +
"dialect 'mvel' " +
"when " +
" $s1 : ExtendedProcedure( ) " +
" $s1 : ExtendedProcedure() " +
"then " +
" update( $s1 ); " +
" update($s1); " +
"end " +
"\n";

KieSession ks = getSessionFromString( source );
TraitFactoryImpl.setMode(mode, ks.getKieBase() );
KieSession ks = getSessionFromString(source);
TraitFactoryImpl.setMode(mode, ks.getKieBase());

ExtendedProcedureImpl procedure1 = new ExtendedProcedureImpl();
ExtendedProcedureImpl procedure2 = new ExtendedProcedureImpl();

PatientImpl patient1 = new PatientImpl();
patient1.setName("John");
procedure1.setSubject( patient1 );
procedure1.setSubject(patient1);
procedure1.setPers(new PatientImpl());

PatientImpl patient2 = new PatientImpl();
patient2.setName("John");
procedure2.setSubject( patient2 );
procedure2.setSubject(patient2);
procedure2.setPers(new PatientImpl());

ks.insert( procedure1 );
ks.insert( procedure2 );
ks.insert(procedure1);
ks.insert(procedure2);

ks.fireAllRules( 500 );
ks.fireAllRules(500);
}


Expand All @@ -237,8 +221,9 @@ public static interface Trunk extends Root { }
@Trait
public static interface Foo extends Trunk { }

@Test
public void traitWithMixedInterfacesExtendingEachOther() {
@ParameterizedTest
@MethodSource("modes")
public void traitWithMixedInterfacesExtendingEachOther(VirtualPropertyMode mode) {
String source = "package org.drools.compiler.test;" +
"import " + BarImpl.class.getCanonicalName() + "; " +
"import " + Foo.class.getCanonicalName() + "; " +
Expand All @@ -256,67 +241,64 @@ public void traitWithMixedInterfacesExtendingEachOther() {

"rule 'Bar Don'" +
"when " +
" $b : BarImpl( this isA Foo.class, this not isA Foo2.class )\n" +
" $b : BarImpl(this isA Foo.class, this not isA Foo2.class)\n" +
" String()\n" +
"then " +
" list.add( 3 ); " +
" retract( $b ); " +
" list.add(3); " +
" retract($b); " +
"end " +

"rule 'Don Bar' " +
"no-loop " +
"when " +
" $b : Foo( ) " +
" $b : Foo() " +
"then " +
" list.add( 1 ); " +
" don( $b, Foo2.class ); " +
" list.add(1); " +
" don($b, Foo2.class); " +
"end " +

"rule 'Cant really shed Foo but Foo2' " +
"when " +
" $b : Foo2() " +
"then " +
" list.add( 2 ); " +
" shed( $b, Foo.class ); " +
" insert( \"done\" );" +
" list.add(2); " +
" shed($b, Foo.class); " +
" insert(\"done\");" +
"end " +

"";


KieSession ks = getSessionFromString( source );
KieSession ks = getSessionFromString(source);
KieBase kieBase = ks.getKieBase();
TraitFactoryImpl.setMode(mode, kieBase);
ArrayList list = new ArrayList();
ks.setGlobal( "list", list );
List<Integer> list = new ArrayList<>();
ks.setGlobal("list", list);

ks.insert( new BarImpl() );
ks.insert(new BarImpl());

int n = ks.fireAllRules();

LOGGER.debug( list.toString() );
assertThat(list).isEqualTo(Arrays.asList(1, 2, 3));
LOGGER.debug(list.toString());
assertThat(list).containsExactly(1, 2, 3);
assertThat(n).isEqualTo(3);
}



@Test
public void testTraitWithNonAccessorMethodShadowing() {
@ParameterizedTest
@MethodSource("modes")
public void testTraitWithNonAccessorMethodShadowing(VirtualPropertyMode mode) throws Exception {
StandaloneTraitFactory factory = createStandaloneTraitFactory();
try {
SomeInterface r = (SomeInterface) factory.don( new SomeClass(), SomeInterface.class );
r.prepare();
assertThat(r.getFoo()).isEqualTo(42);
assertThat(r.doThis("that")).isEqualTo("I did that");
} catch ( LogicalTypeInconsistencyException e ) {
e.printStackTrace();
fail( e.getMessage() );
}
SomeInterface r = (SomeInterface) factory.don(new SomeClass(), SomeInterface.class);
r.prepare();
assertThat(r.getFoo()).isEqualTo(42);
assertThat(r.doThis("that")).isEqualTo("I did that");
}

@Test()
public void testPojoExtendInterface() {
@ParameterizedTest
@MethodSource("modes")
public void testPojoExtendInterface(VirtualPropertyMode mode) {
// DROOLS-697
// It is now allowed for a declared type to extend an interface
// The interface itself will be added to the implements part of the generated class
Expand All @@ -331,9 +313,9 @@ public void testPojoExtendInterface() {
"";

KieHelper kh = new KieHelper();
kh.addContent( s1, ResourceType.DRL );
kh.addContent(s1, ResourceType.DRL);

assertThat(kh.verify().getMessages(Message.Level.ERROR).size()).isEqualTo(0);
assertThat(kh.verify().getMessages(Message.Level.ERROR)).isEmpty();
}

}
Loading

0 comments on commit 23fa0a3

Please sign in to comment.