From 87ccb5352c3aec73ef51098a7cc9c40f29f701ee Mon Sep 17 00:00:00 2001 From: 98001yash Date: Sat, 7 Nov 2026 01:22:12 +0530 Subject: [PATCH 1/5] added the tests for the list polics --- .../api/PolicyControllerIntegrationTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java index 3ec55b6..98830c2 100644 --- a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java +++ b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java @@ -6,9 +6,11 @@ import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.*; import java.time.Instant; +import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -109,4 +111,29 @@ void shouldRejectInvalidPolicyThroughRealHttpEndpoint() throws Exception { ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); } + + @Test + void shouldListPoliciesThroughRealHttpEndpoint() { + String resource = "/api/contract/list"; + + restTemplate.exchange( + baseUrl(), + HttpMethod.POST, + createRequest("list-policy", "List Policy", resource, 10), + PolicyResponse.class + ); + + ResponseEntity> response = + restTemplate.exchange( + baseUrl(), + HttpMethod.GET, + null, + new ParameterizedTypeReference<>() {} + ); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + assertTrue(response.getBody().stream() + .anyMatch(policy -> resource.equals(policy.getResource()))); + } } \ No newline at end of file From 0d8995f4ca0bd7241dab62f0fe19af5dc3702cfc Mon Sep 17 00:00:00 2001 From: 98001yash Date: Sat, 7 Nov 2026 01:22:45 +0530 Subject: [PATCH 2/5] added the tests for retrice polices for resources --- .../api/PolicyControllerIntegrationTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java index 98830c2..e9f943f 100644 --- a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java +++ b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java @@ -136,4 +136,28 @@ void shouldListPoliciesThroughRealHttpEndpoint() { assertTrue(response.getBody().stream() .anyMatch(policy -> resource.equals(policy.getResource()))); } + + @Test + void shouldRetrievePolicyByResourceThroughRealHttpEndpoint() { + String resource = "/api/contract/read"; + + restTemplate.exchange( + baseUrl(), + HttpMethod.POST, + createRequest("read-policy", "Read Policy", resource, 20), + PolicyResponse.class + ); + + ResponseEntity response = + restTemplate.getForEntity( + policyUri(resource), + PolicyResponse.class + ); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + assertEquals("read-policy", response.getBody().getPolicyId()); + assertEquals(resource, response.getBody().getResource()); + assertEquals(20, response.getBody().getCapacity()); + } } \ No newline at end of file From 8f86ca7ece52508f47ebd23c6111a916d48dbbe2 Mon Sep 17 00:00:00 2001 From: 98001yash Date: Sat, 7 Nov 2026 01:23:21 +0530 Subject: [PATCH 3/5] added the 404 and fetch existing polics --- .../api/PolicyControllerIntegrationTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java index e9f943f..fa8b103 100644 --- a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java +++ b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java @@ -160,4 +160,41 @@ void shouldRetrievePolicyByResourceThroughRealHttpEndpoint() { assertEquals(resource, response.getBody().getResource()); assertEquals(20, response.getBody().getCapacity()); } + + @Test + void shouldReturnNotFoundForMissingPolicy() { + ResponseEntity response = + restTemplate.getForEntity( + policyUri("/api/contract/missing"), + String.class + ); + + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + } + + @Test + void shouldUpdatePolicyThroughRealHttpEndpoint() { + String resource = "/api/contract/update"; + + restTemplate.exchange( + baseUrl(), + HttpMethod.POST, + createRequest("update-policy", "Original Policy", resource, 10), + PolicyResponse.class + ); + + ResponseEntity response = + restTemplate.exchange( + policyUri(resource), + HttpMethod.PUT, + createRequest("update-policy", "Updated Policy", resource, 50), + PolicyResponse.class + ); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + assertEquals("Updated Policy", response.getBody().getName()); + assertEquals(50, response.getBody().getCapacity()); + assertEquals(resource, response.getBody().getResource()); + } } \ No newline at end of file From 2d7548d042e954cd58f670dd0eff389004e591da Mon Sep 17 00:00:00 2001 From: 98001yash Date: Sat, 7 Nov 2026 01:24:09 +0530 Subject: [PATCH 4/5] added the helper methogs --- .../api/PolicyControllerIntegrationTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java index fa8b103..2dda734 100644 --- a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java +++ b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java @@ -8,6 +8,7 @@ import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.*; +import org.springframework.web.util.UriComponentsBuilder; import java.time.Instant; import java.util.List; @@ -197,4 +198,72 @@ void shouldUpdatePolicyThroughRealHttpEndpoint() { assertEquals(50, response.getBody().getCapacity()); assertEquals(resource, response.getBody().getResource()); } + + @Test + void shouldReturnNotFoundAfterDeletingPolicy() { + String resource = "/api/contract/delete"; + + restTemplate.exchange( + baseUrl(), + HttpMethod.POST, + createRequest("delete-policy-contract", "Delete Policy", resource, 10), + PolicyResponse.class + ); + + ResponseEntity deleteResponse = + restTemplate.exchange( + policyUri(resource), + HttpMethod.DELETE, + HttpEntity.EMPTY, + Void.class + ); + + assertEquals(HttpStatus.NO_CONTENT, deleteResponse.getStatusCode()); + + ResponseEntity getResponse = + restTemplate.getForEntity( + policyUri(resource), + String.class + ); + + assertEquals(HttpStatus.NOT_FOUND, getResponse.getStatusCode()); + } + + private String baseUrl() { + return "http://localhost:" + port + "/api/v1/policies"; + } + + private java.net.URI policyUri(String resource) { + return UriComponentsBuilder.fromUriString(baseUrl()) + .queryParam("resource", resource) + .build() + .encode() + .toUri(); + } + + private HttpEntity createRequest( + String policyId, + String name, + String resource, + long capacity) { + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + String body = """ + { + "policyId": "%s", + "name": "%s", + "type": "TOKEN_BUCKET", + "status": "ACTIVE", + "capacity": %d, + "refillRate": 1, + "windowDuration": null, + "createdAt": "2026-09-04T00:00:00Z", + "resource": "%s" + } + """.formatted(policyId, name, capacity, resource); + + return new HttpEntity<>(body, headers); + } } \ No newline at end of file From d411b75a34d42c00af251255787303a2c1167379 Mon Sep 17 00:00:00 2001 From: 98001yash Date: Sat, 7 Nov 2026 01:26:18 +0530 Subject: [PATCH 5/5] Add policy API contract integration tests --- .../api/PolicyControllerIntegrationTest.java | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java index 2dda734..41aa0f4 100644 --- a/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java +++ b/src/test/java/com/yashdotdev/distributed_traffic_control/policy/api/PolicyControllerIntegrationTest.java @@ -241,28 +241,24 @@ private java.net.URI policyUri(String resource) { .toUri(); } - private HttpEntity createRequest( - String policyId, - String name, - String resource, - long capacity) { + private HttpEntity createRequest(String policyId, String name, String resource, long capacity) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); String body = """ - { - "policyId": "%s", - "name": "%s", - "type": "TOKEN_BUCKET", - "status": "ACTIVE", - "capacity": %d, - "refillRate": 1, - "windowDuration": null, - "createdAt": "2026-09-04T00:00:00Z", - "resource": "%s" - } - """.formatted(policyId, name, capacity, resource); + { + "policyId": "%s", + "name": "%s", + "type": "TOKEN_BUCKET", + "status": "ACTIVE", + "capacity": %d, + "refillRate": 1, + "windowDuration": null, + "createdAt": "2026-09-04T00:00:00Z", + "resource": "%s" + } + """.formatted(policyId, name, capacity, resource); return new HttpEntity<>(body, headers); }