XformInstanceResource extends BaseResource<OpenmrsObject> { static void adjustXformDocument(Document doc, int patientId, int userId, Date dateEntered) throws SAXException, IOException { Element root = doc.getDocumentElement(); XmlUtils.getOrCreatePath(doc, root, "patient", "patient.patient_id") .setTextContent("" + patientId); XmlUtils.getOrCreatePath(doc, root, "header", "enterer") .setTextContent("" + userId); XmlUtils.getOrCreatePath(doc, root, "header", "date_entered") .setTextContent(Utils.formatUtc8601(dateEntered)); setEncounterDatetime(doc, fixEncounterDatetime(getEncounterDatetime(doc))); Element obs = XmlUtils.getOrCreateChild(doc, root, "obs"); obs.setAttribute("openmrs_concept", "1238^MEDICAL RECORD OBSERVATIONS^99DCT"); obs.setAttribute("openmrs_datatype", "ZZ"); List<String> topLevelElements = Arrays.asList("header", "patient", "encounter", "obs"); for (Element element : getChildren(root)) { if (!topLevelElements.contains(element.getTagName())) { for (Element observation : getChildren(element)) { obs.appendChild(observation); } removeNode(element); } } } XformInstanceResource(); Document getPreparedXformDocument(SimpleObject post); static Date parseTimestamp(String timestamp);  }
@Test public void addForm() throws Exception { Document expected = getXmlResource(getClass(), "expected-instance-add.xml"); Document doc = getXmlResource(getClass(), "original-instance-add.xml"); XformInstanceResource.adjustXformDocument(doc, 8, 1, Utils.parse8601("2014-11-15T12:34:56.789Z")); assertXmlEqual(expected, doc); }
@Test public void editForm() throws Exception { Document expected = getXmlResource(getClass(), "expected-instance-edit.xml"); Document doc = getXmlResource(getClass(), "original-instance-edit.xml"); XformInstanceResource.adjustXformDocument(doc, 9, 2, Utils.parse8601("2014-11-15T12:34:56.789Z")); assertXmlEqual(expected, doc); }
@Test public void moveGroupsIntoObs() throws Exception { Document expected = getXmlResource(getClass(), "expected-grouped.xml"); Document doc = getXmlResource(getClass(), "original-grouped.xml"); XformInstanceResource.adjustXformDocument(doc, 10, 3, Utils.parse8601("2014-11-15T12:34:56.789Z")); assertXmlEqual(expected, doc); }