Skip to content

Commit

Permalink
Refactoring and add integration tests
Browse files Browse the repository at this point in the history
Co-authored-by: skyliam <liamskydamien@gmail.com>
  • Loading branch information
aykborstelmann and liamskydamien committed Nov 24, 2019
1 parent 42d01f0 commit 3a60d49
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.bjs.bjsapi.controllers;

import java.util.List;
import java.util.Optional;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -28,45 +26,61 @@ public StudentScoreController(StudentRepository studentRepository, SportResultRe
this.calculationInformationService = calculationInformationService;
}

@GetMapping("/score")
public ResponseEntity<?> returnScore(@PathVariable("id") Long id) {
return studentRepository.findById(id)
.map(this::calculateScore)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}

Integer calculateScore(Student student) {
return sportResultRepository.findByStudent(student)
.stream()
.mapToInt(sportResult -> calculatePoints(sportResult, student.getFemale()))
.sum();
}

@GetMapping("/score")
public Integer calculateScore(@PathVariable("id") Long id) {
int scoreResult = 0;
Optional<Student> optionalStudent = studentRepository.findById(id);
if (optionalStudent.isPresent()) {
Student student = optionalStudent.get();
List<SportResult> sportResults = sportResultRepository.findByStudent(student);
for (SportResult sportResult : sportResults) {
if (sportResult.getDiscipline().isRUN()) {
double distance = sportResult.getDiscipline().getDistance();
double measurement = sportResult.getResult();
double extra = getExtra(sportResult.getDiscipline().getDistance());
double a = calculationInformationService.getAValue(student.getFemale(), sportResult.getDiscipline());
double c = calculationInformationService.getCValue(student.getFemale(), sportResult.getDiscipline());
double scoreRun = (distance / (measurement + extra) - a) / c;
scoreResult = scoreResult + (int) Math.floor(scoreRun);
} else {
double measurement = Math.sqrt(sportResult.getResult());
double a = calculationInformationService.getAValue(student.getFemale(), sportResult.getDiscipline());
double c = calculationInformationService.getCValue(student.getFemale(), sportResult.getDiscipline());
double scoreOther = (measurement - a) / c;
scoreResult = scoreResult + (int) Math.floor(scoreOther);
}
}

return scoreResult;
private int calculatePoints(SportResult sportResult, Boolean female) {
if (sportResult.getDiscipline().isRUN()) {
return calculateRunningPoints(sportResult, female);
} else {
return calculateNotRunningPoints(sportResult, female);
}
}

private int calculateNotRunningPoints(SportResult sportResult, Boolean female) {
// P = ( √M - a ) / c

final double measurement = sportResult.getResult();

return null;
final double a = calculationInformationService.getAValue(female, sportResult.getDiscipline());
final double c = calculationInformationService.getCValue(female, sportResult.getDiscipline());

final double points = (Math.sqrt(measurement) - a) / c;
return (int) Math.floor(points);
}

private int calculateRunningPoints(SportResult sportResult, Boolean female) {
// P = ( D : (M + Z) ) - a) / c

final double distance = sportResult.getDiscipline().getDistance();
final double measurement = sportResult.getResult();

final double extra = calculateExtra(sportResult.getDiscipline().getDistance());

final double a = calculationInformationService.getAValue(female, sportResult.getDiscipline());
final double c = calculationInformationService.getCValue(female, sportResult.getDiscipline());

final double points = ((distance / (measurement + extra)) - a) / c;
return (int) Math.floor(points);
}

private double getExtra(int distance) {
private double calculateExtra(int distance) {
double extra = 0.0;
if(distance <= 300) {
if (distance <= 300) {
extra = 0.24;
}
else if(distance > 300 && distance <= 400) {
} else if (distance <= 400) {
extra = 0.14;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/
public enum DisciplineType {

RUN_50(true,50),
RUN_75(true,75),
RUN_100(true,100),
RUN_800(true,800),
RUN_2000(true,2000),
RUN_3000(true,3000),
RUN_50(true, 50),
RUN_75(true, 75),
RUN_100(true, 100),
RUN_800(true, 800),
RUN_2000(true, 2000),
RUN_3000(true, 3000),

HIGH_JUMP(false),
LONG_JUMP(false),
Expand All @@ -35,8 +35,7 @@ public int getDistance() {
this.isRun = isRun;
}

public boolean isRUN(){
public boolean isRUN() {
return isRun;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.bjs.bjsapi.controllers;

import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.util.Collections;
import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.bjs.bjsapi.database.model.SportResult;
import com.bjs.bjsapi.database.model.Student;
import com.bjs.bjsapi.database.model.enums.DisciplineType;
import com.bjs.bjsapi.database.repository.SportResultRepository;
import com.bjs.bjsapi.database.repository.StudentRepository;
import com.bjs.bjsapi.helper.CalculationInformationService;

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = StudentScoreController.class)
public class StudentScoreControllerIntegrationTest {

@MockBean
public StudentRepository studentRepository;

@MockBean
public SportResultRepository sportResultRepository;

@MockBean
public CalculationInformationService calculationInformationService;

@Autowired
public MockMvc mockMvc;

@Test
public void test_calculateScore_found() throws Exception {
Student student = new Student();
student.setFemale(true);
SportResult sportResult = new SportResult();

sportResult.setDiscipline(DisciplineType.RUN_50);
sportResult.setResult(7.00F);

doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(3.79000).when(calculationInformationService).getAValue(true, DisciplineType.RUN_50);
doReturn(0.00690).when(calculationInformationService).getCValue(true, DisciplineType.RUN_50);

mockMvc.perform(get("/api/v1/students/{id}/score", 1)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(String.valueOf(451)));
}

@Test
public void test_calculateScore_notFound() throws Exception {
Student student = new Student();
student.setFemale(true);
SportResult sportResult = new SportResult();

sportResult.setDiscipline(DisciplineType.RUN_50);
sportResult.setResult(7.00F);

doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(3.79000).when(calculationInformationService).getAValue(true, DisciplineType.RUN_50);
doReturn(0.00690).when(calculationInformationService).getCValue(true, DisciplineType.RUN_50);

mockMvc.perform(get("/api/v1/students/{id}/score", 2)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -35,7 +34,7 @@ public class StudentScoreControllerTest {
private StudentScoreController studentScoreController;

@Before
public void setUp() throws Exception {
public void setUp() {
studentScoreController = new StudentScoreController(studentRepository, sportResultRepository, calculationInformationService);
}

Expand All @@ -48,12 +47,11 @@ public void test_calculation_run_50() {
sportResult.setDiscipline(DisciplineType.RUN_50);
sportResult.setResult(7.00F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(3.79000).when(calculationInformationService).getAValue(true, DisciplineType.RUN_50);
doReturn(0.00690).when(calculationInformationService).getCValue(true, DisciplineType.RUN_50);

Integer integer = studentScoreController.calculateScore(1L);
Integer integer = studentScoreController.calculateScore(student);

assertThat(integer).isEqualTo(451);
}
Expand All @@ -67,34 +65,32 @@ public void test_calculation_run_75() {
sportResult.setDiscipline(DisciplineType.RUN_75);
sportResult.setResult(9.00F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(4.10000).when(calculationInformationService).getAValue(true, DisciplineType.RUN_75);
doReturn(0.00664).when(calculationInformationService).getCValue(true, DisciplineType.RUN_75);

Integer integer = studentScoreController.calculateScore(1L);
Integer integer = studentScoreController.calculateScore(student);

assertThat(integer).isEqualTo(604);
}

@Test
public void test_calculation_run_100() {
Student student = new Student();
student.setFemale(true);
SportResult sportResult = new SportResult();
@Test
public void test_calculation_run_100() {
Student student = new Student();
student.setFemale(true);
SportResult sportResult = new SportResult();

sportResult.setDiscipline(DisciplineType.RUN_100);
sportResult.setResult(12.00F);
sportResult.setDiscipline(DisciplineType.RUN_100);
sportResult.setResult(12.00F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(4.341).when(calculationInformationService).getAValue(true, DisciplineType.RUN_100);
doReturn(0.00676).when(calculationInformationService).getCValue(true, DisciplineType.RUN_100);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(4.341).when(calculationInformationService).getAValue(true, DisciplineType.RUN_100);
doReturn(0.00676).when(calculationInformationService).getCValue(true, DisciplineType.RUN_100);

Integer integer = studentScoreController.calculateScore(1L);
Integer integer = studentScoreController.calculateScore(student);

assertThat(integer).isEqualTo(566);
}
assertThat(integer).isEqualTo(566);
}

@Test
public void test_calculation_high_jump() {
Expand All @@ -105,39 +101,36 @@ public void test_calculation_high_jump() {
sportResult.setDiscipline(DisciplineType.HIGH_JUMP);
sportResult.setResult(1.50F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Collections.singletonList(sportResult)).when(sportResultRepository).findByStudent(student);
doReturn(0.841).when(calculationInformationService).getAValue(true, DisciplineType.HIGH_JUMP);
doReturn(0.00080).when(calculationInformationService).getCValue(true, DisciplineType.HIGH_JUMP);

Integer integer = studentScoreController.calculateScore(1L);
Integer integer = studentScoreController.calculateScore(student);

assertThat(integer).isEqualTo(479);
}


@Test
public void test_calculation_general() {
Student student = new Student();
student.setFemale(false);
public void test_calculation_general() {
Student student = new Student();
student.setFemale(false);

SportResult resultRUN_100 = new SportResult();
resultRUN_100.setDiscipline(DisciplineType.RUN_100);
resultRUN_100.setResult(12.00F);
resultRUN_100.setDiscipline(DisciplineType.RUN_100);
resultRUN_100.setResult(12.00F);

SportResult resultRUN_800 = new SportResult();
resultRUN_800.setDiscipline(DisciplineType.RUN_800);
resultRUN_800.setResult(210.00F);
SportResult resultRUN_800 = new SportResult();
resultRUN_800.setDiscipline(DisciplineType.RUN_800);
resultRUN_800.setResult(210.00F);

SportResult resultJUMP = new SportResult();
resultJUMP.setDiscipline(DisciplineType.LONG_JUMP);
resultJUMP.setResult(4.20F);
SportResult resultJUMP = new SportResult();
resultJUMP.setDiscipline(DisciplineType.LONG_JUMP);
resultJUMP.setResult(4.20F);

SportResult resultTHROW_200 = new SportResult();
resultTHROW_200.setDiscipline(DisciplineType.BALL_THROWING_200);
resultTHROW_200.setResult(35.00F);
SportResult resultTHROW_200 = new SportResult();
resultTHROW_200.setDiscipline(DisciplineType.BALL_THROWING_200);
resultTHROW_200.setResult(35.00F);

doReturn(Optional.of(student)).when(studentRepository).findById(1L);
doReturn(Arrays.asList(resultJUMP, resultRUN_100, resultRUN_800, resultTHROW_200)).when(sportResultRepository).findByStudent(student);

doReturn(4.00620).when(calculationInformationService).getAValue(false, DisciplineType.RUN_100);
Expand All @@ -150,9 +143,9 @@ public void test_calculation_general() {
doReturn(0.00208).when(calculationInformationService).getCValue(false, DisciplineType.LONG_JUMP);
doReturn(0.01039).when(calculationInformationService).getCValue(false, DisciplineType.BALL_THROWING_200);

Integer integer = studentScoreController.calculateScore(1L);
Integer integer = studentScoreController.calculateScore(student);

assertThat(integer).isEqualTo(1802);
}
}

}

0 comments on commit 3a60d49

Please sign in to comment.