Search in sources :

Example 6 with FormResource

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

the class FormServiceTest method saveFormResource_shouldBeAbleToSaveAnXSLT.

/**
 * @throws IOException
 * @see FormService#saveFormResource(FormResource)
 */
@Test
public void saveFormResource_shouldBeAbleToSaveAnXSLT() throws IOException {
    // set up new form
    Form form = new Form();
    form.setName("form resource test form");
    form.setVersion("42");
    form.setDescription("bleh");
    form = Context.getFormService().saveForm(form);
    // save a resource
    String name = "org.openmrs.module.formentry.xslt";
    String expected = getResourceAsString(FORM_SAMPLE_RESOURCE);
    FormResource resource = new FormResource();
    resource.setForm(form);
    resource.setName(name);
    resource.setDatatypeClassname("org.openmrs.customdatatype.datatype.LongFreeTextDatatype");
    resource.setValue(expected);
    Context.getFormService().saveFormResource(resource);
    // make sure the resource is saved
    Collection<FormResource> formResourcesForForm = Context.getFormService().getFormResourcesForForm(form);
    Assert.assertEquals(1, formResourcesForForm.size());
    FormResource actual = formResourcesForForm.iterator().next();
    Assert.assertEquals(expected, actual.getValue());
}
Also used : Form(org.openmrs.Form) FormResource(org.openmrs.FormResource) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 7 with FormResource

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

the class FormServiceTest method purgeForm_shouldDeleteFormResourcesForDeletedForm.

/**
 * @throws ParseException
 * @see FormService#purgeForm(Form)
 */
@Test
public void purgeForm_shouldDeleteFormResourcesForDeletedForm() throws ParseException {
    // create a new form
    Form form = new Form();
    form.setName("form resource test form");
    form.setVersion("42");
    form.setDescription("bleh");
    form = Context.getFormService().saveForm(form);
    // save a resource
    String name = "Start Date";
    FormResource resource = new FormResource();
    resource.setForm(form);
    resource.setName(name);
    resource.setDatatypeClassname("org.openmrs.customdatatype.datatype.DateDatatype");
    Date expected = new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-16");
    resource.setValue(expected);
    Context.getFormService().saveFormResource(resource);
    // make sure the resource is saved
    FormResource actual = Context.getFormService().getFormResource(form, name);
    assertEquals(expected, actual.getValue());
    // retain the resource id
    Integer savedId = actual.getFormResourceId();
    // delete the form
    Context.getFormService().purgeForm(form);
    // check for the resource
    Assert.assertNull(Context.getFormService().getFormResource(savedId));
}
Also used : Form(org.openmrs.Form) FormResource(org.openmrs.FormResource) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 8 with FormResource

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

the class FormServiceTest method saveFormResource_shouldOverwriteAnExistingResourceWithSameName.

/**
 * @throws ParseException
 * @see FormService#saveFormResource(FormResource)
 */
@Test
public void saveFormResource_shouldOverwriteAnExistingResourceWithSameName() throws ParseException {
    String name = "Start Date";
    // save an original resource
    Form form = Context.getFormService().getForm(1);
    FormResource resource = new FormResource();
    resource.setForm(form);
    resource.setName(name);
    resource.setDatatypeClassname("org.openmrs.customdatatype.datatype.DateDatatype");
    Date previous = new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-16");
    resource.setValue(previous);
    Context.getFormService().saveFormResource(resource);
    // clear the session
    Context.flushSession();
    // save a new resource with the same name
    form = Context.getFormService().getForm(1);
    resource = new FormResource();
    resource.setForm(form);
    resource.setName(name);
    resource.setDatatypeClassname("org.openmrs.customdatatype.datatype.DateDatatype");
    Date expected = new SimpleDateFormat("yyyy-MM-dd").parse("2010-10-16");
    resource.setValue(expected);
    Context.getFormService().saveFormResource(resource);
    // get the current value
    FormResource actual = Context.getFormService().getFormResource(form, name);
    Assert.assertFalse(previous.equals(actual.getValue()));
    Assert.assertEquals(expected, actual.getValue());
}
Also used : Form(org.openmrs.Form) FormResource(org.openmrs.FormResource) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

FormResource (org.openmrs.FormResource)8 Test (org.junit.Test)6 Form (org.openmrs.Form)6 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)6 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 FormService (org.openmrs.api.FormService)1 InvalidFileTypeException (org.openmrs.api.InvalidFileTypeException)1