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:
Starting the services
Testing the
/products/{id}
endpoint (Spring Boot)Testing the
/fallback/{route}
endpoint (Node.js)Verifying PostgreSQL data
Navigate to your Spring Boot project root (where
GatewayApplication.java
is).-
Run:
bashCopyDownload./mvnw spring-boot:run
-
Expected output:
CopyDownloadStarted GatewayApplication in X seconds
-
Navigate to your Node.js project directory (where
app.js
is).-
Run:
bashCopyDownloadnode app.js
-
Expected output:
CopyDownloadServer running on http://localhost:3000
-
curl http://localhost:8080/api/test
-
Expected Response:
CopyDownloadAPI is working! Current time: 2024-02-20T12:00:00Z
curl http://localhost:8080/api/products/123
-
First Call (Cache Miss):
Logs:
Cache miss for product id: 123
-
Response:
jsonCopyDownload{"name":"Sample Product"}
-
Second Call (Cache Hit):
Logs:
Cache hit for product id: 123
Same response as above.
-
Check the table in your IDE’s Database Tool Window or run:
sqlCopyDownloadSELECT * 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.
curl http://localhost:3000/fallback/home
If cached: Returns HTML from
ssr_cache
.If not cached: Returns
<div>No cached version</div>
.
-
Manually insert test data first (if empty):
sqlCopyDownloadINSERT INTO ssr_cache (route, html, expiry) VALUES ('/home', '<div>Home Page</div>', NOW() + INTERVAL '1 hour');
-
Then verify:
sqlCopyDownloadSELECT * FROM ssr_cache WHERE route = '/home';
Use TestRestTemplate
or MockMvc
in a JUnit test:
@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")); }
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>'); });
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 |
-
No Response?
Check logs for both Spring Boot and Node.js servers.
Verify PostgreSQL is running (
psql -U gateway -d gateway_cache
).
-
Empty Cache?
Manually insert test data (shown above).
-
Connection Errors?
Confirm database credentials in
application.properties
(Spring Boot) andpool
config (Node.js).
Let me know if you hit any snags!