<file_sep>package com.gearcalc.entities;


import lombok.ToString;

import javax.persistence.*;

@Entity
@ToString
public class Wheel {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "width")
    private double width;

    @Column(name = "profile")
    private double profile;

    @Column(name = "diameter")
    private int diameterInInches;

    public Wheel() {

    }

    public Wheel(int width, int profile, int diameterInInches) {
        this.width = width;
        this.profile = profile;
        this.diameterInInches = diameterInInches;
    }

    public double getWheelCircumference() {
        return 2 * Math.PI * (this.getTotalWheelDiameter() / 2);
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public double getProfile() {
        return profile;
    }

    public void setProfile(int profile) {
        this.profile = profile;
    }

    public int getDiameterInInches() {
        return diameterInInches;
    }

    public void setDiameterInInches(int diameterInInches) {
        this.diameterInInches = diameterInInches;
    }


    private double getTotalWheelDiameter() {
        double totalProfile = this.getWidth() * (this.getProfile() / 100);
        double diamaterInMm = this.getDiameterInInches() * 2.56;
        return (totalProfile * 2) + (diamaterInMm * 10);
    }
}
<file_sep>package com.gearcalc.entities;


import javax.persistence.*;


@Entity
@Table(name = "gearbox")
public class GearBox {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private double first;
    private double second;
    private double third;
    private double fourth;
    private double fifth;
    private double sixth;

    public GearBox() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getFirst() {
        return first;
    }

    public void setFirst(double first) {
        this.first = first;
    }

    public double getSecond() {
        return second;
    }

    public void setSecond(double second) {
        this.second = second;
    }

    public double getThird() {
        return third;
    }

    public void setThird(double third) {
        this.third = third;
    }

    public double getFourth() {
        return fourth;
    }

    public void setFourth(double fourth) {
        this.fourth = fourth;
    }

    public double getFifth() {
        return fifth;
    }

    public void setFifth(double fifth) {
        this.fifth = fifth;
    }

    public double getSixth() {
        return sixth;
    }

    public void setSixth(double sixth) {
        this.sixth = sixth;
    }

}
<file_sep>package com.gearcalc.service;

import com.gearcalc.GearRatioCalculatorApplication;
import com.gearcalc.common.CarBuilderFactory;
import com.gearcalc.entities.Car;
import com.gearcalc.dto.SpeedResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = GearRatioCalculatorApplication.class)
public class CarServiceTest {

    @Autowired
    private CarService carService;
    private Car car;

    @Before
    public void init() {
        car = CarBuilderFactory.buildE46328ci();
    }

    @Test
    public void testVmaxPerGearValuesShouldBeCorrect() {
        SpeedResponse speedResponse = carService.getVmaxPerGear(car);
        assertEquals(63.5, speedResponse.getSpeedPerGearMap().get("First"), 0.5);
        assertEquals(107.3, speedResponse.getSpeedPerGearMap().get("Second"), 0.5);
        assertEquals(161.0, speedResponse.getSpeedPerGearMap().get("Third"), 0.5);
        assertEquals(215.5, speedResponse.getSpeedPerGearMap().get("Fourth"), 0.5);
        assertEquals(267.3, speedResponse.getSpeedPerGearMap().get("Fifth"), 0.5);
        assertNotNull(speedResponse.getSpeedPerGearMap().get("Sixth"));
    }

}<file_sep>package com.gearcalc.repositories;

import com.gearcalc.entities.GearBox;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GearboxRepository extends JpaRepository<GearBox, Integer> {
}
<file_sep>package com.gearcalc.service;

import com.gearcalc.entities.Wheel;
import com.gearcalc.repositories.WheelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WheelService {

    private WheelRepository wheelRepository;

    @Autowired
    public WheelService(WheelRepository wheelRepository) {
        this.wheelRepository = wheelRepository;
    }

    public List<Wheel> getAllWheels() {
        return wheelRepository.findAll();
    }

    public Wheel getWheelById(int wheelId) {
        return wheelRepository.getOne(wheelId);
    }

}
<file_sep>package com.gearcalc.domain;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class VmaxOnGearParamsTest {

    private VmaxOnGearParams params;

    @Before
    public void init() {
        params = new VmaxOnGearParams(6500, 1.0, 2.93, 2001.2);
    }

    @Test
    public void getVmaxOnGear() {
        assertEquals(266.3, params.getVmaxOnGear(), 0.5);
    }
}<file_sep>package com.gearcalc.common;

