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.http.HttpEntity;
9
import org.springframework.http.HttpHeaders;
10
import org.springframework.http.HttpMethod;
11
import org.springframework.http.MediaType;
12
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Service;
14
import org.springframework.web.client.HttpClientErrorException;
15
import org.springframework.web.client.HttpServerErrorException;
16
import org.springframework.web.client.RestTemplate;
17
18
/**
19
 * Shared cache-check/forward/store logic used by every proxy controller. A cache miss is forwarded
20
 * upstream with whatever {@code ucsb-api-key}/{@code ucsb-api-version} headers the caller sent;
21
 * only a successful (2xx) upstream response gets cached. This is a byte-for-byte mirror of what the
22
 * upstream UCSB API would have returned — no dining-specific business logic (e.g. proj-dining's own
23
 * empty-response-to-204 conversion) belongs here.
24
 */
25
@Service
26
public class DiningProxyService {
27
28
  private final CachedResponseRepository cachedResponseRepository;
29
  private final StatsService statsService;
30
  private final RestTemplate restTemplate;
31
  private final String upstreamHost;
32
33
  public DiningProxyService(
34
      CachedResponseRepository cachedResponseRepository,
35
      StatsService statsService,
36
      RestTemplate restTemplate,
37
      @Value("${app.upstream.host}") String upstreamHost) {
38
    this.cachedResponseRepository = cachedResponseRepository;
39
    this.statsService = statsService;
40
    this.restTemplate = restTemplate;
41
    this.upstreamHost = upstreamHost;
42
  }
43
44
  public ResponseEntity<String> proxyGet(String requestPath, String apiKey, String apiVersion) {
45
    Optional<CachedResponse> cached = cachedResponseRepository.findByRequestPath(requestPath);
46 1 1. proxyGet : negated conditional → KILLED
    if (cached.isPresent()) {
47 1 1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordHit → KILLED
      statsService.recordHit();
48
      CachedResponse c = cached.get();
49 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);
50
      cachedResponseRepository.save(c);
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
      cachedResponseRepository.save(
79
          CachedResponse.builder()
80
              .requestPath(requestPath)
81
              .responseStatus(upstreamResponse.getStatusCode().value())
82
              .responseBody(upstreamResponse.getBody())
83
              .hitCount(1)
84
              .missCount(1)
85
              .build());
86
    }
87
88 1 1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED
    return upstreamResponse;
89
  }
90
}

Mutations

46

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

47

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

49

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_but_not_miss_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_but_not_miss_count()]
Replaced long addition with subtraction → 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_but_not_miss_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:cache_miss_forwards_to_upstream_with_headers_and_caches_2xx_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

88

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