From c5d11e32adc56e10ab6f68763382e8581bb395b6 Mon Sep 17 00:00:00 2001
From: KronosDP <darrel.danadyaksa19@gmail.com>
Date: Sat, 8 Mar 2025 17:55:56 +0700
Subject: [PATCH] [REFACTOR] Add tests for OTP generation failure scenarios in
 OTPServiceTest

---
 .../service/OTPServiceTest.java               | 58 +++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/src/test/java/com/safetypin/authentication/service/OTPServiceTest.java b/src/test/java/com/safetypin/authentication/service/OTPServiceTest.java
index d99e2b8..f2e9b97 100644
--- a/src/test/java/com/safetypin/authentication/service/OTPServiceTest.java
+++ b/src/test/java/com/safetypin/authentication/service/OTPServiceTest.java
@@ -1,5 +1,6 @@
 package com.safetypin.authentication.service;
 
+import com.safetypin.authentication.exception.OTPException;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.InjectMocks;
@@ -10,6 +11,7 @@ import java.lang.reflect.Constructor;
 import java.time.LocalDateTime;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
 
 import static org.junit.jupiter.api.Assertions.*;
 import static org.mockito.Mockito.anyString;
@@ -153,4 +155,60 @@ class OTPServiceTest {
         boolean result = otpService.verifyOTP("nonexistent@example.com", "123456");
         assertFalse(result, "Verification should fail when no OTP is generated for the given email");
     }
+
+    @Test
+    void testGenerateOTPEmailServiceReturnsFalse() {
+        // Mock email service to return false
+        when(emailService.sendOTPMail(anyString(), anyString()))
+                .thenReturn(CompletableFuture.completedFuture(false));
+
+        String email = "user@example.com";
+
+        // Verify that OTPException is thrown
+        OTPException exception = assertThrows(OTPException.class, () -> {
+            otpService.generateOTP(email);
+        }, "Should throw OTPException when email service returns false");
+
+        assertEquals("Failed to send OTP", exception.getMessage());
+    }
+
+    @Test
+    void testGenerateOTPInterruptedException() {
+        // Mock email service to throw InterruptedException
+        CompletableFuture<Boolean> future = new CompletableFuture<>();
+        future.completeExceptionally(new InterruptedException("Test interrupted"));
+        when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(future);
+
+        String email = "user@example.com";
+
+        // Verify that OTPException is thrown
+        OTPException exception = assertThrows(OTPException.class, () -> {
+            otpService.generateOTP(email);
+        }, "Should throw OTPException when InterruptedException occurs");
+
+        assertTrue(exception.getMessage().contains("Failed to send OTP"));
+
+        // Verify that thread was interrupted
+        assertTrue(Thread.currentThread().isInterrupted(), "Thread should be interrupted");
+
+        // Clear the interrupted status for other tests
+        Thread.interrupted();
+    }
+
+    @Test
+    void testGenerateOTPExecutionException() {
+        // Mock email service to throw ExecutionException
+        CompletableFuture<Boolean> future = new CompletableFuture<>();
+        future.completeExceptionally(new ExecutionException("Test execution failed", new RuntimeException("Email service error")));
+        when(emailService.sendOTPMail(anyString(), anyString())).thenReturn(future);
+
+        String email = "user@example.com";
+
+        // Verify that OTPException is thrown
+        OTPException exception = assertThrows(OTPException.class, () -> {
+            otpService.generateOTP(email);
+        }, "Should throw OTPException when ExecutionException occurs");
+
+        assertTrue(exception.getMessage().contains("Failed to send OTP"));
+    }
 }
\ No newline at end of file
-- 
GitLab