From a4c0f24e2e264ba2d52973b4affca83c7f978f7c Mon Sep 17 00:00:00 2001 From: Rizwan Date: Sun, 20 Mar 2022 23:49:32 +0530 Subject: [PATCH 01/23] first commit --- toll_free_calc_app/pom.xml | 55 ++++++++++++ .../TollFeeCalculatorApplicaion.java | 15 ++++ .../configuration/SwaggerConfiguration.java | 34 +++++++ .../calculator/constant/TollFreeDates.java | 44 ++++++++++ .../calculator/constant/TollFreeVehicles.java | 20 +++++ .../TollFeeCalculatorController.java | 75 ++++++++++++++++ .../CustomErrorHandlingControllerAdvice.java | 62 +++++++++++++ .../controller/model/TollFeeDTO.java | 10 +++ .../controller/model/TollFreeDateDTO.java | 22 +++++ .../controller/model/TollFreeVehicleDTO.java | 22 +++++ .../exception/CustomErrorException.java | 30 +++++++ .../calculator/exception/ErrorResponse.java | 68 ++++++++++++++ .../service/TollFeeCalculatorService.java | 12 +++ .../impl/TollFeeCalculatorServiceImpl.java | 88 +++++++++++++++++++ .../src/main/resources/application.properties | 0 .../TollFeeCalculatorServiceImplTest.java | 79 +++++++++++++++++ 16 files changed, 636 insertions(+) create mode 100644 toll_free_calc_app/pom.xml create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplicaion.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java create mode 100644 toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java create mode 100644 toll_free_calc_app/src/main/resources/application.properties create mode 100644 toll_free_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java diff --git a/toll_free_calc_app/pom.xml b/toll_free_calc_app/pom.xml new file mode 100644 index 00000000..5e918449 --- /dev/null +++ b/toll_free_calc_app/pom.xml @@ -0,0 +1,55 @@ + + 4.0.0 + com.evolve-technology.161602 + toll_free_calc_app + 0.0.1-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-test + + + io.springfox + springfox-swagger2 + 2.6.1 + + + io.springfox + springfox-swagger-ui + 2.6.1 + + + org.springframework.boot + spring-boot-devtools + runtime + + + + org.projectlombok + lombok + true + + + + diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplicaion.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplicaion.java new file mode 100644 index 00000000..6bc2d023 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplicaion.java @@ -0,0 +1,15 @@ +package com.evolve_technology.calculator; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class TollFeeCalculatorApplicaion { + + public static void main(String[] args) { + // TODO Auto-generated method stub + SpringApplication.run(TollFeeCalculatorApplicaion.class, args); + + } + +} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java new file mode 100644 index 00000000..fef0447a --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java @@ -0,0 +1,34 @@ +package com.evolve_technology.calculator.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class SwaggerConfiguration extends WebMvcConfigurerAdapter { + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("swagger-ui.html") + .addResourceLocations("classpath:/META-INF/resources/"); + + registry.addResourceHandler("/webjars/**") + .addResourceLocations("classpath:/META-INF/resources/webjars/"); + } +} \ No newline at end of file diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java new file mode 100644 index 00000000..bef73738 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java @@ -0,0 +1,44 @@ +package com.evolve_technology.calculator.constant; + +import java.time.LocalDate; +import java.time.YearMonth; + +public enum TollFreeDates { + JANUARY_1_2013(LocalDate.of(2013,01,01).toString()), + MARCH_28_2013(LocalDate.of(2013,03,28).toString()), + MARCH_29_2013(LocalDate.of(2013,03,29).toString()), + APRIL_1_2013(LocalDate.of(2013,04,01).toString()), + APRIL_30_2013(LocalDate.of(2013,04,30).toString()), + MAY_1_2013(LocalDate.of(2013,05,01).toString()), + MAY_8_2013(LocalDate.of(2013,05,8).toString()), + MAY_9_2013(LocalDate.of(2013,05,9).toString()), + JUNE_5_2013(LocalDate.of(2013,06,5).toString()), + JUNE_6_2013(LocalDate.of(2013,06,6).toString()), + JUNE_21_2013(LocalDate.of(2013,06,21).toString()), + JULY(YearMonth.of(2013,07).toString()), + NOVEMBER_1_2013(LocalDate.of(2013,11,1).toString()), + DECEMBER_24_2013(LocalDate.of(2013,12,24).toString()), + DECEMBER_25_2013(LocalDate.of(2013,12,25).toString()), + DECEMBER_26_2013(LocalDate.of(2013,12,26).toString()), + DECEMBER_31_2013(LocalDate.of(2013,12,31).toString()); + + private String date; + + private TollFreeDates(String date) { + this.date=date; + } + + public String getDate() { + return date; + } +} +// if (month == Calendar.JANUARY && day == 1 || +// month == Calendar.MARCH && (day == 28 || day == 29) || +// month == Calendar.APRIL && (day == 1 || day == 30) || +// month == Calendar.MAY && (day == 1 || day == 8 || day == 9) || +// month == Calendar.JUNE && (day == 5 || day == 6 || day == 21) || +// month == Calendar.JULY || +// month == Calendar.NOVEMBER && day == 1 || +// month == Calendar.DECEMBER && (day == 24 || day == 25 || day == 26 || day == 31)) { +// return true; +//} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java new file mode 100644 index 00000000..fb80a18d --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java @@ -0,0 +1,20 @@ +package com.evolve_technology.calculator.constant; + +public enum TollFreeVehicles { + + MOTORBIKE("Motorbike"), + TRACTOR("Tractor"), + EMERGENCY("Emergency"), + DIPLOMAT("Diplomat"), + FOREIGN("Foreign"), + MILITARY("Military"); + private final String type; + + TollFreeVehicles(String type) { + this.type = type; + } + + public String getType() { + return type; + } +} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java new file mode 100644 index 00000000..4f2bdc79 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java @@ -0,0 +1,75 @@ +package com.evolve_technology.calculator.controller; + +import java.util.List; + +import javax.validation.constraints.Pattern; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.evolve_technology.calculator.controller.model.TollFeeDTO; +import com.evolve_technology.calculator.controller.model.TollFreeDateDTO; +import com.evolve_technology.calculator.controller.model.TollFreeVehicleDTO; +import com.evolve_technology.calculator.exception.CustomErrorException; +import com.evolve_technology.calculator.service.TollFeeCalculatorService; + +@RestController("/v1/api") +@Validated +public class TollFeeCalculatorController { + + @Autowired + TollFeeCalculatorService tollFreeCalculatorService; + + @GetMapping("tollfee/vehicles/{name}/dates/{date}") + public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") + @Pattern(regexp = "^((20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") + String date) { + int amount= tollFreeCalculatorService.getTollFee(date, name); + TollFeeDTO tollFeeDTO=new TollFeeDTO(); + tollFeeDTO.setAmount(amount); + tollFeeDTO.setDate(date); + tollFeeDTO.setName(name); + return new ResponseEntity<>(tollFeeDTO,HttpStatus.OK); + } + + @GetMapping("tollfree/vehicles") + public ResponseEntity> getTollFreeVehicles(){ + List list=tollFreeCalculatorService.getTollFreeVehicles(); + return new ResponseEntity<>(list,HttpStatus.OK); + + } + + @GetMapping("tollfree/vehicles/{name}") + public ResponseEntity getTollFreeVehicleStatusByName(@PathVariable("name") String name){ + Boolean value=tollFreeCalculatorService.isTollFreeVehicle(name); + TollFreeVehicleDTO tollFreeVehicleDTO=new TollFreeVehicleDTO(); + tollFreeVehicleDTO.setName(name); + tollFreeVehicleDTO.setTollFreeVehicle(value); + return new ResponseEntity<>(tollFreeVehicleDTO,HttpStatus.OK); + + } + + @GetMapping("tollfree/dates") + public ResponseEntity> getTollFreeDates(){ + List list=tollFreeCalculatorService.getTollFreeDates(); + return new ResponseEntity<>(list,HttpStatus.OK); + } + + @GetMapping("tollfree/dates/{date}") + public ResponseEntity getTollFreeDateStatus(@PathVariable("date") + @Pattern(regexp = "^((20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$") String date){ + Boolean value=tollFreeCalculatorService.isTollFreeDate(date); + TollFreeDateDTO tollFreeDateDTO=new TollFreeDateDTO(); + tollFreeDateDTO.setDate(date); + tollFreeDateDTO.setIsTollFreeDate(value); + return new ResponseEntity<>(tollFreeDateDTO,HttpStatus.OK); + + } + + +} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java new file mode 100644 index 00000000..80d19a9a --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java @@ -0,0 +1,62 @@ +package com.evolve_technology.calculator.controller.advice; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.validation.ConstraintViolationException; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import com.evolve_technology.calculator.exception.CustomErrorException; +import com.evolve_technology.calculator.exception.ErrorResponse; + +@RestControllerAdvice +public class CustomErrorHandlingControllerAdvice { + + @ExceptionHandler(CustomErrorException.class) + public ResponseEntity handleCustomErrorExceptions( + Exception e + ) { + CustomErrorException customErrorException = (CustomErrorException) e; + + HttpStatus status = customErrorException.getStatus(); + + // converting the stack trace to String + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + customErrorException.printStackTrace(printWriter); + String stackTrace = stringWriter.toString(); + + return new ResponseEntity<>( + new ErrorResponse( + status, + customErrorException.getMessage(), + stackTrace, + customErrorException.getData() + ), + status + ); + } + + @ExceptionHandler(ConstraintViolationException.class) + public ResponseEntity handleCustomParameterConstraintExceptions( + Exception e + ) { + HttpStatus status = HttpStatus.BAD_REQUEST; // 400 + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + e.printStackTrace(printWriter); + String stackTrace = stringWriter.toString(); + return new ResponseEntity<>( + new ErrorResponse( + status, + e.getMessage(), + stackTrace + ), + status + ); + } +} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java new file mode 100644 index 00000000..f38a7ac9 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java @@ -0,0 +1,10 @@ +package com.evolve_technology.calculator.controller.model; + +import lombok.Data; + +@Data +public class TollFeeDTO { + String name; + String date; + int amount; +} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java new file mode 100644 index 00000000..515e879c --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java @@ -0,0 +1,22 @@ +package com.evolve_technology.calculator.controller.model; + +import lombok.Data; + +@Data +public class TollFreeDateDTO { + String date; + Boolean isTollFreeDate; + public String getDate() { + return date; + } + public void setDate(String date) { + this.date = date; + } + public Boolean getIsTollFreeDate() { + return isTollFreeDate; + } + public void setIsTollFreeDate(Boolean isTollFreeDate) { + this.isTollFreeDate = isTollFreeDate; + } + +} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java new file mode 100644 index 00000000..70a4a978 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java @@ -0,0 +1,22 @@ +package com.evolve_technology.calculator.controller.model; + +import lombok.Data; + +@Data +public class TollFreeVehicleDTO { + String name; + boolean isTollFreeVehicle; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public boolean isTollFreeVehicle() { + return isTollFreeVehicle; + } + public void setTollFreeVehicle(boolean isTollFreeVehicle) { + this.isTollFreeVehicle = isTollFreeVehicle; + } + +} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java new file mode 100644 index 00000000..8dfd1e09 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java @@ -0,0 +1,30 @@ +package com.evolve_technology.calculator.exception; + +import org.springframework.http.HttpStatus; + +import lombok.Data; + +@Data +public class CustomErrorException extends RuntimeException { + private HttpStatus status = null; + + private Object data = null; + + public CustomErrorException() { + super(); + } + + public CustomErrorException(String message) { + super(message); + } + + public CustomErrorException(HttpStatus status, String message) { + this(message); + this.status = status; + } + + public CustomErrorException(HttpStatus status, String message, Object data) { + this(status, message); + this.data = data; + } +} \ No newline at end of file diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java new file mode 100644 index 00000000..5d0ce326 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java @@ -0,0 +1,68 @@ +package com.evolve_technology.calculator.exception; + +import java.util.Date; + +import org.springframework.http.HttpStatus; + +import com.fasterxml.jackson.annotation.JsonFormat; + +import lombok.Data; + +@Data +public class ErrorResponse { + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") + private Date timestamp; + + private int code; + + private String status; + + private String message; + + private String stackTrace; + + private Object data; + + public ErrorResponse() { + timestamp = new Date(); + } + + public ErrorResponse( + HttpStatus httpStatus, + String message + ) { + this(); + + this.code = httpStatus.value(); + this.status = httpStatus.name(); + this.message = message; + } + + public ErrorResponse( + HttpStatus httpStatus, + String message, + String stackTrace + ) { + this( + httpStatus, + message + ); + + this.stackTrace = stackTrace; + } + + public ErrorResponse( + HttpStatus httpStatus, + String message, + String stackTrace, + Object data + ) { + this( + httpStatus, + message, + stackTrace + ); + + this.data = data; + } +} \ No newline at end of file diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java new file mode 100644 index 00000000..da999765 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java @@ -0,0 +1,12 @@ +package com.evolve_technology.calculator.service; + +import java.util.List; + +public interface TollFeeCalculatorService { + + public List getTollFreeVehicles(); + public boolean isTollFreeVehicle(String vehicle); + public int getTollFee(final String date, String vehicle); + public Boolean isTollFreeDate(String date); + public List getTollFreeDates(); +} diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java new file mode 100644 index 00000000..ce634c89 --- /dev/null +++ b/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java @@ -0,0 +1,88 @@ +package com.evolve_technology.calculator.service.impl; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Month; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; + +import com.evolve_technology.calculator.constant.TollFreeDates; +import com.evolve_technology.calculator.constant.TollFreeVehicles; +import com.evolve_technology.calculator.exception.CustomErrorException; +import com.evolve_technology.calculator.service.TollFeeCalculatorService; + +@Service +public class TollFeeCalculatorServiceImpl implements TollFeeCalculatorService { + + public List getTollFreeVehicles() { + List list=Stream.of(TollFreeVehicles.values()).map(TollFreeVehicles::getType).collect(Collectors.toList()); + return list; + } + + public boolean isTollFreeVehicle(String vehicle) { + for (TollFreeVehicles tollFreeVehicle : TollFreeVehicles.values()) + { + if (tollFreeVehicle.name().equalsIgnoreCase(vehicle)) + { + return true; + } + } + return false; + } + + @Override + public int getTollFee(String date, String vehicle) { + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + LocalDateTime inputDate=LocalDateTime.parse(date,formatter); + int hour=inputDate.getHour(); + int minute=inputDate.getMinute(); + LocalDate localDate=LocalDate.of(inputDate.getYear(), inputDate.getMonthValue(), inputDate.getDayOfMonth()); + + if(isTollFreeDate(localDate.toString())||isTollFreeVehicle(vehicle)) + return 0; + if (hour == 6 && minute >= 0 && minute <= 29) return 8; + else if (hour == 6 && minute >= 30 && minute <= 59) return 13; + else if (hour == 7 && minute >= 0 && minute <= 59) return 18; + else if (hour == 8 && minute >= 0 && minute <= 29) return 13; + else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8; + else if (hour == 15 && minute >= 0 && minute <= 29) return 13; + else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return 18; + else if (hour == 17 && minute >= 0 && minute <= 59) return 13; + else if (hour == 18 && minute >= 0 && minute <= 29) return 8; + else return 0; + } + + @Override + public Boolean isTollFreeDate(String date) { + try { + LocalDate input=LocalDate.parse(date); + if(input.getMonth()==Month.JULY || input.getDayOfWeek()==DayOfWeek.SATURDAY || input.getDayOfWeek()==DayOfWeek.SUNDAY) + return true; + for (TollFreeDates tollFreeDate : TollFreeDates.values()) { + if(!tollFreeDate.getDate().equals("2013-07")) { + LocalDate dateEnum=LocalDate.parse(tollFreeDate.getDate()); + if (dateEnum.equals(input)) { + return true; + } + } + + } + }catch(Exception e) { + throw new CustomErrorException(HttpStatus.BAD_REQUEST, e.getMessage(), date); + } + return false; + } + + @Override + public List getTollFreeDates() { + List list=Stream.of(TollFreeDates.values()).map(TollFreeDates::getDate).collect(Collectors.toList()); + return list; + } + +} diff --git a/toll_free_calc_app/src/main/resources/application.properties b/toll_free_calc_app/src/main/resources/application.properties new file mode 100644 index 00000000..e69de29b diff --git a/toll_free_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java b/toll_free_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java new file mode 100644 index 00000000..03e700da --- /dev/null +++ b/toll_free_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java @@ -0,0 +1,79 @@ +package com.evolve_technology.calculator.service.impl; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.time.DateTimeException; +import java.time.LocalDate; + +import org.junit.Before; +import org.junit.Test; + +import com.evolve_technology.calculator.exception.CustomErrorException; + + +public class TollFeeCalculatorServiceImplTest { + + TollFeeCalculatorServiceImpl tollFeeCalculatorService; + + @Before + public void init() { + tollFeeCalculatorService=new TollFeeCalculatorServiceImpl(); + } + + @Test + public void getTollFreeVehiclesTest(){ + assertNotNull(tollFeeCalculatorService.getTollFreeVehicles()); + assertDoesNotThrow(()->tollFeeCalculatorService.getTollFreeVehicles()); + } + + @Test + public void getTollFreeDatesTest() { + assertNotNull(tollFeeCalculatorService.getTollFreeDates()); + assertDoesNotThrow(()->tollFeeCalculatorService.getTollFreeDates()); + } + + @Test + public void isTollFreeDate_Positive_Scenarios_Test() { + // JULY MONTH + String july1=LocalDate.of(2013, 07, 01).toString(); + assertTrue(tollFeeCalculatorService.isTollFreeDate(july1)); + + String july21=LocalDate.of(2013, 07, 21).toString(); + assertTrue(tollFeeCalculatorService.isTollFreeDate(july21)); + + // Saturday verifying + String june_saturday=LocalDate.of(2013, 06, 8).toString(); + assertTrue(tollFeeCalculatorService.isTollFreeDate(june_saturday)); + + // sunday Verifying + String june_sunday=LocalDate.of(2013, 06, 9).toString(); + assertTrue(tollFeeCalculatorService.isTollFreeDate(june_sunday)); + + // Enum specific holidays Verifying - 2013-01-01 + String jan1=LocalDate.of(2013, 01, 1).toString(); + assertTrue(tollFeeCalculatorService.isTollFreeDate(jan1)); + + // Enum specific holidays Verifying - 2013-03-28 + String mar28=LocalDate.of(2013, 03, 28).toString(); + assertTrue(tollFeeCalculatorService.isTollFreeDate(mar28)); + } + + @Test + public void isTollFreeDate_Negative_Scenarios_Test() { + // Non Enum specific holidays Verifying - 2013-01-02 Wednesday + String jan1=LocalDate.of(2013, 01, 2).toString(); + assertFalse(tollFeeCalculatorService.isTollFreeDate(jan1)); + + // Non Enum specific holidays Verifying - 2013-01-03 Thursday + String jan3=LocalDate.of(2013, 01, 3).toString(); + assertFalse(tollFeeCalculatorService.isTollFreeDate(jan3)); + + // Passing Invalid date 2013-02-30 + String feb30=" 2013-02-30"; + assertThrows(CustomErrorException.class,()->tollFeeCalculatorService.isTollFreeDate(feb30)); + } +} From 9da55a247a3bd441bd7275dc1d8457b7501e0a11 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Mon, 21 Mar 2022 00:22:41 +0530 Subject: [PATCH 02/23] Code Refactored | Regex modified. --- {toll_free_calc_app => toll_fee_calc_app}/pom.xml | 0 .../calculator/TollFeeCalculatorApplication.java | 4 ++-- .../calculator/configuration/SwaggerConfiguration.java | 0 .../evolve_technology/calculator/constant/TollFreeDates.java | 0 .../calculator/constant/TollFreeVehicles.java | 0 .../calculator/controller/TollFeeCalculatorController.java | 4 ++-- .../advice/CustomErrorHandlingControllerAdvice.java | 0 .../calculator/controller/model/TollFeeDTO.java | 0 .../calculator/controller/model/TollFreeDateDTO.java | 0 .../calculator/controller/model/TollFreeVehicleDTO.java | 0 .../calculator/exception/CustomErrorException.java | 0 .../evolve_technology/calculator/exception/ErrorResponse.java | 0 .../calculator/service/TollFeeCalculatorService.java | 0 .../calculator/service/impl/TollFeeCalculatorServiceImpl.java | 0 .../src/main/resources/application.properties | 0 .../service/impl/TollFeeCalculatorServiceImplTest.java | 0 16 files changed, 4 insertions(+), 4 deletions(-) rename {toll_free_calc_app => toll_fee_calc_app}/pom.xml (100%) rename toll_free_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplicaion.java => toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java (71%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java (91%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/main/resources/application.properties (100%) rename {toll_free_calc_app => toll_fee_calc_app}/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java (100%) diff --git a/toll_free_calc_app/pom.xml b/toll_fee_calc_app/pom.xml similarity index 100% rename from toll_free_calc_app/pom.xml rename to toll_fee_calc_app/pom.xml diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplicaion.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java similarity index 71% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplicaion.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java index 6bc2d023..689839e3 100644 --- a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplicaion.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java @@ -4,11 +4,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class TollFeeCalculatorApplicaion { +public class TollFeeCalculatorApplication { public static void main(String[] args) { // TODO Auto-generated method stub - SpringApplication.run(TollFeeCalculatorApplicaion.class, args); + SpringApplication.run(TollFeeCalculatorApplication.class, args); } diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java similarity index 91% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java index 4f2bdc79..9c701668 100644 --- a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java @@ -27,7 +27,7 @@ public class TollFeeCalculatorController { @GetMapping("tollfee/vehicles/{name}/dates/{date}") public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") - @Pattern(regexp = "^((20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") + @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") String date) { int amount= tollFreeCalculatorService.getTollFee(date, name); TollFeeDTO tollFeeDTO=new TollFeeDTO(); @@ -62,7 +62,7 @@ public ResponseEntity> getTollFreeDates(){ @GetMapping("tollfree/dates/{date}") public ResponseEntity getTollFreeDateStatus(@PathVariable("date") - @Pattern(regexp = "^((20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$") String date){ + @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$") String date){ Boolean value=tollFreeCalculatorService.isTollFreeDate(date); TollFreeDateDTO tollFreeDateDTO=new TollFreeDateDTO(); tollFreeDateDTO.setDate(date); diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java diff --git a/toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java similarity index 100% rename from toll_free_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java rename to toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java diff --git a/toll_free_calc_app/src/main/resources/application.properties b/toll_fee_calc_app/src/main/resources/application.properties similarity index 100% rename from toll_free_calc_app/src/main/resources/application.properties rename to toll_fee_calc_app/src/main/resources/application.properties diff --git a/toll_free_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java similarity index 100% rename from toll_free_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java rename to toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java From 045d6e53bde357f340fbff3662799e6ba9d74c30 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Mon, 21 Mar 2022 00:42:06 +0530 Subject: [PATCH 03/23] test cases. --- .../impl/TollFeeCalculatorServiceImplTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java index 03e700da..982dbfb7 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java @@ -76,4 +76,16 @@ public void isTollFreeDate_Negative_Scenarios_Test() { String feb30=" 2013-02-30"; assertThrows(CustomErrorException.class,()->tollFeeCalculatorService.isTollFreeDate(feb30)); } + + @Test + public void isTollFreeVehicleTest() { + String vehicle=""; + assertFalse(tollFeeCalculatorService.isTollFreeVehicle(vehicle)); + + vehicle="tractor"; + assertTrue(tollFeeCalculatorService.isTollFreeVehicle(vehicle)); + + vehicle="Tractor234"; + assertFalse(tollFeeCalculatorService.isTollFreeVehicle(vehicle)); + } } From 6f282a50eed944e0bc6923cc23a4fa3fef621e7b Mon Sep 17 00:00:00 2001 From: Rizwan Date: Mon, 21 Mar 2022 00:49:27 +0530 Subject: [PATCH 04/23] Getter/Setters removed. --- .../controller/model/TollFreeDateDTO.java | 13 +------------ .../controller/model/TollFreeVehicleDTO.java | 13 +------------ 2 files changed, 2 insertions(+), 24 deletions(-) diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java index 515e879c..4bf75a48 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java @@ -6,17 +6,6 @@ public class TollFreeDateDTO { String date; Boolean isTollFreeDate; - public String getDate() { - return date; - } - public void setDate(String date) { - this.date = date; - } - public Boolean getIsTollFreeDate() { - return isTollFreeDate; - } - public void setIsTollFreeDate(Boolean isTollFreeDate) { - this.isTollFreeDate = isTollFreeDate; - } + } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java index 70a4a978..91494954 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java @@ -6,17 +6,6 @@ public class TollFreeVehicleDTO { String name; boolean isTollFreeVehicle; - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public boolean isTollFreeVehicle() { - return isTollFreeVehicle; - } - public void setTollFreeVehicle(boolean isTollFreeVehicle) { - this.isTollFreeVehicle = isTollFreeVehicle; - } + } From 4bde134c8501996ce6a6daa0e92221b57f65cd69 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Wed, 30 Mar 2022 00:49:17 +0530 Subject: [PATCH 05/23] Code refactored Single Responsibility Principle Addressed. --- .../TollFeeCalculatorController.java | 23 +++-- .../service/TollFeeCalculatorService.java | 12 --- .../calculator/service/TollFeeService.java | 7 ++ .../service/TollFreeDatesService.java | 8 ++ .../service/TollFreeVehicleService.java | 8 ++ .../impl/TollFeeCalculatorServiceImpl.java | 88 ------------------- .../service/impl/TollFeeServiceImpl.java | 47 ++++++++++ .../impl/TollFreeDatesServiceImpl.java | 49 +++++++++++ .../impl/TollFreeVehiclesServiceImpl.java | 33 +++++++ .../TollFeeCalculatorServiceImplTest.java | 46 ++++++---- 10 files changed, 194 insertions(+), 127 deletions(-) delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeDatesService.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeVehicleService.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java index 9c701668..0f8a5a14 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java @@ -15,21 +15,28 @@ import com.evolve_technology.calculator.controller.model.TollFeeDTO; import com.evolve_technology.calculator.controller.model.TollFreeDateDTO; import com.evolve_technology.calculator.controller.model.TollFreeVehicleDTO; -import com.evolve_technology.calculator.exception.CustomErrorException; -import com.evolve_technology.calculator.service.TollFeeCalculatorService; +import com.evolve_technology.calculator.service.TollFeeService; +import com.evolve_technology.calculator.service.TollFreeDatesService; +import com.evolve_technology.calculator.service.TollFreeVehicleService; @RestController("/v1/api") @Validated public class TollFeeCalculatorController { @Autowired - TollFeeCalculatorService tollFreeCalculatorService; + TollFeeService tollFreeService; + + @Autowired + TollFreeDatesService tollFreeDatesService; + + @Autowired + TollFreeVehicleService tollFreeVehicleService; @GetMapping("tollfee/vehicles/{name}/dates/{date}") public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") String date) { - int amount= tollFreeCalculatorService.getTollFee(date, name); + int amount= tollFreeService.getTollFee(date, name); TollFeeDTO tollFeeDTO=new TollFeeDTO(); tollFeeDTO.setAmount(amount); tollFeeDTO.setDate(date); @@ -39,14 +46,14 @@ public ResponseEntity getTollFee(@PathVariable("name") String name,@ @GetMapping("tollfree/vehicles") public ResponseEntity> getTollFreeVehicles(){ - List list=tollFreeCalculatorService.getTollFreeVehicles(); + List list=tollFreeVehicleService.getTollFreeVehicles(); return new ResponseEntity<>(list,HttpStatus.OK); } @GetMapping("tollfree/vehicles/{name}") public ResponseEntity getTollFreeVehicleStatusByName(@PathVariable("name") String name){ - Boolean value=tollFreeCalculatorService.isTollFreeVehicle(name); + Boolean value=tollFreeVehicleService.isTollFreeVehicle(name); TollFreeVehicleDTO tollFreeVehicleDTO=new TollFreeVehicleDTO(); tollFreeVehicleDTO.setName(name); tollFreeVehicleDTO.setTollFreeVehicle(value); @@ -56,14 +63,14 @@ public ResponseEntity getTollFreeVehicleStatusByName(@PathVa @GetMapping("tollfree/dates") public ResponseEntity> getTollFreeDates(){ - List list=tollFreeCalculatorService.getTollFreeDates(); + List list=tollFreeDatesService.getTollFreeDates(); return new ResponseEntity<>(list,HttpStatus.OK); } @GetMapping("tollfree/dates/{date}") public ResponseEntity getTollFreeDateStatus(@PathVariable("date") @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$") String date){ - Boolean value=tollFreeCalculatorService.isTollFreeDate(date); + Boolean value=tollFreeDatesService.isTollFreeDate(date); TollFreeDateDTO tollFreeDateDTO=new TollFreeDateDTO(); tollFreeDateDTO.setDate(date); tollFreeDateDTO.setIsTollFreeDate(value); diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java deleted file mode 100644 index da999765..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeCalculatorService.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.evolve_technology.calculator.service; - -import java.util.List; - -public interface TollFeeCalculatorService { - - public List getTollFreeVehicles(); - public boolean isTollFreeVehicle(String vehicle); - public int getTollFee(final String date, String vehicle); - public Boolean isTollFreeDate(String date); - public List getTollFreeDates(); -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java new file mode 100644 index 00000000..8088479e --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java @@ -0,0 +1,7 @@ +package com.evolve_technology.calculator.service; + +public interface TollFeeService { + + public int getTollFee(final String date, String vehicle); + +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeDatesService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeDatesService.java new file mode 100644 index 00000000..6ab032da --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeDatesService.java @@ -0,0 +1,8 @@ +package com.evolve_technology.calculator.service; + +import java.util.List; + +public interface TollFreeDatesService { + public Boolean isTollFreeDate(String date); + public List getTollFreeDates(); +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeVehicleService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeVehicleService.java new file mode 100644 index 00000000..cabd82d8 --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeVehicleService.java @@ -0,0 +1,8 @@ +package com.evolve_technology.calculator.service; + +import java.util.List; + +public interface TollFreeVehicleService { + public List getTollFreeVehicles(); + public boolean isTollFreeVehicle(String vehicle); +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java deleted file mode 100644 index ce634c89..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImpl.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.evolve_technology.calculator.service.impl; - -import java.time.DayOfWeek; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.Month; -import java.time.format.DateTimeFormatter; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; - -import com.evolve_technology.calculator.constant.TollFreeDates; -import com.evolve_technology.calculator.constant.TollFreeVehicles; -import com.evolve_technology.calculator.exception.CustomErrorException; -import com.evolve_technology.calculator.service.TollFeeCalculatorService; - -@Service -public class TollFeeCalculatorServiceImpl implements TollFeeCalculatorService { - - public List getTollFreeVehicles() { - List list=Stream.of(TollFreeVehicles.values()).map(TollFreeVehicles::getType).collect(Collectors.toList()); - return list; - } - - public boolean isTollFreeVehicle(String vehicle) { - for (TollFreeVehicles tollFreeVehicle : TollFreeVehicles.values()) - { - if (tollFreeVehicle.name().equalsIgnoreCase(vehicle)) - { - return true; - } - } - return false; - } - - @Override - public int getTollFee(String date, String vehicle) { - DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; - LocalDateTime inputDate=LocalDateTime.parse(date,formatter); - int hour=inputDate.getHour(); - int minute=inputDate.getMinute(); - LocalDate localDate=LocalDate.of(inputDate.getYear(), inputDate.getMonthValue(), inputDate.getDayOfMonth()); - - if(isTollFreeDate(localDate.toString())||isTollFreeVehicle(vehicle)) - return 0; - if (hour == 6 && minute >= 0 && minute <= 29) return 8; - else if (hour == 6 && minute >= 30 && minute <= 59) return 13; - else if (hour == 7 && minute >= 0 && minute <= 59) return 18; - else if (hour == 8 && minute >= 0 && minute <= 29) return 13; - else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8; - else if (hour == 15 && minute >= 0 && minute <= 29) return 13; - else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return 18; - else if (hour == 17 && minute >= 0 && minute <= 59) return 13; - else if (hour == 18 && minute >= 0 && minute <= 29) return 8; - else return 0; - } - - @Override - public Boolean isTollFreeDate(String date) { - try { - LocalDate input=LocalDate.parse(date); - if(input.getMonth()==Month.JULY || input.getDayOfWeek()==DayOfWeek.SATURDAY || input.getDayOfWeek()==DayOfWeek.SUNDAY) - return true; - for (TollFreeDates tollFreeDate : TollFreeDates.values()) { - if(!tollFreeDate.getDate().equals("2013-07")) { - LocalDate dateEnum=LocalDate.parse(tollFreeDate.getDate()); - if (dateEnum.equals(input)) { - return true; - } - } - - } - }catch(Exception e) { - throw new CustomErrorException(HttpStatus.BAD_REQUEST, e.getMessage(), date); - } - return false; - } - - @Override - public List getTollFreeDates() { - List list=Stream.of(TollFreeDates.values()).map(TollFreeDates::getDate).collect(Collectors.toList()); - return list; - } - -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java new file mode 100644 index 00000000..df380b1b --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -0,0 +1,47 @@ +package com.evolve_technology.calculator.service.impl; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.evolve_technology.calculator.service.TollFeeService; +import com.evolve_technology.calculator.service.TollFreeDatesService; +import com.evolve_technology.calculator.service.TollFreeVehicleService; + +@Service +public class TollFeeServiceImpl implements TollFeeService { + + @Autowired + TollFreeDatesService tollFreeDatesService; + + @Autowired + TollFreeVehicleService tollFreeVehicleService; + + + @Override + public int getTollFee(String date, String vehicle) { + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + LocalDateTime inputDate=LocalDateTime.parse(date,formatter); + int hour=inputDate.getHour(); + int minute=inputDate.getMinute(); + LocalDate localDate=LocalDate.of(inputDate.getYear(), inputDate.getMonthValue(), inputDate.getDayOfMonth()); + + if(tollFreeDatesService.isTollFreeDate(localDate.toString())||tollFreeVehicleService.isTollFreeVehicle(vehicle)) + return 0; + if (hour == 6 && minute >= 0 && minute <= 29) return 8; + else if (hour == 6 && minute >= 30 && minute <= 59) return 13; + else if (hour == 7 && minute >= 0 && minute <= 59) return 18; + else if (hour == 8 && minute >= 0 && minute <= 29) return 13; + else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8; + else if (hour == 15 && minute >= 0 && minute <= 29) return 13; + else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return 18; + else if (hour == 17 && minute >= 0 && minute <= 59) return 13; + else if (hour == 18 && minute >= 0 && minute <= 29) return 8; + else return 0; + } + + +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java new file mode 100644 index 00000000..53947c90 --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java @@ -0,0 +1,49 @@ +package com.evolve_technology.calculator.service.impl; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.Month; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; + +import com.evolve_technology.calculator.constant.TollFreeDates; +import com.evolve_technology.calculator.exception.CustomErrorException; +import com.evolve_technology.calculator.service.TollFreeDatesService; + + +@Service +public class TollFreeDatesServiceImpl implements TollFreeDatesService { + + @Override + public Boolean isTollFreeDate(String date) { + try { + LocalDate input=LocalDate.parse(date); + if(input.getMonth()==Month.JULY || input.getDayOfWeek()==DayOfWeek.SATURDAY || input.getDayOfWeek()==DayOfWeek.SUNDAY) + return true; + for (TollFreeDates tollFreeDate : TollFreeDates.values()) { + if(!tollFreeDate.getDate().equals("2013-07")) { + LocalDate dateEnum=LocalDate.parse(tollFreeDate.getDate()); + if (dateEnum.equals(input)) { + return true; + } + } + + } + }catch(Exception e) { + throw new CustomErrorException(HttpStatus.BAD_REQUEST, e.getMessage(), date); + } + return false; + } + + @Override + public List getTollFreeDates() { + List list=Stream.of(TollFreeDates.values()).map(TollFreeDates::getDate).collect(Collectors.toList()); + return list; + } + + +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java new file mode 100644 index 00000000..1d47ba63 --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java @@ -0,0 +1,33 @@ +package com.evolve_technology.calculator.service.impl; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.springframework.stereotype.Service; + +import com.evolve_technology.calculator.constant.TollFreeVehicles; +import com.evolve_technology.calculator.service.TollFreeVehicleService; + + +@Service +public class TollFreeVehiclesServiceImpl implements TollFreeVehicleService { + + public List getTollFreeVehicles() { + List list=Stream.of(TollFreeVehicles.values()).map(TollFreeVehicles::getType).collect(Collectors.toList()); + return list; + } + + public boolean isTollFreeVehicle(String vehicle) { + for (TollFreeVehicles tollFreeVehicle : TollFreeVehicles.values()) + { + if (tollFreeVehicle.name().equalsIgnoreCase(vehicle)) + { + return true; + } + } + return false; + } + + +} diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java index 982dbfb7..4119d3fd 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java @@ -6,86 +6,94 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.time.DateTimeException; import java.time.LocalDate; import org.junit.Before; import org.junit.Test; import com.evolve_technology.calculator.exception.CustomErrorException; +import com.evolve_technology.calculator.service.TollFeeService; +import com.evolve_technology.calculator.service.TollFreeDatesService; +import com.evolve_technology.calculator.service.TollFreeVehicleService; public class TollFeeCalculatorServiceImplTest { - TollFeeCalculatorServiceImpl tollFeeCalculatorService; + TollFeeService tollFeeService; + + TollFreeDatesService tollFreeDatesService; + + TollFreeVehicleService tollFreeVehicleService; @Before public void init() { - tollFeeCalculatorService=new TollFeeCalculatorServiceImpl(); + tollFeeService=new TollFeeServiceImpl(); + tollFreeDatesService=new TollFreeDatesServiceImpl(); + tollFreeVehicleService=new TollFreeVehiclesServiceImpl(); } @Test public void getTollFreeVehiclesTest(){ - assertNotNull(tollFeeCalculatorService.getTollFreeVehicles()); - assertDoesNotThrow(()->tollFeeCalculatorService.getTollFreeVehicles()); + assertNotNull(tollFreeVehicleService.getTollFreeVehicles()); + assertDoesNotThrow(()->tollFreeVehicleService.getTollFreeVehicles()); } @Test public void getTollFreeDatesTest() { - assertNotNull(tollFeeCalculatorService.getTollFreeDates()); - assertDoesNotThrow(()->tollFeeCalculatorService.getTollFreeDates()); + assertNotNull(tollFreeDatesService.getTollFreeDates()); + assertDoesNotThrow(()->tollFreeDatesService.getTollFreeDates()); } @Test public void isTollFreeDate_Positive_Scenarios_Test() { // JULY MONTH String july1=LocalDate.of(2013, 07, 01).toString(); - assertTrue(tollFeeCalculatorService.isTollFreeDate(july1)); + assertTrue(tollFreeDatesService.isTollFreeDate(july1)); String july21=LocalDate.of(2013, 07, 21).toString(); - assertTrue(tollFeeCalculatorService.isTollFreeDate(july21)); + assertTrue(tollFreeDatesService.isTollFreeDate(july21)); // Saturday verifying String june_saturday=LocalDate.of(2013, 06, 8).toString(); - assertTrue(tollFeeCalculatorService.isTollFreeDate(june_saturday)); + assertTrue(tollFreeDatesService.isTollFreeDate(june_saturday)); // sunday Verifying String june_sunday=LocalDate.of(2013, 06, 9).toString(); - assertTrue(tollFeeCalculatorService.isTollFreeDate(june_sunday)); + assertTrue(tollFreeDatesService.isTollFreeDate(june_sunday)); // Enum specific holidays Verifying - 2013-01-01 String jan1=LocalDate.of(2013, 01, 1).toString(); - assertTrue(tollFeeCalculatorService.isTollFreeDate(jan1)); + assertTrue(tollFreeDatesService.isTollFreeDate(jan1)); // Enum specific holidays Verifying - 2013-03-28 String mar28=LocalDate.of(2013, 03, 28).toString(); - assertTrue(tollFeeCalculatorService.isTollFreeDate(mar28)); + assertTrue(tollFreeDatesService.isTollFreeDate(mar28)); } @Test public void isTollFreeDate_Negative_Scenarios_Test() { // Non Enum specific holidays Verifying - 2013-01-02 Wednesday String jan1=LocalDate.of(2013, 01, 2).toString(); - assertFalse(tollFeeCalculatorService.isTollFreeDate(jan1)); + assertFalse(tollFreeDatesService.isTollFreeDate(jan1)); // Non Enum specific holidays Verifying - 2013-01-03 Thursday String jan3=LocalDate.of(2013, 01, 3).toString(); - assertFalse(tollFeeCalculatorService.isTollFreeDate(jan3)); + assertFalse(tollFreeDatesService.isTollFreeDate(jan3)); // Passing Invalid date 2013-02-30 String feb30=" 2013-02-30"; - assertThrows(CustomErrorException.class,()->tollFeeCalculatorService.isTollFreeDate(feb30)); + assertThrows(CustomErrorException.class,()->tollFreeDatesService.isTollFreeDate(feb30)); } @Test public void isTollFreeVehicleTest() { String vehicle=""; - assertFalse(tollFeeCalculatorService.isTollFreeVehicle(vehicle)); + assertFalse(tollFreeVehicleService.isTollFreeVehicle(vehicle)); vehicle="tractor"; - assertTrue(tollFeeCalculatorService.isTollFreeVehicle(vehicle)); + assertTrue(tollFreeVehicleService.isTollFreeVehicle(vehicle)); vehicle="Tractor234"; - assertFalse(tollFeeCalculatorService.isTollFreeVehicle(vehicle)); + assertFalse(tollFreeVehicleService.isTollFreeVehicle(vehicle)); } } From 6947461b89ba3b3fda61057f396693174923c9c5 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Wed, 30 Mar 2022 16:59:13 +0530 Subject: [PATCH 06/23] Logging Enabled using log4j2- Review comment addressed. --- toll_fee_calc_app/pom.xml | 119 ++++++++++-------- .../service/impl/TollFeeServiceImpl.java | 7 ++ .../impl/TollFreeDatesServiceImpl.java | 5 + .../impl/TollFreeVehiclesServiceImpl.java | 6 + .../src/main/resources/log4j2.properties | 10 ++ 5 files changed, 94 insertions(+), 53 deletions(-) create mode 100644 toll_fee_calc_app/src/main/resources/log4j2.properties diff --git a/toll_fee_calc_app/pom.xml b/toll_fee_calc_app/pom.xml index 5e918449..a0645ce8 100644 --- a/toll_fee_calc_app/pom.xml +++ b/toll_fee_calc_app/pom.xml @@ -1,55 +1,68 @@ - - 4.0.0 - com.evolve-technology.161602 - toll_free_calc_app - 0.0.1-SNAPSHOT - - org.springframework.boot - spring-boot-starter-parent - 2.4.0 - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-validation - - - org.springframework.boot - spring-boot-starter-test - - - io.springfox - springfox-swagger2 - 2.6.1 - - - io.springfox - springfox-swagger-ui - 2.6.1 - - - org.springframework.boot - spring-boot-devtools - runtime - + + 4.0.0 + com.evolve-technology.161602 + toll_free_calc_app + 0.0.1-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-test + + + io.springfox + springfox-swagger2 + 2.6.1 + + + io.springfox + springfox-swagger-ui + 2.6.1 + + + org.springframework.boot + spring-boot-devtools + runtime + - - org.projectlombok - lombok - true - - - + + org.projectlombok + lombok + true + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index df380b1b..49f8ae73 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -4,6 +4,8 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -14,6 +16,8 @@ @Service public class TollFeeServiceImpl implements TollFeeService { + private static final Logger logger = LogManager.getLogger(TollFeeServiceImpl.class); + @Autowired TollFreeDatesService tollFreeDatesService; @@ -23,6 +27,9 @@ public class TollFeeServiceImpl implements TollFeeService { @Override public int getTollFee(String date, String vehicle) { + + logger.info("Inside getTollFee method :: date = {} and vehicle = {}",date,vehicle); + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; LocalDateTime inputDate=LocalDateTime.parse(date,formatter); int hour=inputDate.getHour(); diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java index 53947c90..09e8485d 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java @@ -7,6 +7,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -18,8 +20,10 @@ @Service public class TollFreeDatesServiceImpl implements TollFreeDatesService { + private static final Logger logger = LogManager.getLogger(TollFreeDatesServiceImpl.class); @Override public Boolean isTollFreeDate(String date) { + logger.info("Inside isTollFreeDate method :: date = {} ",date); try { LocalDate input=LocalDate.parse(date); if(input.getMonth()==Month.JULY || input.getDayOfWeek()==DayOfWeek.SATURDAY || input.getDayOfWeek()==DayOfWeek.SUNDAY) @@ -41,6 +45,7 @@ public Boolean isTollFreeDate(String date) { @Override public List getTollFreeDates() { + logger.info("Inside getTollFreeDates method "); List list=Stream.of(TollFreeDates.values()).map(TollFreeDates::getDate).collect(Collectors.toList()); return list; } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java index 1d47ba63..d224671f 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java @@ -4,6 +4,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Service; import com.evolve_technology.calculator.constant.TollFreeVehicles; @@ -13,12 +15,16 @@ @Service public class TollFreeVehiclesServiceImpl implements TollFreeVehicleService { + private static final Logger logger = LogManager.getLogger(TollFreeVehiclesServiceImpl.class); + public List getTollFreeVehicles() { + logger.info("Inside getTollFreeVehicles method "); List list=Stream.of(TollFreeVehicles.values()).map(TollFreeVehicles::getType).collect(Collectors.toList()); return list; } public boolean isTollFreeVehicle(String vehicle) { + logger.info("Inside isTollFreeVehicle method :: vehicle = {} ",vehicle); for (TollFreeVehicles tollFreeVehicle : TollFreeVehicles.values()) { if (tollFreeVehicle.name().equalsIgnoreCase(vehicle)) diff --git a/toll_fee_calc_app/src/main/resources/log4j2.properties b/toll_fee_calc_app/src/main/resources/log4j2.properties new file mode 100644 index 00000000..f5696d93 --- /dev/null +++ b/toll_fee_calc_app/src/main/resources/log4j2.properties @@ -0,0 +1,10 @@ +log4j.rootLogger=INFO,file,stdout + +# Redirect log messages to console + +appender.console.type=Console +appender.console.name=consoleLogger +appender.console.layout.type= PatternLayout +appender.console.layout.pattern = [Loglevel=%p] [Timestmp=%d{ISO8601}][Thread=%t][Msg=%F %L %m]%n +rootLogger.appenderRef.stdout.ref=consoleLogger +rootLogger.level=info From 3f5153446d85b2163679c9f98cf2299b3ccb9061 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Fri, 1 Apr 2022 01:28:44 +0530 Subject: [PATCH 07/23] Controller decomposed - code refactored --- .../TollFeeCalculatorController.java | 82 ------------------- .../controller/TollFeeController.java | 44 ++++++++++ .../controller/TollFreeDatesController.java | 42 ++++++++++ .../TollFreeVehiclesController.java | 40 +++++++++ 4 files changed, 126 insertions(+), 82 deletions(-) delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeDatesController.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeVehiclesController.java diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java deleted file mode 100644 index 0f8a5a14..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeCalculatorController.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.evolve_technology.calculator.controller; - -import java.util.List; - -import javax.validation.constraints.Pattern; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import com.evolve_technology.calculator.controller.model.TollFeeDTO; -import com.evolve_technology.calculator.controller.model.TollFreeDateDTO; -import com.evolve_technology.calculator.controller.model.TollFreeVehicleDTO; -import com.evolve_technology.calculator.service.TollFeeService; -import com.evolve_technology.calculator.service.TollFreeDatesService; -import com.evolve_technology.calculator.service.TollFreeVehicleService; - -@RestController("/v1/api") -@Validated -public class TollFeeCalculatorController { - - @Autowired - TollFeeService tollFreeService; - - @Autowired - TollFreeDatesService tollFreeDatesService; - - @Autowired - TollFreeVehicleService tollFreeVehicleService; - - @GetMapping("tollfee/vehicles/{name}/dates/{date}") - public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") - @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") - String date) { - int amount= tollFreeService.getTollFee(date, name); - TollFeeDTO tollFeeDTO=new TollFeeDTO(); - tollFeeDTO.setAmount(amount); - tollFeeDTO.setDate(date); - tollFeeDTO.setName(name); - return new ResponseEntity<>(tollFeeDTO,HttpStatus.OK); - } - - @GetMapping("tollfree/vehicles") - public ResponseEntity> getTollFreeVehicles(){ - List list=tollFreeVehicleService.getTollFreeVehicles(); - return new ResponseEntity<>(list,HttpStatus.OK); - - } - - @GetMapping("tollfree/vehicles/{name}") - public ResponseEntity getTollFreeVehicleStatusByName(@PathVariable("name") String name){ - Boolean value=tollFreeVehicleService.isTollFreeVehicle(name); - TollFreeVehicleDTO tollFreeVehicleDTO=new TollFreeVehicleDTO(); - tollFreeVehicleDTO.setName(name); - tollFreeVehicleDTO.setTollFreeVehicle(value); - return new ResponseEntity<>(tollFreeVehicleDTO,HttpStatus.OK); - - } - - @GetMapping("tollfree/dates") - public ResponseEntity> getTollFreeDates(){ - List list=tollFreeDatesService.getTollFreeDates(); - return new ResponseEntity<>(list,HttpStatus.OK); - } - - @GetMapping("tollfree/dates/{date}") - public ResponseEntity getTollFreeDateStatus(@PathVariable("date") - @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$") String date){ - Boolean value=tollFreeDatesService.isTollFreeDate(date); - TollFreeDateDTO tollFreeDateDTO=new TollFreeDateDTO(); - tollFreeDateDTO.setDate(date); - tollFreeDateDTO.setIsTollFreeDate(value); - return new ResponseEntity<>(tollFreeDateDTO,HttpStatus.OK); - - } - - -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java new file mode 100644 index 00000000..6f49f6bc --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java @@ -0,0 +1,44 @@ +package com.evolve_technology.calculator.controller; + +import java.util.Map; + +import javax.validation.constraints.Pattern; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.evolve_technology.calculator.controller.model.TollFeeDTO; +import com.evolve_technology.calculator.service.TollFeeService; + +@RestController("/tollfee") +@Validated +public class TollFeeController { + + @Autowired + TollFeeService tollFreeService; + + @GetMapping("/vehicles/{name}/dates/{date}") + public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") + @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") + String date) { + Map> amount= tollFreeService.getTollFee(date, name); + TollFeeDTO tollFeeDTO=new TollFeeDTO(); +// tollFeeDTO.setAmount(amount); + tollFeeDTO.setDate(date); + tollFeeDTO.setName(name); + return new ResponseEntity<>(tollFeeDTO,HttpStatus.OK); + } + + + + + + + + +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeDatesController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeDatesController.java new file mode 100644 index 00000000..4a025edb --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeDatesController.java @@ -0,0 +1,42 @@ +package com.evolve_technology.calculator.controller; + +import java.util.List; + +import javax.validation.constraints.Pattern; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.evolve_technology.calculator.controller.model.TollFreeDateDTO; +import com.evolve_technology.calculator.service.TollFreeDatesService; + + +@RestController +@Validated +public class TollFreeDatesController { + + @Autowired + TollFreeDatesService tollFreeDatesService; + + @GetMapping("/tollfree/dates") + public ResponseEntity> getTollFreeDates(){ + List list=tollFreeDatesService.getTollFreeDates(); + return new ResponseEntity<>(list,HttpStatus.OK); + } + + @GetMapping("/tollfree/dates/{date}") + public ResponseEntity getTollFreeDateStatus(@PathVariable("date") + @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$") String date){ + Boolean value=tollFreeDatesService.isTollFreeDate(date); + TollFreeDateDTO tollFreeDateDTO=new TollFreeDateDTO(); + tollFreeDateDTO.setDate(date); + tollFreeDateDTO.setIsTollFreeDate(value); + return new ResponseEntity<>(tollFreeDateDTO,HttpStatus.OK); + + } +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeVehiclesController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeVehiclesController.java new file mode 100644 index 00000000..0e706579 --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeVehiclesController.java @@ -0,0 +1,40 @@ +package com.evolve_technology.calculator.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.evolve_technology.calculator.controller.model.TollFreeVehicleDTO; +import com.evolve_technology.calculator.service.TollFreeVehicleService; + + +@RestController +@Validated +public class TollFreeVehiclesController { + + @Autowired + TollFreeVehicleService tollFreeVehicleService; + + @GetMapping("/tollfree/vehicles") + public ResponseEntity> getTollFreeVehicles(){ + List list=tollFreeVehicleService.getTollFreeVehicles(); + return new ResponseEntity<>(list,HttpStatus.OK); + + } + + @GetMapping("/tollfree/vehicles/{name}") + public ResponseEntity getTollFreeVehicleStatusByName(@PathVariable("name") String name){ + Boolean value=tollFreeVehicleService.isTollFreeVehicle(name); + TollFreeVehicleDTO tollFreeVehicleDTO=new TollFreeVehicleDTO(); + tollFreeVehicleDTO.setName(name); + tollFreeVehicleDTO.setTollFreeVehicle(value); + return new ResponseEntity<>(tollFreeVehicleDTO,HttpStatus.OK); + + } +} From 0c857a0b2b6c63dbf9cde39b8f0c2a24af6112f2 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Fri, 1 Apr 2022 01:31:58 +0530 Subject: [PATCH 08/23] base path added. --- toll_fee_calc_app/src/main/resources/application.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/toll_fee_calc_app/src/main/resources/application.properties b/toll_fee_calc_app/src/main/resources/application.properties index e69de29b..d2971a08 100644 --- a/toll_fee_calc_app/src/main/resources/application.properties +++ b/toll_fee_calc_app/src/main/resources/application.properties @@ -0,0 +1 @@ +server.servlet.context-path=/v1/api \ No newline at end of file From 4d43b14673f81318b8b06c48b9600c6d9bb699ae Mon Sep 17 00:00:00 2001 From: Rizwan Date: Sun, 3 Apr 2022 16:37:39 +0530 Subject: [PATCH 09/23] Core Logic Added. --- .../controller/TollFeeController.java | 22 +-- .../controller/model/TollDetails.java | 12 ++ .../calculator/controller/model/TollFee.java | 13 ++ .../controller/model/TollFeeDTO.java | 9 +- .../calculator/service/TollFeeService.java | 7 +- .../service/impl/TollFeeServiceImpl.java | 155 +++++++++++++++--- 6 files changed, 176 insertions(+), 42 deletions(-) create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollDetails.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFee.java diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java index 6f49f6bc..10f612b3 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java @@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; -import com.evolve_technology.calculator.controller.model.TollFeeDTO; +import com.evolve_technology.calculator.controller.model.TollFee; import com.evolve_technology.calculator.service.TollFeeService; @RestController("/tollfee") @@ -23,22 +23,16 @@ public class TollFeeController { TollFeeService tollFreeService; @GetMapping("/vehicles/{name}/dates/{date}") - public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") + public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") String date) { - Map> amount= tollFreeService.getTollFee(date, name); - TollFeeDTO tollFeeDTO=new TollFeeDTO(); -// tollFeeDTO.setAmount(amount); - tollFeeDTO.setDate(date); - tollFeeDTO.setName(name); - return new ResponseEntity<>(tollFeeDTO,HttpStatus.OK); + TollFee tollFee= tollFreeService.getTollFee(date, name); + return new ResponseEntity<>(tollFee,HttpStatus.OK); } - - - - - - + @GetMapping("/records") + public Map>> getTollFeeHistoricalRecords(){ + return tollFreeService.getHistoricalTollRecords(); + } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollDetails.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollDetails.java new file mode 100644 index 00000000..c50ed008 --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollDetails.java @@ -0,0 +1,12 @@ +package com.evolve_technology.calculator.controller.model; + +import java.util.Map; + +import lombok.Data; + +@Data +class TollDetailsPerDay{ + String date; + int totalAmount; + Map travelRecordsHourlyCharged; +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFee.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFee.java new file mode 100644 index 00000000..b8c99d9e --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFee.java @@ -0,0 +1,13 @@ +package com.evolve_technology.calculator.controller.model; + +import java.util.Map; + +import lombok.Data; + +@Data +public class TollFee { + String vehicle; + String date; + int totalAmount; + Map travelRecordsWithinDay; +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java index f38a7ac9..5627294f 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java @@ -1,10 +1,13 @@ package com.evolve_technology.calculator.controller.model; +import java.util.List; + import lombok.Data; @Data public class TollFeeDTO { - String name; - String date; - int amount; + String vehicle; + List tollDetails; } + + diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java index 8088479e..00e077e6 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java @@ -1,7 +1,12 @@ package com.evolve_technology.calculator.service; +import java.util.Map; + +import com.evolve_technology.calculator.controller.model.TollFee; + public interface TollFeeService { - public int getTollFee(final String date, String vehicle); + public TollFee getTollFee(final String date, String vehicle); + public Map>> getHistoricalTollRecords(); } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index 49f8ae73..281249f5 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -3,12 +3,16 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.evolve_technology.calculator.controller.model.TollFee; import com.evolve_technology.calculator.service.TollFeeService; import com.evolve_technology.calculator.service.TollFreeDatesService; import com.evolve_technology.calculator.service.TollFreeVehicleService; @@ -17,38 +21,141 @@ public class TollFeeServiceImpl implements TollFeeService { private static final Logger logger = LogManager.getLogger(TollFeeServiceImpl.class); - + @Autowired TollFreeDatesService tollFreeDatesService; - + @Autowired TollFreeVehicleService tollFreeVehicleService; + + private static Map>> aggregateTollMap = new HashMap<>(); - + private static boolean vehicleExists = false; + @Override - public int getTollFee(String date, String vehicle) { - - logger.info("Inside getTollFee method :: date = {} and vehicle = {}",date,vehicle); - + public TollFee getTollFee(String date, String vehicle) { + + logger.info("Inside getTollFee method :: date = {} and vehicle = {}", date, vehicle); + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; - LocalDateTime inputDate=LocalDateTime.parse(date,formatter); - int hour=inputDate.getHour(); - int minute=inputDate.getMinute(); - LocalDate localDate=LocalDate.of(inputDate.getYear(), inputDate.getMonthValue(), inputDate.getDayOfMonth()); - - if(tollFreeDatesService.isTollFreeDate(localDate.toString())||tollFreeVehicleService.isTollFreeVehicle(vehicle)) + LocalDateTime inputDate = LocalDateTime.parse(date, formatter); + int hour = inputDate.getHour(); + int minute = inputDate.getMinute(); + LocalDate localDate = LocalDate.of(inputDate.getYear(), inputDate.getMonthValue(), inputDate.getDayOfMonth()); + + logger.info(" aggregateTollMap = {} ", aggregateTollMap); + + Integer newFee = tollCalculate(vehicle, localDate, hour, minute); + if (aggregateTollMap.containsKey(vehicle)) { + vehicleExists = true; + } + Map> aggregateTollMapValue = aggregateTollMap.computeIfAbsent(vehicle, k -> { + logger.info("Vehicle {} has crossed the toll first time ", vehicle); + Map> outerMap = new HashMap<>(); + Map innerMap = new HashMap<>(); + innerMap.put(hour, newFee); + outerMap.put(localDate.toString(), innerMap); + return outerMap; + }); + logger.info("key = {} , value = {} ", vehicle, aggregateTollMapValue); + + if (vehicleExists) { + Map> aggregateTollMapUpdatedValue = aggregateTollMap.computeIfPresent(vehicle, + (k, outerMap) -> { + Map innerMap = outerMap.get(localDate.toString()); + if (innerMap == null) { + logger.info("Vehicle {} has not crossed the toll on day {}", vehicle, localDate); + innerMap = new HashMap<>(); + innerMap.put(hour, newFee); + outerMap.put(localDate.toString(), innerMap); + } else { + logger.info("Vehicle {} has crossed the toll on day {}", vehicle, localDate); + Integer existingFee = innerMap.get(hour); + if (existingFee == null) { + logger.info("Vehicle {} has crossed the toll on day {} but not at hour {}", vehicle, + localDate, hour); + innerMap.put(hour, newFee); + outerMap.put(localDate.toString(), innerMap); + } + if (existingFee != null && existingFee < newFee) { + logger.info( + "Vehicle {} has crossed the toll on day {} at hour {} with oldFee {} and new Fee {}", + vehicle, localDate, hour, existingFee, newFee); + innerMap.put(hour, newFee); + outerMap.put(localDate.toString(), innerMap); + } + } + return outerMap; + }); + logger.info("updated value = {} ", aggregateTollMapUpdatedValue); + } + return buildResult(aggregateTollMap, vehicle, localDate.toString()); + } + + private TollFee buildResult(Map>> aggregateTollMap, String vehicle, + String date) { + TollFee tollFee = new TollFee(); + tollFee.setVehicle(vehicle); + tollFee.setDate(date); + tollFee.setTotalAmount(0); + aggregateTollMap.computeIfPresent(vehicle, (k, v) -> { + Map innerMap = v.get(date); + if (innerMap != null) { + int amount = innerMap.entrySet().stream().map(x -> x.getValue()) + .collect(Collectors.summingInt(Integer::intValue)); + if (amount < 60) { + tollFee.setTotalAmount(amount); + } else { + tollFee.setTotalAmount(60); + } + tollFee.setTravelRecordsWithinDay(innerMap.entrySet().stream().collect(Collectors.toMap(x -> { + int key = x.getKey(); + String modifiedKey = String.format( + "vehicle = %1$s passed the toll booth on date = %2$s at hour = %3$s", vehicle, date, + Integer.toString(key)); + return modifiedKey; + }, y -> { + int value = y.getValue(); + String modifiedValue = String.format("Amount charged = %1$s ", Integer.toString(value)); + return modifiedValue; + }))); + } + return v; + }); + + return tollFee; + } + + private int tollCalculate(String vehicle, LocalDate localDate, int hour, int minute) { + if (tollFreeDatesService.isTollFreeDate(localDate.toString()) + || tollFreeVehicleService.isTollFreeVehicle(vehicle)) + return 0; + if (hour == 6 && minute >= 0 && minute <= 29) + return 8; + else if (hour == 6 && minute >= 30 && minute <= 59) + return 13; + else if (hour == 7 && minute >= 0 && minute <= 59) + return 18; + else if (hour == 8 && minute >= 0 && minute <= 29) + return 13; + else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) + return 8; + else if (hour == 15 && minute >= 0 && minute <= 29) + return 13; + else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) + return 18; + else if (hour == 17 && minute >= 0 && minute <= 59) + return 13; + else if (hour == 18 && minute >= 0 && minute <= 29) + return 8; + else return 0; - if (hour == 6 && minute >= 0 && minute <= 29) return 8; - else if (hour == 6 && minute >= 30 && minute <= 59) return 13; - else if (hour == 7 && minute >= 0 && minute <= 59) return 18; - else if (hour == 8 && minute >= 0 && minute <= 29) return 13; - else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8; - else if (hour == 15 && minute >= 0 && minute <= 29) return 13; - else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return 18; - else if (hour == 17 && minute >= 0 && minute <= 59) return 13; - else if (hour == 18 && minute >= 0 && minute <= 29) return 8; - else return 0; } - + @Override + public Map>> getHistoricalTollRecords() { + // TODO Auto-generated method stub + return aggregateTollMap; + } + } From 14376267d5ef750e1e017a6991d23060954b3625 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Mon, 4 Apr 2022 14:57:44 +0530 Subject: [PATCH 10/23] Configuration Externalized | Enum deleted. --- .../calculator/constant/TollFreeDates.java | 44 ------------------- .../calculator/constant/TollFreeVehicles.java | 20 --------- .../properties/TollConfiguration.java | 20 +++++++++ .../service/impl/TollFeeServiceImpl.java | 1 + .../impl/TollFreeDatesServiceImpl.java | 39 +++++++--------- .../impl/TollFreeVehiclesServiceImpl.java | 21 ++++----- .../src/main/resources/application.properties | 14 +++++- 7 files changed, 59 insertions(+), 100 deletions(-) delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java deleted file mode 100644 index bef73738..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeDates.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.evolve_technology.calculator.constant; - -import java.time.LocalDate; -import java.time.YearMonth; - -public enum TollFreeDates { - JANUARY_1_2013(LocalDate.of(2013,01,01).toString()), - MARCH_28_2013(LocalDate.of(2013,03,28).toString()), - MARCH_29_2013(LocalDate.of(2013,03,29).toString()), - APRIL_1_2013(LocalDate.of(2013,04,01).toString()), - APRIL_30_2013(LocalDate.of(2013,04,30).toString()), - MAY_1_2013(LocalDate.of(2013,05,01).toString()), - MAY_8_2013(LocalDate.of(2013,05,8).toString()), - MAY_9_2013(LocalDate.of(2013,05,9).toString()), - JUNE_5_2013(LocalDate.of(2013,06,5).toString()), - JUNE_6_2013(LocalDate.of(2013,06,6).toString()), - JUNE_21_2013(LocalDate.of(2013,06,21).toString()), - JULY(YearMonth.of(2013,07).toString()), - NOVEMBER_1_2013(LocalDate.of(2013,11,1).toString()), - DECEMBER_24_2013(LocalDate.of(2013,12,24).toString()), - DECEMBER_25_2013(LocalDate.of(2013,12,25).toString()), - DECEMBER_26_2013(LocalDate.of(2013,12,26).toString()), - DECEMBER_31_2013(LocalDate.of(2013,12,31).toString()); - - private String date; - - private TollFreeDates(String date) { - this.date=date; - } - - public String getDate() { - return date; - } -} -// if (month == Calendar.JANUARY && day == 1 || -// month == Calendar.MARCH && (day == 28 || day == 29) || -// month == Calendar.APRIL && (day == 1 || day == 30) || -// month == Calendar.MAY && (day == 1 || day == 8 || day == 9) || -// month == Calendar.JUNE && (day == 5 || day == 6 || day == 21) || -// month == Calendar.JULY || -// month == Calendar.NOVEMBER && day == 1 || -// month == Calendar.DECEMBER && (day == 24 || day == 25 || day == 26 || day == 31)) { -// return true; -//} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java deleted file mode 100644 index fb80a18d..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/constant/TollFreeVehicles.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.evolve_technology.calculator.constant; - -public enum TollFreeVehicles { - - MOTORBIKE("Motorbike"), - TRACTOR("Tractor"), - EMERGENCY("Emergency"), - DIPLOMAT("Diplomat"), - FOREIGN("Foreign"), - MILITARY("Military"); - private final String type; - - TollFreeVehicles(String type) { - this.type = type; - } - - public String getType() { - return type; - } -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java new file mode 100644 index 00000000..972e20ef --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java @@ -0,0 +1,20 @@ +package com.evolve_technology.calculator.properties; + +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import lombok.Data; + +@Data +@Component +@ConfigurationProperties(prefix="tollfree") +public class TollConfiguration { + + private String year; + private List dates; + private List vehicles; + private List months; + +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index 281249f5..83e80ee1 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -13,6 +13,7 @@ import org.springframework.stereotype.Service; import com.evolve_technology.calculator.controller.model.TollFee; +import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFeeService; import com.evolve_technology.calculator.service.TollFreeDatesService; import com.evolve_technology.calculator.service.TollFreeVehicleService; diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java index 09e8485d..e387901c 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java @@ -4,50 +4,45 @@ import java.time.LocalDate; import java.time.Month; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.springframework.http.HttpStatus; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.evolve_technology.calculator.constant.TollFreeDates; -import com.evolve_technology.calculator.exception.CustomErrorException; +import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFreeDatesService; @Service public class TollFreeDatesServiceImpl implements TollFreeDatesService { - private static final Logger logger = LogManager.getLogger(TollFreeDatesServiceImpl.class); + + @Autowired + TollConfiguration tollConfiguration; + @Override public Boolean isTollFreeDate(String date) { logger.info("Inside isTollFreeDate method :: date = {} ",date); - try { + LocalDate input=LocalDate.parse(date); - if(input.getMonth()==Month.JULY || input.getDayOfWeek()==DayOfWeek.SATURDAY || input.getDayOfWeek()==DayOfWeek.SUNDAY) + if( input.getDayOfWeek()==DayOfWeek.SATURDAY || input.getDayOfWeek()==DayOfWeek.SUNDAY) { + logger.info("date {} is actually a weekend so no toll Enjoy.",date); + return true; + } + + if(input.getMonth()==Month.JULY) { + logger.info("date {} lies in JULY month so no toll Enjoy.",date); return true; - for (TollFreeDates tollFreeDate : TollFreeDates.values()) { - if(!tollFreeDate.getDate().equals("2013-07")) { - LocalDate dateEnum=LocalDate.parse(tollFreeDate.getDate()); - if (dateEnum.equals(input)) { - return true; - } - } - - } - }catch(Exception e) { - throw new CustomErrorException(HttpStatus.BAD_REQUEST, e.getMessage(), date); } - return false; + + return getTollFreeDates().stream().filter(x -> x.trim().equalsIgnoreCase(input.toString())).findAny().isPresent(); } @Override public List getTollFreeDates() { logger.info("Inside getTollFreeDates method "); - List list=Stream.of(TollFreeDates.values()).map(TollFreeDates::getDate).collect(Collectors.toList()); - return list; + return tollConfiguration.getDates(); } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java index d224671f..90981a49 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java @@ -1,38 +1,33 @@ package com.evolve_technology.calculator.service.impl; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.evolve_technology.calculator.constant.TollFreeVehicles; +import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFreeVehicleService; @Service public class TollFreeVehiclesServiceImpl implements TollFreeVehicleService { + @Autowired + TollConfiguration tollConfiguration; + private static final Logger logger = LogManager.getLogger(TollFreeVehiclesServiceImpl.class); public List getTollFreeVehicles() { logger.info("Inside getTollFreeVehicles method "); - List list=Stream.of(TollFreeVehicles.values()).map(TollFreeVehicles::getType).collect(Collectors.toList()); - return list; + return tollConfiguration.getVehicles(); } public boolean isTollFreeVehicle(String vehicle) { logger.info("Inside isTollFreeVehicle method :: vehicle = {} ",vehicle); - for (TollFreeVehicles tollFreeVehicle : TollFreeVehicles.values()) - { - if (tollFreeVehicle.name().equalsIgnoreCase(vehicle)) - { - return true; - } - } - return false; + List vehicles=getTollFreeVehicles(); + return vehicles.stream().filter(v->v.equalsIgnoreCase(vehicle)).findAny().isPresent(); } diff --git a/toll_fee_calc_app/src/main/resources/application.properties b/toll_fee_calc_app/src/main/resources/application.properties index d2971a08..58d15410 100644 --- a/toll_fee_calc_app/src/main/resources/application.properties +++ b/toll_fee_calc_app/src/main/resources/application.properties @@ -1 +1,13 @@ -server.servlet.context-path=/v1/api \ No newline at end of file +server.servlet.context-path=/v1/api + +# Toll Free Year +tollfree.year=2013 + +# Toll Free Dates +tollfree.dates=2013-01-01 , 2013-03-28 , 2013-03-29 , 2013-04-01 , 2013-04-30 , 2013-05-01 , 2013-05-08 , 2013-05-09 , 2013-06-05 , 2013-06-06 , 2013-06-21 , 2013-11-01 , 2013-12-24 , 2013-12-25 , 2013-12-26 , 2013-12-31 + +# Toll Free Months +tollfree.months= JULY + +# Toll Free Vehicles +tollfree.vehicles= Motorbike , Tractor , Emergency , Diplomat , Foreign , Military \ No newline at end of file From eb46193492412e4c3fcca4c5d1d8def8c5fc37a5 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Mon, 4 Apr 2022 15:15:11 +0530 Subject: [PATCH 11/23] JULY Month validation --- .../calculator/service/impl/TollFreeDatesServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java index e387901c..b0a2ff31 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java @@ -31,7 +31,7 @@ public Boolean isTollFreeDate(String date) { return true; } - if(input.getMonth()==Month.JULY) { + if(tollConfiguration.getMonths().stream().filter(month-> month.trim().equalsIgnoreCase(input.getMonth().toString())).findAny().isPresent()) { logger.info("date {} lies in JULY month so no toll Enjoy.",date); return true; } From 529c209bbde98fb3f6c83e2e44974118634afddd Mon Sep 17 00:00:00 2001 From: Rizwan Date: Thu, 5 May 2022 20:27:32 +0530 Subject: [PATCH 12/23] last commit before refactoring --- .../controller/TollFeeController.java | 6 +- .../properties/TollConfiguration.java | 2 + .../service/impl/TollFeeServiceImpl.java | 6 +- .../impl/TollFreeDatesServiceImpl.java | 1 - .../src/main/resources/application.properties | 4 +- .../TollFeeCalculatorServiceImplTest.java | 99 --------------- .../service/impl/TollFeeServiceImplTest.java | 113 ++++++++++++++++++ 7 files changed, 122 insertions(+), 109 deletions(-) delete mode 100644 toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java create mode 100644 toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java index 10f612b3..0d711590 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java @@ -20,19 +20,19 @@ public class TollFeeController { @Autowired - TollFeeService tollFreeService; + TollFeeService tollFeeService; @GetMapping("/vehicles/{name}/dates/{date}") public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") String date) { - TollFee tollFee= tollFreeService.getTollFee(date, name); + TollFee tollFee= tollFeeService.getTollFee(date, name); return new ResponseEntity<>(tollFee,HttpStatus.OK); } @GetMapping("/records") public Map>> getTollFeeHistoricalRecords(){ - return tollFreeService.getHistoricalTollRecords(); + return tollFeeService.getHistoricalTollRecords(); } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java index 972e20ef..68487047 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java @@ -2,6 +2,7 @@ import java.util.List; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -12,6 +13,7 @@ @ConfigurationProperties(prefix="tollfree") public class TollConfiguration { + @Value("${year}") private String year; private List dates; private List vehicles; diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index 83e80ee1..ae81d5fb 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -13,7 +13,6 @@ import org.springframework.stereotype.Service; import com.evolve_technology.calculator.controller.model.TollFee; -import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFeeService; import com.evolve_technology.calculator.service.TollFreeDatesService; import com.evolve_technology.calculator.service.TollFreeVehicleService; @@ -90,11 +89,10 @@ public TollFee getTollFee(String date, String vehicle) { }); logger.info("updated value = {} ", aggregateTollMapUpdatedValue); } - return buildResult(aggregateTollMap, vehicle, localDate.toString()); + return buildResult( vehicle, localDate.toString()); } - private TollFee buildResult(Map>> aggregateTollMap, String vehicle, - String date) { + private TollFee buildResult( String vehicle,String date) { TollFee tollFee = new TollFee(); tollFee.setVehicle(vehicle); tollFee.setDate(date); diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java index b0a2ff31..774f787b 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java @@ -2,7 +2,6 @@ import java.time.DayOfWeek; import java.time.LocalDate; -import java.time.Month; import java.util.List; import org.apache.logging.log4j.LogManager; diff --git a/toll_fee_calc_app/src/main/resources/application.properties b/toll_fee_calc_app/src/main/resources/application.properties index 58d15410..ca7d1511 100644 --- a/toll_fee_calc_app/src/main/resources/application.properties +++ b/toll_fee_calc_app/src/main/resources/application.properties @@ -1,7 +1,7 @@ server.servlet.context-path=/v1/api -# Toll Free Year -tollfree.year=2013 +# Year +year=2013 # Toll Free Dates tollfree.dates=2013-01-01 , 2013-03-28 , 2013-03-29 , 2013-04-01 , 2013-04-30 , 2013-05-01 , 2013-05-08 , 2013-05-09 , 2013-06-05 , 2013-06-06 , 2013-06-21 , 2013-11-01 , 2013-12-24 , 2013-12-25 , 2013-12-26 , 2013-12-31 diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java deleted file mode 100644 index 4119d3fd..00000000 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeCalculatorServiceImplTest.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.evolve_technology.calculator.service.impl; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.time.LocalDate; - -import org.junit.Before; -import org.junit.Test; - -import com.evolve_technology.calculator.exception.CustomErrorException; -import com.evolve_technology.calculator.service.TollFeeService; -import com.evolve_technology.calculator.service.TollFreeDatesService; -import com.evolve_technology.calculator.service.TollFreeVehicleService; - - -public class TollFeeCalculatorServiceImplTest { - - TollFeeService tollFeeService; - - TollFreeDatesService tollFreeDatesService; - - TollFreeVehicleService tollFreeVehicleService; - - @Before - public void init() { - tollFeeService=new TollFeeServiceImpl(); - tollFreeDatesService=new TollFreeDatesServiceImpl(); - tollFreeVehicleService=new TollFreeVehiclesServiceImpl(); - } - - @Test - public void getTollFreeVehiclesTest(){ - assertNotNull(tollFreeVehicleService.getTollFreeVehicles()); - assertDoesNotThrow(()->tollFreeVehicleService.getTollFreeVehicles()); - } - - @Test - public void getTollFreeDatesTest() { - assertNotNull(tollFreeDatesService.getTollFreeDates()); - assertDoesNotThrow(()->tollFreeDatesService.getTollFreeDates()); - } - - @Test - public void isTollFreeDate_Positive_Scenarios_Test() { - // JULY MONTH - String july1=LocalDate.of(2013, 07, 01).toString(); - assertTrue(tollFreeDatesService.isTollFreeDate(july1)); - - String july21=LocalDate.of(2013, 07, 21).toString(); - assertTrue(tollFreeDatesService.isTollFreeDate(july21)); - - // Saturday verifying - String june_saturday=LocalDate.of(2013, 06, 8).toString(); - assertTrue(tollFreeDatesService.isTollFreeDate(june_saturday)); - - // sunday Verifying - String june_sunday=LocalDate.of(2013, 06, 9).toString(); - assertTrue(tollFreeDatesService.isTollFreeDate(june_sunday)); - - // Enum specific holidays Verifying - 2013-01-01 - String jan1=LocalDate.of(2013, 01, 1).toString(); - assertTrue(tollFreeDatesService.isTollFreeDate(jan1)); - - // Enum specific holidays Verifying - 2013-03-28 - String mar28=LocalDate.of(2013, 03, 28).toString(); - assertTrue(tollFreeDatesService.isTollFreeDate(mar28)); - } - - @Test - public void isTollFreeDate_Negative_Scenarios_Test() { - // Non Enum specific holidays Verifying - 2013-01-02 Wednesday - String jan1=LocalDate.of(2013, 01, 2).toString(); - assertFalse(tollFreeDatesService.isTollFreeDate(jan1)); - - // Non Enum specific holidays Verifying - 2013-01-03 Thursday - String jan3=LocalDate.of(2013, 01, 3).toString(); - assertFalse(tollFreeDatesService.isTollFreeDate(jan3)); - - // Passing Invalid date 2013-02-30 - String feb30=" 2013-02-30"; - assertThrows(CustomErrorException.class,()->tollFreeDatesService.isTollFreeDate(feb30)); - } - - @Test - public void isTollFreeVehicleTest() { - String vehicle=""; - assertFalse(tollFreeVehicleService.isTollFreeVehicle(vehicle)); - - vehicle="tractor"; - assertTrue(tollFreeVehicleService.isTollFreeVehicle(vehicle)); - - vehicle="Tractor234"; - assertFalse(tollFreeVehicleService.isTollFreeVehicle(vehicle)); - } -} diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java new file mode 100644 index 00000000..f5b0722e --- /dev/null +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java @@ -0,0 +1,113 @@ +package com.evolve_technology.calculator.service.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.test.context.TestPropertySource; + +import com.evolve_technology.calculator.service.TollFeeService; +import com.evolve_technology.calculator.service.TollFreeDatesService; +import com.evolve_technology.calculator.service.TollFreeVehicleService; + +@TestPropertySource +public class TollFeeServiceImplTest { + + @InjectMocks + TollFeeService tollFeeService; + + @Mock + TollFreeDatesService tollFreeDatesService; + + @Mock + TollFreeVehicleService tollFreeVehicleService; + + @Value("${tollfree.year}") + private String year; + @Value("${tollfree.dates}") + private List dates; + @Value("${tollfree.vehicles}") + private List vehicles; + @Value("${value.months}") + private List months; + + @Before + public void init() { + Mockito.when(tollFreeVehicleService.getTollFreeVehicles()).thenReturn(vehicles); + Mockito.when(tollFreeDatesService.getTollFreeDates()).thenReturn(dates); + MockitoAnnotations.initMocks(this); + } + + @Test + public void getTollFreeVehiclesTest(){ + assertNotNull(tollFreeVehicleService.getTollFreeVehicles()); + assertDoesNotThrow(()->tollFreeVehicleService.getTollFreeVehicles()); + } + + @Test + public void getTollFreeDatesTest() { + assertNotNull(tollFreeDatesService.getTollFreeDates()); + assertDoesNotThrow(()->tollFreeDatesService.getTollFreeDates()); + } + +// @Test +// public void isTollFreeDate_Positive_Scenarios_Test() { +// // JULY MONTH +// String july1=LocalDate.of(2013, 07, 01).toString(); +// assertTrue(tollFreeDatesService.isTollFreeDate(july1)); +// +// String july21=LocalDate.of(2013, 07, 21).toString(); +// assertTrue(tollFreeDatesService.isTollFreeDate(july21)); +// +// // Saturday verifying +// String june_saturday=LocalDate.of(2013, 06, 8).toString(); +// assertTrue(tollFreeDatesService.isTollFreeDate(june_saturday)); +// +// // sunday Verifying +// String june_sunday=LocalDate.of(2013, 06, 9).toString(); +// assertTrue(tollFreeDatesService.isTollFreeDate(june_sunday)); +// +// // Enum specific holidays Verifying - 2013-01-01 +// String jan1=LocalDate.of(2013, 01, 1).toString(); +// assertTrue(tollFreeDatesService.isTollFreeDate(jan1)); +// +// // Enum specific holidays Verifying - 2013-03-28 +// String mar28=LocalDate.of(2013, 03, 28).toString(); +// assertTrue(tollFreeDatesService.isTollFreeDate(mar28)); +// } +// +// @Test +// public void isTollFreeDate_Negative_Scenarios_Test() { +// // Non Enum specific holidays Verifying - 2013-01-02 Wednesday +// String jan1=LocalDate.of(2013, 01, 2).toString(); +// assertFalse(tollFreeDatesService.isTollFreeDate(jan1)); +// +// // Non Enum specific holidays Verifying - 2013-01-03 Thursday +// String jan3=LocalDate.of(2013, 01, 3).toString(); +// assertFalse(tollFreeDatesService.isTollFreeDate(jan3)); +// +// // Passing Invalid date 2013-02-30 +// String feb30=" 2013-02-30"; +// assertThrows(CustomErrorException.class,()->tollFreeDatesService.isTollFreeDate(feb30)); +// } +// +// @Test +// public void isTollFreeVehicleTest() { +// String vehicle=""; +// assertFalse(tollFreeVehicleService.isTollFreeVehicle(vehicle)); +// +// vehicle="tractor"; +// assertTrue(tollFreeVehicleService.isTollFreeVehicle(vehicle)); +// +// vehicle="Tractor234"; +// assertFalse(tollFreeVehicleService.isTollFreeVehicle(vehicle)); +// } +} From 6f208fcaba448731670d8285cbbda917c2377130 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Thu, 5 May 2022 22:44:57 +0530 Subject: [PATCH 13/23] spring boot removed --- toll_fee_calc_app/pom.xml | 70 ++++------- .../TollFeeCalculatorApplication.java | 5 - .../configuration/SwaggerConfiguration.java | 34 ------ .../controller/TollFeeController.java | 38 ------ .../controller/TollFreeDatesController.java | 42 ------- .../TollFreeVehiclesController.java | 40 ------- .../service/impl/TollFeeServiceImplTest.java | 113 ------------------ 7 files changed, 22 insertions(+), 320 deletions(-) delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeDatesController.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeVehiclesController.java delete mode 100644 toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java diff --git a/toll_fee_calc_app/pom.xml b/toll_fee_calc_app/pom.xml index a0645ce8..28fc6553 100644 --- a/toll_fee_calc_app/pom.xml +++ b/toll_fee_calc_app/pom.xml @@ -5,64 +5,38 @@ com.evolve-technology.161602 toll_free_calc_app 0.0.1-SNAPSHOT - - org.springframework.boot - spring-boot-starter-parent - 2.4.0 - - + + UTF-8 + 11 + 11 + 5.3.19 + - org.springframework.boot - spring-boot-starter-web + org.springframework + spring-core + ${spring-version} - org.springframework.boot - spring-boot-starter - - - org.springframework.boot - spring-boot-starter-logging - - - - - org.springframework.boot - spring-boot-starter-validation - - - org.springframework.boot - spring-boot-starter-test - - - io.springfox - springfox-swagger2 - 2.6.1 - - - io.springfox - springfox-swagger-ui - 2.6.1 - - - org.springframework.boot - spring-boot-devtools - runtime - - + org.springframework + spring-context + ${spring-version} + org.projectlombok lombok true + 1.18.20 + + + org.apache.logging.log4j + log4j-core + 2.17.2 - - - org.springframework.boot - spring-boot-starter-log4j2 + org.apache.logging.log4j + log4j-api + 2.17.2 - diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java index 689839e3..7adfa949 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java @@ -1,14 +1,9 @@ package com.evolve_technology.calculator; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication public class TollFeeCalculatorApplication { public static void main(String[] args) { // TODO Auto-generated method stub - SpringApplication.run(TollFeeCalculatorApplication.class, args); } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java deleted file mode 100644 index fef0447a..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/configuration/SwaggerConfiguration.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.evolve_technology.calculator.configuration; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger2.annotations.EnableSwagger2; - -@Configuration -@EnableSwagger2 -public class SwaggerConfiguration extends WebMvcConfigurerAdapter { - @Bean - public Docket api() { - return new Docket(DocumentationType.SWAGGER_2) - .select() - .apis(RequestHandlerSelectors.any()) - .paths(PathSelectors.any()) - .build(); - } - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("swagger-ui.html") - .addResourceLocations("classpath:/META-INF/resources/"); - - registry.addResourceHandler("/webjars/**") - .addResourceLocations("classpath:/META-INF/resources/webjars/"); - } -} \ No newline at end of file diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java deleted file mode 100644 index 0d711590..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFeeController.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.evolve_technology.calculator.controller; - -import java.util.Map; - -import javax.validation.constraints.Pattern; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import com.evolve_technology.calculator.controller.model.TollFee; -import com.evolve_technology.calculator.service.TollFeeService; - -@RestController("/tollfee") -@Validated -public class TollFeeController { - - @Autowired - TollFeeService tollFeeService; - - @GetMapping("/vehicles/{name}/dates/{date}") - public ResponseEntity getTollFee(@PathVariable("name") String name,@PathVariable("date") - @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(00|0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[0-5][0-9]):(0[0-9]|[0-5][0-9])$") - String date) { - TollFee tollFee= tollFeeService.getTollFee(date, name); - return new ResponseEntity<>(tollFee,HttpStatus.OK); - } - - @GetMapping("/records") - public Map>> getTollFeeHistoricalRecords(){ - return tollFeeService.getHistoricalTollRecords(); - } - -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeDatesController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeDatesController.java deleted file mode 100644 index 4a025edb..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeDatesController.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.evolve_technology.calculator.controller; - -import java.util.List; - -import javax.validation.constraints.Pattern; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import com.evolve_technology.calculator.controller.model.TollFreeDateDTO; -import com.evolve_technology.calculator.service.TollFreeDatesService; - - -@RestController -@Validated -public class TollFreeDatesController { - - @Autowired - TollFreeDatesService tollFreeDatesService; - - @GetMapping("/tollfree/dates") - public ResponseEntity> getTollFreeDates(){ - List list=tollFreeDatesService.getTollFreeDates(); - return new ResponseEntity<>(list,HttpStatus.OK); - } - - @GetMapping("/tollfree/dates/{date}") - public ResponseEntity getTollFreeDateStatus(@PathVariable("date") - @Pattern(regexp = "^((20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$") String date){ - Boolean value=tollFreeDatesService.isTollFreeDate(date); - TollFreeDateDTO tollFreeDateDTO=new TollFreeDateDTO(); - tollFreeDateDTO.setDate(date); - tollFreeDateDTO.setIsTollFreeDate(value); - return new ResponseEntity<>(tollFreeDateDTO,HttpStatus.OK); - - } -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeVehiclesController.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeVehiclesController.java deleted file mode 100644 index 0e706579..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/TollFreeVehiclesController.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.evolve_technology.calculator.controller; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import com.evolve_technology.calculator.controller.model.TollFreeVehicleDTO; -import com.evolve_technology.calculator.service.TollFreeVehicleService; - - -@RestController -@Validated -public class TollFreeVehiclesController { - - @Autowired - TollFreeVehicleService tollFreeVehicleService; - - @GetMapping("/tollfree/vehicles") - public ResponseEntity> getTollFreeVehicles(){ - List list=tollFreeVehicleService.getTollFreeVehicles(); - return new ResponseEntity<>(list,HttpStatus.OK); - - } - - @GetMapping("/tollfree/vehicles/{name}") - public ResponseEntity getTollFreeVehicleStatusByName(@PathVariable("name") String name){ - Boolean value=tollFreeVehicleService.isTollFreeVehicle(name); - TollFreeVehicleDTO tollFreeVehicleDTO=new TollFreeVehicleDTO(); - tollFreeVehicleDTO.setName(name); - tollFreeVehicleDTO.setTollFreeVehicle(value); - return new ResponseEntity<>(tollFreeVehicleDTO,HttpStatus.OK); - - } -} diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java deleted file mode 100644 index f5b0722e..00000000 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.evolve_technology.calculator.service.impl; - -import static org.junit.Assert.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - -import java.util.List; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.test.context.TestPropertySource; - -import com.evolve_technology.calculator.service.TollFeeService; -import com.evolve_technology.calculator.service.TollFreeDatesService; -import com.evolve_technology.calculator.service.TollFreeVehicleService; - -@TestPropertySource -public class TollFeeServiceImplTest { - - @InjectMocks - TollFeeService tollFeeService; - - @Mock - TollFreeDatesService tollFreeDatesService; - - @Mock - TollFreeVehicleService tollFreeVehicleService; - - @Value("${tollfree.year}") - private String year; - @Value("${tollfree.dates}") - private List dates; - @Value("${tollfree.vehicles}") - private List vehicles; - @Value("${value.months}") - private List months; - - @Before - public void init() { - Mockito.when(tollFreeVehicleService.getTollFreeVehicles()).thenReturn(vehicles); - Mockito.when(tollFreeDatesService.getTollFreeDates()).thenReturn(dates); - MockitoAnnotations.initMocks(this); - } - - @Test - public void getTollFreeVehiclesTest(){ - assertNotNull(tollFreeVehicleService.getTollFreeVehicles()); - assertDoesNotThrow(()->tollFreeVehicleService.getTollFreeVehicles()); - } - - @Test - public void getTollFreeDatesTest() { - assertNotNull(tollFreeDatesService.getTollFreeDates()); - assertDoesNotThrow(()->tollFreeDatesService.getTollFreeDates()); - } - -// @Test -// public void isTollFreeDate_Positive_Scenarios_Test() { -// // JULY MONTH -// String july1=LocalDate.of(2013, 07, 01).toString(); -// assertTrue(tollFreeDatesService.isTollFreeDate(july1)); -// -// String july21=LocalDate.of(2013, 07, 21).toString(); -// assertTrue(tollFreeDatesService.isTollFreeDate(july21)); -// -// // Saturday verifying -// String june_saturday=LocalDate.of(2013, 06, 8).toString(); -// assertTrue(tollFreeDatesService.isTollFreeDate(june_saturday)); -// -// // sunday Verifying -// String june_sunday=LocalDate.of(2013, 06, 9).toString(); -// assertTrue(tollFreeDatesService.isTollFreeDate(june_sunday)); -// -// // Enum specific holidays Verifying - 2013-01-01 -// String jan1=LocalDate.of(2013, 01, 1).toString(); -// assertTrue(tollFreeDatesService.isTollFreeDate(jan1)); -// -// // Enum specific holidays Verifying - 2013-03-28 -// String mar28=LocalDate.of(2013, 03, 28).toString(); -// assertTrue(tollFreeDatesService.isTollFreeDate(mar28)); -// } -// -// @Test -// public void isTollFreeDate_Negative_Scenarios_Test() { -// // Non Enum specific holidays Verifying - 2013-01-02 Wednesday -// String jan1=LocalDate.of(2013, 01, 2).toString(); -// assertFalse(tollFreeDatesService.isTollFreeDate(jan1)); -// -// // Non Enum specific holidays Verifying - 2013-01-03 Thursday -// String jan3=LocalDate.of(2013, 01, 3).toString(); -// assertFalse(tollFreeDatesService.isTollFreeDate(jan3)); -// -// // Passing Invalid date 2013-02-30 -// String feb30=" 2013-02-30"; -// assertThrows(CustomErrorException.class,()->tollFreeDatesService.isTollFreeDate(feb30)); -// } -// -// @Test -// public void isTollFreeVehicleTest() { -// String vehicle=""; -// assertFalse(tollFreeVehicleService.isTollFreeVehicle(vehicle)); -// -// vehicle="tractor"; -// assertTrue(tollFreeVehicleService.isTollFreeVehicle(vehicle)); -// -// vehicle="Tractor234"; -// assertFalse(tollFreeVehicleService.isTollFreeVehicle(vehicle)); -// } -} From 5a2bf507debd70c6d5fbd3394a9d83bf50c9639b Mon Sep 17 00:00:00 2001 From: Rizwan Date: Wed, 11 May 2022 11:37:00 +0530 Subject: [PATCH 14/23] code refactored. --- toll_fee_calc_app/pom.xml | 11 ++ .../exception/CustomErrorException.java | 1 + .../calculator/exception/ErrorResponse.java | 4 +- .../properties/TollConfiguration.java | 2 - .../calculator/service/TollFeeService.java | 7 +- .../service/impl/TollFeeServiceImpl.java | 176 +++++------------- .../calculator/util/TollRules.java | 26 +++ .../calculator/util/TollUtil.java | 27 +++ 8 files changed, 113 insertions(+), 141 deletions(-) create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollRules.java create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java diff --git a/toll_fee_calc_app/pom.xml b/toll_fee_calc_app/pom.xml index 28fc6553..4ce806e1 100644 --- a/toll_fee_calc_app/pom.xml +++ b/toll_fee_calc_app/pom.xml @@ -21,6 +21,11 @@ org.springframework spring-context ${spring-version} + + + org.springframework + spring-web + ${spring-version} org.projectlombok @@ -38,5 +43,11 @@ log4j-api 2.17.2 + + javax.validation + validation-api + 2.0.1.Final + + diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java index 8dfd1e09..c44a1ff0 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java @@ -1,5 +1,6 @@ package com.evolve_technology.calculator.exception; + import org.springframework.http.HttpStatus; import lombok.Data; diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java index 5d0ce326..45ba3597 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java @@ -4,13 +4,11 @@ import org.springframework.http.HttpStatus; -import com.fasterxml.jackson.annotation.JsonFormat; - import lombok.Data; @Data public class ErrorResponse { - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") +// @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") private Date timestamp; private int code; diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java index 68487047..da04e50e 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java @@ -3,14 +3,12 @@ import java.util.List; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import lombok.Data; @Data @Component -@ConfigurationProperties(prefix="tollfree") public class TollConfiguration { @Value("${year}") diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java index 00e077e6..cce2a6be 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java @@ -1,12 +1,11 @@ package com.evolve_technology.calculator.service; -import java.util.Map; +import java.time.LocalDateTime; +import java.util.List; import com.evolve_technology.calculator.controller.model.TollFee; public interface TollFeeService { - public TollFee getTollFee(final String date, String vehicle); - public Map>> getHistoricalTollRecords(); - + public Integer getTollFee(final List list, String vehicle); } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index ae81d5fb..6c832cdd 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -2,20 +2,17 @@ import java.time.LocalDate; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.evolve_technology.calculator.controller.model.TollFee; import com.evolve_technology.calculator.service.TollFeeService; -import com.evolve_technology.calculator.service.TollFreeDatesService; -import com.evolve_technology.calculator.service.TollFreeVehicleService; +import com.evolve_technology.calculator.util.TollUtil; @Service public class TollFeeServiceImpl implements TollFeeService { @@ -23,138 +20,53 @@ public class TollFeeServiceImpl implements TollFeeService { private static final Logger logger = LogManager.getLogger(TollFeeServiceImpl.class); @Autowired - TollFreeDatesService tollFreeDatesService; + TollUtil tollUtil; - @Autowired - TollFreeVehicleService tollFreeVehicleService; - - private static Map>> aggregateTollMap = new HashMap<>(); + private static Map> tollMap=new HashMap<>(); - private static boolean vehicleExists = false; - - @Override - public TollFee getTollFee(String date, String vehicle) { - - logger.info("Inside getTollFee method :: date = {} and vehicle = {}", date, vehicle); - - DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; - LocalDateTime inputDate = LocalDateTime.parse(date, formatter); - int hour = inputDate.getHour(); - int minute = inputDate.getMinute(); - LocalDate localDate = LocalDate.of(inputDate.getYear(), inputDate.getMonthValue(), inputDate.getDayOfMonth()); - - logger.info(" aggregateTollMap = {} ", aggregateTollMap); - - Integer newFee = tollCalculate(vehicle, localDate, hour, minute); - if (aggregateTollMap.containsKey(vehicle)) { - vehicleExists = true; - } - Map> aggregateTollMapValue = aggregateTollMap.computeIfAbsent(vehicle, k -> { - logger.info("Vehicle {} has crossed the toll first time ", vehicle); - Map> outerMap = new HashMap<>(); - Map innerMap = new HashMap<>(); - innerMap.put(hour, newFee); - outerMap.put(localDate.toString(), innerMap); - return outerMap; - }); - logger.info("key = {} , value = {} ", vehicle, aggregateTollMapValue); - - if (vehicleExists) { - Map> aggregateTollMapUpdatedValue = aggregateTollMap.computeIfPresent(vehicle, - (k, outerMap) -> { - Map innerMap = outerMap.get(localDate.toString()); - if (innerMap == null) { - logger.info("Vehicle {} has not crossed the toll on day {}", vehicle, localDate); - innerMap = new HashMap<>(); - innerMap.put(hour, newFee); - outerMap.put(localDate.toString(), innerMap); - } else { - logger.info("Vehicle {} has crossed the toll on day {}", vehicle, localDate); - Integer existingFee = innerMap.get(hour); - if (existingFee == null) { - logger.info("Vehicle {} has crossed the toll on day {} but not at hour {}", vehicle, - localDate, hour); - innerMap.put(hour, newFee); - outerMap.put(localDate.toString(), innerMap); - } - if (existingFee != null && existingFee < newFee) { - logger.info( - "Vehicle {} has crossed the toll on day {} at hour {} with oldFee {} and new Fee {}", - vehicle, localDate, hour, existingFee, newFee); - innerMap.put(hour, newFee); - outerMap.put(localDate.toString(), innerMap); - } - } - return outerMap; - }); - logger.info("updated value = {} ", aggregateTollMapUpdatedValue); - } - return buildResult( vehicle, localDate.toString()); - } - - private TollFee buildResult( String vehicle,String date) { - TollFee tollFee = new TollFee(); - tollFee.setVehicle(vehicle); - tollFee.setDate(date); - tollFee.setTotalAmount(0); - aggregateTollMap.computeIfPresent(vehicle, (k, v) -> { - Map innerMap = v.get(date); - if (innerMap != null) { - int amount = innerMap.entrySet().stream().map(x -> x.getValue()) - .collect(Collectors.summingInt(Integer::intValue)); - if (amount < 60) { - tollFee.setTotalAmount(amount); - } else { - tollFee.setTotalAmount(60); - } - tollFee.setTravelRecordsWithinDay(innerMap.entrySet().stream().collect(Collectors.toMap(x -> { - int key = x.getKey(); - String modifiedKey = String.format( - "vehicle = %1$s passed the toll booth on date = %2$s at hour = %3$s", vehicle, date, - Integer.toString(key)); - return modifiedKey; - }, y -> { - int value = y.getValue(); - String modifiedValue = String.format("Amount charged = %1$s ", Integer.toString(value)); - return modifiedValue; - }))); + public Integer getTollFee(List inputDates,String vehicle) { + logger.info("Inside getTollFee method :: inputDates = {} and vehicle = {}", inputDates, vehicle); + for(LocalDateTime localDateTime : inputDates) { + int hour = localDateTime.getHour(); + int minute = localDateTime.getMinute(); + LocalDate localDate = LocalDate.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth()); + Integer newFee = tollUtil.tollCompute(vehicle, localDate, hour, minute); + if(!tollMap.containsKey(localDate)) { + Map innerMap=tollMap.computeIfAbsent(localDate, k->{ + logger.info("Vehicle {} has crossed the toll first time on date {} ", vehicle ,localDate); + Map map = new HashMap<>(); + map.put(hour, newFee); + return map; + }); + logger.info("key = {} , value = {} ", localDate, innerMap); + }else { + Map innerMapExisting = tollMap.computeIfPresent(localDate, + (k, outerMap) -> { + logger.info("Vehicle {} has crossed the toll on day {}", vehicle, localDate); + Integer existingFee = outerMap.get(hour); + if (existingFee == null) { + logger.info("Vehicle {} has crossed the toll on day {} but not at hour {}", vehicle, + localDate, hour); + outerMap.put(hour, newFee); + } + if (existingFee != null && existingFee < newFee) { + logger.info( + "Vehicle {} has crossed the toll on day {} at hour {} with oldFee {} and new Fee {}", + vehicle, localDate, hour, existingFee, newFee); + outerMap.put(hour, newFee); + } + + return outerMap; + }); + logger.info("updated value = {} ", innerMapExisting); } - return v; - }); - - return tollFee; + } + return 0; } - - private int tollCalculate(String vehicle, LocalDate localDate, int hour, int minute) { - if (tollFreeDatesService.isTollFreeDate(localDate.toString()) - || tollFreeVehicleService.isTollFreeVehicle(vehicle)) - return 0; - if (hour == 6 && minute >= 0 && minute <= 29) - return 8; - else if (hour == 6 && minute >= 30 && minute <= 59) - return 13; - else if (hour == 7 && minute >= 0 && minute <= 59) - return 18; - else if (hour == 8 && minute >= 0 && minute <= 29) - return 13; - else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) - return 8; - else if (hour == 15 && minute >= 0 && minute <= 29) - return 13; - else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) - return 18; - else if (hour == 17 && minute >= 0 && minute <= 59) - return 13; - else if (hour == 18 && minute >= 0 && minute <= 29) - return 8; - else - return 0; + + public int process(Map> tollMap) { + return 0; } - @Override - public Map>> getHistoricalTollRecords() { - // TODO Auto-generated method stub - return aggregateTollMap; - } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollRules.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollRules.java new file mode 100644 index 00000000..44d4dc63 --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollRules.java @@ -0,0 +1,26 @@ +package com.evolve_technology.calculator.util; + +public class TollRules { + public static int getHourlyRate(int hour, int minute) { + if (hour == 6 && minute >= 0 && minute <= 29) + return 8; + else if (hour == 6 && minute >= 30 && minute <= 59) + return 13; + else if (hour == 7 && minute >= 0 && minute <= 59) + return 18; + else if (hour == 8 && minute >= 0 && minute <= 29) + return 13; + else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) + return 8; + else if (hour == 15 && minute >= 0 && minute <= 29) + return 13; + else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) + return 18; + else if (hour == 17 && minute >= 0 && minute <= 59) + return 13; + else if (hour == 18 && minute >= 0 && minute <= 29) + return 8; + else + return 0; + } +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java new file mode 100644 index 00000000..60baa3f2 --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java @@ -0,0 +1,27 @@ +package com.evolve_technology.calculator.util; + +import java.time.LocalDate; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.evolve_technology.calculator.service.TollFreeDatesService; +import com.evolve_technology.calculator.service.TollFreeVehicleService; + +@Component +public class TollUtil { + @Autowired + TollFreeDatesService tollFreeDatesService; + + @Autowired + TollFreeVehicleService tollFreeVehicleService; + + + public int tollCompute(String vehicle, LocalDate localDate, int hour, int minute) { + if (tollFreeDatesService.isTollFreeDate(localDate.toString()) + || tollFreeVehicleService.isTollFreeVehicle(vehicle)) + return 0; + return TollRules.getHourlyRate(hour,minute); + } + +} From ff72da82ea608f92ac55ceb912b925601e4d8355 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Thu, 12 May 2022 17:15:13 +0530 Subject: [PATCH 15/23] Final Push --- toll_fee_calc_app/pom.xml | 69 ++++++ .../TollFeeCalculatorApplication.java | 12 +- .../properties/TollConfiguration.java | 13 ++ .../service/impl/TollFeeServiceImpl.java | 20 +- .../impl/TollFreeVehiclesServiceImpl.java | 2 +- .../src/main/resources/application.properties | 6 +- .../calculator/config/AppConfig.java | 81 +++++++ .../service/impl/TollFeeServiceImplTest.java | 203 ++++++++++++++++++ .../src/test/resources/application.properties | 13 ++ 9 files changed, 406 insertions(+), 13 deletions(-) create mode 100644 toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java create mode 100644 toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java create mode 100644 toll_fee_calc_app/src/test/resources/application.properties diff --git a/toll_fee_calc_app/pom.xml b/toll_fee_calc_app/pom.xml index 4ce806e1..5b86dfdb 100644 --- a/toll_fee_calc_app/pom.xml +++ b/toll_fee_calc_app/pom.xml @@ -12,6 +12,53 @@ 5.3.19 + + org.junit.jupiter + junit-jupiter-engine + 5.5.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.5.2 + test + + + org.junit.jupiter + junit-jupiter-params + 5.5.2 + test + + + org.junit.platform + junit-platform-suite + 1.8.1 + test + + + + org.junit.platform + junit-platform-launcher + 1.3.2 + test + + + org.mockito + mockito-junit-jupiter + 3.2.4 + test + + + org.springframework + spring-test + ${spring-version} + org.springframework spring-core @@ -50,4 +97,26 @@ + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M3 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + + diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java index 7adfa949..78d0d3eb 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java @@ -1,10 +1,18 @@ package com.evolve_technology.calculator; -public class TollFeeCalculatorApplication { +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +import com.evolve_technology.calculator.properties.TollConfiguration; + +public class TollFeeCalculatorApplication { + @Autowired + TollConfiguration tollConfiguration; + public static void main(String[] args) { // TODO Auto-generated method stub - } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java index da04e50e..c75b5787 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java @@ -3,18 +3,31 @@ import java.util.List; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.stereotype.Component; import lombok.Data; @Data @Component +@Configuration public class TollConfiguration { @Value("${year}") private String year; + @Value("#{'${dates}'.split(',')}") private List dates; + @Value("#{'${vehicles}'.split(',')}") private List vehicles; + @Value("#{'${months}'.split(',')}") private List months; + + @Bean + public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { + return new PropertySourcesPlaceholderConfigurer(); + } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index 6c832cdd..1db33528 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -14,7 +15,7 @@ import com.evolve_technology.calculator.service.TollFeeService; import com.evolve_technology.calculator.util.TollUtil; -@Service +@Service() public class TollFeeServiceImpl implements TollFeeService { private static final Logger logger = LogManager.getLogger(TollFeeServiceImpl.class); @@ -22,7 +23,7 @@ public class TollFeeServiceImpl implements TollFeeService { @Autowired TollUtil tollUtil; - private static Map> tollMap=new HashMap<>(); + private Map> tollMap=new HashMap<>(); public Integer getTollFee(List inputDates,String vehicle) { logger.info("Inside getTollFee method :: inputDates = {} and vehicle = {}", inputDates, vehicle); @@ -61,12 +62,17 @@ public Integer getTollFee(List inputDates,String vehicle) { logger.info("updated value = {} ", innerMapExisting); } } - return 0; + return process(); } - public int process(Map> tollMap) { - return 0; + public int process() { + Map tollRecords=new HashMap<>(); + tollMap.keySet().forEach(k->{ + int sum=tollMap.get(k).values().stream().collect(Collectors.summingInt(Integer::intValue)); + tollRecords.put(k, sum>60 ? 60 : sum ); + }); + logger.info("tollMap {} :: "+tollMap); + logger.info("tollRecords {} :: "+tollRecords); + return tollRecords.values().stream().collect(Collectors.summingInt(Integer::intValue)); } - - } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java index 90981a49..b0dc02fc 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java @@ -27,7 +27,7 @@ public List getTollFreeVehicles() { public boolean isTollFreeVehicle(String vehicle) { logger.info("Inside isTollFreeVehicle method :: vehicle = {} ",vehicle); List vehicles=getTollFreeVehicles(); - return vehicles.stream().filter(v->v.equalsIgnoreCase(vehicle)).findAny().isPresent(); + return vehicles.stream().filter(v->v.trim().equalsIgnoreCase(vehicle)).findAny().isPresent(); } diff --git a/toll_fee_calc_app/src/main/resources/application.properties b/toll_fee_calc_app/src/main/resources/application.properties index ca7d1511..37ded940 100644 --- a/toll_fee_calc_app/src/main/resources/application.properties +++ b/toll_fee_calc_app/src/main/resources/application.properties @@ -4,10 +4,10 @@ server.servlet.context-path=/v1/api year=2013 # Toll Free Dates -tollfree.dates=2013-01-01 , 2013-03-28 , 2013-03-29 , 2013-04-01 , 2013-04-30 , 2013-05-01 , 2013-05-08 , 2013-05-09 , 2013-06-05 , 2013-06-06 , 2013-06-21 , 2013-11-01 , 2013-12-24 , 2013-12-25 , 2013-12-26 , 2013-12-31 +dates=2013-01-01 , 2013-03-28 , 2013-03-29 , 2013-04-01 , 2013-04-30 , 2013-05-01 , 2013-05-08 , 2013-05-09 , 2013-06-05 , 2013-06-06 , 2013-06-21 , 2013-11-01 , 2013-12-24 , 2013-12-25 , 2013-12-26 , 2013-12-31 # Toll Free Months -tollfree.months= JULY +months= JULY # Toll Free Vehicles -tollfree.vehicles= Motorbike , Tractor , Emergency , Diplomat , Foreign , Military \ No newline at end of file +vehicles= Motorbike , Tractor , Emergency , Diplomat , Foreign , Military \ No newline at end of file diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java new file mode 100644 index 00000000..835cd2cc --- /dev/null +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java @@ -0,0 +1,81 @@ +package com.evolve_technology.calculator.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.Scope; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.test.context.ContextConfiguration; + +import com.evolve_technology.calculator.properties.TollConfiguration; +import com.evolve_technology.calculator.service.TollFeeService; +import com.evolve_technology.calculator.service.TollFreeDatesService; +import com.evolve_technology.calculator.service.TollFreeVehicleService; +import com.evolve_technology.calculator.service.impl.TollFeeServiceImpl; +import com.evolve_technology.calculator.service.impl.TollFreeDatesServiceImpl; +import com.evolve_technology.calculator.service.impl.TollFreeVehiclesServiceImpl; +import com.evolve_technology.calculator.util.TollRules; +import com.evolve_technology.calculator.util.TollUtil; + +@ContextConfiguration +@PropertySource("classpath:application.properties") +@ComponentScan(basePackageClasses = TollConfiguration.class) +public class AppConfig { + // this bean will be injected into the OrderServiceTest class + @Bean + @Scope("prototype") + public TollFeeService tollFeeService() { + TollFeeService tollFeeService = new TollFeeServiceImpl(); + // set properties, etc. + return tollFeeService; + } + + @Bean + @Scope("prototype") + public TollFreeDatesService tollFreeDatesService() { + TollFreeDatesService tollFreeDatesService = new TollFreeDatesServiceImpl(); + // set properties, etc. + return tollFreeDatesService; + } + + @Bean + @Scope("prototype") + public TollFreeVehicleService tollFreeVehicleService() { + TollFreeVehicleService tollFreeVehicleService = new TollFreeVehiclesServiceImpl(); + // set properties, etc. + return tollFreeVehicleService; + } + + + @Bean + @Scope("prototype") + public TollRules tollRules() { + TollRules tollRules = new TollRules(); + // set properties, etc. + return tollRules; + } + + @Bean + @Scope("prototype") + public TollUtil tollUtil() { + TollUtil tollUtil = new TollUtil(); + // set properties, etc. + return tollUtil; + } + +// @Bean() +// public TollConfiguration tollConfiguration() { +// TollConfiguration tollConfiguration = new TollConfiguration(); +//// tollConfiguration.setDates(null); +//// tollConfiguration.setMonths(null); +//// tollConfiguration.setVehicles(null); +//// tollConfiguration.setYear(null); +// // set properties, etc. +// return tollConfiguration; +// } + + @Bean + public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { + return new PropertySourcesPlaceholderConfigurer(); + } +} diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java new file mode 100644 index 00000000..8e41c0c1 --- /dev/null +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java @@ -0,0 +1,203 @@ +package com.evolve_technology.calculator.service.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.evolve_technology.calculator.config.AppConfig; +import com.evolve_technology.calculator.service.TollFeeService; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(loader=AnnotationConfigContextLoader.class,classes=AppConfig.class) +@TestPropertySource(locations = "classpath:application.properties") +class TollFeeServiceImplTest { + + @Autowired + TollFeeService tollFeeService; + + /* + * Jan 2013 + * 5,12,19,26 are saturday + */ + @Test + void testGetTollFeeSaturdayJanuary2013() { + + List inputDates=Stream.of(LocalDateTime.of(2013, 01, 05, 8, 8),LocalDateTime.of(2013, 01, 12, 8, 8) + ,LocalDateTime.of(2013, 01, 19, 8, 8),LocalDateTime.of(2013, 01, 26, 8, 8)). + collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "car"), 0); + } + + /* + * Feb 2013 + * 2,9,16,23 are saturday + */ + @Test + void testGetTollFeeSaturdayFebruary2013() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 02, 8, 8),LocalDateTime.of(2013, 02, 16, 8, 8) + ,LocalDateTime.of(2013, 02, 9, 8, 8),LocalDateTime.of(2013, 02, 23, 8, 8)). + collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "car"), 0); + } + + /* + * Feb 2013 + * 4,11,18,25 are Monday + *NON Free Vehicles Test + */ + @Test + void testGetTollFeeMondayFebruary2013Car() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 8, 8),LocalDateTime.of(2013, 02, 18, 8, 8) + ,LocalDateTime.of(2013, 02, 11, 8, 8),LocalDateTime.of(2013, 02, 25, 8, 8)). + collect(Collectors.toList()); + assertNotEquals(tollFeeService.getTollFee(inputDates, "car"), 0); + + } + + /* + * Feb 2013 + * 4,11,18,25 are Monday + * Free Vehicles Test + */ + @Test + void testGetTollFeeMondayFebruary2013FreeVehicles() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 8, 8),LocalDateTime.of(2013, 02, 18, 8, 8) + ,LocalDateTime.of(2013, 02, 11, 8, 8),LocalDateTime.of(2013, 02, 25, 8, 8)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Tractor"), 0); + assertEquals(tollFeeService.getTollFee(inputDates, "Motorbike"), 0); + assertEquals(tollFeeService.getTollFee(inputDates, "Emergency"), 0); + assertEquals(tollFeeService.getTollFee(inputDates, "Diplomat"), 0); + assertEquals(tollFeeService.getTollFee(inputDates, "Foreign"), 0); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 + * As per the Rule it is charged 8. + */ + @Test + void testGetTollFeeMondayFebruary2013At6Hr10Min() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 8); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 + * As per the Rule it is charged 8. + * if (hour == 6 && minute >= 0 && minute <= 29) + return 8; + else if (hour == 6 && minute >= 30 && minute <= 59) + return 13; + */ + @Test + void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20Min() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 8); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 && 06:40 + * As per the Rule max within an hour is charged that is 13 max of(8,13) + */ + @Test + void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40Min() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20),LocalDateTime.of(2013, 02, 04, 06, 40)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 13); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 + * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 + */ + @Test + void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10Min() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 31); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 + * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 + */ + @Test + void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10Min() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 44); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 + * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 + */ + @Test + void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30Min() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) + ,LocalDateTime.of(2013, 02, 04, 8, 30)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 44); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 && 15:10 + * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 plus 13= 57 + */ + @Test + void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30MinAnd15Hr10Min() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) + ,LocalDateTime.of(2013, 02, 04, 8, 30),LocalDateTime.of(2013, 02, 04, 15, 10)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 57); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 && 15:10 && 15:30 + * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 plus + * max of (13,18) is 18= 62 but it can't go beyond 60 for a day. + */ + @Test + void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30MinAnd15Hr10MinAnd15HrMin30() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) + ,LocalDateTime.of(2013, 02, 04, 8, 30),LocalDateTime.of(2013, 02, 04, 15, 10),LocalDateTime.of(2013, 02, 04, 15, 30)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 60); + } +} diff --git a/toll_fee_calc_app/src/test/resources/application.properties b/toll_fee_calc_app/src/test/resources/application.properties new file mode 100644 index 00000000..37ded940 --- /dev/null +++ b/toll_fee_calc_app/src/test/resources/application.properties @@ -0,0 +1,13 @@ +server.servlet.context-path=/v1/api + +# Year +year=2013 + +# Toll Free Dates +dates=2013-01-01 , 2013-03-28 , 2013-03-29 , 2013-04-01 , 2013-04-30 , 2013-05-01 , 2013-05-08 , 2013-05-09 , 2013-06-05 , 2013-06-06 , 2013-06-21 , 2013-11-01 , 2013-12-24 , 2013-12-25 , 2013-12-26 , 2013-12-31 + +# Toll Free Months +months= JULY + +# Toll Free Vehicles +vehicles= Motorbike , Tractor , Emergency , Diplomat , Foreign , Military \ No newline at end of file From 4b88731fff2460ac1edcafb25adcf0003619d0f2 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Thu, 12 May 2022 17:31:35 +0530 Subject: [PATCH 16/23] More Testing. --- .../service/impl/TollFeeServiceImplTest.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java index 8e41c0c1..1904074c 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java @@ -200,4 +200,51 @@ void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 60); } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 && 15:10 && 15:30 + * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 plus + * max of (13,18) is 18= 62 but it can't go beyond 60 for a day. + * + * And Feb 2013 Tuesday 5th + * 05-02-2013 06:10 + * + * Monday=60 plus Tuesday=8= 68 + */ + @Test + void testGetTollFeeMondayFebruary2013MultipleDatesMondayPlusTuesday() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) + ,LocalDateTime.of(2013, 02, 04, 8, 30),LocalDateTime.of(2013, 02, 04, 15, 10),LocalDateTime.of(2013, 02, 04, 15, 30), + LocalDateTime.of(2013, 02, 05, 6, 10)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 68); + } + + /* + * Feb 2013 Monday 4th + * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 && 15:10 && 15:30 + * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 plus + * max of (13,18) is 18= 62 but it can't go beyond 60 for a day. + * + * And Feb 2013 Tuesday 5th + * 05-02-2013 06:10 + * + * And Feb 2013 Wednesday 6th + * 06-02-2013 17:10 && 18:10 + * + * Monday=60 plus Tuesday=8 plus Wednesday= 13 plus 8 =89 + */ + @Test + void testGetTollFeeMondayFebruary2013MultipleDatesMondayPlusTuesdayPlusWednesday() { + List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) + ,LocalDateTime.of(2013, 02, 04, 8, 30),LocalDateTime.of(2013, 02, 04, 15, 10),LocalDateTime.of(2013, 02, 04, 15, 30), + LocalDateTime.of(2013, 02, 05, 6, 10),LocalDateTime.of(2013, 02, 06, 17, 10),LocalDateTime.of(2013, 02, 06, 18, 10)). + collect(Collectors.toList()); + + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 89); + } } From 0884e03e4d520a0d3bfffe30b2493714eefbf22b Mon Sep 17 00:00:00 2001 From: Rizwan Date: Thu, 12 May 2022 17:47:32 +0530 Subject: [PATCH 17/23] code refactored. --- .../calculator/controller/model/TollDetails.java | 12 ------------ .../calculator/controller/model/TollFee.java | 13 ------------- .../calculator/controller/model/TollFeeDTO.java | 13 ------------- .../controller/model/TollFreeDateDTO.java | 11 ----------- .../controller/model/TollFreeVehicleDTO.java | 11 ----------- .../calculator/service/TollFeeService.java | 2 -- 6 files changed, 62 deletions(-) delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollDetails.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFee.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollDetails.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollDetails.java deleted file mode 100644 index c50ed008..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollDetails.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.evolve_technology.calculator.controller.model; - -import java.util.Map; - -import lombok.Data; - -@Data -class TollDetailsPerDay{ - String date; - int totalAmount; - Map travelRecordsHourlyCharged; -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFee.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFee.java deleted file mode 100644 index b8c99d9e..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFee.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.evolve_technology.calculator.controller.model; - -import java.util.Map; - -import lombok.Data; - -@Data -public class TollFee { - String vehicle; - String date; - int totalAmount; - Map travelRecordsWithinDay; -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java deleted file mode 100644 index 5627294f..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFeeDTO.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.evolve_technology.calculator.controller.model; - -import java.util.List; - -import lombok.Data; - -@Data -public class TollFeeDTO { - String vehicle; - List tollDetails; -} - - diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java deleted file mode 100644 index 4bf75a48..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeDateDTO.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.evolve_technology.calculator.controller.model; - -import lombok.Data; - -@Data -public class TollFreeDateDTO { - String date; - Boolean isTollFreeDate; - - -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java deleted file mode 100644 index 91494954..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/model/TollFreeVehicleDTO.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.evolve_technology.calculator.controller.model; - -import lombok.Data; - -@Data -public class TollFreeVehicleDTO { - String name; - boolean isTollFreeVehicle; - - -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java index cce2a6be..368d016c 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFeeService.java @@ -3,8 +3,6 @@ import java.time.LocalDateTime; import java.util.List; -import com.evolve_technology.calculator.controller.model.TollFee; - public interface TollFeeService { public Integer getTollFee(final List list, String vehicle); From 03c344ab803fa378867d6fcce702c8a730456517 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Thu, 12 May 2022 17:53:52 +0530 Subject: [PATCH 18/23] code cleanup. --- toll_fee_calc_app/pom.xml | 12 ------------ .../calculator/config/AppConfig.java | 10 ---------- .../src/test/resources/application.properties | 13 ------------- 3 files changed, 35 deletions(-) delete mode 100644 toll_fee_calc_app/src/test/resources/application.properties diff --git a/toll_fee_calc_app/pom.xml b/toll_fee_calc_app/pom.xml index 5b86dfdb..12e02831 100644 --- a/toll_fee_calc_app/pom.xml +++ b/toll_fee_calc_app/pom.xml @@ -36,24 +36,12 @@ 1.8.1 test - org.junit.platform junit-platform-launcher 1.3.2 test - - org.mockito - mockito-junit-jupiter - 3.2.4 - test - org.springframework spring-test diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java index 835cd2cc..5b241085 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java @@ -63,16 +63,6 @@ public TollUtil tollUtil() { return tollUtil; } -// @Bean() -// public TollConfiguration tollConfiguration() { -// TollConfiguration tollConfiguration = new TollConfiguration(); -//// tollConfiguration.setDates(null); -//// tollConfiguration.setMonths(null); -//// tollConfiguration.setVehicles(null); -//// tollConfiguration.setYear(null); -// // set properties, etc. -// return tollConfiguration; -// } @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { diff --git a/toll_fee_calc_app/src/test/resources/application.properties b/toll_fee_calc_app/src/test/resources/application.properties deleted file mode 100644 index 37ded940..00000000 --- a/toll_fee_calc_app/src/test/resources/application.properties +++ /dev/null @@ -1,13 +0,0 @@ -server.servlet.context-path=/v1/api - -# Year -year=2013 - -# Toll Free Dates -dates=2013-01-01 , 2013-03-28 , 2013-03-29 , 2013-04-01 , 2013-04-30 , 2013-05-01 , 2013-05-08 , 2013-05-09 , 2013-06-05 , 2013-06-06 , 2013-06-21 , 2013-11-01 , 2013-12-24 , 2013-12-25 , 2013-12-26 , 2013-12-31 - -# Toll Free Months -months= JULY - -# Toll Free Vehicles -vehicles= Motorbike , Tractor , Emergency , Diplomat , Foreign , Military \ No newline at end of file From 33aa870c560ee60bee9720d0455fae54135b5358 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Thu, 12 May 2022 18:27:39 +0530 Subject: [PATCH 19/23] corner cases. --- .../service/impl/TollFeeServiceImpl.java | 4 ++++ .../service/impl/TollFeeServiceImplTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index 1db33528..195e2430 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -10,8 +10,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import com.evolve_technology.calculator.exception.CustomErrorException; import com.evolve_technology.calculator.service.TollFeeService; import com.evolve_technology.calculator.util.TollUtil; @@ -26,6 +28,8 @@ public class TollFeeServiceImpl implements TollFeeService { private Map> tollMap=new HashMap<>(); public Integer getTollFee(List inputDates,String vehicle) { + if(inputDates==null || vehicle==null) + throw new CustomErrorException(HttpStatus.BAD_REQUEST, "inputDates and vehicle must not be null "); logger.info("Inside getTollFee method :: inputDates = {} and vehicle = {}", inputDates, vehicle); for(LocalDateTime localDateTime : inputDates) { int hour = localDateTime.getHour(); diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java index 1904074c..15457ec5 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.time.LocalDateTime; import java.util.List; @@ -17,6 +18,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; import com.evolve_technology.calculator.config.AppConfig; +import com.evolve_technology.calculator.exception.CustomErrorException; import com.evolve_technology.calculator.service.TollFeeService; @ExtendWith(SpringExtension.class) @@ -40,6 +42,22 @@ void testGetTollFeeSaturdayJanuary2013() { assertEquals(tollFeeService.getTollFee(inputDates, "car"), 0); } + /* + * Null as inputs + */ + @Test + void testGetTollFeeWithNullInputs() { + // Both input null + assertThrows(CustomErrorException.class,()->tollFeeService.getTollFee(null, null)); + + //only vehicle null + List inputDates=Stream.of(LocalDateTime.of(2013, 01, 05, 8, 8),LocalDateTime.of(2013, 01, 12, 8, 8) + ,LocalDateTime.of(2013, 01, 19, 8, 8),LocalDateTime.of(2013, 01, 26, 8, 8)). + collect(Collectors.toList()); + + assertThrows(CustomErrorException.class,()->tollFeeService.getTollFee(inputDates, null)); + } + /* * Feb 2013 * 2,9,16,23 are saturday From dc5f95d24a9b9e7e14bf8f772146e22c50030512 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Thu, 12 May 2022 20:40:46 +0530 Subject: [PATCH 20/23] code changes. --- .../calculator/service/impl/TollFeeServiceImpl.java | 4 ++-- .../calculator/service/impl/TollFeeServiceImplTest.java | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index 195e2430..434efb68 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -28,8 +28,8 @@ public class TollFeeServiceImpl implements TollFeeService { private Map> tollMap=new HashMap<>(); public Integer getTollFee(List inputDates,String vehicle) { - if(inputDates==null || vehicle==null) - throw new CustomErrorException(HttpStatus.BAD_REQUEST, "inputDates and vehicle must not be null "); + if(inputDates==null || vehicle==null || inputDates.isEmpty() || vehicle.isBlank()) + throw new CustomErrorException(HttpStatus.BAD_REQUEST, "inputDates and vehicle must not be null or empty. "); logger.info("Inside getTollFee method :: inputDates = {} and vehicle = {}", inputDates, vehicle); for(LocalDateTime localDateTime : inputDates) { int hour = localDateTime.getHour(); diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java index 15457ec5..6cc9ab87 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -56,6 +57,9 @@ void testGetTollFeeWithNullInputs() { collect(Collectors.toList()); assertThrows(CustomErrorException.class,()->tollFeeService.getTollFee(inputDates, null)); + + List inputDatesEmpty=new ArrayList<>(); + assertThrows(CustomErrorException.class,()->tollFeeService.getTollFee(inputDatesEmpty, "")); } /* From 17998efea15ef732361b6c9a5ed32c68fb718735 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Fri, 13 May 2022 22:09:20 +0530 Subject: [PATCH 21/23] Review Comment Addressed- 1. Spring dependency removed Manual Autowiring of components enabled 2. Reduction from 2 to 1 Map. 3. LocalDateTime efficient use --- toll_fee_calc_app/pom.xml | 26 ++----- .../TollFeeCalculatorApplication.java | 9 --- .../CustomErrorHandlingControllerAdvice.java | 62 ---------------- .../exception/CustomErrorException.java | 11 +-- .../calculator/exception/ErrorResponse.java | 66 ----------------- .../properties/TollConfiguration.java | 63 +++++++++++----- .../service/impl/TollFeeServiceImpl.java | 25 +++---- .../impl/TollFreeDatesServiceImpl.java | 8 +-- .../impl/TollFreeVehiclesServiceImpl.java | 7 +- .../calculator/util/TollUtil.java | 13 ++-- .../calculator/config/AppConfig.java | 71 ------------------- .../service/impl/TollFeeServiceImplTest.java | 17 ++--- 12 files changed, 85 insertions(+), 293 deletions(-) delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java delete mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java delete mode 100644 toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java diff --git a/toll_fee_calc_app/pom.xml b/toll_fee_calc_app/pom.xml index 12e02831..45d720a2 100644 --- a/toll_fee_calc_app/pom.xml +++ b/toll_fee_calc_app/pom.xml @@ -12,6 +12,12 @@ 5.3.19 + + junit + junit + 4.11 + test + org.junit.jupiter junit-jupiter-engine @@ -42,26 +48,6 @@ 1.3.2 test - - org.springframework - spring-test - ${spring-version} - - - org.springframework - spring-core - ${spring-version} - - - org.springframework - spring-context - ${spring-version} - - - org.springframework - spring-web - ${spring-version} - org.projectlombok lombok diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java index 78d0d3eb..65036443 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java @@ -1,15 +1,6 @@ package com.evolve_technology.calculator; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -import com.evolve_technology.calculator.properties.TollConfiguration; - - public class TollFeeCalculatorApplication { - @Autowired - TollConfiguration tollConfiguration; public static void main(String[] args) { // TODO Auto-generated method stub diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java deleted file mode 100644 index 80d19a9a..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/controller/advice/CustomErrorHandlingControllerAdvice.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.evolve_technology.calculator.controller.advice; - -import java.io.PrintWriter; -import java.io.StringWriter; - -import javax.validation.ConstraintViolationException; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -import com.evolve_technology.calculator.exception.CustomErrorException; -import com.evolve_technology.calculator.exception.ErrorResponse; - -@RestControllerAdvice -public class CustomErrorHandlingControllerAdvice { - - @ExceptionHandler(CustomErrorException.class) - public ResponseEntity handleCustomErrorExceptions( - Exception e - ) { - CustomErrorException customErrorException = (CustomErrorException) e; - - HttpStatus status = customErrorException.getStatus(); - - // converting the stack trace to String - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - customErrorException.printStackTrace(printWriter); - String stackTrace = stringWriter.toString(); - - return new ResponseEntity<>( - new ErrorResponse( - status, - customErrorException.getMessage(), - stackTrace, - customErrorException.getData() - ), - status - ); - } - - @ExceptionHandler(ConstraintViolationException.class) - public ResponseEntity handleCustomParameterConstraintExceptions( - Exception e - ) { - HttpStatus status = HttpStatus.BAD_REQUEST; // 400 - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - e.printStackTrace(printWriter); - String stackTrace = stringWriter.toString(); - return new ResponseEntity<>( - new ErrorResponse( - status, - e.getMessage(), - stackTrace - ), - status - ); - } -} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java index c44a1ff0..24f3a77c 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java @@ -1,13 +1,10 @@ package com.evolve_technology.calculator.exception; -import org.springframework.http.HttpStatus; - import lombok.Data; @Data public class CustomErrorException extends RuntimeException { - private HttpStatus status = null; private Object data = null; @@ -19,13 +16,9 @@ public CustomErrorException(String message) { super(message); } - public CustomErrorException(HttpStatus status, String message) { - this(message); - this.status = status; - } - public CustomErrorException(HttpStatus status, String message, Object data) { - this(status, message); + public CustomErrorException( String message, Object data) { + this(message); this.data = data; } } \ No newline at end of file diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java deleted file mode 100644 index 45ba3597..00000000 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/ErrorResponse.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.evolve_technology.calculator.exception; - -import java.util.Date; - -import org.springframework.http.HttpStatus; - -import lombok.Data; - -@Data -public class ErrorResponse { -// @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") - private Date timestamp; - - private int code; - - private String status; - - private String message; - - private String stackTrace; - - private Object data; - - public ErrorResponse() { - timestamp = new Date(); - } - - public ErrorResponse( - HttpStatus httpStatus, - String message - ) { - this(); - - this.code = httpStatus.value(); - this.status = httpStatus.name(); - this.message = message; - } - - public ErrorResponse( - HttpStatus httpStatus, - String message, - String stackTrace - ) { - this( - httpStatus, - message - ); - - this.stackTrace = stackTrace; - } - - public ErrorResponse( - HttpStatus httpStatus, - String message, - String stackTrace, - Object data - ) { - this( - httpStatus, - message, - stackTrace - ); - - this.data = data; - } -} \ No newline at end of file diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java index c75b5787..1d93bb9f 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java @@ -1,33 +1,62 @@ package com.evolve_technology.calculator.properties; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; import java.util.List; +import java.util.Properties; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; -import org.springframework.stereotype.Component; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.evolve_technology.calculator.service.impl.TollFeeServiceImpl; import lombok.Data; @Data -@Component -@Configuration public class TollConfiguration { - - @Value("${year}") + private static final Logger logger = LogManager.getLogger(TollConfiguration.class); private String year; - @Value("#{'${dates}'.split(',')}") private List dates; - @Value("#{'${vehicles}'.split(',')}") private List vehicles; - @Value("#{'${months}'.split(',')}") private List months; - @Bean - public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { - return new PropertySourcesPlaceholderConfigurer(); - } + public TollConfiguration(){ + try (InputStream input = TollConfiguration.class.getClassLoader().getResourceAsStream("application.properties")) { + + Properties prop = new Properties(); + if (input == null) { + logger.info("Sorry, unable to find application.properties"); + return; + } + + //load a properties file from class path, inside static method + prop.load(input); + + //get the property value and print it out + if(prop.getProperty("dates")!=null) { + setDates(Arrays.asList(prop.getProperty("dates").split(","))); + } + + if(prop.getProperty("months")!=null) { + setMonths(Arrays.asList(prop.getProperty("months").split(","))); + } + + if(prop.getProperty("vehicles")!=null) { + setVehicles(Arrays.asList(prop.getProperty("vehicles").split(","))); + } + + setYear(prop.getProperty("year")); + + logger.info("dates :: {} "+getDates()); + logger.info("months :: {} "+getMonths()); + logger.info("vehicles :: {} "+getVehicles()); + logger.info("year :: {} "+getYear()); + + } catch (IOException ex) { + ex.printStackTrace(); + logger.error(ex.getMessage()); + } + } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index 434efb68..c1364a1c 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -9,32 +9,31 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Service; import com.evolve_technology.calculator.exception.CustomErrorException; import com.evolve_technology.calculator.service.TollFeeService; import com.evolve_technology.calculator.util.TollUtil; -@Service() public class TollFeeServiceImpl implements TollFeeService { private static final Logger logger = LogManager.getLogger(TollFeeServiceImpl.class); - @Autowired TollUtil tollUtil; private Map> tollMap=new HashMap<>(); + public TollFeeServiceImpl() { + this.tollUtil=new TollUtil(); + } + public Integer getTollFee(List inputDates,String vehicle) { if(inputDates==null || vehicle==null || inputDates.isEmpty() || vehicle.isBlank()) - throw new CustomErrorException(HttpStatus.BAD_REQUEST, "inputDates and vehicle must not be null or empty. "); + throw new CustomErrorException( "inputDates and vehicle must not be null or empty. "); logger.info("Inside getTollFee method :: inputDates = {} and vehicle = {}", inputDates, vehicle); for(LocalDateTime localDateTime : inputDates) { int hour = localDateTime.getHour(); int minute = localDateTime.getMinute(); - LocalDate localDate = LocalDate.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth()); + LocalDate localDate = localDateTime.toLocalDate(); Integer newFee = tollUtil.tollCompute(vehicle, localDate, hour, minute); if(!tollMap.containsKey(localDate)) { Map innerMap=tollMap.computeIfAbsent(localDate, k->{ @@ -70,13 +69,11 @@ public Integer getTollFee(List inputDates,String vehicle) { } public int process() { - Map tollRecords=new HashMap<>(); - tollMap.keySet().forEach(k->{ - int sum=tollMap.get(k).values().stream().collect(Collectors.summingInt(Integer::intValue)); - tollRecords.put(k, sum>60 ? 60 : sum ); - }); logger.info("tollMap {} :: "+tollMap); - logger.info("tollRecords {} :: "+tollRecords); - return tollRecords.values().stream().collect(Collectors.summingInt(Integer::intValue)); + return tollMap.entrySet().stream().collect(Collectors.toMap(e->e.getKey(), + e->{ + int sum=e.getValue().values().stream().collect(Collectors.summingInt(Integer::intValue)); + return sum>60 ? 60 : sum; + })).values().stream().collect(Collectors.summingInt(Integer::intValue)); } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java index 774f787b..dff594f6 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java @@ -6,20 +6,20 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFreeDatesService; -@Service public class TollFreeDatesServiceImpl implements TollFreeDatesService { private static final Logger logger = LogManager.getLogger(TollFreeDatesServiceImpl.class); - @Autowired TollConfiguration tollConfiguration; + public TollFreeDatesServiceImpl() { + tollConfiguration=new TollConfiguration(); + } + @Override public Boolean isTollFreeDate(String date) { logger.info("Inside isTollFreeDate method :: date = {} ",date); diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java index b0dc02fc..7ed9d17d 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java @@ -4,21 +4,20 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFreeVehicleService; -@Service public class TollFreeVehiclesServiceImpl implements TollFreeVehicleService { - @Autowired TollConfiguration tollConfiguration; private static final Logger logger = LogManager.getLogger(TollFreeVehiclesServiceImpl.class); + public TollFreeVehiclesServiceImpl() { + tollConfiguration=new TollConfiguration(); + } public List getTollFreeVehicles() { logger.info("Inside getTollFreeVehicles method "); return tollConfiguration.getVehicles(); diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java index 60baa3f2..490d183d 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java @@ -2,20 +2,21 @@ import java.time.LocalDate; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - import com.evolve_technology.calculator.service.TollFreeDatesService; import com.evolve_technology.calculator.service.TollFreeVehicleService; +import com.evolve_technology.calculator.service.impl.TollFreeDatesServiceImpl; +import com.evolve_technology.calculator.service.impl.TollFreeVehiclesServiceImpl; -@Component public class TollUtil { - @Autowired TollFreeDatesService tollFreeDatesService; - @Autowired TollFreeVehicleService tollFreeVehicleService; + public TollUtil() { + tollFreeDatesService=new TollFreeDatesServiceImpl(); + tollFreeVehicleService=new TollFreeVehiclesServiceImpl(); + } + public int tollCompute(String vehicle, LocalDate localDate, int hour, int minute) { if (tollFreeDatesService.isTollFreeDate(localDate.toString()) diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java deleted file mode 100644 index 5b241085..00000000 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/config/AppConfig.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.evolve_technology.calculator.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.PropertySource; -import org.springframework.context.annotation.Scope; -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; -import org.springframework.test.context.ContextConfiguration; - -import com.evolve_technology.calculator.properties.TollConfiguration; -import com.evolve_technology.calculator.service.TollFeeService; -import com.evolve_technology.calculator.service.TollFreeDatesService; -import com.evolve_technology.calculator.service.TollFreeVehicleService; -import com.evolve_technology.calculator.service.impl.TollFeeServiceImpl; -import com.evolve_technology.calculator.service.impl.TollFreeDatesServiceImpl; -import com.evolve_technology.calculator.service.impl.TollFreeVehiclesServiceImpl; -import com.evolve_technology.calculator.util.TollRules; -import com.evolve_technology.calculator.util.TollUtil; - -@ContextConfiguration -@PropertySource("classpath:application.properties") -@ComponentScan(basePackageClasses = TollConfiguration.class) -public class AppConfig { - // this bean will be injected into the OrderServiceTest class - @Bean - @Scope("prototype") - public TollFeeService tollFeeService() { - TollFeeService tollFeeService = new TollFeeServiceImpl(); - // set properties, etc. - return tollFeeService; - } - - @Bean - @Scope("prototype") - public TollFreeDatesService tollFreeDatesService() { - TollFreeDatesService tollFreeDatesService = new TollFreeDatesServiceImpl(); - // set properties, etc. - return tollFreeDatesService; - } - - @Bean - @Scope("prototype") - public TollFreeVehicleService tollFreeVehicleService() { - TollFreeVehicleService tollFreeVehicleService = new TollFreeVehiclesServiceImpl(); - // set properties, etc. - return tollFreeVehicleService; - } - - - @Bean - @Scope("prototype") - public TollRules tollRules() { - TollRules tollRules = new TollRules(); - // set properties, etc. - return tollRules; - } - - @Bean - @Scope("prototype") - public TollUtil tollUtil() { - TollUtil tollUtil = new TollUtil(); - // set properties, etc. - return tollUtil; - } - - - @Bean - public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { - return new PropertySourcesPlaceholderConfigurer(); - } -} diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java index 6cc9ab87..9fb5a549 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java @@ -10,26 +10,21 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.context.support.AnnotationConfigContextLoader; -import com.evolve_technology.calculator.config.AppConfig; import com.evolve_technology.calculator.exception.CustomErrorException; import com.evolve_technology.calculator.service.TollFeeService; -@ExtendWith(SpringExtension.class) -@ContextConfiguration(loader=AnnotationConfigContextLoader.class,classes=AppConfig.class) -@TestPropertySource(locations = "classpath:application.properties") class TollFeeServiceImplTest { - @Autowired TollFeeService tollFeeService; + @BeforeEach + public void setup() { + tollFeeService=new TollFeeServiceImpl(); + + } /* * Jan 2013 * 5,12,19,26 are saturday From c6a6472d4b64a7eb8a273c1f0e1cccfa55175c16 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Tue, 17 May 2022 22:37:20 +0530 Subject: [PATCH 22/23] Review comment. --- .../TollFeeCalculatorApplication.java | 2 +- .../exception/CustomErrorException.java | 4 +- .../properties/TollConfiguration.java | 79 +++-- .../service/impl/TollFeeServiceImpl.java | 73 ++-- .../impl/TollFreeDatesServiceImpl.java | 32 +- .../impl/TollFreeVehiclesServiceImpl.java | 17 +- .../calculator/util/TollUtil.java | 17 +- .../calculator/validation/TollValidation.java | 14 + .../service/impl/TollFeeServiceImplTest.java | 319 +++++++++--------- 9 files changed, 277 insertions(+), 280 deletions(-) create mode 100644 toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/validation/TollValidation.java diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java index 65036443..20db8019 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java @@ -1,7 +1,7 @@ package com.evolve_technology.calculator; public class TollFeeCalculatorApplication { - + public static void main(String[] args) { // TODO Auto-generated method stub } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java index 24f3a77c..4b522bd2 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java @@ -1,6 +1,5 @@ package com.evolve_technology.calculator.exception; - import lombok.Data; @Data @@ -16,8 +15,7 @@ public CustomErrorException(String message) { super(message); } - - public CustomErrorException( String message, Object data) { + public CustomErrorException(String message, Object data) { this(message); this.data = data; } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java index 1d93bb9f..d9cbb60a 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java @@ -9,8 +9,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.evolve_technology.calculator.service.impl.TollFeeServiceImpl; - import lombok.Data; @Data @@ -20,43 +18,44 @@ public class TollConfiguration { private List dates; private List vehicles; private List months; - - public TollConfiguration(){ - try (InputStream input = TollConfiguration.class.getClassLoader().getResourceAsStream("application.properties")) { - - Properties prop = new Properties(); - - if (input == null) { - logger.info("Sorry, unable to find application.properties"); - return; - } - - //load a properties file from class path, inside static method - prop.load(input); - - //get the property value and print it out - if(prop.getProperty("dates")!=null) { - setDates(Arrays.asList(prop.getProperty("dates").split(","))); - } - - if(prop.getProperty("months")!=null) { - setMonths(Arrays.asList(prop.getProperty("months").split(","))); - } - - if(prop.getProperty("vehicles")!=null) { - setVehicles(Arrays.asList(prop.getProperty("vehicles").split(","))); - } - - setYear(prop.getProperty("year")); - - logger.info("dates :: {} "+getDates()); - logger.info("months :: {} "+getMonths()); - logger.info("vehicles :: {} "+getVehicles()); - logger.info("year :: {} "+getYear()); - - } catch (IOException ex) { - ex.printStackTrace(); - logger.error(ex.getMessage()); - } + + public TollConfiguration() { + try (InputStream input = TollConfiguration.class.getClassLoader() + .getResourceAsStream("application.properties")) { + + Properties prop = new Properties(); + + if (input == null) { + logger.info("Sorry, unable to find application.properties"); + return; + } + + // load a properties file from class path, inside static method + prop.load(input); + + // get the property value and print it out + if (prop.getProperty("dates") != null) { + setDates(Arrays.asList(prop.getProperty("dates").split(","))); + } + + if (prop.getProperty("months") != null) { + setMonths(Arrays.asList(prop.getProperty("months").split(","))); + } + + if (prop.getProperty("vehicles") != null) { + setVehicles(Arrays.asList(prop.getProperty("vehicles").split(","))); + } + + setYear(prop.getProperty("year")); + + logger.info("dates :: {} " + getDates()); + logger.info("months :: {} " + getMonths()); + logger.info("vehicles :: {} " + getVehicles()); + logger.info("year :: {} " + getYear()); + + } catch (IOException ex) { + ex.printStackTrace(); + logger.error(ex.getMessage()); + } } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index c1364a1c..3fdc0187 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -10,9 +10,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.evolve_technology.calculator.exception.CustomErrorException; import com.evolve_technology.calculator.service.TollFeeService; import com.evolve_technology.calculator.util.TollUtil; +import com.evolve_technology.calculator.validation.TollValidation; public class TollFeeServiceImpl implements TollFeeService { @@ -20,60 +20,43 @@ public class TollFeeServiceImpl implements TollFeeService { TollUtil tollUtil; - private Map> tollMap=new HashMap<>(); - - public TollFeeServiceImpl() { - this.tollUtil=new TollUtil(); + private Map tollMap = new HashMap<>(); + + public TollFeeServiceImpl(TollUtil tollUtil) { + this.tollUtil = tollUtil; } - - public Integer getTollFee(List inputDates,String vehicle) { - if(inputDates==null || vehicle==null || inputDates.isEmpty() || vehicle.isBlank()) - throw new CustomErrorException( "inputDates and vehicle must not be null or empty. "); + + public Integer getTollFee(List inputDates, String vehicle) { + TollValidation.validate(inputDates, vehicle); logger.info("Inside getTollFee method :: inputDates = {} and vehicle = {}", inputDates, vehicle); - for(LocalDateTime localDateTime : inputDates) { + for (LocalDateTime localDateTime : inputDates) { int hour = localDateTime.getHour(); int minute = localDateTime.getMinute(); LocalDate localDate = localDateTime.toLocalDate(); Integer newFee = tollUtil.tollCompute(vehicle, localDate, hour, minute); - if(!tollMap.containsKey(localDate)) { - Map innerMap=tollMap.computeIfAbsent(localDate, k->{ - logger.info("Vehicle {} has crossed the toll first time on date {} ", vehicle ,localDate); - Map map = new HashMap<>(); - map.put(hour, newFee); - return map; - }); - logger.info("key = {} , value = {} ", localDate, innerMap); - }else { - Map innerMapExisting = tollMap.computeIfPresent(localDate, - (k, outerMap) -> { - logger.info("Vehicle {} has crossed the toll on day {}", vehicle, localDate); - Integer existingFee = outerMap.get(hour); - if (existingFee == null) { - logger.info("Vehicle {} has crossed the toll on day {} but not at hour {}", vehicle, - localDate, hour); - outerMap.put(hour, newFee); - } - if (existingFee != null && existingFee < newFee) { - logger.info( - "Vehicle {} has crossed the toll on day {} at hour {} with oldFee {} and new Fee {}", - vehicle, localDate, hour, existingFee, newFee); - outerMap.put(hour, newFee); - } - - return outerMap; - }); - logger.info("updated value = {} ", innerMapExisting); + String key = localDate.toString() + ":" + hour; + if (!tollMap.containsKey(key)) { + tollMap.put(key, newFee); + } else { + if (newFee > tollMap.get(key)) + tollMap.put(key, newFee); } } return process(); } - + public int process() { - logger.info("tollMap {} :: "+tollMap); - return tollMap.entrySet().stream().collect(Collectors.toMap(e->e.getKey(), - e->{ - int sum=e.getValue().values().stream().collect(Collectors.summingInt(Integer::intValue)); - return sum>60 ? 60 : sum; - })).values().stream().collect(Collectors.summingInt(Integer::intValue)); + logger.info("tollMap {} :: " + tollMap); + Map map = new HashMap<>(); + for (String key : tollMap.keySet()) { + LocalDate localDate = LocalDate.parse(key.split(":")[0]); + if (!map.containsKey(localDate)) { + map.putIfAbsent(localDate, tollMap.get(key)); + } else { + int sum = tollMap.get(key) + map.get(localDate); + map.put(localDate, sum > 60 ? 60 : sum); + } + } + return map.values().stream().collect(Collectors.summingInt(Integer::intValue)); } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java index dff594f6..55ec51c9 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java @@ -10,32 +10,33 @@ import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFreeDatesService; - public class TollFreeDatesServiceImpl implements TollFreeDatesService { private static final Logger logger = LogManager.getLogger(TollFreeDatesServiceImpl.class); - + TollConfiguration tollConfiguration; - - public TollFreeDatesServiceImpl() { - tollConfiguration=new TollConfiguration(); + + public TollFreeDatesServiceImpl(TollConfiguration tollConfiguration) { + this.tollConfiguration = tollConfiguration; } - + @Override public Boolean isTollFreeDate(String date) { - logger.info("Inside isTollFreeDate method :: date = {} ",date); + logger.info("Inside isTollFreeDate method :: date = {} ", date); - LocalDate input=LocalDate.parse(date); - if( input.getDayOfWeek()==DayOfWeek.SATURDAY || input.getDayOfWeek()==DayOfWeek.SUNDAY) { - logger.info("date {} is actually a weekend so no toll Enjoy.",date); + LocalDate input = LocalDate.parse(date); + if (input.getDayOfWeek() == DayOfWeek.SATURDAY || input.getDayOfWeek() == DayOfWeek.SUNDAY) { + logger.info("date {} is actually a weekend so no toll Enjoy.", date); return true; } - - if(tollConfiguration.getMonths().stream().filter(month-> month.trim().equalsIgnoreCase(input.getMonth().toString())).findAny().isPresent()) { - logger.info("date {} lies in JULY month so no toll Enjoy.",date); + + if (tollConfiguration.getMonths().stream() + .filter(month -> month.trim().equalsIgnoreCase(input.getMonth().toString())).findAny().isPresent()) { + logger.info("date {} lies in JULY month so no toll Enjoy.", date); return true; } - - return getTollFreeDates().stream().filter(x -> x.trim().equalsIgnoreCase(input.toString())).findAny().isPresent(); + + return getTollFreeDates().stream().filter(x -> x.trim().equalsIgnoreCase(input.toString())).findAny() + .isPresent(); } @Override @@ -44,5 +45,4 @@ public List getTollFreeDates() { return tollConfiguration.getDates(); } - } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java index 7ed9d17d..f0e46b49 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java @@ -8,26 +8,25 @@ import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFreeVehicleService; - public class TollFreeVehiclesServiceImpl implements TollFreeVehicleService { TollConfiguration tollConfiguration; - + private static final Logger logger = LogManager.getLogger(TollFreeVehiclesServiceImpl.class); - - public TollFreeVehiclesServiceImpl() { - tollConfiguration=new TollConfiguration(); + + public TollFreeVehiclesServiceImpl(TollConfiguration tollConfiguration) { + this.tollConfiguration = tollConfiguration; } + public List getTollFreeVehicles() { logger.info("Inside getTollFreeVehicles method "); return tollConfiguration.getVehicles(); } public boolean isTollFreeVehicle(String vehicle) { - logger.info("Inside isTollFreeVehicle method :: vehicle = {} ",vehicle); - List vehicles=getTollFreeVehicles(); - return vehicles.stream().filter(v->v.trim().equalsIgnoreCase(vehicle)).findAny().isPresent(); + logger.info("Inside isTollFreeVehicle method :: vehicle = {} ", vehicle); + List vehicles = getTollFreeVehicles(); + return vehicles.stream().filter(v -> v.trim().equalsIgnoreCase(vehicle)).findAny().isPresent(); } - } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java index 490d183d..8bdd2668 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java @@ -4,25 +4,22 @@ import com.evolve_technology.calculator.service.TollFreeDatesService; import com.evolve_technology.calculator.service.TollFreeVehicleService; -import com.evolve_technology.calculator.service.impl.TollFreeDatesServiceImpl; -import com.evolve_technology.calculator.service.impl.TollFreeVehiclesServiceImpl; public class TollUtil { TollFreeDatesService tollFreeDatesService; TollFreeVehicleService tollFreeVehicleService; - - public TollUtil() { - tollFreeDatesService=new TollFreeDatesServiceImpl(); - tollFreeVehicleService=new TollFreeVehiclesServiceImpl(); + + public TollUtil(TollFreeDatesService tollFreeDatesService, TollFreeVehicleService tollFreeVehicleService) { + this.tollFreeDatesService = tollFreeDatesService; + this.tollFreeVehicleService = tollFreeVehicleService; } - - + public int tollCompute(String vehicle, LocalDate localDate, int hour, int minute) { if (tollFreeDatesService.isTollFreeDate(localDate.toString()) || tollFreeVehicleService.isTollFreeVehicle(vehicle)) return 0; - return TollRules.getHourlyRate(hour,minute); + return TollRules.getHourlyRate(hour, minute); } - + } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/validation/TollValidation.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/validation/TollValidation.java new file mode 100644 index 00000000..ed835e41 --- /dev/null +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/validation/TollValidation.java @@ -0,0 +1,14 @@ +package com.evolve_technology.calculator.validation; + +import java.time.LocalDateTime; +import java.util.List; + +import com.evolve_technology.calculator.exception.CustomErrorException; + +public class TollValidation { + public static void validate(List inputDates, String vehicle) { + if (inputDates == null || vehicle == null || inputDates.isEmpty() || vehicle.isBlank()) + throw new CustomErrorException("inputDates and vehicle must not be null or empty. "); + } + +} diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java index 9fb5a549..ec98d6da 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java @@ -14,254 +14,261 @@ import org.junit.jupiter.api.Test; import com.evolve_technology.calculator.exception.CustomErrorException; +import com.evolve_technology.calculator.properties.TollConfiguration; import com.evolve_technology.calculator.service.TollFeeService; +import com.evolve_technology.calculator.service.TollFreeDatesService; +import com.evolve_technology.calculator.service.TollFreeVehicleService; +import com.evolve_technology.calculator.util.TollUtil; class TollFeeServiceImplTest { - + TollFeeService tollFeeService; - + @BeforeEach public void setup() { - tollFeeService=new TollFeeServiceImpl(); - + TollConfiguration tollConfiguration = new TollConfiguration(); + TollFreeVehicleService tollFreeVehicleService = new TollFreeVehiclesServiceImpl(tollConfiguration); + TollFreeDatesService tollFreeDatesService = new TollFreeDatesServiceImpl(tollConfiguration); + TollUtil tollUtil = new TollUtil(tollFreeDatesService, tollFreeVehicleService); + tollFeeService = new TollFeeServiceImpl(tollUtil); + } + /* - * Jan 2013 - * 5,12,19,26 are saturday + * Jan 2013 5,12,19,26 are saturday */ - @Test + @Test void testGetTollFeeSaturdayJanuary2013() { - - List inputDates=Stream.of(LocalDateTime.of(2013, 01, 05, 8, 8),LocalDateTime.of(2013, 01, 12, 8, 8) - ,LocalDateTime.of(2013, 01, 19, 8, 8),LocalDateTime.of(2013, 01, 26, 8, 8)). - collect(Collectors.toList()); + + List inputDates = Stream + .of(LocalDateTime.of(2013, 01, 05, 8, 8), LocalDateTime.of(2013, 01, 12, 8, 8), + LocalDateTime.of(2013, 01, 19, 8, 8), LocalDateTime.of(2013, 01, 26, 8, 8)) + .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "car"), 0); } - + /* * Null as inputs */ - @Test + @Test void testGetTollFeeWithNullInputs() { // Both input null - assertThrows(CustomErrorException.class,()->tollFeeService.getTollFee(null, null)); - - //only vehicle null - List inputDates=Stream.of(LocalDateTime.of(2013, 01, 05, 8, 8),LocalDateTime.of(2013, 01, 12, 8, 8) - ,LocalDateTime.of(2013, 01, 19, 8, 8),LocalDateTime.of(2013, 01, 26, 8, 8)). - collect(Collectors.toList()); - - assertThrows(CustomErrorException.class,()->tollFeeService.getTollFee(inputDates, null)); - - List inputDatesEmpty=new ArrayList<>(); - assertThrows(CustomErrorException.class,()->tollFeeService.getTollFee(inputDatesEmpty, "")); + assertThrows(CustomErrorException.class, () -> tollFeeService.getTollFee(null, null)); + + // only vehicle null + List inputDates = Stream + .of(LocalDateTime.of(2013, 01, 05, 8, 8), LocalDateTime.of(2013, 01, 12, 8, 8), + LocalDateTime.of(2013, 01, 19, 8, 8), LocalDateTime.of(2013, 01, 26, 8, 8)) + .collect(Collectors.toList()); + + assertThrows(CustomErrorException.class, () -> tollFeeService.getTollFee(inputDates, null)); + + List inputDatesEmpty = new ArrayList<>(); + assertThrows(CustomErrorException.class, () -> tollFeeService.getTollFee(inputDatesEmpty, "")); } - + /* - * Feb 2013 - * 2,9,16,23 are saturday + * Feb 2013 2,9,16,23 are saturday */ - @Test + @Test void testGetTollFeeSaturdayFebruary2013() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 02, 8, 8),LocalDateTime.of(2013, 02, 16, 8, 8) - ,LocalDateTime.of(2013, 02, 9, 8, 8),LocalDateTime.of(2013, 02, 23, 8, 8)). - collect(Collectors.toList()); + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 02, 8, 8), LocalDateTime.of(2013, 02, 16, 8, 8), + LocalDateTime.of(2013, 02, 9, 8, 8), LocalDateTime.of(2013, 02, 23, 8, 8)) + .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "car"), 0); } - + /* - * Feb 2013 - * 4,11,18,25 are Monday - *NON Free Vehicles Test + * Feb 2013 4,11,18,25 are Monday NON Free Vehicles Test */ - @Test + @Test void testGetTollFeeMondayFebruary2013Car() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 8, 8),LocalDateTime.of(2013, 02, 18, 8, 8) - ,LocalDateTime.of(2013, 02, 11, 8, 8),LocalDateTime.of(2013, 02, 25, 8, 8)). - collect(Collectors.toList()); + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 8, 8), LocalDateTime.of(2013, 02, 18, 8, 8), + LocalDateTime.of(2013, 02, 11, 8, 8), LocalDateTime.of(2013, 02, 25, 8, 8)) + .collect(Collectors.toList()); assertNotEquals(tollFeeService.getTollFee(inputDates, "car"), 0); - + } - + /* - * Feb 2013 - * 4,11,18,25 are Monday - * Free Vehicles Test + * Feb 2013 4,11,18,25 are Monday Free Vehicles Test */ - @Test + @Test void testGetTollFeeMondayFebruary2013FreeVehicles() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 8, 8),LocalDateTime.of(2013, 02, 18, 8, 8) - ,LocalDateTime.of(2013, 02, 11, 8, 8),LocalDateTime.of(2013, 02, 25, 8, 8)). - collect(Collectors.toList()); - + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 8, 8), LocalDateTime.of(2013, 02, 18, 8, 8), + LocalDateTime.of(2013, 02, 11, 8, 8), LocalDateTime.of(2013, 02, 25, 8, 8)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Tractor"), 0); assertEquals(tollFeeService.getTollFee(inputDates, "Motorbike"), 0); assertEquals(tollFeeService.getTollFee(inputDates, "Emergency"), 0); assertEquals(tollFeeService.getTollFee(inputDates, "Diplomat"), 0); assertEquals(tollFeeService.getTollFee(inputDates, "Foreign"), 0); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 - * As per the Rule it is charged 8. + * Feb 2013 Monday 4th 04-02-2013 06:10 As per the Rule it is charged 8. */ - @Test + @Test void testGetTollFeeMondayFebruary2013At6Hr10Min() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10)). - collect(Collectors.toList()); - + List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10)).collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 8); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 - * As per the Rule it is charged 8. - * if (hour == 6 && minute >= 0 && minute <= 29) - return 8; - else if (hour == 6 && minute >= 30 && minute <= 59) - return 13; + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 As per the Rule it is charged + * 8. if (hour == 6 && minute >= 0 && minute <= 29) return 8; else if (hour == 6 + * && minute >= 30 && minute <= 59) return 13; */ - @Test + @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20Min() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20)). - collect(Collectors.toList()); - + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), LocalDateTime.of(2013, 02, 04, 06, 20)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 8); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 && 06:40 - * As per the Rule max within an hour is charged that is 13 max of(8,13) + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 As per the Rule max + * within an hour is charged that is 13 max of(8,13) */ - @Test + @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40Min() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20),LocalDateTime.of(2013, 02, 04, 06, 40)). - collect(Collectors.toList()); - + List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 13); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 - * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 As per the + * Rule max within an hour is charged that is max of 8,13 plus 18 =31 */ - @Test + @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10Min() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10)). - collect(Collectors.toList()); - + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), LocalDateTime.of(2013, 02, 04, 07, 10)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 31); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 - * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 As + * per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 + * plus 13 =44 */ - @Test + @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10Min() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10)). - collect(Collectors.toList()); - + List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), LocalDateTime.of(2013, 02, 04, 8, 10)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 44); } /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 - * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && + * 08:30 As per the Rule max within an hour is charged that is max of 8,13 plus + * 18 =31 plus 13 =44 */ - @Test + @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30Min() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) - ,LocalDateTime.of(2013, 02, 04, 8, 30)). - collect(Collectors.toList()); - + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), LocalDateTime.of(2013, 02, 04, 07, 10), + LocalDateTime.of(2013, 02, 04, 8, 10), LocalDateTime.of(2013, 02, 04, 8, 30)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 44); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 && 15:10 - * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 plus 13= 57 + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && + * 08:30 && 15:10 As per the Rule max within an hour is charged that is max of + * 8,13 plus 18 =31 plus 13 =44 plus 13= 57 */ - @Test + @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30MinAnd15Hr10Min() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) - ,LocalDateTime.of(2013, 02, 04, 8, 30),LocalDateTime.of(2013, 02, 04, 15, 10)). - collect(Collectors.toList()); - + List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), LocalDateTime.of(2013, 02, 04, 8, 10), + LocalDateTime.of(2013, 02, 04, 8, 30), LocalDateTime.of(2013, 02, 04, 15, 10)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 57); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 && 15:10 && 15:30 - * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 plus - * max of (13,18) is 18= 62 but it can't go beyond 60 for a day. + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && + * 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged that is + * max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 but it + * can't go beyond 60 for a day. */ - @Test + @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30MinAnd15Hr10MinAnd15HrMin30() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) - ,LocalDateTime.of(2013, 02, 04, 8, 30),LocalDateTime.of(2013, 02, 04, 15, 10),LocalDateTime.of(2013, 02, 04, 15, 30)). - collect(Collectors.toList()); - + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), LocalDateTime.of(2013, 02, 04, 07, 10), + LocalDateTime.of(2013, 02, 04, 8, 10), LocalDateTime.of(2013, 02, 04, 8, 30), + LocalDateTime.of(2013, 02, 04, 15, 10), LocalDateTime.of(2013, 02, 04, 15, 30)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 60); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 && 15:10 && 15:30 - * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 plus - * max of (13,18) is 18= 62 but it can't go beyond 60 for a day. + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && + * 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged that is + * max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 but it + * can't go beyond 60 for a day. * - * And Feb 2013 Tuesday 5th - * 05-02-2013 06:10 + * And Feb 2013 Tuesday 5th 05-02-2013 06:10 * * Monday=60 plus Tuesday=8= 68 */ - @Test + @Test void testGetTollFeeMondayFebruary2013MultipleDatesMondayPlusTuesday() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) - ,LocalDateTime.of(2013, 02, 04, 8, 30),LocalDateTime.of(2013, 02, 04, 15, 10),LocalDateTime.of(2013, 02, 04, 15, 30), - LocalDateTime.of(2013, 02, 05, 6, 10)). - collect(Collectors.toList()); - + List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), LocalDateTime.of(2013, 02, 04, 8, 10), + LocalDateTime.of(2013, 02, 04, 8, 30), LocalDateTime.of(2013, 02, 04, 15, 10), + LocalDateTime.of(2013, 02, 04, 15, 30), LocalDateTime.of(2013, 02, 05, 6, 10)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 68); } - + /* - * Feb 2013 Monday 4th - * 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && 08:30 && 15:10 && 15:30 - * As per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 plus 13 =44 plus - * max of (13,18) is 18= 62 but it can't go beyond 60 for a day. + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && + * 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged that is + * max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 but it + * can't go beyond 60 for a day. * - * And Feb 2013 Tuesday 5th - * 05-02-2013 06:10 + * And Feb 2013 Tuesday 5th 05-02-2013 06:10 * - * And Feb 2013 Wednesday 6th - * 06-02-2013 17:10 && 18:10 + * And Feb 2013 Wednesday 6th 06-02-2013 17:10 && 18:10 * - * Monday=60 plus Tuesday=8 plus Wednesday= 13 plus 8 =89 + * Monday=60 plus Tuesday=8 plus Wednesday= 13 plus 8 =89 */ - @Test + @Test void testGetTollFeeMondayFebruary2013MultipleDatesMondayPlusTuesdayPlusWednesday() { - List inputDates=Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10),LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40),LocalDateTime.of(2013, 02, 04, 07, 10),LocalDateTime.of(2013, 02, 04, 8, 10) - ,LocalDateTime.of(2013, 02, 04, 8, 30),LocalDateTime.of(2013, 02, 04, 15, 10),LocalDateTime.of(2013, 02, 04, 15, 30), - LocalDateTime.of(2013, 02, 05, 6, 10),LocalDateTime.of(2013, 02, 06, 17, 10),LocalDateTime.of(2013, 02, 06, 18, 10)). - collect(Collectors.toList()); - + List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), LocalDateTime.of(2013, 02, 04, 8, 10), + LocalDateTime.of(2013, 02, 04, 8, 30), LocalDateTime.of(2013, 02, 04, 15, 10), + LocalDateTime.of(2013, 02, 04, 15, 30), LocalDateTime.of(2013, 02, 05, 6, 10), + LocalDateTime.of(2013, 02, 06, 17, 10), LocalDateTime.of(2013, 02, 06, 18, 10)) + .collect(Collectors.toList()); + assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 89); } } From 965b773e1b3cbfb229312b7e6a46ef17315d59d9 Mon Sep 17 00:00:00 2001 From: Rizwan Date: Tue, 17 May 2022 23:06:41 +0530 Subject: [PATCH 23/23] Formatted. --- .../TollFeeCalculatorApplication.java | 1 - .../exception/CustomErrorException.java | 3 +- .../properties/TollConfiguration.java | 6 +- .../service/TollFreeDatesService.java | 1 + .../service/TollFreeVehicleService.java | 1 + .../service/impl/TollFeeServiceImpl.java | 13 +- .../impl/TollFreeDatesServiceImpl.java | 18 +- .../impl/TollFreeVehiclesServiceImpl.java | 9 +- .../calculator/util/TollRules.java | 1 + .../calculator/util/TollUtil.java | 6 +- .../calculator/validation/TollValidation.java | 9 +- .../service/impl/TollFeeServiceImplTest.java | 198 +++++++++++------- 12 files changed, 168 insertions(+), 98 deletions(-) diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java index 20db8019..5efc0e20 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/TollFeeCalculatorApplication.java @@ -5,5 +5,4 @@ public class TollFeeCalculatorApplication { public static void main(String[] args) { // TODO Auto-generated method stub } - } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java index 4b522bd2..100a7bcb 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/exception/CustomErrorException.java @@ -4,7 +4,6 @@ @Data public class CustomErrorException extends RuntimeException { - private Object data = null; public CustomErrorException() { @@ -19,4 +18,4 @@ public CustomErrorException(String message, Object data) { this(message); this.data = data; } -} \ No newline at end of file +} diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java index d9cbb60a..3e07bd21 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/properties/TollConfiguration.java @@ -13,7 +13,8 @@ @Data public class TollConfiguration { - private static final Logger logger = LogManager.getLogger(TollConfiguration.class); + private static final Logger logger = LogManager + .getLogger(TollConfiguration.class); private String year; private List dates; private List vehicles; @@ -43,7 +44,8 @@ public TollConfiguration() { } if (prop.getProperty("vehicles") != null) { - setVehicles(Arrays.asList(prop.getProperty("vehicles").split(","))); + setVehicles( + Arrays.asList(prop.getProperty("vehicles").split(","))); } setYear(prop.getProperty("year")); diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeDatesService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeDatesService.java index 6ab032da..b18c2969 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeDatesService.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeDatesService.java @@ -4,5 +4,6 @@ public interface TollFreeDatesService { public Boolean isTollFreeDate(String date); + public List getTollFreeDates(); } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeVehicleService.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeVehicleService.java index cabd82d8..8afe0fd7 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeVehicleService.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/TollFreeVehicleService.java @@ -4,5 +4,6 @@ public interface TollFreeVehicleService { public List getTollFreeVehicles(); + public boolean isTollFreeVehicle(String vehicle); } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java index 3fdc0187..1611b75d 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImpl.java @@ -16,7 +16,8 @@ public class TollFeeServiceImpl implements TollFeeService { - private static final Logger logger = LogManager.getLogger(TollFeeServiceImpl.class); + private static final Logger logger = LogManager + .getLogger(TollFeeServiceImpl.class); TollUtil tollUtil; @@ -28,12 +29,15 @@ public TollFeeServiceImpl(TollUtil tollUtil) { public Integer getTollFee(List inputDates, String vehicle) { TollValidation.validate(inputDates, vehicle); - logger.info("Inside getTollFee method :: inputDates = {} and vehicle = {}", inputDates, vehicle); + logger.info( + "Inside getTollFee method :: inputDates = {} and vehicle = {}", + inputDates, vehicle); for (LocalDateTime localDateTime : inputDates) { int hour = localDateTime.getHour(); int minute = localDateTime.getMinute(); LocalDate localDate = localDateTime.toLocalDate(); - Integer newFee = tollUtil.tollCompute(vehicle, localDate, hour, minute); + Integer newFee = tollUtil.tollCompute(vehicle, localDate, hour, + minute); String key = localDate.toString() + ":" + hour; if (!tollMap.containsKey(key)) { tollMap.put(key, newFee); @@ -57,6 +61,7 @@ public int process() { map.put(localDate, sum > 60 ? 60 : sum); } } - return map.values().stream().collect(Collectors.summingInt(Integer::intValue)); + return map.values().stream() + .collect(Collectors.summingInt(Integer::intValue)); } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java index 55ec51c9..55bc80f3 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeDatesServiceImpl.java @@ -11,7 +11,8 @@ import com.evolve_technology.calculator.service.TollFreeDatesService; public class TollFreeDatesServiceImpl implements TollFreeDatesService { - private static final Logger logger = LogManager.getLogger(TollFreeDatesServiceImpl.class); + private static final Logger logger = LogManager + .getLogger(TollFreeDatesServiceImpl.class); TollConfiguration tollConfiguration; @@ -24,19 +25,24 @@ public Boolean isTollFreeDate(String date) { logger.info("Inside isTollFreeDate method :: date = {} ", date); LocalDate input = LocalDate.parse(date); - if (input.getDayOfWeek() == DayOfWeek.SATURDAY || input.getDayOfWeek() == DayOfWeek.SUNDAY) { - logger.info("date {} is actually a weekend so no toll Enjoy.", date); + if (input.getDayOfWeek() == DayOfWeek.SATURDAY + || input.getDayOfWeek() == DayOfWeek.SUNDAY) { + logger.info("date {} is actually a weekend so no toll Enjoy.", + date); return true; } if (tollConfiguration.getMonths().stream() - .filter(month -> month.trim().equalsIgnoreCase(input.getMonth().toString())).findAny().isPresent()) { + .filter(month -> month.trim() + .equalsIgnoreCase(input.getMonth().toString())) + .findAny().isPresent()) { logger.info("date {} lies in JULY month so no toll Enjoy.", date); return true; } - return getTollFreeDates().stream().filter(x -> x.trim().equalsIgnoreCase(input.toString())).findAny() - .isPresent(); + return getTollFreeDates().stream() + .filter(x -> x.trim().equalsIgnoreCase(input.toString())) + .findAny().isPresent(); } @Override diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java index f0e46b49..ea471324 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/service/impl/TollFreeVehiclesServiceImpl.java @@ -12,7 +12,8 @@ public class TollFreeVehiclesServiceImpl implements TollFreeVehicleService { TollConfiguration tollConfiguration; - private static final Logger logger = LogManager.getLogger(TollFreeVehiclesServiceImpl.class); + private static final Logger logger = LogManager + .getLogger(TollFreeVehiclesServiceImpl.class); public TollFreeVehiclesServiceImpl(TollConfiguration tollConfiguration) { this.tollConfiguration = tollConfiguration; @@ -24,9 +25,11 @@ public List getTollFreeVehicles() { } public boolean isTollFreeVehicle(String vehicle) { - logger.info("Inside isTollFreeVehicle method :: vehicle = {} ", vehicle); + logger.info("Inside isTollFreeVehicle method :: vehicle = {} ", + vehicle); List vehicles = getTollFreeVehicles(); - return vehicles.stream().filter(v -> v.trim().equalsIgnoreCase(vehicle)).findAny().isPresent(); + return vehicles.stream().filter(v -> v.trim().equalsIgnoreCase(vehicle)) + .findAny().isPresent(); } } diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollRules.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollRules.java index 44d4dc63..d8f31fc4 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollRules.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollRules.java @@ -1,6 +1,7 @@ package com.evolve_technology.calculator.util; public class TollRules { + public static int getHourlyRate(int hour, int minute) { if (hour == 6 && minute >= 0 && minute <= 29) return 8; diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java index 8bdd2668..d80e189c 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/util/TollUtil.java @@ -10,12 +10,14 @@ public class TollUtil { TollFreeVehicleService tollFreeVehicleService; - public TollUtil(TollFreeDatesService tollFreeDatesService, TollFreeVehicleService tollFreeVehicleService) { + public TollUtil(TollFreeDatesService tollFreeDatesService, + TollFreeVehicleService tollFreeVehicleService) { this.tollFreeDatesService = tollFreeDatesService; this.tollFreeVehicleService = tollFreeVehicleService; } - public int tollCompute(String vehicle, LocalDate localDate, int hour, int minute) { + public int tollCompute(String vehicle, LocalDate localDate, int hour, + int minute) { if (tollFreeDatesService.isTollFreeDate(localDate.toString()) || tollFreeVehicleService.isTollFreeVehicle(vehicle)) return 0; diff --git a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/validation/TollValidation.java b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/validation/TollValidation.java index ed835e41..4c37d952 100644 --- a/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/validation/TollValidation.java +++ b/toll_fee_calc_app/src/main/java/com/evolve_technology/calculator/validation/TollValidation.java @@ -6,9 +6,12 @@ import com.evolve_technology.calculator.exception.CustomErrorException; public class TollValidation { - public static void validate(List inputDates, String vehicle) { - if (inputDates == null || vehicle == null || inputDates.isEmpty() || vehicle.isBlank()) - throw new CustomErrorException("inputDates and vehicle must not be null or empty. "); + public static void validate(List inputDates, + String vehicle) { + if (inputDates == null || vehicle == null || inputDates.isEmpty() + || vehicle.isBlank()) + throw new CustomErrorException( + "inputDates and vehicle must not be null or empty. "); } } diff --git a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java index ec98d6da..df7be330 100644 --- a/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java +++ b/toll_fee_calc_app/src/test/java/com/evolve_technology/calculator/service/impl/TollFeeServiceImplTest.java @@ -27,9 +27,12 @@ class TollFeeServiceImplTest { @BeforeEach public void setup() { TollConfiguration tollConfiguration = new TollConfiguration(); - TollFreeVehicleService tollFreeVehicleService = new TollFreeVehiclesServiceImpl(tollConfiguration); - TollFreeDatesService tollFreeDatesService = new TollFreeDatesServiceImpl(tollConfiguration); - TollUtil tollUtil = new TollUtil(tollFreeDatesService, tollFreeVehicleService); + TollFreeVehicleService tollFreeVehicleService = new TollFreeVehiclesServiceImpl( + tollConfiguration); + TollFreeDatesService tollFreeDatesService = new TollFreeDatesServiceImpl( + tollConfiguration); + TollUtil tollUtil = new TollUtil(tollFreeDatesService, + tollFreeVehicleService); tollFeeService = new TollFeeServiceImpl(tollUtil); } @@ -41,8 +44,10 @@ public void setup() { void testGetTollFeeSaturdayJanuary2013() { List inputDates = Stream - .of(LocalDateTime.of(2013, 01, 05, 8, 8), LocalDateTime.of(2013, 01, 12, 8, 8), - LocalDateTime.of(2013, 01, 19, 8, 8), LocalDateTime.of(2013, 01, 26, 8, 8)) + .of(LocalDateTime.of(2013, 01, 05, 8, 8), + LocalDateTime.of(2013, 01, 12, 8, 8), + LocalDateTime.of(2013, 01, 19, 8, 8), + LocalDateTime.of(2013, 01, 26, 8, 8)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "car"), 0); } @@ -53,18 +58,23 @@ void testGetTollFeeSaturdayJanuary2013() { @Test void testGetTollFeeWithNullInputs() { // Both input null - assertThrows(CustomErrorException.class, () -> tollFeeService.getTollFee(null, null)); + assertThrows(CustomErrorException.class, + () -> tollFeeService.getTollFee(null, null)); // only vehicle null List inputDates = Stream - .of(LocalDateTime.of(2013, 01, 05, 8, 8), LocalDateTime.of(2013, 01, 12, 8, 8), - LocalDateTime.of(2013, 01, 19, 8, 8), LocalDateTime.of(2013, 01, 26, 8, 8)) + .of(LocalDateTime.of(2013, 01, 05, 8, 8), + LocalDateTime.of(2013, 01, 12, 8, 8), + LocalDateTime.of(2013, 01, 19, 8, 8), + LocalDateTime.of(2013, 01, 26, 8, 8)) .collect(Collectors.toList()); - assertThrows(CustomErrorException.class, () -> tollFeeService.getTollFee(inputDates, null)); + assertThrows(CustomErrorException.class, + () -> tollFeeService.getTollFee(inputDates, null)); List inputDatesEmpty = new ArrayList<>(); - assertThrows(CustomErrorException.class, () -> tollFeeService.getTollFee(inputDatesEmpty, "")); + assertThrows(CustomErrorException.class, + () -> tollFeeService.getTollFee(inputDatesEmpty, "")); } /* @@ -73,8 +83,10 @@ void testGetTollFeeWithNullInputs() { @Test void testGetTollFeeSaturdayFebruary2013() { List inputDates = Stream - .of(LocalDateTime.of(2013, 02, 02, 8, 8), LocalDateTime.of(2013, 02, 16, 8, 8), - LocalDateTime.of(2013, 02, 9, 8, 8), LocalDateTime.of(2013, 02, 23, 8, 8)) + .of(LocalDateTime.of(2013, 02, 02, 8, 8), + LocalDateTime.of(2013, 02, 16, 8, 8), + LocalDateTime.of(2013, 02, 9, 8, 8), + LocalDateTime.of(2013, 02, 23, 8, 8)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "car"), 0); } @@ -85,8 +97,10 @@ void testGetTollFeeSaturdayFebruary2013() { @Test void testGetTollFeeMondayFebruary2013Car() { List inputDates = Stream - .of(LocalDateTime.of(2013, 02, 04, 8, 8), LocalDateTime.of(2013, 02, 18, 8, 8), - LocalDateTime.of(2013, 02, 11, 8, 8), LocalDateTime.of(2013, 02, 25, 8, 8)) + .of(LocalDateTime.of(2013, 02, 04, 8, 8), + LocalDateTime.of(2013, 02, 18, 8, 8), + LocalDateTime.of(2013, 02, 11, 8, 8), + LocalDateTime.of(2013, 02, 25, 8, 8)) .collect(Collectors.toList()); assertNotEquals(tollFeeService.getTollFee(inputDates, "car"), 0); @@ -98,8 +112,10 @@ void testGetTollFeeMondayFebruary2013Car() { @Test void testGetTollFeeMondayFebruary2013FreeVehicles() { List inputDates = Stream - .of(LocalDateTime.of(2013, 02, 04, 8, 8), LocalDateTime.of(2013, 02, 18, 8, 8), - LocalDateTime.of(2013, 02, 11, 8, 8), LocalDateTime.of(2013, 02, 25, 8, 8)) + .of(LocalDateTime.of(2013, 02, 04, 8, 8), + LocalDateTime.of(2013, 02, 18, 8, 8), + LocalDateTime.of(2013, 02, 11, 8, 8), + LocalDateTime.of(2013, 02, 25, 8, 8)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Tractor"), 0); @@ -114,122 +130,143 @@ void testGetTollFeeMondayFebruary2013FreeVehicles() { */ @Test void testGetTollFeeMondayFebruary2013At6Hr10Min() { - List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10)).collect(Collectors.toList()); + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10)) + .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 8); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 As per the Rule it is charged - * 8. if (hour == 6 && minute >= 0 && minute <= 29) return 8; else if (hour == 6 - * && minute >= 30 && minute <= 59) return 13; + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 As per the Rule it is + * charged 8. if (hour == 6 && minute >= 0 && minute <= 29) return 8; else + * if (hour == 6 && minute >= 30 && minute <= 59) return 13; */ @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20Min() { List inputDates = Stream - .of(LocalDateTime.of(2013, 02, 04, 06, 10), LocalDateTime.of(2013, 02, 04, 06, 20)) + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 8); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 As per the Rule max - * within an hour is charged that is 13 max of(8,13) + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 As per the Rule + * max within an hour is charged that is 13 max of(8,13) */ @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40Min() { - List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), - LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40)) + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 13); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 As per the - * Rule max within an hour is charged that is max of 8,13 plus 18 =31 + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 As per + * the Rule max within an hour is charged that is max of 8,13 plus 18 =31 */ @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10Min() { List inputDates = Stream - .of(LocalDateTime.of(2013, 02, 04, 06, 10), LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40), LocalDateTime.of(2013, 02, 04, 07, 10)) + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 31); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 As - * per the Rule max within an hour is charged that is max of 8,13 plus 18 =31 - * plus 13 =44 + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 + * As per the Rule max within an hour is charged that is max of 8,13 plus 18 + * =31 plus 13 =44 */ @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10Min() { - List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), - LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40), - LocalDateTime.of(2013, 02, 04, 07, 10), LocalDateTime.of(2013, 02, 04, 8, 10)) + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), + LocalDateTime.of(2013, 02, 04, 8, 10)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 44); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && - * 08:30 As per the Rule max within an hour is charged that is max of 8,13 plus - * 18 =31 plus 13 =44 + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 + * && 08:30 As per the Rule max within an hour is charged that is max of + * 8,13 plus 18 =31 plus 13 =44 */ @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30Min() { List inputDates = Stream - .of(LocalDateTime.of(2013, 02, 04, 06, 10), LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40), LocalDateTime.of(2013, 02, 04, 07, 10), - LocalDateTime.of(2013, 02, 04, 8, 10), LocalDateTime.of(2013, 02, 04, 8, 30)) + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), + LocalDateTime.of(2013, 02, 04, 8, 10), + LocalDateTime.of(2013, 02, 04, 8, 30)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 44); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && - * 08:30 && 15:10 As per the Rule max within an hour is charged that is max of - * 8,13 plus 18 =31 plus 13 =44 plus 13= 57 + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 + * && 08:30 && 15:10 As per the Rule max within an hour is charged that is + * max of 8,13 plus 18 =31 plus 13 =44 plus 13= 57 */ @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30MinAnd15Hr10Min() { - List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), - LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40), - LocalDateTime.of(2013, 02, 04, 07, 10), LocalDateTime.of(2013, 02, 04, 8, 10), - LocalDateTime.of(2013, 02, 04, 8, 30), LocalDateTime.of(2013, 02, 04, 15, 10)) + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), + LocalDateTime.of(2013, 02, 04, 8, 10), + LocalDateTime.of(2013, 02, 04, 8, 30), + LocalDateTime.of(2013, 02, 04, 15, 10)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 57); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && - * 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged that is - * max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 but it - * can't go beyond 60 for a day. + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 + * && 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged + * that is max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 + * but it can't go beyond 60 for a day. */ @Test void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr10MinAnd8Hr30MinAnd15Hr10MinAnd15HrMin30() { List inputDates = Stream - .of(LocalDateTime.of(2013, 02, 04, 06, 10), LocalDateTime.of(2013, 02, 04, 06, 20), - LocalDateTime.of(2013, 02, 04, 06, 40), LocalDateTime.of(2013, 02, 04, 07, 10), - LocalDateTime.of(2013, 02, 04, 8, 10), LocalDateTime.of(2013, 02, 04, 8, 30), - LocalDateTime.of(2013, 02, 04, 15, 10), LocalDateTime.of(2013, 02, 04, 15, 30)) + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), + LocalDateTime.of(2013, 02, 04, 8, 10), + LocalDateTime.of(2013, 02, 04, 8, 30), + LocalDateTime.of(2013, 02, 04, 15, 10), + LocalDateTime.of(2013, 02, 04, 15, 30)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 60); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && - * 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged that is - * max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 but it - * can't go beyond 60 for a day. + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 + * && 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged + * that is max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 + * but it can't go beyond 60 for a day. * * And Feb 2013 Tuesday 5th 05-02-2013 06:10 * @@ -237,21 +274,26 @@ void testGetTollFeeMondayFebruary2013At6Hr10MinAnd20MinAnd40MinAnd7Hr10MinAnd8Hr */ @Test void testGetTollFeeMondayFebruary2013MultipleDatesMondayPlusTuesday() { - List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), - LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40), - LocalDateTime.of(2013, 02, 04, 07, 10), LocalDateTime.of(2013, 02, 04, 8, 10), - LocalDateTime.of(2013, 02, 04, 8, 30), LocalDateTime.of(2013, 02, 04, 15, 10), - LocalDateTime.of(2013, 02, 04, 15, 30), LocalDateTime.of(2013, 02, 05, 6, 10)) + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), + LocalDateTime.of(2013, 02, 04, 8, 10), + LocalDateTime.of(2013, 02, 04, 8, 30), + LocalDateTime.of(2013, 02, 04, 15, 10), + LocalDateTime.of(2013, 02, 04, 15, 30), + LocalDateTime.of(2013, 02, 05, 6, 10)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 68); } /* - * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 && - * 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged that is - * max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 but it - * can't go beyond 60 for a day. + * Feb 2013 Monday 4th 04-02-2013 06:10 && 06:20 && 06:40 && 07:10 && 08:10 + * && 08:30 && 15:10 && 15:30 As per the Rule max within an hour is charged + * that is max of 8,13 plus 18 =31 plus 13 =44 plus max of (13,18) is 18= 62 + * but it can't go beyond 60 for a day. * * And Feb 2013 Tuesday 5th 05-02-2013 06:10 * @@ -261,12 +303,18 @@ void testGetTollFeeMondayFebruary2013MultipleDatesMondayPlusTuesday() { */ @Test void testGetTollFeeMondayFebruary2013MultipleDatesMondayPlusTuesdayPlusWednesday() { - List inputDates = Stream.of(LocalDateTime.of(2013, 02, 04, 06, 10), - LocalDateTime.of(2013, 02, 04, 06, 20), LocalDateTime.of(2013, 02, 04, 06, 40), - LocalDateTime.of(2013, 02, 04, 07, 10), LocalDateTime.of(2013, 02, 04, 8, 10), - LocalDateTime.of(2013, 02, 04, 8, 30), LocalDateTime.of(2013, 02, 04, 15, 10), - LocalDateTime.of(2013, 02, 04, 15, 30), LocalDateTime.of(2013, 02, 05, 6, 10), - LocalDateTime.of(2013, 02, 06, 17, 10), LocalDateTime.of(2013, 02, 06, 18, 10)) + List inputDates = Stream + .of(LocalDateTime.of(2013, 02, 04, 06, 10), + LocalDateTime.of(2013, 02, 04, 06, 20), + LocalDateTime.of(2013, 02, 04, 06, 40), + LocalDateTime.of(2013, 02, 04, 07, 10), + LocalDateTime.of(2013, 02, 04, 8, 10), + LocalDateTime.of(2013, 02, 04, 8, 30), + LocalDateTime.of(2013, 02, 04, 15, 10), + LocalDateTime.of(2013, 02, 04, 15, 30), + LocalDateTime.of(2013, 02, 05, 6, 10), + LocalDateTime.of(2013, 02, 06, 17, 10), + LocalDateTime.of(2013, 02, 06, 18, 10)) .collect(Collectors.toList()); assertEquals(tollFeeService.getTollFee(inputDates, "Car"), 89);