From 639c94cc973714c8178541e5a666d6528900dcc3 Mon Sep 17 00:00:00 2001
From: Damien MARCHAT <dmarchat@takima.fr>
Date: Mon, 10 Mar 2025 13:58:40 +0100
Subject: [PATCH] trying ou test part

---
 pom.xml                                       | 24 ++++++
 .../temporal/TemporalQueues.java              |  7 ++
 .../temporal/TemporalUtils.java               |  3 -
 .../CookieWorkflowMockRealClusterTest.java    | 76 +++++++++++++++++++
 .../bakery/cookie/CookieWorkflowMockTest.java | 70 +++++++++++++++++
 .../bakery/cookie/CookieWorkflowTest.java     | 59 ++++++++++++++
 6 files changed, 236 insertions(+), 3 deletions(-)
 create mode 100644 src/main/java/io/takima/temporalpractice/temporal/TemporalQueues.java
 create mode 100644 src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowMockRealClusterTest.java
 create mode 100644 src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowMockTest.java
 create mode 100644 src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowTest.java

diff --git a/pom.xml b/pom.xml
index a6c3baa..6710515 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,6 +27,30 @@
 			<artifactId>temporal-spring-boot-starter</artifactId>
 			<version>1.27.1</version>
 		</dependency>
+		<dependency>
+			<groupId>org.junit.jupiter</groupId>
+			<artifactId>junit-jupiter-api</artifactId>
+			<version>5.11.4</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.junit.jupiter</groupId>
+			<artifactId>junit-jupiter-engine</artifactId>
+			<version>5.11.4</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.mockito</groupId>
+			<artifactId>mockito-core</artifactId>
+			<version>5.14.2</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.mockito</groupId>
+			<artifactId>mockito-junit-jupiter</artifactId>
+			<version>5.12.0</version>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
 
 	<build>
