add tests
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.ratelimiting.bootstarterapp;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.RequestBuilder;
|
||||
|
||||
import com.baeldung.ratelimiting.bucket4japp.service.PricingPlan;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Bucket4jRateLimitApp.class)
|
||||
@TestPropertySource(properties = "spring.config.location=classpath:ratelimiting/application-bucket4j-starter.yml")
|
||||
@AutoConfigureMockMvc
|
||||
public class Bucket4jBootStarterRateLimitIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void givenTriangleAreaCalculator_whenRequestsWithinRateLimit_thenAccepted() throws Exception {
|
||||
|
||||
RequestBuilder request = post("/api/v1/area/triangle").contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.content("{ \"height\": 8, \"base\": 10 }")
|
||||
.header("X-api-key", "FX001-UBSZ5YRYQ");
|
||||
|
||||
for (int i = 1; i <= PricingPlan.FREE.bucketCapacity(); i++) {
|
||||
mockMvc.perform(request)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(header().exists("X-Rate-Limit-Remaining"))
|
||||
.andExpect(jsonPath("$.shape", equalTo("triangle")))
|
||||
.andExpect(jsonPath("$.area", equalTo(40d)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTriangleAreaCalculator_whenRequestRateLimitTriggered_thenRejected() throws Exception {
|
||||
|
||||
RequestBuilder request = post("/api/v1/area/triangle").contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.content("{ \"height\": 8, \"base\": 10 }")
|
||||
.header("X-api-key", "FX001-ZBSY6YSLP");
|
||||
|
||||
for (int i = 1; i <= PricingPlan.FREE.bucketCapacity(); i++) {
|
||||
mockMvc.perform(request); // exhaust limit
|
||||
}
|
||||
|
||||
mockMvc.perform(request)
|
||||
.andExpect(status().isTooManyRequests())
|
||||
.andExpect(jsonPath("$.message", equalTo("You have exhausted your API Request Quota")))
|
||||
.andExpect(header().exists("X-Rate-Limit-Retry-After-Seconds"));
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.ratelimiting.bucket4japp;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.RequestBuilder;
|
||||
|
||||
import com.baeldung.ratelimiting.bucket4japp.service.PricingPlan;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Bucket4jRateLimitApp.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class Bucket4jRateLimitIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void givenRectangleAreaCalculator_whenRequestsWithinRateLimit_thenAccepted() throws Exception {
|
||||
|
||||
RequestBuilder request = post("/api/v1/area/rectangle").contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.content("{ \"length\": 12, \"width\": 10 }")
|
||||
.header("X-api-key", "FX001-UBSZ5YRYQ");
|
||||
|
||||
for (int i = 1; i <= PricingPlan.FREE.bucketCapacity(); i++) {
|
||||
mockMvc.perform(request)
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(header().exists("X-Rate-Limit-Remaining"))
|
||||
.andExpect(jsonPath("$.shape", equalTo("rectangle")))
|
||||
.andExpect(jsonPath("$.area", equalTo(120d)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenReactangleAreaCalculator_whenRequestRateLimitTriggered_thenRejected() throws Exception {
|
||||
|
||||
RequestBuilder request = post("/api/v1/area/rectangle").contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.content("{ \"length\": 12, \"width\": 10 }")
|
||||
.header("X-api-key", "FX001-ZBSY6YSLP");
|
||||
|
||||
for (int i = 1; i <= PricingPlan.FREE.bucketCapacity(); i++) {
|
||||
mockMvc.perform(request); // exhaust limit
|
||||
}
|
||||
|
||||
mockMvc.perform(request)
|
||||
.andExpect(status().isTooManyRequests())
|
||||
.andExpect(status().reason("You have exhausted your API Request Quota"))
|
||||
.andExpect(header().exists("X-Rate-Limit-Retry-After-Seconds"));
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.ratelimiting.bucket4japp;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.github.bucket4j.Bandwidth;
|
||||
import io.github.bucket4j.Bucket;
|
||||
import io.github.bucket4j.Bucket4j;
|
||||
import io.github.bucket4j.Refill;
|
||||
|
||||
public class Bucket4jUsageUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBucketLimit_whenExceedLimit_thenConsumeReturnsFalse() {
|
||||
Refill refill = Refill.intervally(10, Duration.ofMinutes(1));
|
||||
Bandwidth limit = Bandwidth.classic(10, refill);
|
||||
Bucket bucket = Bucket4j.builder()
|
||||
.addLimit(limit)
|
||||
.build();
|
||||
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
assertTrue(bucket.tryConsume(1));
|
||||
}
|
||||
assertFalse(bucket.tryConsume(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipletLimits_whenExceedSmallerLimit_thenConsumeReturnsFalse() {
|
||||
Bucket bucket = Bucket4j.builder()
|
||||
.addLimit(Bandwidth.classic(10, Refill.intervally(10, Duration.ofMinutes(1))))
|
||||
.addLimit(Bandwidth.classic(5, Refill.intervally(5, Duration.ofSeconds(20))))
|
||||
.build();
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
assertTrue(bucket.tryConsume(1));
|
||||
}
|
||||
assertFalse(bucket.tryConsume(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBucketLimit_whenThrottleRequests_thenConsumeReturnsTrue() throws InterruptedException {
|
||||
Refill refill = Refill.intervally(1, Duration.ofSeconds(2));
|
||||
Bandwidth limit = Bandwidth.classic(1, refill);
|
||||
Bucket bucket = Bucket4j.builder()
|
||||
.addLimit(limit)
|
||||
.build();
|
||||
|
||||
assertTrue(bucket.tryConsume(1));
|
||||
|
||||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
executor.schedule(new AssertTryConsume(bucket, latch), 2, TimeUnit.SECONDS);
|
||||
|
||||
latch.await();
|
||||
}
|
||||
|
||||
static class AssertTryConsume implements Runnable {
|
||||
|
||||
private Bucket bucket;
|
||||
private CountDownLatch latch;
|
||||
|
||||
AssertTryConsume(Bucket bucket, CountDownLatch latch) {
|
||||
this.bucket = bucket;
|
||||
this.latch = latch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
assertTrue(bucket.tryConsume(1));
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.ratelimiting.bucket4japp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.ratelimiting.bucket4japp.service.PricingPlan;
|
||||
import com.baeldung.ratelimiting.bucket4japp.service.PricingPlanService;
|
||||
|
||||
import io.github.bucket4j.Bucket;
|
||||
|
||||
public class PricingPlanServiceUnitTest {
|
||||
|
||||
private PricingPlanService service = new PricingPlanService();
|
||||
|
||||
@Test
|
||||
public void givenAPIKey_whenFreePlan_thenReturnFreePlanBucket() {
|
||||
Bucket bucket = service.resolveBucket("FX001-UBSZ5YRYQ");
|
||||
|
||||
assertEquals(PricingPlan.FREE.bucketCapacity(), bucket.getAvailableTokens());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAPIKey_whenBasiclan_thenReturnBasicPlanBucket() {
|
||||
Bucket bucket = service.resolveBucket("BX001-MBSZ5YRYP");
|
||||
|
||||
assertEquals(PricingPlan.BASIC.bucketCapacity(), bucket.getAvailableTokens());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAPIKey_whenProfessionalPlan_thenReturnProfessionalPlanBucket() {
|
||||
Bucket bucket = service.resolveBucket("PX001-NBSZ5YRYY");
|
||||
|
||||
assertEquals(PricingPlan.PROFESSIONAL.bucketCapacity(), bucket.getAvailableTokens());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user