| 1 | package edu.ucsb.cs156.dining.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 4 | ||
| 5 | import java.time.OffsetDateTime; | |
| 6 | import java.time.format.DateTimeFormatter; | |
| 7 | import java.util.Arrays; | |
| 8 | import lombok.extern.slf4j.Slf4j; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.beans.factory.annotation.Value; | |
| 11 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 12 | import org.springframework.http.HttpEntity; | |
| 13 | import org.springframework.http.HttpHeaders; | |
| 14 | import org.springframework.http.HttpMethod; | |
| 15 | import org.springframework.http.HttpStatus; | |
| 16 | import org.springframework.http.MediaType; | |
| 17 | import org.springframework.http.ResponseEntity; | |
| 18 | import org.springframework.stereotype.Service; | |
| 19 | import org.springframework.web.client.HttpClientErrorException; | |
| 20 | import org.springframework.web.client.HttpServerErrorException; | |
| 21 | import org.springframework.web.client.RestTemplate; | |
| 22 | import org.springframework.web.server.ResponseStatusException; | |
| 23 | ||
| 24 | @Service | |
| 25 | @Slf4j | |
| 26 | public class DiningMenuAPIService { | |
| 27 | @Value("${app.startDate:2024-01-01T00:00:00-08:00}") | |
| 28 | private OffsetDateTime startDate; | |
| 29 | ||
| 30 | @Value("${app.endDate:2024-12-31T23:59:59-08:00}") | |
| 31 | private OffsetDateTime endDate; | |
| 32 | ||
| 33 | @Autowired private ObjectMapper objectMapper; | |
| 34 | ||
| 35 | @Value("${app.ucsb.api.key}") | |
| 36 | private String apiKey; | |
| 37 | ||
| 38 | private RestTemplate restTemplate; | |
| 39 | ||
| 40 | public DiningMenuAPIService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
| 41 | this.restTemplate = restTemplateBuilder.build(); | |
| 42 | } | |
| 43 | ||
| 44 | public static final String GET_DAYS = | |
| 45 | "https://api.ucsb.edu/dining/menu/v1/"; | |
| 46 | ||
| 47 | public static final String GET_COMMONS = | |
| 48 | "https://api.ucsb.edu/dining/menu/v1/{date-time}"; | |
| 49 | ||
| 50 | public static final String GET_MEALS = | |
| 51 | "https://api.ucsb.edu/dining/menu/v1/{date-time}/{dining-common-code}"; | |
| 52 | ||
| 53 | public String getDays() throws Exception { | |
| 54 | HttpHeaders headers = new HttpHeaders(); | |
| 55 |
1
1. getDays : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 56 |
1
1. getDays : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 57 |
1
1. getDays : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 58 |
1
1. getDays : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 59 | ||
| 60 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 61 | ||
| 62 | String url = GET_DAYS; | |
| 63 | log.info("url=" + url); | |
| 64 | ||
| 65 | String retVal = ""; | |
| 66 | MediaType contentType = null; | |
| 67 | HttpStatus statusCode = null; | |
| 68 | ||
| 69 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 70 | contentType = re.getHeaders().getContentType(); | |
| 71 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 72 | retVal = re.getBody(); | |
| 73 | ||
| 74 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 75 |
1
1. getDays : replaced return value with "" for edu/ucsb/cs156/dining/services/DiningMenuAPIService::getDays → KILLED |
return retVal; |
| 76 | } | |
| 77 | ||
| 78 | public String getCommons(OffsetDateTime dateTime) { | |
| 79 | HttpHeaders headers = new HttpHeaders(); | |
| 80 |
1
1. getCommons : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 81 |
1
1. getCommons : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 82 |
1
1. getCommons : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 83 |
1
1. getCommons : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 84 | ||
| 85 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 86 | ||
| 87 | DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; | |
| 88 | String formattedDateTime = dateTime.format(formatter); | |
| 89 | ||
| 90 | String url = GET_COMMONS | |
| 91 | .replace("{date-time}", formattedDateTime); | |
| 92 | ||
| 93 | log.info("url=" + url); | |
| 94 | ||
| 95 | String retVal = ""; | |
| 96 | MediaType contentType = null; | |
| 97 | HttpStatus statusCode = null; | |
| 98 | ||
| 99 | ResponseEntity<String> re = | |
| 100 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 101 | contentType = re.getHeaders().getContentType(); | |
| 102 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 103 | retVal = re.getBody(); | |
| 104 | ||
| 105 |
1
1. getCommons : negated conditional → KILLED |
if (retVal.equals("null")) |
| 106 | { | |
| 107 | retVal = "{\"error\": \"Commons doesn't serve meals on given day.\"}"; | |
| 108 | } | |
| 109 | ||
| 110 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 111 |
1
1. getCommons : replaced return value with "" for edu/ucsb/cs156/dining/services/DiningMenuAPIService::getCommons → KILLED |
return retVal; |
| 112 | } | |
| 113 | ||
| 114 | public String getMeals(OffsetDateTime dateTime, String diningCommonCode) { | |
| 115 | HttpHeaders headers = new HttpHeaders(); | |
| 116 |
1
1. getMeals : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 117 |
1
1. getMeals : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 118 |
1
1. getMeals : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 119 |
1
1. getMeals : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 120 | ||
| 121 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 122 | ||
| 123 | DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; | |
| 124 | String formattedDateTime = dateTime.format(formatter); | |
| 125 | ||
| 126 | String url = GET_MEALS | |
| 127 | .replace("{date-time}", formattedDateTime) | |
| 128 | .replace("{dining-common-code}", diningCommonCode); | |
| 129 | ||
| 130 | log.info("url=" + url); | |
| 131 | ||
| 132 | String retVal = ""; | |
| 133 | MediaType contentType = null; | |
| 134 | HttpStatus statusCode = null; | |
| 135 | ||
| 136 | ResponseEntity<String> re = | |
| 137 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 138 | contentType = re.getHeaders().getContentType(); | |
| 139 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 140 | retVal = re.getBody(); | |
| 141 | ||
| 142 |
1
1. getMeals : negated conditional → KILLED |
if (retVal.equals("null")) |
| 143 | { | |
| 144 | retVal = "{\"error\": \"Meals are not served at given commons on given day.\"}"; | |
| 145 | } | |
| 146 | ||
| 147 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 148 |
1
1. getMeals : replaced return value with "" for edu/ucsb/cs156/dining/services/DiningMenuAPIService::getMeals → KILLED |
return retVal; |
| 149 | } | |
| 150 | } | |
Mutations | ||
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 57 |
1.1 |
|
| 58 |
1.1 |
|
| 75 |
1.1 |
|
| 80 |
1.1 |
|
| 81 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 105 |
1.1 |
|
| 111 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |
|
| 118 |
1.1 |
|
| 119 |
1.1 |
|
| 142 |
1.1 |
|
| 148 |
1.1 |