Skip to content

Commit

Permalink
Adding tests. Create request validator. Controller return 400 code if…
Browse files Browse the repository at this point in the history
… validation request is failed.
  • Loading branch information
KepLer100500 committed Sep 21, 2024
1 parent 401cfb4 commit b734632
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.kepler.model.VacationRequest;
import com.kepler.model.VacationResponse;
import com.kepler.service.VacationCalculatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -13,15 +15,16 @@

@RestController
@RequestMapping("/api/v1")
@Slf4j
public class VacationCalculatorController {
@Autowired
private VacationCalculatorService vacationCalculatorService;

@GetMapping("/calculate")
public ResponseEntity<VacationResponse> calculateVacationPay(@RequestParam double averageSalary,
@RequestParam int vacationDays,
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
public ResponseEntity<?> calculateVacationPay(@RequestParam double averageSalary,
@RequestParam int vacationDays,
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {

VacationRequest request = VacationRequest
.builder()
Expand All @@ -31,13 +34,21 @@ public ResponseEntity<VacationResponse> calculateVacationPay(@RequestParam doubl
.endDate(endDate)
.build();

Double totalPay = vacationCalculatorService.calculateVacationPay(request);

VacationResponse response = VacationResponse
.builder()
.totalPay(
vacationCalculatorService.calculateVacationPay(request)
)
.totalPay(totalPay)
.build();

return ResponseEntity.ok(response);
HttpStatus httpStatus;
if (totalPay != null) { // service returns null if wrong request
httpStatus = HttpStatus.OK;
} else {
httpStatus = HttpStatus.BAD_REQUEST;
}

return new ResponseEntity<>(response, httpStatus);

}
}
2 changes: 1 addition & 1 deletion src/main/java/com/kepler/model/VacationResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
@Data
@Builder
public class VacationResponse {
private double totalPay;
private Double totalPay;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ public class VacationCalculatorService {
@Autowired
private DayOffProxy dayOffProxy;

@Autowired
private ValidatorService validatorService;

/**
* @param request salary, days, start / end vacation
* @return cash for vacation
*/
public double calculateVacationPay(VacationRequest request) {
public Double calculateVacationPay(VacationRequest request) {
if(!validatorService.isRequestCorrect(request)) { // if validation input parameters is failed - don't need to calculate
return null;
}

double dailySalary = request.getAverageSalary() / 29.3; // average salary
int totalVacationDays = request.getVacationDays();

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/kepler/service/ValidatorService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kepler.service;

import com.kepler.model.VacationRequest;
import org.springframework.stereotype.Service;

@Service
public class ValidatorService {
public boolean isRequestCorrect(VacationRequest request) {
if (request.getAverageSalary() < 0) {
return false;
}

if (request.getAverageSalary() == 0) {
return false;
}

if (request.getVacationDays() < 0) {
return false;
}

if (request.getVacationDays() == 0) {
return false;
}

if (request.getStartDate() != null
&& request.getEndDate() != null
&& request.getStartDate().isAfter(request.getEndDate())) {
return false;
}

if(request.getStartDate() != null && request.getEndDate() == null) {
return false;
}

if(request.getEndDate() != null && request.getStartDate() == null) {
return false;
}

return true;
}
}
110 changes: 101 additions & 9 deletions src/test/java/com/kepler/AppTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,43 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class AppTests {
@Autowired
private VacationCalculatorService vacationCalculatorService;
@Autowired
private RestTemplate restTemplate;
@Value("${point.url}")
private String dayOffUrl;

@Test
@Order(10)
@DisplayName("availability external api")
public void testIsDayOffApi() {
String url = "https://isdayoff.ru/api/getdata?year=2024&month=3&day=8";
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
assertEquals(response.getBody(), "1");
}

/**
* case: user define only salary and count days
*/
@Test
@DisplayName("partial test case")
@Order(20)
@DisplayName("partial request")
public void testCalculateVacationPayWithoutDates() {
VacationRequest request = VacationRequest
.builder()
Expand All @@ -34,11 +53,9 @@ public void testCalculateVacationPayWithoutDates() {
assertEquals(14334.47, vacationPay, 0.1);
}

/**
* case: user define salary, count days, start vacation, end vacation
*/
@Test
@DisplayName("full test case")
@Order(30)
@DisplayName("full request")
public void testCalculateVacationPayWithHolidays() {
VacationRequest request = VacationRequest
.builder()
Expand All @@ -51,4 +68,79 @@ public void testCalculateVacationPayWithHolidays() {
assertEquals(8191.12, vacationPay, 0.1);
}

@Test
@Order(40)
@DisplayName("negative salary")
void testNegativeAverageSalary() {
VacationRequest request = VacationRequest
.builder()
.averageSalary(-100000)
.vacationDays(20)
.startDate(LocalDate.now())
.endDate(LocalDate.now().plusDays(10))
.build();

assertNull(vacationCalculatorService.calculateVacationPay(request));
}

@Test
@Order(50)
@DisplayName("zero salary")
void testZeroAverageSalary() {
VacationRequest request = VacationRequest
.builder()
.averageSalary(0)
.vacationDays(20)
.startDate(LocalDate.now())
.endDate(LocalDate.now().plusDays(10))
.build();

assertNull(vacationCalculatorService.calculateVacationPay(request));
}

@Test
@Order(60)
@DisplayName("negative days")
void testNegativeDays() {
VacationRequest request = VacationRequest
.builder()
.averageSalary(100000)
.vacationDays(-20)
.startDate(LocalDate.now())
.endDate(LocalDate.now().plusDays(10))
.build();

assertNull(vacationCalculatorService.calculateVacationPay(request));
}

@Test
@Order(70)
@DisplayName("zero days")
void testZeroDays() {
VacationRequest request = VacationRequest
.builder()
.averageSalary(100000)
.vacationDays(0)
.startDate(LocalDate.now())
.endDate(LocalDate.now().plusDays(10))
.build();

assertNull(vacationCalculatorService.calculateVacationPay(request));
}

@Test
@Order(80)
@DisplayName("start day is after end day")
void testStartDateAfterEndDate() {
VacationRequest request = VacationRequest
.builder()
.averageSalary(100000)
.vacationDays(0)
.startDate(LocalDate.now().plusDays(10))
.endDate(LocalDate.now())
.build();

assertNull(vacationCalculatorService.calculateVacationPay(request));
}

}

0 comments on commit b734632

Please sign in to comment.