-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Venkata Saidurga Polamraju <[email protected]>
- Loading branch information
1 parent
23c1629
commit f068bec
Showing
8 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/Purpose.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.mosip.esignet.api.validator; | ||
|
||
import io.mosip.esignet.api.util.ErrorConstants; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({FIELD, TYPE_USE}) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = PurposeValidator.class) | ||
@Documented | ||
public @interface Purpose { | ||
String message() default ErrorConstants.INVALID_PURPOSE; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
22 changes: 22 additions & 0 deletions
22
esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/PurposeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.mosip.esignet.api.validator; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.util.StringUtils; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class PurposeValidator implements ConstraintValidator<Purpose, String> { | ||
|
||
@Value("${mosip.esignet.claim-detail.purpose.min-length}") | ||
private int minLength; | ||
|
||
@Value("${mosip.esignet.claim-detail.purpose.max-length}") | ||
private int maxLength; | ||
|
||
@Override | ||
public boolean isValid(String purpose, ConstraintValidatorContext constraintValidatorContext) { | ||
int length = StringUtils.hasText(purpose) ? purpose.length() : 0; | ||
return length >= minLength && length <= maxLength; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
esignet-integration-api/src/test/java/io/mosip/esignet/api/validator/ValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.mosip.esignet.api.validator; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.mockito.Mock; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ValidatorTest { | ||
|
||
@InjectMocks | ||
private PurposeValidator purposeValidator; | ||
|
||
@Mock | ||
private ConstraintValidatorContext constraintValidatorContext; | ||
|
||
@Before | ||
public void setUp() { | ||
ReflectionTestUtils.setField(purposeValidator, "minLength", 3); | ||
ReflectionTestUtils.setField(purposeValidator, "maxLength", 300); | ||
} | ||
|
||
@Test | ||
public void testIsValid_WithValidPurpose_thenPass() { | ||
String purpose = "Purpose"; | ||
boolean isValid = purposeValidator.isValid(purpose, constraintValidatorContext); | ||
assertTrue(isValid); | ||
} | ||
|
||
@Test | ||
public void testIsValid_WithPurposeWithInvalidLength_thenFail() { | ||
String purpose = "In"; | ||
boolean isValid = purposeValidator.isValid(purpose, constraintValidatorContext); | ||
assertFalse(isValid); | ||
} | ||
|
||
@Test | ||
public void testIsValid_WithNullPurpose_theFail() { | ||
String purpose = null; | ||
boolean isValid = purposeValidator.isValid(purpose, constraintValidatorContext); | ||
assertFalse(isValid); | ||
} | ||
|
||
@Test | ||
public void testIsValid_WithEmptyPurpose_thenFail() { | ||
String purpose = ""; | ||
boolean isValid = purposeValidator.isValid(purpose, constraintValidatorContext); | ||
assertFalse(isValid); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters