Skip to content

Commit 20016a4

Browse files
authored
Refactor OTPService with testing and Full JavaDocs (#23)
- Complete JavaDocs for all methods - Remove unused port field from `RedisConfig`. - Remove unneeded `junit-jupiter` maven dependency since it's included with `spring-boot-starter-test`. - Add `testcontainers-redis` maven dependency to include `RedisContainer` instead of `GenericContainer`. - Rename `send` method in `OTPService` to `sendOTP`. - Convert OTP message to be constant. - Add public modifier for exception handler. - Rename tests names and display messages for Should_When convention. - Rename constant strings with MESSAGE suffix.
1 parent 69d704f commit 20016a4

26 files changed

+434
-246
lines changed

config/.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repos:
44
hooks:
55
- id: trailing-whitespace
66
- id: end-of-file-fixer
7-
- id: check-yaml # Validates the syntax of YAML files.
7+
- id: check-yaml
88

99
- repo: local
1010
hooks:

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@
6464
<artifactId>spring-boot-starter-test</artifactId>
6565
<scope>test</scope>
6666
</dependency>
67-
<dependency>
68-
<groupId>org.junit.jupiter</groupId>
69-
<artifactId>junit-jupiter</artifactId>
70-
<version>5.8.1</version>
71-
<scope>test</scope>
72-
</dependency>
7367
<dependency>
7468
<groupId>org.testcontainers</groupId>
7569
<artifactId>junit-jupiter</artifactId>
@@ -82,6 +76,12 @@
8276
<version>1.20.0</version>
8377
<scope>test</scope>
8478
</dependency>
79+
<dependency>
80+
<groupId>com.redis</groupId>
81+
<artifactId>testcontainers-redis</artifactId>
82+
<version>2.2.2</version>
83+
<scope>test</scope>
84+
</dependency>
8585
<!--==================== Redis ====================-->
8686
<dependency>
8787
<groupId>org.springframework.boot</groupId>

src/main/java/com/ittovative/otpservice/OtpServiceApplication.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
public class OtpServiceApplication {
2222

2323
/**
24-
* The entry point of application.
24+
* The entry point of the OTP Service application.
25+
* <p>
26+
* This method starts the Spring Boot application by invoking {@link SpringApplication#run(Class, String...)}.
27+
* It serves as the main entry point to launch the OTP service,
28+
* which handles OTP generation, validation, and related tasks.
29+
* </p>
2530
*
26-
* @param args the input arguments
31+
* @param args The input arguments passed from the command line.
2732
*/
2833
public static void main(final String[] args) {
2934
SpringApplication.run(OtpServiceApplication.class, args);

src/main/java/com/ittovative/otpservice/config/RedisConfig.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
package com.ittovative.otpservice.config;
22

3-
import org.springframework.beans.factory.annotation.Value;
43
import org.springframework.context.annotation.Bean;
54
import org.springframework.context.annotation.Configuration;
65
import org.springframework.data.redis.connection.RedisConnectionFactory;
76
import org.springframework.data.redis.core.RedisTemplate;
87

98
@Configuration
109
public class RedisConfig {
11-
@Value("${spring.data.redis.port}")
12-
private static int port;
13-
14-
public static int getPort() {
15-
return port;
16-
}
1710

1811
/**
19-
* Redis template.
12+
* Creates and configures a {@link RedisTemplate} for interacting with Redis.
13+
* <p>
14+
* This template provides a high-level abstraction for Redis operations using
15+
* the given {@link RedisConnectionFactory} to establish the connection.
2016
*
21-
* @param connectionFactory the connection factory
22-
* @return the redis template
17+
* @param connectionFactory the Redis connection factory used to establish the connection
18+
* @return a configured {@link RedisTemplate} for Redis operations
2319
*/
2420
@Bean
2521
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {

src/main/java/com/ittovative/otpservice/config/TwilioConfig.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ public class TwilioConfig {
1313
@Value("${twilio.auth-token}")
1414
private String authToken;
1515

16-
@Value("${twilio.verified-number}")
17-
private static String verifiedNumber;
18-
19-
public static String getVerifiedNumber() {
20-
return verifiedNumber;
21-
}
22-
2316
/**
24-
* Initialize Twilio with account SID and auth token.
17+
* Initializes the Twilio API with the provided account SID and authentication token.
18+
* <p>
19+
* This method is annotated with {@code @PostConstruct}, ensuring that it is called
20+
* automatically after the Spring container has set up all the properties and dependencies
21+
* for the {@code TwilioConfig} bean. It uses the {@link com.twilio.Twilio#init(String, String)}
22+
* method to initialize the Twilio SDK.
23+
* </p>
24+
*
25+
* <p>
26+
* This configuration allows the application to interact with the Twilio API for sending SMS,
27+
* making calls, or using other Twilio services.
28+
* </p>
2529
*/
2630
@PostConstruct
2731
public void initialize() {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.ittovative.otpservice.constant;
22

3-
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION;
3+
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION_MESSAGE;
44

55
public final class APIResponseConstant {
6-
public static final String OTP_SENT = "OTP sent successfully!";
7-
public static final String TOKEN_VERIFIED = "Token verified successfully!";
6+
public static final String OTP_SENT_MESSAGE = "OTP sent successfully!";
7+
public static final String TOKEN_VERIFIED_MESSAGE = "Token verified successfully!";
88

99
private APIResponseConstant() {
10-
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
10+
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION_MESSAGE);
1111
}
1212
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.ittovative.otpservice.constant;
22

3-
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION;
3+
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION_MESSAGE;
44

55
public final class AspectConstant {
66
public static final String BEFORE_MESSAGE = "Executing ===> {}.{} with arguments: [{}]";
77
public static final String AFTER_RETURN_MESSAGE = "Finished ===> {}.{} with arguments: [{}] and returned {}";
88
public static final String AFTER_THROW_MESSAGE = "Exception {} in ===> {}.{} with arguments: [{}]";
99

1010
private AspectConstant() {
11-
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
11+
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION_MESSAGE);
1212
}
1313
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.ittovative.otpservice.constant;
22

33
public final class ExceptionConstant {
4-
public static final String UTILITY_CLASS_INSTANTIATION = "Utility class should not be instantiated!";
5-
public static final String VALIDATION_ERROR = "Validation error!";
6-
public static final String INVALID_PHONE_NUMBER_FORMAT =
4+
public static final String UTILITY_CLASS_INSTANTIATION_MESSAGE = "Utility class should not be instantiated!";
5+
public static final String VALIDATION_ERROR_MESSAGE = "Validation error!";
6+
public static final String INVALID_PHONE_NUMBER_FORMAT_MESSAGE =
77
"Invalid phone number format! (only digits, plus-sign and dashes are allowed).";
8-
public static final String INVALID_TOKEN_FORMAT = "Token must be a 6-digit number!";
9-
public static final String INVALID_TOKEN = "Invalid token!";
10-
public static final String TOKEN_EXPIRED = "Token has expired!";
8+
public static final String INVALID_TOKEN_FORMAT_MESSAGE = "Token must be a 6-digit number!";
9+
public static final String INVALID_TOKEN_MESSAGE = "Invalid token!";
10+
public static final String TOKEN_EXPIRED_MESSAGE = "Token has expired!";
1111

1212

1313
private ExceptionConstant() {
14-
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
14+
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION_MESSAGE);
1515
}
1616
}

src/main/java/com/ittovative/otpservice/constant/HTTPConstant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.ittovative.otpservice.constant;
22

3-
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION;
3+
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION_MESSAGE;
44

55
public final class HTTPConstant {
66
public static final String APPLICATION_JSON = "application/json";
@@ -19,6 +19,6 @@ public final class HTTPConstant {
1919
public static final String TOO_MANY_REQUESTS = "429";
2020

2121
private HTTPConstant() {
22-
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
22+
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION_MESSAGE);
2323
}
2424
}

src/main/java/com/ittovative/otpservice/constant/RedisConstant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.springframework.beans.factory.annotation.Value;
44

5-
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION;
5+
import static com.ittovative.otpservice.constant.ExceptionConstant.UTILITY_CLASS_INSTANTIATION_MESSAGE;
66

77
public final class RedisConstant {
88
public static final String VERIFICATION_TOKEN_HASH = "VerificationToken";
@@ -11,6 +11,6 @@ public final class RedisConstant {
1111
public static final Integer EXPIRY_DATE_IN_MIN = 5;
1212

1313
private RedisConstant() {
14-
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION);
14+
throw new IllegalStateException(UTILITY_CLASS_INSTANTIATION_MESSAGE);
1515
}
1616
}

0 commit comments

Comments
 (0)