Search in sources :

Example 6 with Program

use of org.openmrs.Program in project openmrs-core by openmrs.

the class ProgramValidator method validate.

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty or whitespace
 * @should pass validation if description is null or empty or whitespace
 * @should fail validation if program name already in use
 * @should fail validation if concept is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should pass validation and save edited program
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    Program p = (Program) obj;
    if (p == null) {
        errors.rejectValue("program", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "concept", "error.concept");
        Program existingProgram = Context.getProgramWorkflowService().getProgramByName(p.getName());
        if (existingProgram != null && !existingProgram.getUuid().equals(p.getUuid())) {
            errors.rejectValue("name", "general.error.nameAlreadyInUse");
        }
        if (existingProgram != null && existingProgram.getUuid().equals(p.getUuid())) {
            Context.evictFromSession(existingProgram);
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name");
    }
}
Also used : Program(org.openmrs.Program)

Example 7 with Program

use of org.openmrs.Program in project openmrs-core by openmrs.

the class ProgramValidatorTest method validate_shouldPassValidationAndSaveEditedProgram.

/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationAndSaveEditedProgram() {
    Program program = Context.getProgramWorkflowService().getProgram(3);
    program.setDescription("Edited description");
    Errors errors = new BindException(program, "program");
    programValidator.validate(program, errors);
    Assert.assertFalse(errors.hasErrors());
    Context.getProgramWorkflowService().saveProgram(program);
    program = Context.getProgramWorkflowService().getProgram(3);
    Assert.assertTrue(program.getDescription().equals("Edited description"));
}
Also used : Errors(org.springframework.validation.Errors) Program(org.openmrs.Program) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 8 with Program

use of org.openmrs.Program in project openmrs-core by openmrs.

the class ProgramValidatorTest method validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues.

/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() {
    Program prog = new Program();
    prog.setName("Hypochondriasis program");
    prog.setDescription("This is Hypochondriasis program");
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) Program(org.openmrs.Program) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 9 with Program

use of org.openmrs.Program in project openmrs-core by openmrs.

the class ProgramValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.

/**
 * @see ProgramValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
    Program prog = new Program();
    prog.setName("Hypochondriasis program");
    prog.setConcept(Context.getConceptService().getConcept(3));
    Errors errors = new BindException(prog, "prog");
    programValidator.validate(prog, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) Program(org.openmrs.Program) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 10 with Program

use of org.openmrs.Program in project openmrs-core by openmrs.

the class SerializedObjectDAOTest method getObjectByUuid_shouldReturnTheSavedObject.

@Test
public void getObjectByUuid_shouldReturnTheSavedObject() {
    Program data = dao.getObjectByUuid(Program.class, "83b452ca-a4c8-4bf2-9e0b-8bbddf2f9901");
    assertEquals(data.getId().intValue(), 2);
    assertEquals(data.getName(), "TestProgram2");
}
Also used : Program(org.openmrs.Program) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

Program (org.openmrs.Program)43 Test (org.junit.Test)35 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)26 PatientProgram (org.openmrs.PatientProgram)22 ProgramWorkflow (org.openmrs.ProgramWorkflow)8 BindException (org.springframework.validation.BindException)8 Errors (org.springframework.validation.Errors)8 Concept (org.openmrs.Concept)7 ArrayList (java.util.ArrayList)6 ProgramWorkflowState (org.openmrs.ProgramWorkflowState)6 Date (java.util.Date)4 Patient (org.openmrs.Patient)4 ProgramWorkflowDAO (org.openmrs.api.db.ProgramWorkflowDAO)2 MissingPropertyException (groovy.lang.MissingPropertyException)1 HashMap (java.util.HashMap)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 ObjectNode (org.codehaus.jackson.node.ObjectNode)1 Criteria (org.hibernate.Criteria)1 DateTime (org.joda.time.DateTime)1 Ignore (org.junit.Ignore)1