44import static org .junit .jupiter .api .Assertions .assertNotNull ;
55import static org .junit .jupiter .api .Assertions .assertTrue ;
66
7+ import com .fasterxml .jackson .databind .JsonNode ;
8+ import com .fasterxml .jackson .databind .ObjectMapper ;
79import org .junit .jupiter .api .Test ;
810import org .springframework .beans .factory .annotation .Autowired ;
911import org .springframework .boot .test .context .SpringBootTest ;
1012import org .springframework .boot .test .web .client .TestRestTemplate ;
1113import org .springframework .boot .test .web .server .LocalServerPort ;
1214import org .springframework .http .ResponseEntity ;
1315
14- import com .fasterxml .jackson .databind .JsonNode ;
15- import com .fasterxml .jackson .databind .ObjectMapper ;
16-
1716@ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
1817class ReferenceApplicationTests {
1918
20- @ LocalServerPort
21- private int port ;
22-
23- @ Autowired
24- private TestRestTemplate restTemplate ;
25-
26- private final ObjectMapper objectMapper = new ObjectMapper ();
27-
28- @ Test
29- void testRollDice () throws Exception {
30- ResponseEntity <String > response = restTemplate .getForEntity (
31- "http://localhost:" + port + "/rolldice" , String .class );
32-
33- assertEquals (200 , response .getStatusCode ().value ());
34-
35- JsonNode json = objectMapper .readTree (response .getBody ());
36- assertNotNull (json .get ("result" ));
37- assertNotNull (json .get ("player" ));
38-
39- int result = json .get ("result" ).asInt ();
40- assertTrue (result >= 1 && result <= 6 );
41- }
42-
43- @ Test
44- void testRollDiceWithPlayer () throws Exception {
45- ResponseEntity <String > response = restTemplate .getForEntity (
46- "http://localhost:" + port + "/rolldice?player=testplayer" , String .class );
47-
48- assertEquals (200 , response .getStatusCode ().value ());
49-
50- JsonNode json = objectMapper .readTree (response .getBody ());
51- assertEquals ("testplayer" , json .get ("player" ).asText ());
52- }
53-
54- @ Test
55- void testRollMultipleDice () throws Exception {
56- ResponseEntity <String > response = restTemplate .getForEntity (
57- "http://localhost:" + port + "/rolldice?rolls=3" , String .class );
58-
59- assertEquals (200 , response .getStatusCode ().value ());
60-
61- JsonNode json = objectMapper .readTree (response .getBody ());
62- assertNotNull (json .get ("results" ));
63- assertNotNull (json .get ("sum" ));
64- assertEquals (3 , json .get ("results" ).size ());
65- }
66-
67- @ Test
68- void testFibonacci () throws Exception {
69- ResponseEntity <String > response = restTemplate .getForEntity (
70- "http://localhost:" + port + "/fibonacci?n=10" , String .class );
71-
72- assertEquals (200 , response .getStatusCode ().value ());
73-
74- JsonNode json = objectMapper .readTree (response .getBody ());
75- assertEquals (10 , json .get ("n" ).asInt ());
76- assertEquals ("55" , json .get ("result" ).asText ());
77- }
78-
79- @ Test
80- void testHealth () throws Exception {
81- ResponseEntity <String > response = restTemplate .getForEntity (
82- "http://localhost:" + port + "/health" , String .class );
83-
84- assertEquals (200 , response .getStatusCode ().value ());
85-
86- JsonNode json = objectMapper .readTree (response .getBody ());
87- assertEquals ("UP" , json .get ("status" ).asText ());
88- assertEquals ("dice-server" , json .get ("service" ).asText ());
89- }
90- }
19+ @ LocalServerPort private int port ;
20+
21+ @ Autowired private TestRestTemplate restTemplate ;
22+
23+ private final ObjectMapper objectMapper = new ObjectMapper ();
24+
25+ @ Test
26+ void testRollDice () throws Exception {
27+ ResponseEntity <String > response =
28+ restTemplate .getForEntity ("http://localhost:" + port + "/rolldice" , String .class );
29+
30+ assertEquals (200 , response .getStatusCode ().value ());
31+
32+ JsonNode json = objectMapper .readTree (response .getBody ());
33+ assertNotNull (json .get ("result" ));
34+ assertNotNull (json .get ("player" ));
35+
36+ int result = json .get ("result" ).asInt ();
37+ assertTrue (result >= 1 && result <= 6 );
38+ }
39+
40+ @ Test
41+ void testRollDiceWithPlayer () throws Exception {
42+ ResponseEntity <String > response =
43+ restTemplate .getForEntity (
44+ "http://localhost:" + port + "/rolldice?player=testplayer" , String .class );
45+
46+ assertEquals (200 , response .getStatusCode ().value ());
47+
48+ JsonNode json = objectMapper .readTree (response .getBody ());
49+ assertEquals ("testplayer" , json .get ("player" ).asText ());
50+ }
51+
52+ @ Test
53+ void testRollMultipleDice () throws Exception {
54+ ResponseEntity <String > response =
55+ restTemplate .getForEntity ("http://localhost:" + port + "/rolldice?rolls=3" , String .class );
56+
57+ assertEquals (200 , response .getStatusCode ().value ());
58+
59+ JsonNode json = objectMapper .readTree (response .getBody ());
60+ assertNotNull (json .get ("results" ));
61+ assertNotNull (json .get ("sum" ));
62+ assertEquals (3 , json .get ("results" ).size ());
63+ }
64+
65+ @ Test
66+ void testFibonacci () throws Exception {
67+ ResponseEntity <String > response =
68+ restTemplate .getForEntity ("http://localhost:" + port + "/fibonacci?n=10" , String .class );
69+
70+ assertEquals (200 , response .getStatusCode ().value ());
71+
72+ JsonNode json = objectMapper .readTree (response .getBody ());
73+ assertEquals (10 , json .get ("n" ).asInt ());
74+ assertEquals ("55" , json .get ("result" ).asText ());
75+ }
76+
77+ @ Test
78+ void testHealth () throws Exception {
79+ ResponseEntity <String > response =
80+ restTemplate .getForEntity ("http://localhost:" + port + "/health" , String .class );
81+
82+ assertEquals (200 , response .getStatusCode ().value ());
83+
84+ JsonNode json = objectMapper .readTree (response .getBody ());
85+ assertEquals ("UP" , json .get ("status" ).asText ());
86+ assertEquals ("dice-server" , json .get ("service" ).asText ());
87+ }
88+ }
0 commit comments