CachedEndpointsController.java

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
Location : maybeTruncateResponseBody
Killed by : edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests]/[method:admin_can_list_cached_endpoints_with_response_header_and_body_and_hit_counts()]
negated conditional → KILLED

2.2
Location : maybeTruncateResponseBody
Killed by : edu.ucsb.cs156.diningcachingproxy.DiningProxyIntegrationTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.DiningProxyIntegrationTests]/[method:a_cache_miss_is_stored_and_a_repeat_request_is_served_from_the_cache()]
changed conditional boundary → KILLED

46

1.1
Location : maybeTruncateResponseBody
Killed by : edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests]/[method:list_cached_endpoints_truncates_long_response_bodies()]
Replaced integer subtraction with addition → KILLED

48

1.1
Location : maybeTruncateResponseBody
Killed by : edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests]/[method:admin_can_list_cached_endpoints_with_response_header_and_body_and_hit_counts()]
replaced return value with "" for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController$CachedEndpointDTO::maybeTruncateResponseBody → KILLED

56

1.1
Location : allEndpoints
Killed by : edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests]/[method:admin_can_list_cached_endpoints_with_response_header_and_body_and_hit_counts()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController::allEndpoints → KILLED

67

1.1
Location : lambda$deleteEndpointEntry$0
Killed by : edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests]/[method:admin_tries_to_delete_a_cached_entry_not_found()]
replaced return value with null for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController::lambda$deleteEndpointEntry$0 → KILLED

68

1.1
Location : deleteEndpointEntry
Killed by : edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests]/[method:logged_in_admins_can_delete_cached_endpoints()]
removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponseRepository::delete → KILLED

69

1.1
Location : deleteEndpointEntry
Killed by : edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests]/[method:logged_in_admins_can_delete_cached_endpoints()]
replaced return value with null for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController::deleteEndpointEntry → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1