Skip to content
Snippets Groups Projects
Commit 881dce2b authored by Christoph Thelen's avatar Christoph Thelen
Browse files

Move feedback calculations from Service to Model class

parent 0a0a4f71
Branches
No related merge requests found
......@@ -27,6 +27,8 @@ import java.util.List;
* The feedback values of a single session.
*/
public class Feedback {
private static final double Z_THRESHOLD = 0.1;
public static final int MIN_FEEDBACK_TYPE = 0;
public static final int MAX_FEEDBACK_TYPE = 3;
......@@ -50,6 +52,23 @@ public class Feedback {
return values;
}
public double getAverage() {
final double count = this.getCount();
final double sum = values.get(FEEDBACK_OK) + values.get(FEEDBACK_SLOWER) * 2
+ values.get(FEEDBACK_AWAY) * 3;
if (Math.abs(count) < Z_THRESHOLD) {
return 0;
}
return sum / count;
}
public int getCount() {
return values.get(FEEDBACK_FASTER) + values.get(FEEDBACK_OK)
+ values.get(FEEDBACK_SLOWER) + values.get(FEEDBACK_AWAY);
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
......
......@@ -42,7 +42,6 @@ import java.util.Set;
public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPublisherAware {
private static final int DEFAULT_SCHEDULER_DELAY = 5000;
private static final double Z_THRESHOLD = 0.1;
/**
* minutes, after which the feedback is deleted
......@@ -125,28 +124,17 @@ public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPub
@Override
public int countFeedbackByRoomId(final String roomId) {
final Feedback feedback = this.getByRoomId(roomId);
final List<Integer> values = feedback.getValues();
return values.get(Feedback.FEEDBACK_FASTER) + values.get(Feedback.FEEDBACK_OK)
+ values.get(Feedback.FEEDBACK_SLOWER) + values.get(Feedback.FEEDBACK_AWAY);
return feedback.getCount();
}
@Override
public double calculateAverageFeedback(final String roomId) {
final Room room = roomService.get(roomId);
if (room == null) {
throw new NotFoundException();
}
final Feedback feedback = feedbackStorage.getByRoom(room);
final List<Integer> values = feedback.getValues();
final double count = values.get(Feedback.FEEDBACK_FASTER) + values.get(Feedback.FEEDBACK_OK)
+ values.get(Feedback.FEEDBACK_SLOWER) + values.get(Feedback.FEEDBACK_AWAY);
final double sum = values.get(Feedback.FEEDBACK_OK) + values.get(Feedback.FEEDBACK_SLOWER) * 2
+ values.get(Feedback.FEEDBACK_AWAY) * 3;
if (Math.abs(count) < Z_THRESHOLD) {
final Feedback feedback = this.getByRoomId(roomId);
if (feedback.getCount() == 0) {
throw new NoContentException();
}
return sum / count;
return feedback.getAverage();
}
@Override
......
......@@ -19,8 +19,7 @@ package de.thm.arsnova.model;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
public class FeedbackTest {
......@@ -64,4 +63,35 @@ public class FeedbackTest {
assertFalse(f1.equals(f2));
assertFalse(f2.equals(f1));
}
@Test
public void shouldCalculateAverageValue() {
Feedback f = new Feedback(1, 0, 0, 1);
double expected = 1.5;
double actual = f.getAverage();
assertEquals(expected, actual, 0.01);
}
@Test
public void averageCalculationShouldAvoidDivisionByZero() {
Feedback f = new Feedback(0, 0, 0, 0);
double expected = 0;
double actual = f.getAverage();
assertEquals(expected, actual, 0.01);
}
@Test
public void shouldCountVotes() {
Feedback f = new Feedback(2, 4, 8, 16);
int expected = 30;
int actual = f.getCount();
assertEquals(expected, actual);
}
}
......@@ -3,6 +3,8 @@ package de.thm.arsnova.service;
import de.thm.arsnova.event.DeleteFeedbackForRoomsEvent;
import de.thm.arsnova.model.Feedback;
import de.thm.arsnova.model.Room;
import de.thm.arsnova.web.exceptions.NoContentException;
import de.thm.arsnova.web.exceptions.NotFoundException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -17,9 +19,9 @@ import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class FeedbackServiceTest {
......@@ -42,7 +44,8 @@ public class FeedbackServiceTest {
this.room = new Room();
room.setId(this.roomId);
when(this.roomService.get(this.roomId)).thenReturn(room);
lenient().when(this.roomService.get(anyString())).thenReturn(null);
lenient().when(this.roomService.get(eq(this.roomId))).thenReturn(room);
FeedbackStorageService fss = new FeedbackStorageServiceImpl();
this.feedbackService = new FeedbackServiceImpl(fss, this.roomService);
......@@ -70,6 +73,16 @@ public class FeedbackServiceTest {
assertEquals(expected, actual, 0.01);
}
@Test(expected = NoContentException.class)
public void averageCalculationShouldThrowNoContentExceptionWhenNoFeedbackIsPresent() {
feedbackService.calculateAverageFeedback(roomId);
}
@Test(expected = NotFoundException.class)
public void averageCalculationShouldThrowNotFoundExceptionWhenRoomIsUnknown() {
feedbackService.calculateAverageFeedback("room-id-does-not-exist");
}
@Test
public void shouldReturnCompleteFeedbackEntity() {
feedbackService.save(roomId, 0, "user-id-one");
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment