Skip to content

Commit

Permalink
refact statemachine and ruleengine to using junit5 and jdk17
Browse files Browse the repository at this point in the history
  • Loading branch information
significantfrank committed Apr 21, 2024
1 parent bf350e5 commit c9de978
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 113 deletions.
15 changes: 0 additions & 15 deletions cola-components/cola-component-ruleengine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,5 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ public Fact<?> getFact(String factName) {
.orElse(null);
}

public boolean contains(String factName){
return getFact(factName) != null;
}

public boolean contains(Fact fact){
if(fact == null){
return false;
}
return getFact(fact.getName()) != null;
}

public int size(){
return facts.size();
}

/**
* Return a copy of the facts as a map. It is not intended to manipulate
* facts outside of the rules engine (aka other than manipulating them through rules).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
import com.alibaba.cola.ruleengine.api.Rule;
import com.alibaba.cola.ruleengine.api.RuleEngine;
import com.alibaba.cola.ruleengine.core.*;
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CompositeRuleTest {

RuleEngine fizzBuzzEngine;

@Before
@BeforeEach
public void setUp(){
fizzBuzzEngine = new DefaultRuleEngine();
}
Expand All @@ -28,7 +27,7 @@ public void test_fizz_first(){
Rule rule = assembleRules(1,2,3);

fizzBuzzEngine.fire(rule, facts);
assertThat(facts.getFact("fizz").getValue()).isEqualTo("Fizz");
assertEquals(facts.getFact("fizz").getValue(), "Fizz");
}

@Test
Expand All @@ -39,7 +38,7 @@ public void test_buzz_first(){
Rule rule = assembleRules(2,1,3);

fizzBuzzEngine.fire(rule, facts);
assertThat(facts.getFact("buzz").getValue()).isEqualTo("Buzz");
assertEquals(facts.getFact("buzz").getValue(), "Buzz");
}

@Test
Expand All @@ -50,8 +49,8 @@ public void test_fizzBuzz_first(){
Rule rule = assembleRules(2,3,1);

fizzBuzzEngine.fire(rule, facts);
assertThat(facts.getFact("fizz").getValue()).isEqualTo("Fizz");
assertThat(facts.getFact("buzz").getValue()).isEqualTo("Buzz");
assertEquals(facts.getFact("fizz").getValue(), "Fizz");
assertEquals(facts.getFact("buzz").getValue(), "Buzz");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.alibaba.cola.ruleengine.api.Fact;
import com.alibaba.cola.ruleengine.api.Facts;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;
import org.junit.Test;

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


public class FactsTest {

Expand All @@ -18,9 +16,9 @@ public void factsMustHaveUniqueName() {
facts.add(new Fact<>("foo", 1));
facts.add(new Fact<>("foo", 2));

assertThat(facts).hasSize(1);

Fact<?> fact = facts.getFact("foo");
assertThat(fact.getValue()).isEqualTo(2);
assertEquals(fact.getValue(),2);
}

@Test
Expand All @@ -30,17 +28,17 @@ public void testAdd() {
facts.add(fact1);
facts.add(fact2);

assertThat(facts).contains(fact1);
assertThat(facts).contains(fact2);
assertTrue(facts.contains(fact1));
assertTrue(facts.contains(fact2));
}

@Test
public void testPut() {
facts.put("foo", 1);
facts.put("bar", 2);

assertThat(facts).contains(new Fact<>("foo", 1));
assertThat(facts).contains(new Fact<>("bar", 2));
assertTrue(facts.contains(new Fact<>("foo", 1)));
assertTrue(facts.contains(new Fact<>("bar", 2)));
}

@Test
Expand All @@ -49,7 +47,7 @@ public void testRemove() {
facts.add(foo);
facts.remove(foo);

assertThat(facts).isEmpty();
assertTrue(facts.size() == 0);
}

@Test
Expand All @@ -58,23 +56,23 @@ public void testRemoveByName() {
facts.add(foo);
facts.remove("foo");

assertThat(facts).isEmpty();
assertTrue(facts.size() == 0);
}

@Test
public void testGet() {
Fact<Integer> fact = new Fact<>("foo", 1);
facts.add(fact);
Integer value = facts.get("foo");
assertThat(value).isEqualTo(1);
assertEquals(value, 1);
}

@Test
public void testGetFact() {
Fact<Integer> fact = new Fact<>("foo", 1);
facts.add(fact);
Fact<?> retrievedFact = facts.getFact("foo");
assertThat(retrievedFact).isEqualTo(fact);
assertEquals(retrievedFact, fact);
}

@Test
Expand All @@ -84,16 +82,17 @@ public void testAsMap() {
facts.add(fact1);
facts.add(fact2);
Map<String, Object> map = facts.asMap();
assertThat(map).containsKeys("foo", "bar");
assertThat(map).containsValues(1, 2);
assertTrue(map.containsKey("foo"));
assertTrue(map.containsKey("bar"));
}

@Test
public void testClear() {
Facts facts = new Facts();
facts.add(new Fact<>("foo", 1));
facts.clear();
assertThat(facts).isEmpty();

assertTrue(facts.size() == 0);
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package com.alibaba.cola.ruleengine;

import com.alibaba.cola.ruleengine.api.Action;
import com.alibaba.cola.ruleengine.api.Facts;
import com.alibaba.cola.ruleengine.api.Rule;
import com.alibaba.cola.ruleengine.api.RuleEngine;
import com.alibaba.cola.ruleengine.core.*;
import org.junit.Before;
import org.junit.Test;
import com.alibaba.cola.ruleengine.core.DefaultRule;
import com.alibaba.cola.ruleengine.core.DefaultRuleEngine;
import com.alibaba.cola.ruleengine.core.NaturalRules;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

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

public class PriorityTest {

RuleEngine ruleEngine;

@Before
@BeforeEach
public void setUp() {
ruleEngine = new DefaultRuleEngine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.alibaba.cola.ruleengine.api.Rule;
import com.alibaba.cola.ruleengine.api.RuleEngine;
import com.alibaba.cola.ruleengine.core.*;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class RuleBuilderTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.alibaba.cola.ruleengine.api.Rule;
import com.alibaba.cola.ruleengine.api.RuleEngine;
import com.alibaba.cola.ruleengine.core.*;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class RuleEngineTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

//import com.alibaba.cola.ruleengine.fizzbuzz.v1.FizzBuzz;
import com.alibaba.cola.ruleengine.fizzbuzz.v2.FizzBuzz;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;


public class FizzBuzzTest {

Expand All @@ -14,7 +15,7 @@ public void num_given_1() {
//when
String result = FizzBuzz.count(input);
//then
Assertions.assertThat(result).isEqualTo("1");
assertEquals(result, "1");
}

@Test
Expand All @@ -24,8 +25,7 @@ public void fizz_given_3() {
//when
String result = FizzBuzz.count(input);
//then
Assertions.assertThat(result).isEqualTo("Fizz");

assertEquals(result, "Fizz");
}

@Test
Expand All @@ -35,7 +35,7 @@ public void buzz_given_5() {
//when
String result = FizzBuzz.count(input);
//then
Assertions.assertThat(result).isEqualTo("Buzz");
assertEquals(result, "Buzz");
}

@Test
Expand All @@ -45,6 +45,6 @@ public void fizz_buzz_given_15() {
//when
String result = FizzBuzz.count(input);
//then
Assertions.assertThat(result).isEqualTo("FizzBuzz");
assertEquals(result, "FizzBuzz");
}
}
8 changes: 8 additions & 0 deletions cola-components/cola-component-statemachine/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<parent>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-components-parent</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


/**
* @author dingchenchen
Expand Down Expand Up @@ -55,11 +56,11 @@ public void testChoice(){

StateMachine<StateMachineTest.States, StateMachineTest.Events, Context> stateMachine = builder.build("ChoiceConditionMachine");
StateMachineTest.States target1 = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context("1"));
Assert.assertEquals(StateMachineTest.States.STATE1,target1);
Assertions.assertEquals(StateMachineTest.States.STATE1,target1);
StateMachineTest.States target2 = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context("2"));
Assert.assertEquals(StateMachineTest.States.STATE2,target2);
Assertions.assertEquals(StateMachineTest.States.STATE2,target2);
StateMachineTest.States target3 = stateMachine.fireEvent(StateMachineTest.States.STATE1, StateMachineTest.Events.EVENT1, new Context("3"));
Assert.assertEquals(StateMachineTest.States.STATE3,target3);
Assertions.assertEquals(StateMachineTest.States.STATE3,target3);
}

private Condition<Context> checkCondition1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import com.alibaba.cola.statemachine.impl.Debugger;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.stream.Stream;

Expand Down Expand Up @@ -83,7 +83,7 @@ public boolean isSystemEvent(){
}
}

@Before
@BeforeEach
public void init(){
Debugger.enableDebug();
}
Expand Down

0 comments on commit c9de978

Please sign in to comment.