DiningProxyService.java

1
package edu.ucsb.cs156.diningcachingproxy.services;
2
3
import edu.ucsb.cs156.diningcachingproxy.collections.CachedResponse;
4
import edu.ucsb.cs156.diningcachingproxy.collections.CachedResponseRepository;
5
import java.util.List;
6
import java.util.Optional;
7
import org.springframework.beans.factory.annotation.Value;
8
import org.springframework.dao.DuplicateKeyException;
9
import org.springframework.http.HttpEntity;
10
import org.springframework.http.HttpHeaders;
11
import org.springframework.http.HttpMethod;
12
import org.springframework.http.MediaType;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.stereotype.Service;
15
import org.springframework.web.client.HttpClientErrorException;
16
import org.springframework.web.client.HttpServerErrorException;
17
import org.springframework.web.client.RestTemplate;
18
19
/**
20
 * Shared cache-check/forward/store logic used by every proxy controller. A cache miss is forwarded
21
 * upstream with whatever {@code ucsb-api-key}/{@code ucsb-api-version} headers the caller sent;
22
 * only a successful (2xx) upstream response gets cached. This is a byte-for-byte mirror of what the
23
 * upstream UCSB API would have returned — no dining-specific business logic (e.g. proj-dining's own
24
 * empty-response-to-204 conversion) belongs here.
25
 */
26
@Service
27
public class DiningProxyService {
28
29
  private final CachedResponseRepository cachedResponseRepository;
30
  private final StatsService statsService;
31
  private final RestTemplate restTemplate;
32
  private final String upstreamHost;
33
34
  public DiningProxyService(
35
      CachedResponseRepository cachedResponseRepository,
36
      StatsService statsService,
37
      RestTemplate restTemplate,
38
      @Value("${app.upstream.host}") String upstreamHost) {
39
    this.cachedResponseRepository = cachedResponseRepository;
40
    this.statsService = statsService;
41
    this.restTemplate = restTemplate;
42
    this.upstreamHost = upstreamHost;
43
  }
44
45
  public ResponseEntity<String> proxyGet(String requestPath, String apiKey, String apiVersion) {
46
    Optional<CachedResponse> cached = cachedResponseRepository.findByRequestPath(requestPath);
47 1 1. proxyGet : negated conditional → KILLED
    if (cached.isPresent()) {
48 1 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordHit → KILLED
      statsService.recordHit();
49
      CachedResponse c = cached.get();
50 1 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponseRepository::findAndIncrementHitCountByRequestPath → KILLED
      cachedResponseRepository.findAndIncrementHitCountByRequestPath(requestPath);
51 1 1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED
      return ResponseEntity.status(c.getResponseStatus())
52
          .contentType(MediaType.APPLICATION_JSON)
53
          .body(c.getResponseBody());
54
    }
55
56 1 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordMiss → KILLED
    statsService.recordMiss();
57
58
    HttpHeaders headers = new HttpHeaders();
59 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(List.of(MediaType.APPLICATION_JSON));
60 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
61 1 1. proxyGet : negated conditional → KILLED
    if (apiKey != null) {
62 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::set → KILLED
      headers.set("ucsb-api-key", apiKey);
63
    }
64 1 1. proxyGet : negated conditional → KILLED
    if (apiVersion != null) {
65 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::set → KILLED
      headers.set("ucsb-api-version", apiVersion);
66
    }
67
68
    ResponseEntity<String> upstreamResponse;
69
    try {
70
      upstreamResponse =
71
          restTemplate.exchange(
72
              upstreamHost + requestPath, HttpMethod.GET, new HttpEntity<>(headers), String.class);
73
    } catch (HttpClientErrorException | HttpServerErrorException e) {
74 1 1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED
      return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsString());
75
    }
76
77 1 1. proxyGet : negated conditional → KILLED
    if (upstreamResponse.getStatusCode().is2xxSuccessful()) {
78
      try {
79
        cachedResponseRepository.save(
80
            CachedResponse.builder()
81
                .requestPath(requestPath)
82
                .responseStatus(upstreamResponse.getStatusCode().value())
83
                .responseBody(upstreamResponse.getBody())
84
                .hitCount(1)
85
                .build());
86
      } catch (DuplicateKeyException e) {
87
        // A concurrent request for the same path missed the cache too and saved its response
88
        // first; the unique index on requestPath rejected this second copy. That entry is
89
        // equivalent, so serve the upstream response as-is rather than failing the request.
90
      }
91
    }
92
93 1 1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED
    return upstreamResponse;
94
  }
95
}

Mutations

47

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:a_non_2xx_response_that_does_not_throw_is_passed_through_and_not_cached()]
negated conditional → KILLED

48

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:cache_hit_returns_cached_response_and_increments_hit_count()]
removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordHit → KILLED

50

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:cache_hit_returns_cached_response_and_increments_hit_count()]
removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponseRepository::findAndIncrementHitCountByRequestPath → KILLED

51

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:cache_hit_returns_cached_response_and_increments_hit_count()]
replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED

56

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:duplicate_key_on_save_still_returns_upstream_response()]
removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordMiss → KILLED

59

1.1
Location : proxyGet
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()]
removed call to org/springframework/http/HttpHeaders::setAccept → KILLED

60

1.1
Location : proxyGet
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()]
removed call to org/springframework/http/HttpHeaders::setContentType → KILLED

61

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:cache_miss_forwards_to_upstream_with_headers_and_caches_2xx_response()]
negated conditional → KILLED

62

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:cache_miss_forwards_to_upstream_with_headers_and_caches_2xx_response()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

64

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:cache_miss_forwards_to_upstream_with_headers_and_caches_2xx_response()]
negated conditional → KILLED

65

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:cache_miss_forwards_to_upstream_with_headers_and_caches_2xx_response()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

74

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:server_error_response_is_passed_through_and_not_cached()]
replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED

77

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:a_non_2xx_response_that_does_not_throw_is_passed_through_and_not_cached()]
negated conditional → KILLED

93

1.1
Location : proxyGet
Killed by : edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.diningcachingproxy.services.DiningProxyServiceTests]/[method:a_non_2xx_response_that_does_not_throw_is_passed_through_and_not_cached()]
replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1