-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MOSIP-23551: Added validators for DocType and DocCat code in valid document entity #937
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
import javax.persistence.Table; | ||
|
||
import io.mosip.admin.bulkdataupload.entity.id.ValidDocumentID; | ||
import io.mosip.admin.validator.DocCatCode; | ||
import io.mosip.admin.validator.DocTypeCode; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
@@ -43,12 +45,13 @@ public class ValidDocument extends BaseEntity implements Serializable { | |
private static final long serialVersionUID = -3111581667845281498L; | ||
|
||
@Id | ||
@AttributeOverrides({ | ||
@AttributeOverride(name = "docTypeCode", column = @Column(name = "doctyp_code", nullable = false, length = 36)), | ||
@AttributeOverride(name = "docCategoryCode", column = @Column(name = "doccat_code", nullable = false, length = 36)) }) | ||
|
||
@Column(name = "doctyp_code", nullable = false, length = 36) | ||
@DocTypeCode(message = "docType is Invalid") | ||
private String docTypeCode; | ||
|
||
@Id | ||
@Column(name = "doccat_code", nullable = false, length = 36) | ||
@DocCatCode(message = "docCategory is Invalid") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is always better to set error_code in the message instead of message directly. |
||
private String docCategoryCode; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.mosip.admin.validator; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
@Documented | ||
@Constraint(validatedBy = DocCatCodeValidator.class) | ||
@Target({ ElementType.FIELD, ElementType.TYPE_USE, ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DocCatCode { | ||
|
||
String message() default "docCategory is Invalid"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is always better to set error_code in the message instead of message directly. |
||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.mosip.admin.validator; | ||
|
||
import java.util.List; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import io.mosip.admin.bulkdataupload.repositories.DocumentCategoryRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class DocCatCodeValidator implements ConstraintValidator<DocCatCode, String> { | ||
|
||
private List<String> docCatCode; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its a list then why is the variable name singular? |
||
|
||
@Autowired | ||
private DocumentCategoryRepository documentCategoryRepository; | ||
|
||
@Override | ||
public void initialize(DocCatCode constraintAnnotation) { | ||
if(documentCategoryRepository == null){ | ||
/* Note: An additional validation was getting triggered by doInvoke() method of | ||
* RepositoryListItemWriter class with documentCategoryRepository equal to null | ||
* which is not desired. This if clause is being used to escape that additional | ||
* validation step. | ||
*/ | ||
return; | ||
} | ||
docCatCode = documentCategoryRepository.findAllByIsDeletedFalseOrIsDeletedIsNull(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
|
||
if(documentCategoryRepository == null){ | ||
/* Note: An additional validation was getting triggered by doInvoke() method of | ||
* RepositoryListItemWriter class with documentCategoryRepository equal to null | ||
* which is not desired. This if clause is being used to escape that additional | ||
* validation step. | ||
*/ | ||
return true; | ||
} | ||
|
||
if(null != value && !value.isEmpty()) { | ||
return docCatCode.contains(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when a new document category is added? how and when the docCatCode list is refreshed? |
||
} | ||
return false; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.mosip.admin.validator; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
|
||
|
||
|
||
@Documented | ||
@Constraint(validatedBy = DocTypeCodeValidator.class) | ||
@Target({ ElementType.FIELD, ElementType.TYPE_USE, ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DocTypeCode { | ||
|
||
String message() default "docType is Invalid"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is always better to set error_code in the message instead of message directly. |
||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.mosip.admin.validator; | ||
|
||
import java.util.List; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import io.mosip.admin.bulkdataupload.repositories.DocumentTypeRepository; | ||
|
||
public class DocTypeCodeValidator implements ConstraintValidator<DocTypeCode, String> { | ||
|
||
private List<String> docTypeCode; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its a list then why is the variable name singular? |
||
|
||
@Autowired | ||
DocumentTypeRepository documentTypeRepository; | ||
|
||
|
||
@Override | ||
public void initialize(DocTypeCode constraintAnnotation) { | ||
if(documentTypeRepository == null){ | ||
/* Note: An additional validation was getting triggered by doInvoke() method of | ||
* RepositoryListItemWriter class with documentTypeRepository equal to null | ||
* which is not desired. This if clause is being used to escape that additional | ||
* validation step. | ||
*/ | ||
return; | ||
} | ||
docTypeCode = documentTypeRepository.findAllByIsDeletedFalseOrIsDeletedIsNull(); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if(documentTypeRepository == null){ | ||
/* Note: An additional validation was getting triggered by doInvoke() method of | ||
* RepositoryListItemWriter class with documentTypeRepository equal to null | ||
* which is not desired. This if clause is being used to escape that additional | ||
* validation step. | ||
*/ | ||
return true; | ||
} | ||
|
||
if(null != value && !value.isEmpty()) { | ||
return docTypeCode.contains(value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens when a new document category is added? how and when the docCatCode list is refreshed? |
||
} | ||
return false; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,7 +113,7 @@ public void setUp() throws Exception { | |
@Test | ||
@WithUserDetails(value = "zonal-admin") | ||
public void t002lostRidTest() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's rename the test method name based on the agreed convention |
||
String str = "{\r\n \"id\": null,\r\n \"version\": null,\r\n \"responsetime\": \"2019-12-02T09:45:24.512Z\",\r\n \"metadata\": null,\r\n \"response\": [{\"registrationId\":\"1234\",\"registrationDate\":\"2021-12-14 16:29:13,436\"}],\r\n \"errors\": []\r\n}"; | ||
String str = "{\r\n \"id\": null,\r\n \"version\": null,\r\n \"responsetime\": \"2019-12-02T09:45:24.512Z\",\r\n \"metadata\": null,\r\n \"response\": [{\"registrationId\":\"1234\",\"registrationDate\":\"2022-09-14 16:29:13,436\"}],\r\n \"errors\": []\r\n}"; | ||
searchInfoReq.getRequest().setSort(new ArrayList<SortInfo>()); | ||
|
||
mockRestServiceServer.expect(requestTo(lstRidUrl)) | ||
|
@@ -122,7 +122,7 @@ public void t002lostRidTest() throws Exception { | |
AdminDataUtil.checkResponse( | ||
(mockMvc.perform(MockMvcRequestBuilders.post("/lostRid").contentType(MediaType.APPLICATION_JSON) | ||
.content(mapper.writeValueAsString(searchInfoReq))).andReturn()), | ||
null); | ||
"ADMN-LRID-001"); | ||
|
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is always better to set error_code in the message instead of message directly.