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.services.EvictStaleCachedResponsesJobFactory;
6
import edu.ucsb.cs156.jobs.entities.Job;
7
import io.swagger.v3.oas.annotations.Operation;
8
import io.swagger.v3.oas.annotations.Parameter;
9
import io.swagger.v3.oas.annotations.tags.Tag;
10
import java.time.Instant;
11
import java.util.List;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.security.access.prepost.PreAuthorize;
14
import org.springframework.web.bind.annotation.GetMapping;
15
import org.springframework.web.bind.annotation.PostMapping;
16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestParam;
18
import org.springframework.web.bind.annotation.RestController;
19
20
/**
21
 * Lets an admin see every API endpoint the proxy has cached so far, with a hit count for each — the
22
 * observation-only groundwork for per-endpoint TTL policy (see issue #2).
23
 */
24
@Tag(name = "Admin - Cached Endpoints")
25
@RequestMapping("/api/admin/endpoints")
26
@RestController
27
public class CachedEndpointsController extends ApiController {
28
29
  @Autowired private CachedResponseRepository cachedResponseRepository;
30
  @Autowired private EvictStaleCachedResponsesJobFactory evictStaleCachedResponsesJobFactory;
31
32
  public record CachedEndpointDTO(String requestPath, long hitCount, Instant lastAccessedAt) {
33
    public CachedEndpointDTO(CachedResponse cachedResponse) {
34
      this(
35
          cachedResponse.getRequestPath(),
36
          cachedResponse.getHitCount(),
37
          cachedResponse.getLastAccessedAt());
38
    }
39
  }
40
41
  @Operation(summary = "List all API endpoints the proxy has cached, with hit counts")
42
  @PreAuthorize("hasRole('ROLE_ADMIN')")
43
  @GetMapping("")
44
  public List<CachedEndpointDTO> allEndpoints() {
45 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();
46
  }
47
48
  @Operation(
49
      summary =
50
          "Run cache eviction now: delete cached endpoints not accessed in the last maxAgeDays"
51
              + " days (defaults to the same max age the nightly run uses)")
52
  @PreAuthorize("hasRole('ROLE_ADMIN')")
53
  @PostMapping("/evict")
54
  public Job evictStaleEndpoints(
55
      @Parameter(name = "maxAgeDays") @RequestParam(required = false) Integer maxAgeDays) {
56 2 1. evictStaleEndpoints : negated conditional → KILLED
2. evictStaleEndpoints : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController::evictStaleEndpoints → KILLED
    return maxAgeDays == null
57
        ? evictStaleCachedResponsesJobFactory.launch()
58
        : evictStaleCachedResponsesJobFactory.launch(maxAgeDays);
59
  }
60
}

Mutations

45

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_hit_counts_and_last_access()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/diningcachingproxy/controller/CachedEndpointsController::allEndpoints → KILLED

56

1.1
Location : evictStaleEndpoints
Killed by : edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.controller.CachedEndpointsControllerTests]/[method:admin_can_trigger_eviction_with_the_default_max_age()]
negated conditional → KILLED

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

Active mutators

Tests examined


Report generated by PIT 1.22.1