import com.gearcalc.entities.Car;
import com.gearcalc.entities.GearBox;
import com.gearcalc.entities.Wheel;

public class CarBuilderFactory {

    private CarBuilderFactory() {}

    public static Car buildE46328ci() {
        Car e46 = new Car();
        e46.setCarType("COUPE");
        e46.setFinalDriveRatio(2.93);
        e46.setId(1);
        e46.setModel("328ci");
        e46.setPlatform("E46");
        e46.setRedLine(6500);
        e46.setTransmissionType("MANUAL");
        e46.setWheel(buildWheel());
        e46.setGearBox(buildGearbox());

        return e46;
    }

    public static Wheel buildWheel() {
        return new Wheel(255, 35, 18);
    }

    public static GearBox buildGearbox() {
        GearBox gearBox = new GearBox();
        gearBox.setId(1);
        gearBox.setFirst(4.21);
        gearBox.setSecond(2.49);
        gearBox.setThird(1.66);
        gearBox.setFourth(1.24);
        gearBox.setFifth(1.00);
        gearBox.setSixth(0);

        return gearBox;
    }
}
<file_sep>package com.gearcalc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@SpringBootApplication
@ComponentScan(basePackages = "com.gearcalc")
@EnableJpaRepositories(basePackages = "com.gearcalc.repositories")
@EntityScan(basePackages = "com.gearcalc.entities")
public class GearRatioCalculatorApplication {

    public static void main(String[] args) {
        SpringApplication.run(GearRatioCalculatorApplication.class, args);
    }


}
<file_sep>package com.gearcalc.repositories;

import com.gearcalc.entities.Wheel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface WheelRepository extends JpaRepository<Wheel, Integer> {
}
<file_sep>package com.gearcalc.domain;

public class VmaxOnGearParams {

    private int redLine;
    private double gearRatio;
    private double finalRatio;
    private double wheelCircumference;

    public VmaxOnGearParams(int redLine, double gearRatio, double finalRatio, double wheelCircumference) {
        this.redLine = redLine;
        this.gearRatio = gearRatio;
        this.finalRatio = finalRatio;
        this.wheelCircumference = wheelCircumference;
    }

    public double getVmaxOnGear() {
        return (this.redLine / (this.gearRatio * this.finalRatio) * (this.wheelCircumference / 1000) * 60) / 1000;
    }

}
<file_sep>package com.gearcalc.rest;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CommonRestControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Value("classpath:samples/sampleCircumference.json")
    private Resource wheelResource;

    @Value("classpath:samples/sampleCalculateSpeed.json")
    private Resource carResource;

    private String wheelJson;
    private String carJson;

    @Before
    public void init() throws IOException {
        File wheelResourceFile = wheelResource.getFile();
        File carResourceFile = carResource.getFile();

        wheelJson = new String(Files.readAllBytes(wheelResourceFile.toPath()));
        carJson = new String(Files.readAllBytes(carResourceFile.toPath()));
    }

    @Test
    public void getWheelCircumference() throws Exception {
        RequestBuilder requestBuilder = MockMvcRequestBuilders
                .post("/api/wheel/circumference")
                .accept(MediaType.APPLICATION_JSON).content(wheelJson)
                .contentType(MediaType.APPLICATION_JSON);

        MvcResult result = mockMvc.perform(requestBuilder).andReturn();
        MockHttpServletResponse response = result.getResponse();

        assertEquals(HttpStatus.OK.value(), response.getStatus());
    }

    @Test
    public void getVmaxPerGear() throws Exception {
        RequestBuilder requestBuilder = MockMvcRequestBuilders
                .post("/api/calculateSpeed")
                .accept(MediaType.APPLICATION_JSON).content(carJson)
                .contentType(MediaType.APPLICATION_JSON);

        MvcResult result = mockMvc.perform(requestBuilder).andReturn();
        MockHttpServletResponse response = result.getResponse();

        assertEquals(HttpStatus.OK.value(), response.getStatus());
    }
}