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.Instant;
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
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 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);
51
      cachedResponseRepository.save(c);
52 1 1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED
      return ResponseEntity.status(c.getResponseStatus())
53
          .contentType(MediaType.APPLICATION_JSON)
54
          .body(c.getResponseBody());
55
    }
56
57 1 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordMiss → KILLED
    statsService.recordMiss();
58
59
    HttpHeaders headers = new HttpHeaders();
60 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED
    headers.setAccept(List.of(MediaType.APPLICATION_JSON));
61 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED
    headers.setContentType(MediaType.APPLICATION_JSON);
62 1 1. proxyGet : negated conditional → KILLED
    if (apiKey != null) {
63 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::set → KILLED
      headers.set("ucsb-api-key", apiKey);
64
    }
65 1 1. proxyGet : negated conditional → KILLED
    if (apiVersion != null) {
66 1 1. proxyGet : removed call to org/springframework/http/HttpHeaders::set → KILLED
      headers.set("ucsb-api-version", apiVersion);
67
    }
68
69
    ResponseEntity<String> upstreamResponse;
70
    try {
71
      upstreamResponse =
72
          restTemplate.exchange(
73
              upstreamHost + requestPath, HttpMethod.GET, new HttpEntity<>(headers), String.class);
74
    } catch (HttpClientErrorException | HttpServerErrorException e) {
75 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());
76
    }
77
78 1 1. proxyGet : negated conditional → KILLED
    if (upstreamResponse.getStatusCode().is2xxSuccessful()) {
79
      cachedResponseRepository.save(
80
          CachedResponse.builder()
81
              .requestPath(requestPath)
82
              .responseStatus(upstreamResponse.getStatusCode().value())
83
              .responseBody(upstreamResponse.getBody())
84
              .cachedAt(Instant.now())
85
              .hitCount(1)
86
              .build());
87
    }
88
89 1 1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED
    return upstreamResponse;
90
  }
91
}

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

52

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

57

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 edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordMiss → 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::setAccept → KILLED

61

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

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

63

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

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

75

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

78

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

89

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