package org.openmrs.validator;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Collections;

import org.junit.Test;
import org.openmrs.Location;
import org.openmrs.PatientIdentifierType;
import org.openmrs.api.APIException;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.test.Verifies;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Tests methods on the {@link ValidateUtil} class.
 */
public class ValidateUtilTest extends BaseContextSensitiveTest {
	
	/**
	 * @see {@link ValidateUtil#validate(Object)}
	 */
	@Test(expected = APIException.class)
	@Verifies(value = "should throw APIException if errors occur during validation", method = "validate(Object)")
	public void validate_shouldThrowAPIExceptionIfErrorsOccurDuringValidation() throws Exception {
		ValidateUtil util = new ValidateUtil();
		util.setValidators(Collections.singletonList((Validator) new LocationValidator()));
		
		Location loc = new Location();
		ValidateUtil.validate(loc);
	}
	
	/**
	 * @see {@link ValidateUtil#validateFieldLengths(org.springframework.validation.Errors, Class, String...)}
	 */
	@Test
	@Verifies(value = "fail validation if name field length is too long", method = "validateFieldLengths(org.springframework.validation.Errors, Class, String...)")
	public void validateFieldLength_shouldRejectValueWhenNameIsToLong() {
		PatientIdentifierType patientIdentifierType = new PatientIdentifierType();
		patientIdentifierType.setName("asdfghjkl asdfghjkl asdfghjkl asdfghjkl asdfghjkl xx");
		
		BindException errors = new BindException(patientIdentifierType, "patientIdentifierType");
		ValidateUtil.validateFieldLengths(errors, PatientIdentifierType.class, "name");
		assertTrue(errors.hasFieldErrors("name"));
	}
	
	/**
	 * @see {@link ValidateUtil#validateFieldLengths(org.springframework.validation.Errors, Class, String...)}
	 */
	@Test
	@Verifies(value = "pass validation if name field length is equal to maximum length", method = "validateFieldLengths(org.springframework.validation.Errors, Class, String...)")
	public void validateFieldLength_shouldNotRejectValueWhenNameIsEqualMax() {
		PatientIdentifierType patientIdentifierType = new PatientIdentifierType();
		patientIdentifierType.setName("asdfghjkl asdfghjkl asdfghjkl asdfghjkl asdfghjkl ");
		
		BindException errors = new BindException(patientIdentifierType, "patientIdentifierType");
		ValidateUtil.validateFieldLengths(errors, PatientIdentifierType.class, "name");
		assertFalse(errors.hasFieldErrors("name"));
	}

	/**
     * @see ValidateUtil#validate(Object,Errors)
     * @verifies populate errors if object invalid
     */
    @Test
    public void validate_shouldPopulateErrorsIfObjectInvalid() throws Exception {
    	ValidateUtil util = new ValidateUtil();
		util.setValidators(Collections.singletonList((Validator) new LocationValidator()));
		
		Location loc = new Location();
		Errors errors = new BindException(loc, "");
		ValidateUtil.validate(loc, errors);
		
		assertTrue(errors.hasErrors());
    }
}

@@ -122,41 +122,8 @@
 %0D%0A%0D%0A
-import java.util.Collections;%0D%0A%0D%0A
 impo
@@ -138,24 +138,24 @@
 unit.Test;%0D%0A
+
 import org.o
@@ -443,58 +443,8 @@
 rs;%0D
-%0Aimport org.springframework.validation.Validator;%0D
 %0A%0D%0A/
@@ -781,24 +781,24 @@
 (Object)%22)%0D%0A
+
 %09public void
@@ -886,142 +886,8 @@
  %7B%0D%0A
-%09%09ValidateUtil util = new ValidateUtil();%0D%0A%09%09util.setValidators(Collections.singletonList((Validator) new LocationValidator()));%0D%0A%09%09%0D%0A
 %09%09Lo
@@ -2713,143 +2713,8 @@
 on %7B
-%0D%0A    %09ValidateUtil util = new ValidateUtil();%0D%0A%09%09util.setValidators(Collections.singletonList((Validator) new LocationValidator()));%0D%0A
 %09%09%0D%0A

Rename AdministrationService.validateInManualFlushMode, and refactor it and ValidateUtil