|
| 1 | +package com.ganatan.controllers; |
| 2 | + |
| 3 | +import jakarta.ws.rs.core.Response; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.*; |
| 7 | + |
| 8 | +class LatencyControllerTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + void simulateLatency_shouldReturnOk() { |
| 12 | + LatencyController controller = new LatencyController(); |
| 13 | + int delayMs = 300; |
| 14 | + |
| 15 | + Response response = controller.simulateLatency(delayMs); |
| 16 | + |
| 17 | + assertEquals(200, response.getStatus()); |
| 18 | + String body = response.getEntity().toString(); |
| 19 | + assertTrue(body.contains("\"status\":\"ok\"")); |
| 20 | + assertTrue(body.contains("\"latency_ms\":" + delayMs)); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void simulateLatency_withZeroDelay_shouldReturnImmediately() { |
| 25 | + LatencyController controller = new LatencyController(); |
| 26 | + int delayMs = 0; |
| 27 | + |
| 28 | + Response response = controller.simulateLatency(delayMs); |
| 29 | + |
| 30 | + assertEquals(200, response.getStatus()); |
| 31 | + String body = response.getEntity().toString(); |
| 32 | + assertTrue(body.contains("\"status\":\"ok\"")); |
| 33 | + assertTrue(body.contains("\"latency_ms\":0")); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void simulateLatency_shouldHandleInterruptedException() { |
| 38 | + LatencyController controller = new LatencyController(); |
| 39 | + |
| 40 | + Thread.currentThread().interrupt(); // force l'interruption |
| 41 | + |
| 42 | + Response response = controller.simulateLatency(100); |
| 43 | + |
| 44 | + assertEquals(500, response.getStatus()); |
| 45 | + String body = response.getEntity().toString(); |
| 46 | + assertTrue(body.contains("\"status\":\"error\"")); |
| 47 | + assertTrue(body.contains("Interrupted during sleep.")); |
| 48 | + |
| 49 | + Thread.interrupted(); // réinitialise l'état du thread après le test |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +//package com.ganatan.controllers; |
| 55 | +// |
| 56 | +//import jakarta.ws.rs.core.Response; |
| 57 | +//import org.junit.jupiter.api.Test; |
| 58 | +// |
| 59 | +//import static org.junit.jupiter.api.Assertions.*; |
| 60 | +// |
| 61 | +//class LatencyControllerTest { |
| 62 | +// |
| 63 | +// @Test |
| 64 | +// void simulateLatency_shouldReturnOkStatus() { |
| 65 | +// LatencyController controller = new LatencyController(); |
| 66 | +// |
| 67 | +// int delayMs = 500; // 500 ms |
| 68 | +// Response response = controller.simulateLatency(delayMs); |
| 69 | +// |
| 70 | +// assertEquals(200, response.getStatus()); |
| 71 | +// String entity = response.getEntity().toString(); |
| 72 | +// assertTrue(entity.contains("\"status\":\"ok\"")); |
| 73 | +// assertTrue(entity.contains("\"latency_ms\":" + delayMs)); |
| 74 | +// } |
| 75 | +//} |
0 commit comments