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.time.Clock;
6
import java.util.List;
7
import java.util.Optional;
8
import org.springframework.beans.factory.annotation.Value;
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
  private final Clock clock;
34
35
  public DiningProxyService(
36
      CachedResponseRepository cachedResponseRepository,
37
      StatsService statsService,
38
      RestTemplate restTemplate,
39
      @Value("${app.upstream.host}") String upstreamHost,
40
      Clock clock) {
41
    this.cachedResponseRepository = cachedResponseRepository;
42
    this.statsService = statsService;
43
    this.restTemplate = restTemplate;
44
    this.upstreamHost = upstreamHost;
45
    this.clock = clock;
46
  }
47
48
  public ResponseEntity<String> proxyGet(String requestPath, String apiKey, String apiVersion) {
49
    Optional<CachedResponse> cached = cachedResponseRepository.findByRequestPath(requestPath);
50 1 1. proxyGet : negated conditional → KILLED
    if (cached.isPresent()) {
51 1 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordHit → KILLED
      statsService.recordHit();
52
      CachedResponse c = cached.get();
53 2 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponse::setHitCount → KILLED
2. proxyGet : Replaced long addition with subtraction → KILLED
      c.setHitCount(c.getHitCount() + 1);
54 1 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponse::setLastAccessedAt → KILLED
      c.setLastAccessedAt(clock.instant());
55
      cachedResponseRepository.save(c);
56 1 1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED
      return ResponseEntity.status(c.getResponseStatus())
57
          .contentType(MediaType.APPLICATION_JSON)
58
          .body(c.getResponseBody());
59
    }
60
61 1 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordMiss → KILLED
    statsService.recordMiss();
62
63
    HttpHeaders headers = new HttpHeaders();
64 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(List.of(MediaType.APPLICATION_JSON));
65 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
66 1 1. proxyGet : negated conditional → KILLED
    if (apiKey != null) {
67 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::set → KILLED
      headers.set("ucsb-api-key", apiKey);
68
    }
69 1 1. proxyGet : negated conditional → KILLED
    if (apiVersion != null) {
70 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::set → KILLED
      headers.set("ucsb-api-version", apiVersion);
71
    }
72
73
    ResponseEntity<String> upstreamResponse;
74
    try {
75
      upstreamResponse =
76
          restTemplate.exchange(
77
              upstreamHost + requestPath, HttpMethod.GET, new HttpEntity<>(headers), String.class);
78
    } catch (HttpClientErrorException | HttpServerErrorException e) {
79 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());
80
    }
81
82 1 1. proxyGet : negated conditional → KILLED
    if (upstreamResponse.getStatusCode().is2xxSuccessful()) {
83
      cachedResponseRepository.save(
84
          CachedResponse.builder()
85
              .requestPath(requestPath)
86
              .responseStatus(upstreamResponse.getStatusCode().value())
87
              .responseBody(upstreamResponse.getBody())
88
              .hitCount(1)
89
              .lastAccessedAt(clock.instant())
90
              .build());
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

50

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

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_increments_hit_count_and_updates_last_access()]
removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordHit → KILLED

53

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_increments_hit_count_and_updates_last_access()]
removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponse::setHitCount → KILLED

2.2
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_increments_hit_count_and_updates_last_access()]
Replaced long addition with subtraction → KILLED

54

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_increments_hit_count_and_updates_last_access()]
removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponse::setLastAccessedAt → 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:cache_hit_returns_cached_response_increments_hit_count_and_updates_last_access()]
replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → 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_with_timestamp()]
removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordMiss → KILLED

64

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

65

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

66

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_with_timestamp()]
negated conditional → KILLED

67

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_with_timestamp()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

69

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_with_timestamp()]
negated conditional → KILLED

70

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_with_timestamp()]
removed call to org/springframework/http/HttpHeaders::set → KILLED

79

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

82

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