|
| 1 | +package com.synchlabs.geolocateapi.presentation.controller; |
| 2 | + |
| 3 | +import com.synchlabs.geolocateapi.application.port.in.GeoQueryUseCase; |
| 4 | +import com.synchlabs.geolocateapi.presentation.ratelimit.RateLimitService; |
| 5 | +import com.synchlabs.geolocateapi.presentation.ratelimit.RateLimitTier; |
| 6 | +import io.github.bucket4j.Bandwidth; |
| 7 | +import io.github.bucket4j.Bucket; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 12 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 13 | +import org.springframework.test.web.servlet.MockMvc; |
| 14 | + |
| 15 | +import java.time.Duration; |
| 16 | + |
| 17 | +import static org.mockito.ArgumentMatchers.any; |
| 18 | +import static org.mockito.ArgumentMatchers.eq; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 23 | + |
| 24 | +/** |
| 25 | + * Input validation tests for {@link GeoController}. |
| 26 | + * |
| 27 | + * Verifies that constraint violations on path variables and query parameters |
| 28 | + * are rejected with HTTP 400 before reaching the application layer. |
| 29 | + * |
| 30 | + * Type: Controller slice test (MockMvc). |
| 31 | + */ |
| 32 | +@WebMvcTest(controllers = GeoController.class) |
| 33 | +class GeoControllerValidationTest { |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private MockMvc mockMvc; |
| 37 | + |
| 38 | + @MockBean |
| 39 | + private GeoQueryUseCase service; |
| 40 | + |
| 41 | + @MockBean |
| 42 | + private RateLimitService rateLimitService; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setUp() { |
| 46 | + when(rateLimitService.resolveClientId(any())).thenReturn("test-client"); |
| 47 | + when(rateLimitService.resolveBucket(eq("test-client"), any(RateLimitTier.class))) |
| 48 | + .thenAnswer(inv -> fullBucket(((RateLimitTier) inv.getArgument(1)).getCapacity())); |
| 49 | + } |
| 50 | + |
| 51 | + // ========= COORDINATES VALIDATION ========= |
| 52 | + |
| 53 | + @Test |
| 54 | + void shouldReturn400WhenLatitudeExceedsMaximum() throws Exception { |
| 55 | + mockMvc.perform(get("/api/v1/geo/coordinates?lat=999&lon=0")) |
| 56 | + .andExpect(status().isBadRequest()) |
| 57 | + .andExpect(jsonPath("$.message").exists()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void shouldReturn400WhenLatitudeBelowMinimum() throws Exception { |
| 62 | + mockMvc.perform(get("/api/v1/geo/coordinates?lat=-999&lon=0")) |
| 63 | + .andExpect(status().isBadRequest()) |
| 64 | + .andExpect(jsonPath("$.message").exists()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void shouldReturn400WhenLongitudeExceedsMaximum() throws Exception { |
| 69 | + mockMvc.perform(get("/api/v1/geo/coordinates?lat=0&lon=999")) |
| 70 | + .andExpect(status().isBadRequest()) |
| 71 | + .andExpect(jsonPath("$.message").exists()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void shouldReturn400WhenLongitudeBelowMinimum() throws Exception { |
| 76 | + mockMvc.perform(get("/api/v1/geo/coordinates?lat=0&lon=-999")) |
| 77 | + .andExpect(status().isBadRequest()) |
| 78 | + .andExpect(jsonPath("$.message").exists()); |
| 79 | + } |
| 80 | + |
| 81 | + // ========= CITY VALIDATION ========= |
| 82 | + |
| 83 | + @Test |
| 84 | + void shouldReturn400WhenCityIsBlank() throws Exception { |
| 85 | + // use template form so MockMvc encodes the space correctly to %20 |
| 86 | + mockMvc.perform(get("/api/v1/geo/city/{city}", " ")) |
| 87 | + .andExpect(status().isBadRequest()) |
| 88 | + .andExpect(jsonPath("$.message").exists()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void shouldReturn400WhenCityExceedsMaxLength() throws Exception { |
| 93 | + var longCity = "A".repeat(101); |
| 94 | + |
| 95 | + mockMvc.perform(get("/api/v1/geo/city/" + longCity)) |
| 96 | + .andExpect(status().isBadRequest()) |
| 97 | + .andExpect(jsonPath("$.message").exists()); |
| 98 | + } |
| 99 | + |
| 100 | + private Bucket fullBucket(long capacity) { |
| 101 | + return Bucket.builder() |
| 102 | + .addLimit(Bandwidth.builder() |
| 103 | + .capacity(capacity) |
| 104 | + .refillGreedy(capacity, Duration.ofMinutes(1)) |
| 105 | + .build()) |
| 106 | + .build(); |
| 107 | + } |
| 108 | +} |
0 commit comments