Created
August 6, 2014 10:47
-
-
Save roniemicro/dcc97a841f152476aa4b to your computer and use it in GitHub Desktop.
Location validator
This file contains hidden or 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
| package org.sharedhealth.mci.validation.constraints; | |
| import javax.validation.Constraint; | |
| import javax.validation.Payload; | |
| import java.lang.annotation.Documented; | |
| import java.lang.annotation.Retention; | |
| import java.lang.annotation.Target; | |
| import org.sharedhealth.mci.validation.constraintvalidator.LocationValidator; | |
| import static java.lang.annotation.ElementType.FIELD; | |
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
| @Target({FIELD}) | |
| @Retention(RUNTIME) | |
| @Constraint(validatedBy = LocationValidator.class) | |
| @Documented | |
| public @interface Location { | |
| String message() default "Location does not exist"; | |
| Class<?>[] groups() default {}; | |
| Class<? extends Payload>[] payload() default {}; | |
| } |
This file contains hidden or 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
| package org.sharedhealth.mci.validation.constraintvalidator; | |
| import javax.validation.ConstraintValidator; | |
| import javax.validation.ConstraintValidatorContext; | |
| import java.util.regex.Pattern; | |
| import org.apache.commons.lang3.StringUtils; | |
| import org.sharedhealth.mci.validation.constraints.Location; | |
| import org.sharedhealth.mci.web.model.Address; | |
| import org.sharedhealth.mci.web.service.LocationService; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.stereotype.Component; | |
| @Component | |
| public class LocationValidator implements ConstraintValidator<Location, Address> { | |
| private static final Logger logger = LoggerFactory.getLogger(LocationValidator.class); | |
| private LocationService locationService; | |
| @Autowired | |
| public LocationValidator(LocationService locationService) { | |
| this.locationService = locationService; | |
| } | |
| @Override | |
| public void initialize(Location constraintAnnotation) { | |
| } | |
| @Override | |
| public boolean isValid(Address value, ConstraintValidatorContext context) { | |
| if(value == null) return true; | |
| String geoCode = getGeoCode(value); | |
| logger.debug("Validation testing for code : [" + geoCode + "]"); | |
| if(!(Pattern.compile("[\\d]{2,10}").matcher(geoCode).matches())) return false; | |
| try { | |
| org.sharedhealth.mci.web.model.Location location = locationService.findByGeoCode(geoCode).get(); | |
| if(!StringUtils.isBlank(location.getGeoCode())) { | |
| return true; | |
| } | |
| } catch (Exception e) { | |
| return false; | |
| } | |
| return false; | |
| } | |
| private String getGeoCode(Address a) { | |
| return a.getDivisionId() + a.getDistrictId() + a.getUpazillaId() + a.getCityCorporation() + a.getWard(); | |
| } | |
| } |
This file contains hidden or 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
| package org.sharedhealth.mci.web.controller; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import org.mockito.Mock; | |
| import org.sharedhealth.mci.web.model.Address; | |
| import org.sharedhealth.mci.web.model.Location; | |
| import org.sharedhealth.mci.web.model.Patient; | |
| import org.sharedhealth.mci.web.service.LocationService; | |
| import org.sharedhealth.mci.web.service.PatientService; | |
| import org.sharedhealth.mci.web.utils.concurrent.PreResolvedListenableFuture; | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.test.web.servlet.MockMvc; | |
| import org.springframework.test.web.servlet.setup.MockMvcBuilders; | |
| import static org.mockito.Mockito.verify; | |
| import static org.mockito.Mockito.when; | |
| import static org.mockito.MockitoAnnotations.initMocks; | |
| import static org.springframework.http.HttpStatus.CREATED; | |
| import static org.springframework.http.HttpStatus.OK; | |
| import static org.springframework.http.MediaType.APPLICATION_JSON; | |
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | |
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; | |
| public class PatientControllerTest { | |
| @Mock | |
| private PatientService patientService; | |
| @Mock | |
| private LocationService locationService; | |
| private Patient patient; | |
| private Location locaiton; | |
| private MockMvc mockMvc; | |
| private String nationalId = "1234567890123"; | |
| private String birthRegistrationNumber = "12345678901234567"; | |
| private String name = "Roni Kumar Saha"; | |
| private String uid = "11111111111"; | |
| public static final String API_END_POINT = "/api/v1/patients"; | |
| public static final String GEO_CODE = "1004092001"; | |
| @Before | |
| public void setup() { | |
| initMocks(this); | |
| mockMvc = MockMvcBuilders.standaloneSetup(new PatientController(patientService, locationService)).build(); | |
| patient = new Patient(); | |
| patient.setNationalId(nationalId); | |
| patient.setBirthRegistrationNumber(birthRegistrationNumber); | |
| patient.setFirstName("Scott"); | |
| patient.setLastName("Tiger"); | |
| patient.setGender("1"); | |
| patient.setDateOfBirth("2014-12-01"); | |
| Address address = new Address(); | |
| address.setAddressLine("house-10"); | |
| address.setDivisionId("10"); | |
| address.setDistrictId("04"); | |
| address.setUpazillaId("09"); | |
| address.setCityCorporation("20"); | |
| address.setVillage("10"); | |
| address.setWard("01"); | |
| address.setCountry("103"); | |
| patient.setAddress(address); | |
| locaiton = new Location(); | |
| locaiton.setGeoCode(GEO_CODE); | |
| locaiton.setDivisionId("10"); | |
| locaiton.setDistrictId("04"); | |
| locaiton.setUpazillaId("09"); | |
| locaiton.setPaurashavaId("20"); | |
| locaiton.setUnionId("01"); | |
| } | |
| @Test | |
| public void shouldCreatePatientAndReturnHealthId() throws Exception { | |
| String json = new ObjectMapper().writeValueAsString(patient); | |
| String healthId = "healthId-100"; | |
| when(locationService.findByGeoCode(GEO_CODE)).thenReturn(new PreResolvedListenableFuture<>(locaiton)); | |
| when(patientService.create(patient)).thenReturn(new PreResolvedListenableFuture<>(healthId)); | |
| mockMvc.perform(post(API_END_POINT).content(json).contentType(APPLICATION_JSON)) | |
| .andExpect(request().asyncResult(new ResponseEntity<>(healthId, CREATED))); | |
| verify(patientService).create(patient); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment