Skip to content

Commit 58b5405

Browse files
committed
Improve backend-java
1 parent 41f9fe0 commit 58b5405

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

backend-java/src/main/java/com/ganatan/controllers/HealthController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class HealthController {
1515
@GET
1616
public Response getStatus() {
1717
Map<String, Object> response = new LinkedHashMap<>();
18+
System.out.println("00000000001");
1819

1920
response.put("status", "ok");
2021

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.ganatan.controllers;
2+
3+
import jakarta.ws.rs.GET;
4+
import jakarta.ws.rs.Path;
5+
import jakarta.ws.rs.PathParam;
6+
import jakarta.ws.rs.Produces;
7+
import jakarta.ws.rs.core.MediaType;
8+
import jakarta.ws.rs.core.Response;
9+
10+
@Path("/latency")
11+
@Produces(MediaType.APPLICATION_JSON)
12+
public class LatencyController {
13+
14+
@GET
15+
@Path("/{delay}")
16+
public Response simulateLatency(@PathParam("delay") int delayMs) {
17+
try {
18+
Thread.sleep(delayMs);
19+
} catch (InterruptedException e) {
20+
Thread.currentThread().interrupt();
21+
return Response.serverError()
22+
.entity("{\"status\":\"error\",\"message\":\"Interrupted during sleep.\"}")
23+
.build();
24+
}
25+
26+
String json = String.format("{\"status\":\"ok\",\"latency_ms\":%d}", delayMs);
27+
return Response.ok(json).build();
28+
}
29+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)