diff --git a/src/main/java/com/ittovative/otpservice/config/SwaggerConfig.java b/src/main/java/com/ittovative/otpservice/config/SwaggerConfig.java index 9b37bbf..0dbbffd 100644 --- a/src/main/java/com/ittovative/otpservice/config/SwaggerConfig.java +++ b/src/main/java/com/ittovative/otpservice/config/SwaggerConfig.java @@ -1,7 +1,8 @@ package com.ittovative.otpservice.config; -import com.ittovative.otpservice.dto.OtpRequestDto; -import com.ittovative.otpservice.dto.TokenDto; +import com.ittovative.otpservice.dto.OTPRequestDTO; +import com.ittovative.otpservice.dto.TokenDTO; +import com.ittovative.otpservice.util.APIResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.ExampleObject; @@ -9,13 +10,12 @@ import io.swagger.v3.oas.annotations.parameters.RequestBody; import io.swagger.v3.oas.annotations.responses.ApiResponse; import org.apache.coyote.BadRequestException; -import org.springframework.http.ResponseEntity; -import static com.ittovative.otpservice.constant.HttpConstant.APPLICATION_JSON; -import static com.ittovative.otpservice.constant.HttpConstant.BAD_REQUEST; -import static com.ittovative.otpservice.constant.HttpConstant.CREATED; -import static com.ittovative.otpservice.constant.HttpConstant.NOT_FOUND; -import static com.ittovative.otpservice.constant.HttpConstant.OK; +import static com.ittovative.otpservice.constant.HTTPConstant.APPLICATION_JSON; +import static com.ittovative.otpservice.constant.HTTPConstant.BAD_REQUEST; +import static com.ittovative.otpservice.constant.HTTPConstant.CREATED; +import static com.ittovative.otpservice.constant.HTTPConstant.NOT_FOUND; +import static com.ittovative.otpservice.constant.HTTPConstant.OK; import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_PHONE_NUMBER_FORMAT_RESPONSE_DESCRIPTION; import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_PHONE_NUMBER_FORMAT_RESPONSE_EXAMPLE; import static com.ittovative.otpservice.constant.SwaggerConstant.INVALID_TOKEN_RESPONSE_DESCRIPTION; @@ -43,7 +43,7 @@ public interface SwaggerConfig { description = SEND_OTP_REQUEST_BODY_DESCRIPTION, content = @Content( mediaType = APPLICATION_JSON, - schema = @Schema(implementation = OtpRequestDto.class), + schema = @Schema(implementation = OTPRequestDTO.class), examples = @ExampleObject(value = SEND_OTP_REQUEST_BODY_EXAMPLE) ) ), @@ -53,7 +53,7 @@ public interface SwaggerConfig { description = OTP_SENT_RESPONSE_DESCRIPTION, content = @Content( mediaType = APPLICATION_JSON, - schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class), + schema = @Schema(implementation = APIResponse.class), examples = @ExampleObject(value = OTP_SENT_RESPONSE_EXAMPLE) ) ), @@ -62,13 +62,13 @@ public interface SwaggerConfig { description = INVALID_PHONE_NUMBER_FORMAT_RESPONSE_DESCRIPTION, content = @Content( mediaType = APPLICATION_JSON, - schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class), + schema = @Schema(implementation = APIResponse.class), examples = @ExampleObject(value = INVALID_PHONE_NUMBER_FORMAT_RESPONSE_EXAMPLE) ) ) } ) - ResponseEntity> sendMessage(OtpRequestDto otpRequestDto); + APIResponse sendMessage(OTPRequestDTO otpRequestDto); @Operation(summary = VERIFY_TOKEN_SUMMARY, description = VERIFY_TOKEN_DESCRIPTION, requestBody = @RequestBody( @@ -76,7 +76,7 @@ public interface SwaggerConfig { description = VERIFY_TOKEN_REQUEST_BODY_DESCRIPTION, content = @Content( mediaType = APPLICATION_JSON, - schema = @Schema(implementation = OtpRequestDto.class), + schema = @Schema(implementation = OTPRequestDTO.class), examples = @ExampleObject(value = VERIFY_TOKEN_REQUEST_BODY_EXAMPLE) ) ), @@ -86,7 +86,7 @@ public interface SwaggerConfig { description = TOKEN_VERIFIED_RESPONSE_DESCRIPTION, content = @Content( mediaType = APPLICATION_JSON, - schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class), + schema = @Schema(implementation = APIResponse.class), examples = @ExampleObject(value = TOKEN_VERIFIED_RESPONSE_EXAMPLE) ) ), @@ -95,7 +95,7 @@ public interface SwaggerConfig { description = TOKEN_EXPIRED_RESPONSE_DESCRIPTION, content = @Content( mediaType = APPLICATION_JSON, - schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class), + schema = @Schema(implementation = APIResponse.class), examples = @ExampleObject(value = TOKEN_EXPIRED_RESPONSE_EXAMPLE) ) ), @@ -104,12 +104,11 @@ public interface SwaggerConfig { description = INVALID_TOKEN_RESPONSE_DESCRIPTION, content = @Content( mediaType = APPLICATION_JSON, - schema = @Schema(implementation = com.ittovative.otpservice.util.ApiResponse.class), + schema = @Schema(implementation = APIResponse.class), examples = @ExampleObject(value = INVALID_TOKEN_RESPONSE_EXAMPLE) ) ), } ) - ResponseEntity> verify( - TokenDto tokenDto) throws BadRequestException; + APIResponse verify(TokenDTO tokenDto) throws BadRequestException; } diff --git a/src/main/java/com/ittovative/otpservice/constant/ApiResponseConstant.java b/src/main/java/com/ittovative/otpservice/constant/APIResponseConstant.java similarity index 82% rename from src/main/java/com/ittovative/otpservice/constant/ApiResponseConstant.java rename to src/main/java/com/ittovative/otpservice/constant/APIResponseConstant.java index 810ab01..fb4bf2f 100644 --- a/src/main/java/com/ittovative/otpservice/constant/ApiResponseConstant.java +++ b/src/main/java/com/ittovative/otpservice/constant/APIResponseConstant.java @@ -2,11 +2,11 @@ import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION; -public final class ApiResponseConstant { +public final class APIResponseConstant { public static final String OTP_SENT = "OTP sent successfully!"; public static final String TOKEN_VERIFIED = "Token verified successfully!"; - private ApiResponseConstant() { + private APIResponseConstant() { throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION); } } diff --git a/src/main/java/com/ittovative/otpservice/constant/HttpConstant.java b/src/main/java/com/ittovative/otpservice/constant/HTTPConstant.java similarity index 93% rename from src/main/java/com/ittovative/otpservice/constant/HttpConstant.java rename to src/main/java/com/ittovative/otpservice/constant/HTTPConstant.java index 9d44ace..d45032d 100644 --- a/src/main/java/com/ittovative/otpservice/constant/HttpConstant.java +++ b/src/main/java/com/ittovative/otpservice/constant/HTTPConstant.java @@ -2,7 +2,7 @@ import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION; -public final class HttpConstant { +public final class HTTPConstant { public static final String APPLICATION_JSON = "application/json"; public static final String OK = "200"; @@ -18,7 +18,7 @@ public final class HttpConstant { public static final String METHOD_NOT_ALLOWED = "405"; public static final String TOO_MANY_REQUESTS = "429"; - private HttpConstant() { + private HTTPConstant() { throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION); } } diff --git a/src/main/java/com/ittovative/otpservice/controller/OTPController.java b/src/main/java/com/ittovative/otpservice/controller/OTPController.java new file mode 100644 index 0000000..9c5e30a --- /dev/null +++ b/src/main/java/com/ittovative/otpservice/controller/OTPController.java @@ -0,0 +1,57 @@ +package com.ittovative.otpservice.controller; + +import com.ittovative.otpservice.config.SwaggerConfig; +import com.ittovative.otpservice.constant.APIResponseConstant; +import com.ittovative.otpservice.constant.SwaggerConstant; +import com.ittovative.otpservice.dto.OTPRequestDTO; +import com.ittovative.otpservice.dto.TokenDTO; +import com.ittovative.otpservice.service.OTPService; +import com.ittovative.otpservice.util.APIResponse; +import com.ittovative.otpservice.util.ResponseUtil; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import org.apache.coyote.BadRequestException; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1/sms") +@Tag(name = SwaggerConstant.CONTROLLER_NAME, description = SwaggerConstant.CONTROLLER_DESCRIPTION) +public class OTPController implements SwaggerConfig { + private final OTPService otpService; + + public OTPController(OTPService otpService) { + this.otpService = otpService; + } + + /** + * Send message response entity. + * + * @param otpRequestDto the otp request dto + * @return the response entity + */ + @Override + @PostMapping + public APIResponse sendMessage(@RequestBody @Valid OTPRequestDTO otpRequestDto) { + otpService.send(otpRequestDto); + return ResponseUtil.createUnifiedResponse(HttpStatus.CREATED.value(), APIResponseConstant.OTP_SENT, null); + } + + /** + * Verify response entity. + * + * @param tokenDto the verify otp request dto + * @return the response entity + * @throws BadRequestException the bad request exception + */ + @Override + @PostMapping("/verify") + public APIResponse verify(@RequestBody @Valid TokenDTO tokenDto) + throws BadRequestException { + otpService.verifyToken(tokenDto); + return ResponseUtil.createUnifiedResponse(HttpStatus.OK.value(), APIResponseConstant.TOKEN_VERIFIED, null); + } +} diff --git a/src/main/java/com/ittovative/otpservice/controller/OtpController.java b/src/main/java/com/ittovative/otpservice/controller/OtpController.java deleted file mode 100644 index 7731bd5..0000000 --- a/src/main/java/com/ittovative/otpservice/controller/OtpController.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.ittovative.otpservice.controller; - -import com.ittovative.otpservice.config.SwaggerConfig; -import com.ittovative.otpservice.dto.OtpRequestDto; -import com.ittovative.otpservice.dto.TokenDto; -import com.ittovative.otpservice.service.OtpService; -import com.ittovative.otpservice.util.ApiResponse; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.validation.Valid; -import org.apache.coyote.BadRequestException; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import static com.ittovative.otpservice.constant.ApiResponseConstant.OTP_SENT; -import static com.ittovative.otpservice.constant.ApiResponseConstant.TOKEN_VERIFIED; -import static com.ittovative.otpservice.constant.SwaggerConstant.CONTROLLER_DESCRIPTION; -import static com.ittovative.otpservice.constant.SwaggerConstant.CONTROLLER_NAME; - -@RestController -@RequestMapping("/api/v1/sms") -@Tag(name = CONTROLLER_NAME, description = CONTROLLER_DESCRIPTION) -public class OtpController implements SwaggerConfig { - private final OtpService otpService; - - public OtpController(OtpService otpService) { - this.otpService = otpService; - } - - /** - * Send message response entity. - * - * @param otpRequestDto the otp request dto - * @return the response entity - */ - @PostMapping - public ResponseEntity> sendMessage(@RequestBody @Valid OtpRequestDto otpRequestDto) { - otpService.send(otpRequestDto); - - ApiResponse apiResponse = new ApiResponse<>(HttpStatus.CREATED.value(), OTP_SENT, null); - return new ResponseEntity<>(apiResponse, HttpStatus.CREATED); - } - - /** - * Verify response entity. - * - * @param tokenDto the verify otp request dto - * @return the response entity - * @throws BadRequestException the bad request exception - */ - @PostMapping("/verify") - public ResponseEntity> verify(@RequestBody @Valid TokenDto tokenDto) - throws BadRequestException { - otpService.verifyToken(tokenDto); - - ApiResponse apiResponse = new ApiResponse<>(HttpStatus.OK.value(), TOKEN_VERIFIED, null); - return new ResponseEntity<>(apiResponse, HttpStatus.OK); - } -} diff --git a/src/main/java/com/ittovative/otpservice/dto/OtpRequestDto.java b/src/main/java/com/ittovative/otpservice/dto/OTPRequestDTO.java similarity index 92% rename from src/main/java/com/ittovative/otpservice/dto/OtpRequestDto.java rename to src/main/java/com/ittovative/otpservice/dto/OTPRequestDTO.java index 30d07d7..60ba06e 100644 --- a/src/main/java/com/ittovative/otpservice/dto/OtpRequestDto.java +++ b/src/main/java/com/ittovative/otpservice/dto/OTPRequestDTO.java @@ -5,6 +5,6 @@ import static com.ittovative.otpservice.constant.ExceptionConstant.INVALID_PHONE_NUMBER_FORMAT; -public record OtpRequestDto( +public record OTPRequestDTO( @NotBlank @Pattern(regexp = "^[+0-9-]+$", message = INVALID_PHONE_NUMBER_FORMAT) String toPhoneNumber) { } diff --git a/src/main/java/com/ittovative/otpservice/dto/TokenDto.java b/src/main/java/com/ittovative/otpservice/dto/TokenDTO.java similarity index 95% rename from src/main/java/com/ittovative/otpservice/dto/TokenDto.java rename to src/main/java/com/ittovative/otpservice/dto/TokenDTO.java index 4b5037e..38557ac 100644 --- a/src/main/java/com/ittovative/otpservice/dto/TokenDto.java +++ b/src/main/java/com/ittovative/otpservice/dto/TokenDTO.java @@ -6,7 +6,7 @@ import static com.ittovative.otpservice.constant.ExceptionConstant.INVALID_PHONE_NUMBER_FORMAT; import static com.ittovative.otpservice.constant.ExceptionConstant.INVALID_TOKEN_FORMAT; -public record TokenDto( +public record TokenDTO( @NotBlank @Pattern(regexp = "^[+0-9-]+$", message = INVALID_PHONE_NUMBER_FORMAT) String phoneNumber, @NotBlank @Pattern(regexp = "^\\d{6}$", message = INVALID_TOKEN_FORMAT) String token) { } diff --git a/src/main/java/com/ittovative/otpservice/exception/GeneralExceptionHandler.java b/src/main/java/com/ittovative/otpservice/exception/GeneralExceptionHandler.java index 151feb8..9c0568f 100644 --- a/src/main/java/com/ittovative/otpservice/exception/GeneralExceptionHandler.java +++ b/src/main/java/com/ittovative/otpservice/exception/GeneralExceptionHandler.java @@ -1,34 +1,31 @@ package com.ittovative.otpservice.exception; -import com.ittovative.otpservice.util.ApiResponse; +import com.ittovative.otpservice.constant.ExceptionConstant; +import com.ittovative.otpservice.util.APIResponse; +import com.ittovative.otpservice.util.ResponseUtil; import com.twilio.exception.TwilioException; import org.apache.coyote.BadRequestException; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.context.request.WebRequest; import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; -import static com.ittovative.otpservice.constant.ExceptionConstant.VALIDATION_ERROR; - @ControllerAdvice public class GeneralExceptionHandler { /** * Handle validation exceptions response entity. * - * @param exception the exception - * @param webRequest the web request + * @param exception the exception * @return the response entity */ @ExceptionHandler(MethodArgumentNotValidException.class) - public ResponseEntity>> handleValidationExceptions( - MethodArgumentNotValidException exception, WebRequest webRequest) { + public APIResponse> handleValidationExceptions( + MethodArgumentNotValidException exception) { Map errors = new HashMap<>(); exception @@ -41,66 +38,57 @@ public ResponseEntity>> handleValidationExceptio errors.put(fieldName, errorMessage); }); - ApiResponse> apiResponse = - new ApiResponse<>(HttpStatus.BAD_REQUEST.value(), VALIDATION_ERROR, errors); - return new ResponseEntity<>(apiResponse, HttpStatus.BAD_REQUEST); + return ResponseUtil.createUnifiedResponse( + HttpStatus.BAD_REQUEST.value(), ExceptionConstant.VALIDATION_ERROR, errors); } /** * Handle response entity. * - * @param exception the exception - * @param webRequest the web request + * @param exception the exception * @return the response entity */ @ExceptionHandler(NoSuchElementException.class) - ResponseEntity> handle( - NoSuchElementException exception, WebRequest webRequest) { - ApiResponse apiResponse = - new ApiResponse<>(HttpStatus.NOT_FOUND.value(), exception.getMessage(), null); - return new ResponseEntity<>(apiResponse, HttpStatus.NOT_FOUND); + APIResponse handle( + NoSuchElementException exception) { + return ResponseUtil.createUnifiedResponse(HttpStatus.NOT_FOUND.value(), + exception.getMessage(), null); } /** * Handle response entity. * - * @param exception the exception - * @param webRequest the web request + * @param exception the exception * @return the response entity */ @ExceptionHandler(TwilioException.class) - ResponseEntity> handle(TwilioException exception, WebRequest webRequest) { - ApiResponse apiResponse = - new ApiResponse<>(HttpStatus.BAD_REQUEST.value(), exception.getMessage(), null); - return new ResponseEntity<>(apiResponse, HttpStatus.BAD_REQUEST); + APIResponse handle(TwilioException exception) { + return ResponseUtil.createUnifiedResponse(HttpStatus.BAD_REQUEST.value(), + exception.getMessage(), null); } /** * Handle response entity. * - * @param exception the exception - * @param webRequest the web request + * @param exception the exception * @return the response entity */ @ExceptionHandler(BadRequestException.class) - ResponseEntity> handle( - BadRequestException exception, WebRequest webRequest) { - ApiResponse apiResponse = - new ApiResponse<>(HttpStatus.BAD_REQUEST.value(), exception.getMessage(), null); - return new ResponseEntity<>(apiResponse, HttpStatus.BAD_REQUEST); + APIResponse handle( + BadRequestException exception) { + return ResponseUtil.createUnifiedResponse(HttpStatus.BAD_REQUEST.value(), + exception.getMessage(), null); } /** * Handle response entity. * - * @param exception the exception - * @param webRequest the web request + * @param exception the exception * @return the response entity */ @ExceptionHandler(Exception.class) - ResponseEntity> handle(Exception exception, WebRequest webRequest) { - ApiResponse apiResponse = - new ApiResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getMessage(), null); - return new ResponseEntity<>(apiResponse, HttpStatus.INTERNAL_SERVER_ERROR); + APIResponse handle(Exception exception) { + return ResponseUtil.createUnifiedResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), + exception.getMessage(), null); } } diff --git a/src/main/java/com/ittovative/otpservice/aspect/LoggingAspect.java b/src/main/java/com/ittovative/otpservice/log/LoggingAspect.java similarity index 97% rename from src/main/java/com/ittovative/otpservice/aspect/LoggingAspect.java rename to src/main/java/com/ittovative/otpservice/log/LoggingAspect.java index 079fae1..1f42fa6 100644 --- a/src/main/java/com/ittovative/otpservice/aspect/LoggingAspect.java +++ b/src/main/java/com/ittovative/otpservice/log/LoggingAspect.java @@ -1,4 +1,4 @@ -package com.ittovative.otpservice.aspect; +package com.ittovative.otpservice.log; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; diff --git a/src/main/java/com/ittovative/otpservice/service/OtpService.java b/src/main/java/com/ittovative/otpservice/service/OTPService.java similarity index 89% rename from src/main/java/com/ittovative/otpservice/service/OtpService.java rename to src/main/java/com/ittovative/otpservice/service/OTPService.java index 5f4f78a..766ea00 100644 --- a/src/main/java/com/ittovative/otpservice/service/OtpService.java +++ b/src/main/java/com/ittovative/otpservice/service/OTPService.java @@ -1,7 +1,7 @@ package com.ittovative.otpservice.service; -import com.ittovative.otpservice.dto.OtpRequestDto; -import com.ittovative.otpservice.dto.TokenDto; +import com.ittovative.otpservice.dto.OTPRequestDTO; +import com.ittovative.otpservice.dto.TokenDTO; import org.apache.coyote.BadRequestException; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -9,7 +9,7 @@ import java.security.SecureRandom; @Service -public class OtpService { +public class OTPService { private final TwilioSenderService twilioSenderService; private final VerificationService verificationService; private final String fromPhoneNumber; @@ -23,7 +23,7 @@ public class OtpService { * @param twilioSenderService the twilio sender service * @param verificationService the verification service */ - public OtpService( + public OTPService( @Value("${twilio.sender-number}") final String fromPhoneNumber, final TwilioSenderService twilioSenderService, final VerificationService verificationService) { @@ -50,7 +50,7 @@ public String generateOtp() { * @param otpRequestDto the otp request dto * @return the string */ - public String send(OtpRequestDto otpRequestDto) { + public String send(OTPRequestDTO otpRequestDto) { String otp = generateOtp(); String receiverPhoneNumber = otpRequestDto.toPhoneNumber(); String smsMessage = "Here is your otp : " + otp; @@ -65,7 +65,7 @@ public String send(OtpRequestDto otpRequestDto) { * @param tokenDto the verify otp request dto * @throws BadRequestException the bad request exception */ - public void verifyToken(TokenDto tokenDto) throws BadRequestException { + public void verifyToken(TokenDTO tokenDto) throws BadRequestException { String phoneNumber = tokenDto.phoneNumber(); String token = tokenDto.token(); verificationService.validateUserToken(phoneNumber, token); diff --git a/src/main/java/com/ittovative/otpservice/util/APIResponse.java b/src/main/java/com/ittovative/otpservice/util/APIResponse.java new file mode 100644 index 0000000..301a319 --- /dev/null +++ b/src/main/java/com/ittovative/otpservice/util/APIResponse.java @@ -0,0 +1,4 @@ +package com.ittovative.otpservice.util; + +public record APIResponse(int statusCode, String message, T body) { +} diff --git a/src/main/java/com/ittovative/otpservice/util/ApiResponse.java b/src/main/java/com/ittovative/otpservice/util/ApiResponse.java deleted file mode 100644 index 6c8533d..0000000 --- a/src/main/java/com/ittovative/otpservice/util/ApiResponse.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.ittovative.otpservice.util; - -import java.time.LocalDateTime; - -public class ApiResponse { - private final int statusCode; - private final String message; - private final LocalDateTime timestamp; - private final T body; - - public ApiResponse(int statusCode, String message, T body) { - this.statusCode = statusCode; - this.message = message; - this.timestamp = LocalDateTime.now(); - this.body = body; - } - - public int getStatusCode() { - return statusCode; - } - - public String getMessage() { - return message; - } - - public LocalDateTime getTimestamp() { - return timestamp; - } - - public T getBody() { - return body; - } -} diff --git a/src/main/java/com/ittovative/otpservice/util/ResponseUtil.java b/src/main/java/com/ittovative/otpservice/util/ResponseUtil.java new file mode 100644 index 0000000..33cd936 --- /dev/null +++ b/src/main/java/com/ittovative/otpservice/util/ResponseUtil.java @@ -0,0 +1,20 @@ +package com.ittovative.otpservice.util; + +public final class ResponseUtil { + + /** + * Create unified response api response. + * + * @param the type parameter + * @param statusCode the status code + * @param message the message + * @param body the body + * @return the api response + */ + public static APIResponse createUnifiedResponse(int statusCode, String message, T body) { + return new APIResponse<>(statusCode, message, body); + } + + private ResponseUtil() { + } +} diff --git a/src/test/java/com/ittovative/otpservice/OtpServiceApplicationTests.java b/src/test/java/com/ittovative/otpservice/OTPServiceApplicationTests.java similarity index 89% rename from src/test/java/com/ittovative/otpservice/OtpServiceApplicationTests.java rename to src/test/java/com/ittovative/otpservice/OTPServiceApplicationTests.java index a71848c..6690c37 100644 --- a/src/test/java/com/ittovative/otpservice/OtpServiceApplicationTests.java +++ b/src/test/java/com/ittovative/otpservice/OTPServiceApplicationTests.java @@ -6,7 +6,7 @@ @SpringBootTest @DisplayName("Main Application") -class OtpServiceApplicationTests { +class OTPServiceApplicationTests { @Test @DisplayName("Load application context") diff --git a/src/test/java/com/ittovative/otpservice/service/OtpServiceTest.java b/src/test/java/com/ittovative/otpservice/service/OTPServiceTest.java similarity index 82% rename from src/test/java/com/ittovative/otpservice/service/OtpServiceTest.java rename to src/test/java/com/ittovative/otpservice/service/OTPServiceTest.java index cef8ad9..be54705 100644 --- a/src/test/java/com/ittovative/otpservice/service/OtpServiceTest.java +++ b/src/test/java/com/ittovative/otpservice/service/OTPServiceTest.java @@ -1,7 +1,7 @@ package com.ittovative.otpservice.service; -import com.ittovative.otpservice.dto.OtpRequestDto; -import com.ittovative.otpservice.dto.TokenDto; +import com.ittovative.otpservice.dto.OTPRequestDTO; +import com.ittovative.otpservice.dto.TokenDTO; import com.twilio.exception.ApiException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -22,7 +22,7 @@ @SpringBootTest @Testcontainers -class OtpServiceTest { +class OTPServiceTest { @Value("${twilio.verified-number}") private String verifiedNumber; private static GenericContainer redis; @@ -33,7 +33,7 @@ class OtpServiceTest { @Mock private RedisConnectionFactory redisConnectionFactoryMock; @SpyBean - private OtpService otpService; + private OTPService otpService; @BeforeAll static void beforeAll() { @@ -50,28 +50,28 @@ public void setUp() { @Disabled("To avoid Free Twilio Limit") @DisplayName("Send ") void sendSuccessfulMessage() { - String actualToken = otpService.send(new OtpRequestDto(verifiedNumber)); + String actualToken = otpService.send(new OTPRequestDTO(verifiedNumber)); Assertions.assertDoesNotThrow( - () -> otpService.verifyToken(new TokenDto(verifiedNumber, actualToken))); + () -> otpService.verifyToken(new TokenDTO(verifiedNumber, actualToken))); } @Test @DisplayName("Sending an unsuccessful message : Too short of a Phone Number") void sendMessageToAWrongPhoneNumber() { - Assertions.assertThrows(Exception.class, () -> otpService.send(new OtpRequestDto("+1111"))); + Assertions.assertThrows(Exception.class, () -> otpService.send(new OTPRequestDTO("+1111"))); } @Test @DisplayName("Sending an unsuccessful message : Sender and Receiver have the same Phone Number") void sendMessageToYourself() { Assertions.assertThrows( - Exception.class, () -> otpService.send(new OtpRequestDto("+15075541680"))); + Exception.class, () -> otpService.send(new OTPRequestDTO("+15075541680"))); } @Test @DisplayName("Sending an unsuccessful message : Sending without a code at the start of the message") void sendToAnUncodedPhoneNumber() { Assertions.assertThrows( - ApiException.class, () -> otpService.send(new OtpRequestDto("201007540077"))); + ApiException.class, () -> otpService.send(new OTPRequestDTO("201007540077"))); } }