diff --git a/src/main/java/io/takima/temporalpractice/temporal/TemporalQueues.java b/src/main/java/io/takima/temporalpractice/temporal/TemporalQueues.java
new file mode 100644
index 0000000..39bbf54
--- /dev/null
+++ b/src/main/java/io/takima/temporalpractice/temporal/TemporalQueues.java
@@ -0,0 +1,7 @@
+package io.takima.temporalpractice.temporal;
+
+public class TemporalQueues {
+    public static final String COOKIE_QUEUE = "cookie";
+    public static final String BATTER_QUEUE = "batter";
+    public static final String BAKE_QUEUE = "bake";
+}
diff --git a/src/main/java/io/takima/temporalpractice/temporal/TemporalUtils.java b/src/main/java/io/takima/temporalpractice/temporal/TemporalUtils.java
index 5479688..b978e0c 100644
--- a/src/main/java/io/takima/temporalpractice/temporal/TemporalUtils.java
+++ b/src/main/java/io/takima/temporalpractice/temporal/TemporalUtils.java
@@ -11,9 +11,6 @@ import java.time.Duration;
 
 public class TemporalUtils {
 
-    public static final String COOKIE_QUEUE = "cookie";
-    public static final String BATTER_QUEUE = "batter";
-    public static final String BAKE_QUEUE = "bake";
     public static final String MASTERING_SIGNALS_QUEUE = "mastering-signals";
 
     // Represents the connection to your local cluster. For now, lets keep it simple
diff --git a/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowMockRealClusterTest.java b/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowMockRealClusterTest.java
new file mode 100644
index 0000000..27da6c6
--- /dev/null
+++ b/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowMockRealClusterTest.java
@@ -0,0 +1,76 @@
+package io.takima.temporalpractice.bakery.cookie;
+
+
+import io.takima.temporalpractice.bakery.batter.BatterServiceImpl;
+import io.takima.temporalpractice.bakery.bake.BakeServiceImpl;
+import io.takima.temporalpractice.bakery.batter.BatterService.Batter;
+import io.takima.temporalpractice.bakery.cookie.CookieWorkflow.*;
+import io.temporal.client.WorkflowClient;
+import io.temporal.testing.TestWorkflowEnvironment;
+import io.temporal.testing.TestWorkflowExtension;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+import static io.takima.temporalpractice.temporal.TemporalQueues.*;
+
+@ExtendWith(MockitoExtension.class)
+class CookieWorkflowMockRealClusterTest {
+
+    @RegisterExtension
+    public static final TestWorkflowExtension testWorkflowExtension =
+            TestWorkflowExtension.newBuilder()
+                    .useExternalService()
+                    .setNamespace("default")
+                    .registerWorkflowImplementationTypes(BestCookieWorkflow.class)
+                    .setDoNotStart(true)
+                    .build();
+
+    @Mock
+    private BatterServiceImpl mockBatterService;
+
+    @BeforeEach
+    void setUp(TestWorkflowEnvironment testEnv) {
+        when(mockBatterService.prepareCookieBatter(any()))
+                .thenReturn(new Batter(Topping.CHOCOLATE, 90));
+
+        testEnv.newWorker(COOKIE_QUEUE)
+                .registerWorkflowImplementationTypes(BestCookieWorkflow.class);
+        testEnv.newWorker(BATTER_QUEUE)
+                .registerActivitiesImplementations(mockBatterService);
+        testEnv.newWorker(BAKE_QUEUE)
+                .registerActivitiesImplementations(new BakeServiceImpl());
+        testEnv.start();
+    }
+
+    @Test
+    void testFullWorkflowWithMockedBatterService(CookieWorkflow cookieWorkflow) {
+        CookiePreference preferences = new CookiePreference(3, Baking.SOFT, Topping.CHOCOLATE);
+
+        WorkflowClient.start(cookieWorkflow::orderCookie, preferences);
+
+        OrderStatus statusBeforeSignal = cookieWorkflow.getOrderStatus();
+        assertFalse(statusBeforeSignal.ovenReady(), "Oven should not be ready yet");
+
+        cookieWorkflow.ovenReady();
+
+        OrderStatus statusAfterSignal = cookieWorkflow.getOrderStatus();
+        assertTrue(statusAfterSignal.ovenReady(), "Oven should now be ready");
+
+        CookieOrder result = cookieWorkflow.orderCookie(preferences);
+        assertNotNull(result, "Resulting CookieOrder should not be null");
+        assertNotNull(result.cookies(), "Cookies should not be null");
+        assertEquals(3, result.cookies().size(), "Should produce exactly 3 cookies");
+
+        for (Cookie cookie : result.cookies()) {
+            assertEquals(Baking.SOFT, cookie.baking(), "Cookie should be SOFT");
+            assertEquals(Topping.CHOCOLATE, cookie.topping(), "Cookie should have CHOCOLATE topping");
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowMockTest.java b/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowMockTest.java
new file mode 100644
index 0000000..462ad5b
--- /dev/null
+++ b/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowMockTest.java
@@ -0,0 +1,70 @@
+package io.takima.temporalpractice.bakery.cookie;
+
+import io.takima.temporalpractice.bakery.batter.BatterServiceImpl;
+import io.takima.temporalpractice.bakery.bake.BakeServiceImpl;
+import io.takima.temporalpractice.bakery.batter.BatterService.Batter;
+import io.takima.temporalpractice.bakery.cookie.CookieWorkflow.*;
+import io.temporal.client.WorkflowClient;
+import io.temporal.testing.TestWorkflowEnvironment;
+import io.temporal.testing.TestWorkflowExtension;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+import static io.takima.temporalpractice.temporal.TemporalQueues.*;
+
+@ExtendWith(MockitoExtension.class)
+class CookieWorkflowMockTest {
+
+    @RegisterExtension
+    public static final TestWorkflowExtension testWorkflowExtension =
+            TestWorkflowExtension.newBuilder()
+                    .registerWorkflowImplementationTypes(BestCookieWorkflow.class)
+                    .setDoNotStart(true)
+                    .build();
+
+    @Mock
+    private BatterServiceImpl mockBatterService;
+
+    @BeforeEach
+    void setUp(TestWorkflowEnvironment testEnv) {
+        when(mockBatterService.prepareCookieBatter(any()))
+                .thenReturn(new Batter(Topping.CHOCOLATE, 90));
+
+        testEnv.newWorker(COOKIE_QUEUE).registerWorkflowImplementationTypes(BestCookieWorkflow.class);
+        testEnv.newWorker(BATTER_QUEUE).registerActivitiesImplementations(mockBatterService);
+        testEnv.newWorker(BAKE_QUEUE).registerActivitiesImplementations(new BakeServiceImpl());
+        testEnv.start();
+    }
+
+    @Test
+    void testFullWorkflowWithMockedBatterService(CookieWorkflow cookieWorkflow) {
+        CookiePreference preferences = new CookiePreference(3, Baking.SOFT, Topping.CHOCOLATE);
+
+        WorkflowClient.start(cookieWorkflow::orderCookie, preferences);
+
+        OrderStatus statusBeforeSignal = cookieWorkflow.getOrderStatus();
+        assertFalse(statusBeforeSignal.ovenReady(), "Oven should not be ready");
+
+        cookieWorkflow.ovenReady();
+
+        OrderStatus statusAfterSignal = cookieWorkflow.getOrderStatus();
+        assertTrue(statusAfterSignal.ovenReady(), "Oven should now be ready");
+
+        CookieOrder result = cookieWorkflow.orderCookie(preferences);
+        assertNotNull(result, "Resulting CookieOrder should not be null");
+        assertNotNull(result.cookies(), "Cookies should not be null");
+        assertEquals(3, result.cookies().size(), "Should produce exactly 3 cookies");
+
+        for (Cookie cookie : result.cookies()) {
+            assertEquals(Baking.SOFT, cookie.baking(), "Cookie should be SOFT");
+            assertEquals(Topping.CHOCOLATE, cookie.topping(), "Cookie should have CHOCOLATE topping");
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowTest.java b/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowTest.java
new file mode 100644
index 0000000..eafb65b
--- /dev/null
+++ b/src/test/java/io/takima/temporalpractice/bakery/cookie/CookieWorkflowTest.java
@@ -0,0 +1,59 @@
+package io.takima.temporalpractice.bakery.cookie;
+
+import io.takima.temporalpractice.bakery.bake.BakeServiceImpl;
+import io.takima.temporalpractice.bakery.batter.BatterServiceImpl;
+import io.takima.temporalpractice.bakery.cookie.CookieWorkflow.*;
+import io.temporal.client.WorkflowClient;
+import io.temporal.testing.TestWorkflowEnvironment;
+import io.temporal.testing.TestWorkflowExtension;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static io.takima.temporalpractice.temporal.TemporalQueues.*;
+import static org.junit.jupiter.api.Assertions.*;
+
+
+class CookieWorkflowTest {
+    @RegisterExtension
+    public static final TestWorkflowExtension testWorkflowExtension =
+            TestWorkflowExtension.newBuilder()
+                    .registerWorkflowImplementationTypes(BestCookieWorkflow.class)
+                    .setActivityImplementations()
+                    .setDoNotStart(true)
+                    .build();
+
+    @BeforeEach
+    void setUp(TestWorkflowEnvironment testEnv) {
+        testEnv.newWorker(COOKIE_QUEUE).registerWorkflowImplementationTypes(BestCookieWorkflow.class);
+        testEnv.newWorker(BATTER_QUEUE).registerActivitiesImplementations(new BatterServiceImpl());
+        testEnv.newWorker(BAKE_QUEUE).registerActivitiesImplementations(new BakeServiceImpl());
+        testEnv.start();
+    }
+
+
+    @Test
+    void testFullWorkflow(CookieWorkflow cookieWorkflow) {
+        CookiePreference preferences = new CookiePreference(3, Baking.SOFT, Topping.CHOCOLATE);
+
+        WorkflowClient.start(cookieWorkflow::orderCookie, preferences);
+        OrderStatus statusBeforeSignal = cookieWorkflow.getOrderStatus();
+        assertFalse(statusBeforeSignal.ovenReady(), "Oven should not be ready");
+
+        cookieWorkflow.ovenReady();
+
+        OrderStatus statusAfterSignal = cookieWorkflow.getOrderStatus();
+        assertTrue(statusAfterSignal.ovenReady(), "Oven should now be ready");
+
+        CookieOrder result = cookieWorkflow.orderCookie(preferences);
+
+        assertNotNull(result, "Resulting CookieOrder should not be null");
+        assertNotNull(result.cookies(), "Cookies should not be null");
+        assertEquals(3, result.cookies().size(), "Should produce exactly 3 cookies");
+
+        for (Cookie cookie : result.cookies()) {
+            assertEquals(Baking.SOFT, cookie.baking(), "Cookie should be SOFT");
+            assertEquals(Topping.CHOCOLATE, cookie.topping(), "Cookie should have CHOCOLATE topping");
+        }
+    }
+}
\ No newline at end of file
-- 
GitLab