| 1 | package edu.ucsb.cs156.diningcachingproxy.controller; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.diningcachingproxy.collections.CachedResponse; | |
| 4 | import edu.ucsb.cs156.diningcachingproxy.collections.CachedResponseRepository; | |
| 5 | import edu.ucsb.cs156.diningcachingproxy.errors.EntityNotFoundException; | |
| 6 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.Parameter; | |
| 8 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 9 | import java.util.List; | |
| 10 | import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 12 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 13 | import org.springframework.web.bind.annotation.GetMapping; | |
| 14 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 15 | import org.springframework.web.bind.annotation.RequestParam; | |
| 16 | import org.springframework.web.bind.annotation.RestController; | |
| 17 | ||
| 18 | /** | |
| 19 | * Lets an admin see every API endpoint the proxy has cached so far, with a hit count for each — the | |
| 20 | * observation-only groundwork for per-endpoint TTL policy (see issue #2). | |
| 21 | */ | |
| 22 | @Tag(name = "Admin - Cached Endpoints") | |
| 23 | @RequestMapping("/api/admin/endpoints") | |
| 24 | @RestController | |
| 25 | public class CachedEndpointsController extends ApiController { | |
| 26 | ||
| 27 | @Autowired private CachedResponseRepository cachedResponseRepository; | |
| 28 | ||
| 29 | public record CachedEndpointDTO( | |
| 30 | String requestPath, int responseStatus, String responseBody, long hitCount) { | |
| 31 | public CachedEndpointDTO(CachedResponse cachedResponse) { | |
| 32 | this( | |
| 33 | cachedResponse.getRequestPath(), | |
| 34 | cachedResponse.getResponseStatus(), | |
| 35 | maybeTruncateResponseBody(cachedResponse.getResponseBody()), | |
| 36 | cachedResponse.getHitCount()); | |
| 37 | } | |
| 38 | ||
| 39 | // Returns the truncated response body text if it exceeds the set maxResponseBodyLength | |
| 40 | // Otherwise returns the original response body | |
| 41 | private static String maybeTruncateResponseBody(String responseBody) { | |
| 42 | String truncateFlag = "...[truncated]"; | |
| 43 | int maxResponseBodyLength = 128; | |
| 44 |
2
1. maybeTruncateResponseBody : negated conditional → KILLED 2. maybeTruncateResponseBody : changed conditional boundary → KILLED |
if (responseBody.length() > maxResponseBodyLength) { |
| 45 | responseBody = | |
| 46 |
1
1. maybeTruncateResponseBody : Replaced integer subtraction with addition → KILLED |
responseBody.substring(0, maxResponseBodyLength - truncateFlag.length()) + truncateFlag; |
| 47 | } | |
| 48 |
1
1. maybeTruncateResponseBody : replaced return value with "" for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController$CachedEndpointDTO::maybeTruncateResponseBody → KILLED |
return responseBody; |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | @Operation(summary = "List all API endpoints the proxy has cached, with hit counts") | |
| 53 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 54 | @GetMapping("") | |
| 55 | public List<CachedEndpointDTO> allEndpoints() { | |
| 56 |
1
1. allEndpoints : replaced return value with Collections.emptyList for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController::allEndpoints → KILLED |
return cachedResponseRepository.findAll().stream().map(CachedEndpointDTO::new).toList(); |
| 57 | } | |
| 58 | ||
| 59 | @Operation(summary = "Delete a single cached endpoint entry") | |
| 60 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 61 | @DeleteMapping("/delete") | |
| 62 | public Object deleteEndpointEntry( | |
| 63 | @Parameter(name = "requestPath") @RequestParam String requestPath) { | |
| 64 | CachedResponse cachedResponse = | |
| 65 | cachedResponseRepository | |
| 66 | .findByRequestPath(requestPath) | |
| 67 |
1
1. lambda$deleteEndpointEntry$0 : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController::lambda$deleteEndpointEntry$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(CachedResponse.class, requestPath)); |
| 68 |
1
1. deleteEndpointEntry : removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponseRepository::delete → KILLED |
cachedResponseRepository.delete(cachedResponse); |
| 69 |
1
1. deleteEndpointEntry : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController::deleteEndpointEntry → KILLED |
return genericMessage("Cached entry for request path %s deleted".formatted(requestPath)); |
| 70 | } | |
| 71 | } | |
Mutations | ||
| 44 |
1.1 2.2 |
|
| 46 |
1.1 |
|
| 48 |
1.1 |
|
| 56 |
1.1 |
|
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 69 |
1.1 |