File size: 2,542 Bytes
dc510b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.erp.system;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        properties = {
                "app.seeding.enabled=false",
                "app.self-ping.fixed-rate-ms=3600000"
        }
)
public class SpringBootResilienceIntegrationTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void testContextLoadsAndHealthEndpointResponds() {
        ResponseEntity<Map> response = restTemplate.getForEntity("/health", Map.class);
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertTrue(response.getBody().containsKey("status"));
        assertTrue(response.getBody().containsKey("databases"));
    }

    @Test
    public void testPostgresEndpointReturns503WhenUnreachable() {
        ResponseEntity<Map> response = restTemplate.getForEntity("/api/erp/postgres", Map.class);
        assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals(503, response.getBody().get("status"));
        assertEquals("Service Unavailable", response.getBody().get("error"));
        assertTrue(response.getBody().get("diagnostic").toString().contains("DNS resolution failure"));
    }

    @Test
    public void testRootEndpointReturns200() {
        ResponseEntity<Map> response = restTemplate.getForEntity("/", Map.class);
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals("ERP System", response.getBody().get("application"));
        assertEquals("running", response.getBody().get("status"));
    }

    @Test
    public void testPingEndpointReturns200() {
        ResponseEntity<Map> response = restTemplate.getForEntity("/ping", Map.class);
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertNotNull(response.getBody());
        assertEquals("pong", response.getBody().get("message"));
    }
}