From cdf3407c1df38d4bd72921c24b6cac789169863d Mon Sep 17 00:00:00 2001 From: Paulo Henrique Alves Date: Tue, 30 Jul 2024 15:49:56 -0300 Subject: [PATCH] Upgrade junit lib to 5; Update tests; Reactoring heritage. --- pom.xml | 7 ++-- .../eigr/spawn/api/actors/StatefulActor.java | 6 +-- .../java/io/eigr/spawn/internal/Entity.java | 2 +- .../eigr/spawn/AbstractContainerBaseTest.java | 18 +++++++-- src/test/java/io/eigr/spawn/SpawnTest.java | 35 +++++++++--------- src/test/java/io/eigr/spawn/WorkflowTest.java | 37 +++++++++---------- 6 files changed, 58 insertions(+), 47 deletions(-) diff --git a/pom.xml b/pom.xml index b0805ca..af5238f 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ 11 UTF-8 3.1.8 - 4.13.2 + 5.10.3 8.4.0 4.12.0 4.27.2 @@ -65,8 +65,8 @@ - junit - junit + org.junit.jupiter + junit-jupiter ${junit.version} test @@ -165,6 +165,7 @@ + ${project.build.directory}/generated-sources/protobuf ${project.build.directory}/generated-test-sources/protobuf diff --git a/src/main/java/io/eigr/spawn/api/actors/StatefulActor.java b/src/main/java/io/eigr/spawn/api/actors/StatefulActor.java index 9fc788e..e8eb613 100644 --- a/src/main/java/io/eigr/spawn/api/actors/StatefulActor.java +++ b/src/main/java/io/eigr/spawn/api/actors/StatefulActor.java @@ -3,10 +3,8 @@ import java.lang.reflect.ParameterizedType; public interface StatefulActor extends BaseActor { - default Class getStateType() { - return (Class) ((ParameterizedType) - getClass().getGenericSuperclass()) - .getActualTypeArguments()[0]; + default Class getStateType(Class entityClass) { + return entityClass; } @Override default Boolean isStateful() { diff --git a/src/main/java/io/eigr/spawn/internal/Entity.java b/src/main/java/io/eigr/spawn/internal/Entity.java index 725b92b..67bb828 100644 --- a/src/main/java/io/eigr/spawn/internal/Entity.java +++ b/src/main/java/io/eigr/spawn/internal/Entity.java @@ -110,7 +110,7 @@ public static Entity fromStatefulActorToEntity(BehaviorCtx ctx, Class actor) try { Constructor constructor = actor.getConstructor(); StatefulActor stActor = (StatefulActor) constructor.newInstance(); - Class stateType = stActor.getStateType(); + Class stateType = stActor.getStateType(stActor.getClass().getGenericSuperclass().getClass()); ActorBehavior behavior = stActor.configure(ctx); if (behavior.getClass().isAssignableFrom(NamedActorBehavior.class)) { diff --git a/src/test/java/io/eigr/spawn/AbstractContainerBaseTest.java b/src/test/java/io/eigr/spawn/AbstractContainerBaseTest.java index 6835f6e..a753994 100644 --- a/src/test/java/io/eigr/spawn/AbstractContainerBaseTest.java +++ b/src/test/java/io/eigr/spawn/AbstractContainerBaseTest.java @@ -11,6 +11,11 @@ import io.eigr.spawn.test.actors.JoeActor; import io.eigr.spawn.test.actors.StatelessNamedActor; import io.eigr.spawn.test.actors.UnNamedActor; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testcontainers.Testcontainers; @@ -20,14 +25,15 @@ abstract class AbstractContainerBaseTest { private static final Logger log = LoggerFactory.getLogger(AbstractContainerBaseTest.class); - private static final GenericContainer SPAWN_CONTAINER; + private static GenericContainer SPAWN_CONTAINER; private static final String spawnProxyImage = "eigr/spawn-proxy:1.4.1-rc.1"; private static final String userFunctionPort = "8091"; private static final String spawnProxyPort = "9004"; - protected static final Spawn spawnSystem; + protected static Spawn spawnSystem; protected static final String spawnSystemName = "spawn-system-test"; - static { + @BeforeAll + public static void setup() { Testcontainers.exposeHostPorts(8091); SPAWN_CONTAINER = new GenericContainer<>(DockerImageName.parse(spawnProxyImage)) @@ -73,5 +79,11 @@ abstract class AbstractContainerBaseTest { throw new RuntimeException(e); } } + + @AfterAll + public static void teardown() { + SPAWN_CONTAINER.stop(); + + } } diff --git a/src/test/java/io/eigr/spawn/SpawnTest.java b/src/test/java/io/eigr/spawn/SpawnTest.java index a7f50b1..c0f940f 100644 --- a/src/test/java/io/eigr/spawn/SpawnTest.java +++ b/src/test/java/io/eigr/spawn/SpawnTest.java @@ -8,24 +8,25 @@ import io.eigr.spawn.test.actors.JoeActor; import io.eigr.spawn.test.actors.StatelessNamedActor; import io.eigr.spawn.test.actors.UnNamedActor; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import java.util.Optional; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -public class SpawnTest extends AbstractContainerBaseTest { +class SpawnTest extends AbstractContainerBaseTest { @Test - public void testNamedInvocation() throws ActorCreationException, ActorInvocationException { + void testNamedInvocation() throws ActorCreationException, ActorInvocationException { ActorRef joeActor = spawnSystem.createActorRef( ActorIdentity.of(spawnSystemName, "JoeActor")); Class type = joeActor.getType(); - assertEquals(type, JoeActor.class); - assertNotNull(joeActor); + Assertions.assertEquals(type, JoeActor.class); + Assertions.assertNotNull(joeActor); Actor.Request msg = Actor.Request.newBuilder() .setLanguage("Erlang") @@ -36,20 +37,20 @@ public void testNamedInvocation() throws ActorCreationException, ActorInvocation if (maybeReply.isPresent()) { Actor.Reply reply = maybeReply.get(); - assertNotNull(reply); - assertEquals("Hi Erlang. Hello From Java", reply.getResponse()); + Assertions.assertNotNull(reply); + Assertions.assertEquals("Hi Erlang. Hello From Java", reply.getResponse()); } } @Test - public void testUnNamedInvocation() throws ActorCreationException, ActorInvocationException { + void testUnNamedInvocation() throws ActorCreationException, ActorInvocationException { ActorRef unNamedJoeActor = spawnSystem.createActorRef( ActorIdentity.of(spawnSystemName, "UnNamedJoeActor", "UnNamedActor")); Class type = unNamedJoeActor.getType(); - assertEquals(type, UnNamedActor.class); - assertNotNull(unNamedJoeActor); + Assertions.assertEquals(type, UnNamedActor.class); + Assertions.assertNotNull(unNamedJoeActor); Actor.Request msg = Actor.Request.newBuilder() .setLanguage("Erlang") @@ -60,20 +61,20 @@ public void testUnNamedInvocation() throws ActorCreationException, ActorInvocati if (maybeReply.isPresent()) { Actor.Reply reply = maybeReply.get(); - assertNotNull(reply); - assertEquals("Hi Erlang. Hello From Java", reply.getResponse()); + Assertions.assertNotNull(reply); + Assertions.assertEquals("Hi Erlang. Hello From Java", reply.getResponse()); } } @Test - public void testStatelessInvocation() throws ActorCreationException, ActorInvocationException { + void testStatelessInvocation() throws ActorCreationException, ActorInvocationException { ActorRef statelessNamedActor = spawnSystem.createActorRef( ActorIdentity.of(spawnSystemName, "StatelessNamedActor")); Class type = statelessNamedActor.getType(); - assertEquals(type, StatelessNamedActor.class); - assertNotNull(statelessNamedActor); + Assertions.assertEquals(type, StatelessNamedActor.class); + Assertions.assertNotNull(statelessNamedActor); Actor.Request msg = Actor.Request.newBuilder() .setLanguage("Elixir") @@ -84,8 +85,8 @@ public void testStatelessInvocation() throws ActorCreationException, ActorInvoca if (maybeReply.isPresent()) { Actor.Reply reply = maybeReply.get(); - assertNotNull(reply); - assertEquals("Hi Elixir. Hello From Java", reply.getResponse()); + Assertions.assertNotNull(reply); + Assertions.assertEquals("Hi Elixir. Hello From Java", reply.getResponse()); } } } diff --git a/src/test/java/io/eigr/spawn/WorkflowTest.java b/src/test/java/io/eigr/spawn/WorkflowTest.java index ccb06d0..3bbd2e4 100644 --- a/src/test/java/io/eigr/spawn/WorkflowTest.java +++ b/src/test/java/io/eigr/spawn/WorkflowTest.java @@ -9,52 +9,51 @@ import io.eigr.spawn.api.actors.workflows.SideEffect; import io.eigr.spawn.api.exceptions.SpawnException; import io.eigr.spawn.java.test.domain.Actor; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; - -public class WorkflowTest extends AbstractContainerBaseTest { +class WorkflowTest extends AbstractContainerBaseTest { private ActorRef joeActorRef; - @Before + @BeforeEach public void before() throws SpawnException { joeActorRef = spawnSystem.createActorRef( ActorIdentity.of(spawnSystemName, "JoeActor")); } @Test - public void testBroadcastBuilder() { + void testBroadcastBuilder() { Broadcast broadcast = Broadcast.to("test.channel", "hi", Actor.Request.getDefaultInstance()); final Protocol.Broadcast protocolBroadcast = broadcast.build(); - assertEquals("test.channel", protocolBroadcast.getChannelGroup()); - assertNotNull(protocolBroadcast.getValue()); + Assertions.assertEquals("test.channel", protocolBroadcast.getChannelGroup()); + Assertions.assertNotNull(protocolBroadcast.getValue()); } @Test - public void testForwardBuilder() throws Exception { + void testForwardBuilder() throws Exception { Forward forward = Forward.to(joeActorRef, "hi"); final Protocol.Forward protocolForward = forward.build(); - assertEquals("hi", protocolForward.getActionName()); - assertEquals("JoeActor", protocolForward.getActor()); + Assertions.assertEquals("hi", protocolForward.getActionName()); + Assertions.assertEquals("JoeActor", protocolForward.getActor()); } @Test - public void testPipeBuilder() throws Exception { + void testPipeBuilder() throws Exception { Pipe pipe = Pipe.to(joeActorRef, "hi"); final Protocol.Pipe protocolPipe = pipe.build(); - assertEquals("hi", protocolPipe.getActionName()); - assertEquals("JoeActor", protocolPipe.getActor()); + Assertions.assertEquals("hi", protocolPipe.getActionName()); + Assertions.assertEquals("JoeActor", protocolPipe.getActor()); } @Test - public void testSideEffectBuilder() throws Exception { + void testSideEffectBuilder() throws Exception { SideEffect effect = SideEffect.to(joeActorRef, "hi", Actor.Request.getDefaultInstance()); final Protocol.SideEffect protocolSideEffect = effect.build(); Protocol.InvocationRequest request = protocolSideEffect.getRequest(); - assertNotNull(request); - assertEquals("hi", request.getActionName()); - assertEquals("JoeActor", request.getActor().getId().getName()); + Assertions.assertNotNull(request); + Assertions.assertEquals("hi", request.getActionName()); + Assertions.assertEquals("JoeActor", request.getActor().getId().getName()); } }