From e9bc235b1c4917968531c2a9cb50043fe560108d Mon Sep 17 00:00:00 2001 From: Ganesh Pagade Date: Sun, 21 Oct 2018 19:10:30 +0530 Subject: [PATCH] added test --- .../controller/GreetingController.java | 7 +- .../ZuulRatelimitDemoApplicationTests.java | 16 --- .../controller/GreetingControllerTest.java | 98 +++++++++++++++++++ 3 files changed, 103 insertions(+), 18 deletions(-) delete mode 100644 spring-cloud/spring-cloud-zuul-throttling/src/test/java/com/baeldung/spring/cloud/zuulratelimitdemo/ZuulRatelimitDemoApplicationTests.java create mode 100644 spring-cloud/spring-cloud-zuul-throttling/src/test/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingControllerTest.java diff --git a/spring-cloud/spring-cloud-zuul-throttling/src/main/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingController.java b/spring-cloud/spring-cloud-zuul-throttling/src/main/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingController.java index ed380d17ea..4a27324b4d 100644 --- a/spring-cloud/spring-cloud-zuul-throttling/src/main/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingController.java +++ b/spring-cloud/spring-cloud-zuul-throttling/src/main/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingController.java @@ -11,13 +11,16 @@ import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/greeting") public class GreetingController { + public static final String SIMPLE_RESPONSE = "Hi!"; + public static final String ADVANCED_RESPONSE = "Hello, how you doing?"; + @GetMapping("/simple") public ResponseEntity serviceA() { - return ResponseEntity.ok("Hi!"); + return ResponseEntity.ok(SIMPLE_RESPONSE); } @GetMapping("/advanced") public ResponseEntity serviceB() { - return ResponseEntity.ok("Hello, how you doing?"); + return ResponseEntity.ok(ADVANCED_RESPONSE); } } diff --git a/spring-cloud/spring-cloud-zuul-throttling/src/test/java/com/baeldung/spring/cloud/zuulratelimitdemo/ZuulRatelimitDemoApplicationTests.java b/spring-cloud/spring-cloud-zuul-throttling/src/test/java/com/baeldung/spring/cloud/zuulratelimitdemo/ZuulRatelimitDemoApplicationTests.java deleted file mode 100644 index 134d400b21..0000000000 --- a/spring-cloud/spring-cloud-zuul-throttling/src/test/java/com/baeldung/spring/cloud/zuulratelimitdemo/ZuulRatelimitDemoApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.spring.cloud.zuulratelimitdemo; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class ZuulRatelimitDemoApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/spring-cloud/spring-cloud-zuul-throttling/src/test/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingControllerTest.java b/spring-cloud/spring-cloud-zuul-throttling/src/test/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingControllerTest.java new file mode 100644 index 0000000000..6b6dba1704 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul-throttling/src/test/java/com/baeldung/spring/cloud/zuulratelimitdemo/controller/GreetingControllerTest.java @@ -0,0 +1,98 @@ +package com.baeldung.spring.cloud.zuulratelimitdemo.controller; + +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_LIMIT; +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_QUOTA; +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_REMAINING; +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_REMAINING_QUOTA; +import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_RESET; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; + +import java.util.concurrent.TimeUnit; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +@AutoConfigureTestDatabase +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class GreetingControllerTest { + + private static final String SIMPLE_GREETING = "/greeting/simple"; + private static final String ADVANCED_GREETING = "/greeting/advanced"; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void testNotExceedingCapacityRequest() { + ResponseEntity response = this.restTemplate.getForEntity(SIMPLE_GREETING, String.class); + HttpHeaders headers = response.getHeaders(); + String key = "rate-limit-application_serviceSimple_127.0.0.1"; + assertHeaders(headers, key, false, false); + assertEquals(OK, response.getStatusCode()); + } + + @Test + public void testExceedingCapacity() throws InterruptedException { + ResponseEntity response = this.restTemplate + .getForEntity(ADVANCED_GREETING, String.class); + HttpHeaders headers = response.getHeaders(); + String key = "rate-limit-application_serviceAdvanced_127.0.0.1"; + assertHeaders(headers, key, false, false); + assertEquals(OK, response.getStatusCode()); + + for (int i = 0; i < 2; i++) { + response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class); + } + + assertEquals(TOO_MANY_REQUESTS, response.getStatusCode()); + assertNotEquals(GreetingController.ADVANCED_RESPONSE, response.getBody()); + + TimeUnit.SECONDS.sleep(2); + + response = this.restTemplate.getForEntity(ADVANCED_GREETING, String.class); + headers = response.getHeaders(); + assertHeaders(headers, key, false, false); + assertEquals(OK, response.getStatusCode()); + } + + private void assertHeaders(HttpHeaders headers, String key, boolean nullable, + boolean quotaHeaders) { + String quota = headers.getFirst(HEADER_QUOTA + key); + String remainingQuota = headers.getFirst(HEADER_REMAINING_QUOTA + key); + String limit = headers.getFirst(HEADER_LIMIT + key); + String remaining = headers.getFirst(HEADER_REMAINING + key); + String reset = headers.getFirst(HEADER_RESET + key); + + if (nullable) { + if (quotaHeaders) { + assertNull(quota); + assertNull(remainingQuota); + } else { + assertNull(limit); + assertNull(remaining); + } + assertNull(reset); + } else { + if (quotaHeaders) { + assertNotNull(quota); + assertNotNull(remainingQuota); + } else { + assertNotNull(limit); + assertNotNull(remaining); + } + assertNotNull(reset); + } + } +}