step‐by‐step guide to test the API calls in Gateway and Client adapter - Wiz-DevTech/prettygirllz GitHub Wiki

Here’s a step-by-step guide to test the API calls in your application, including both the Java Spring Boot backend (ApiController) and the Node.js fallback server. We’ll cover:

  1. Starting the services

  2. Testing the /products/{id} endpoint (Spring Boot)

  3. Testing the /fallback/{route} endpoint (Node.js)

  4. Verifying PostgreSQL data


Step 1: Start the Services

1. Start Spring Boot (Java)

  • Navigate to your Spring Boot project root (where GatewayApplication.java is).

  • Run:

    bash
    Copy
    Download
    ./mvnw spring-boot:run
    • Expected output:

      Copy
      Download
      Started GatewayApplication in X seconds

2. Start Node.js Server

  • Navigate to your Node.js project directory (where app.js is).

  • Run:

    bash
    Copy
    Download
    node app.js
    • Expected output:

      Copy
      Download
      Server running on http://localhost:3000

Step 2: Test the Spring Boot API Endpoints

1. Health Check (/api/test)

bash
Copy
Download
curl http://localhost:8080/api/test
  • Expected Response:

    Copy
    Download
    API is working! Current time: 2024-02-20T12:00:00Z

2. Product Endpoint (/api/products/{id})

bash
Copy
Download
curl http://localhost:8080/api/products/123
  • First Call (Cache Miss):

    • Logs: Cache miss for product id: 123

    • Response:

      json
      Copy
      Download
      {"name":"Sample Product"}
  • Second Call (Cache Hit):

    • Logs: Cache hit for product id: 123

    • Same response as above.

3. Verify PostgreSQL (api_responses Table)

  • Check the table in your IDE’s Database Tool Window or run:

    sql
    Copy
    Download
    SELECT * FROM api_responses WHERE id = '123';
  • You should see the cached response with:

    • id = "123"

    • response_json = "{\"name\":\"Sample Product\"}"

    • expiry set to 1 hour in the future.


Step 3: Test the Node.js Fallback Endpoint

1. Fallback Endpoint (/fallback/{route})

bash
Copy
Download
curl http://localhost:3000/fallback/home
  • If cached: Returns HTML from ssr_cache.

  • If not cached: Returns <div>No cached version</div>.

2. Check Database (ssr_cache Table)

  • Manually insert test data first (if empty):

    sql
    Copy
    Download
    INSERT INTO ssr_cache (route, html, expiry) 
    VALUES ('/home', '<div>Home Page</div>', NOW() + INTERVAL '1 hour');
  • Then verify:

    sql
    Copy
    Download
    SELECT * FROM ssr_cache WHERE route = '/home';

Step 4: Automated Testing (Optional)

1. Spring Boot (Java)

Use TestRestTemplate or MockMvc in a JUnit test:

java
Copy
Download
@Test
public void testProductEndpoint() {
    ResponseEntity<String> response = restTemplate.getForEntity(
        "http://localhost:8080/api/products/456", String.class
    );
    assertEquals(200, response.getStatusCodeValue());
    assertTrue(response.getBody().contains("Sample Product"));
}

2. Node.js (Jest/Supertest)

javascript
Copy
Download
const request = require('supertest');
const app = require('./app');

test('GET /fallback/home returns cached HTML', async () => { const res = await request(app).get('/fallback/home'); expect(res.status).toBe(200); expect(res.text).toContain('<div>'); });


Expected Outcomes

Endpoint Cache State Response PostgreSQL Action
GET /api/products/123 Miss {"name":"Sample Product"} Inserts new row in api_responses
GET /api/products/123 Hit Same as above No change
GET /fallback/home Hit HTML from ssr_cache None
GET /fallback/home Miss
No cached version
None

Troubleshooting

  1. No Response?

    • Check logs for both Spring Boot and Node.js servers.

    • Verify PostgreSQL is running (psql -U gateway -d gateway_cache).

  2. Empty Cache?

    • Manually insert test data (shown above).

  3. Connection Errors?

    • Confirm database credentials in application.properties (Spring Boot) and pool config (Node.js).

Let me know if you hit any snags!

⚠️ **GitHub.com Fallback** ⚠️