Skip to content
Snippets Groups Projects
Commit 1e6d4d74 authored by Danae Savvidi's avatar Danae Savvidi :laughing:
Browse files

fix endpoint & tests

parent de1e8969
Branches
Tags
2 merge requests!8412425.0.0 release,!732Resolve "Add endpoint for TAM star ratings issue"
...@@ -26,6 +26,7 @@ import java.util.*; ...@@ -26,6 +26,7 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -273,10 +274,13 @@ public class HomeController { ...@@ -273,10 +274,13 @@ public class HomeController {
*/ */
@GetMapping("/api/average-star-rating") @GetMapping("/api/average-star-rating")
@ResponseBody @ResponseBody
public Map<Long, Double> averageStarRating(@RequestParam("ids") List<Long> ids) { public ResponseEntity<Map<Long, Double>> averageStarRating(@RequestParam("ids") List<Long> ids) {
return ids.stream().filter(id -> ps.canViewFeedback(id)) Map<Long, Double> result = ids.stream()
.collect(Collectors.toMap(id -> id, .filter(id -> ps.canViewFeedback(id))
.collect(Collectors.toMap(
id -> id,
id -> fs.getAvgStarRating(fs.countRatings(fr.findByAssistant(id))))); id -> fs.getAvgStarRating(fs.countRatings(fr.findByAssistant(id)))));
return ResponseEntity.ok(result);
} }
/** /**
......
...@@ -22,11 +22,11 @@ import org.springframework.context.annotation.Profile; ...@@ -22,11 +22,11 @@ import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import nl.tudelft.labracore.lib.security.LabradorSecurityConfig; import nl.tudelft.labracore.app.security.LabradorSecurityConfigWithApi;
@Configuration @Configuration
@Profile("!production") @Profile("!production")
public class DevSecurityConfig extends LabradorSecurityConfig { public class DevSecurityConfig extends LabradorSecurityConfigWithApi {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
......
...@@ -21,11 +21,11 @@ import org.springframework.context.annotation.Configuration; ...@@ -21,11 +21,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import nl.tudelft.labracore.lib.security.LabradorSecurityConfig; import nl.tudelft.labracore.app.security.LabradorSecurityConfigWithApi;
@Configuration @Configuration
@Profile("production") @Profile("production")
public class ProductionSecurityConfig extends LabradorSecurityConfig { public class ProductionSecurityConfig extends LabradorSecurityConfigWithApi {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
super.configure(http); super.configure(http);
......
...@@ -157,7 +157,7 @@ public class FeedbackService { ...@@ -157,7 +157,7 @@ public class FeedbackService {
totalRatings += ratings.get(i) * (i + 1); totalRatings += ratings.get(i) * (i + 1);
totalCount += ratings.get(i); totalCount += ratings.get(i);
} }
return totalCount == 0 ? null return totalCount == 0 ? 0
: BigDecimal.valueOf((double) totalRatings / totalCount) : BigDecimal.valueOf((double) totalRatings / totalCount)
.setScale(1, RoundingMode.HALF_UP) .setScale(1, RoundingMode.HALF_UP)
.doubleValue(); .doubleValue();
......
...@@ -79,7 +79,6 @@ ...@@ -79,7 +79,6 @@
Ratings range from 1 (needs improvement) to 5 (excellent) for request handling performance. Ratings range from 1 (needs improvement) to 5 (excellent) for request handling performance.
</p> </p>
</div> </div>
<h2 class="font-500" th:text="|Average Star Rating is ${starRating}|"></h2>
</div> </div>
<div class="surface__content"> <div class="surface__content">
<canvas id="feedback-histogram"></canvas> <canvas id="feedback-histogram"></canvas>
......
...@@ -212,12 +212,12 @@ public class HomeControllerTest { ...@@ -212,12 +212,12 @@ public class HomeControllerTest {
when(feedbackService.countRatings(anyList())).thenReturn(List.of(2, 1, 1)); when(feedbackService.countRatings(anyList())).thenReturn(List.of(2, 1, 1));
when(feedbackService.getAvgStarRating(anyList())).thenReturn(4.5); when(feedbackService.getAvgStarRating(anyList())).thenReturn(4.5);
MvcResult result = mvc.perform(get("/average-star-rating") MvcResult result = mvc.perform(get("/api/average-star-rating")
.param("ids", "1", "2", "3") .param("ids", "1", "2", "3")
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andReturn(); .andReturn();
assertEquals(result.getResponse().getContentAsString(),"{\"1\":4.5,\"2\":4.5,\"3\":4.5}"); assertEquals("{\"1\":4.5,\"2\":4.5,\"3\":4.5}", result.getResponse().getContentAsString());
} }
@Test @Test
......
...@@ -20,7 +20,6 @@ package nl.tudelft.queue.service; ...@@ -20,7 +20,6 @@ package nl.tudelft.queue.service;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -250,13 +249,13 @@ public class FeedbackServiceTest { ...@@ -250,13 +249,13 @@ public class FeedbackServiceTest {
@Test @Test
void getAvgStarRating() { void getAvgStarRating() {
assertEquals(3.06, fs.getAvgStarRating(List.of(3, 2, 5, 1, 4)), 0.01); assertEquals(3.1, fs.getAvgStarRating(List.of(3, 2, 5, 1, 4)), 0.01);
} }
@Test @Test
void getAvgStarRatingEmptyOrZerosList() { void getAvgStarRatingEmptyOrZerosList() {
assertNull(fs.getAvgStarRating(List.of())); assertEquals(0, fs.getAvgStarRating(List.of()));
assertNull(fs.getAvgStarRating(List.of(0, 0, 0, 0, 0))); assertEquals(0, fs.getAvgStarRating(List.of(0, 0, 0, 0, 0)));
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment