@Test
  public void shouldChangeObservationsOfPreviousEncounters() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");

    List<Document> documents = new ArrayList<>();
    documents.add(
        new Document(
            "/radiology/foo.jpg",
            null,
            "5f596de5-5caa-11e3-a4c0-0800271c1b75",
            "6d0ae386-707a-4629-9850-f15206e63kj0",
            encounterDate,
            false));

    VisitDocumentRequest visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            firstVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            secondEncounterTypeUUID,
            encounterDate,
            documents,
            secondProviderUuid,
            secondLocationUuid,
            null);
    executeDataSet("visitDocumentData.xml");
    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(secondEncounterUuid);
    for (Obs obs : encounter.getAllObs(true)) {
      if (obs.getUuid().equals("6d0ae386-707a-4629-9850-f15606e63666")
          || obs.getUuid().equals("6d0ae386-707a-4629-9850-f15206e63kj0")) {
        assertThat(obs.getVoided(), is(true));
      }
    }

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), "5f596de5-5caa-11e3-a4c0-0800271c1b75");

    assertNotNull(savedDoc);
    assertThat(savedDoc.getConcept().getId(), is(333));
    assertThat(
        savedDoc.getGroupMembers().iterator().next().getValueText(), is("/radiology/foo.jpg"));
    assertEquals(FIRST_LOCATION_UUID, encounter.getLocation().getUuid());
  }
  private void updateInitialDiagnosis(
      Obs diagnosisObs,
      BahmniDiagnosisRequest bahmniDiagnosis,
      Concept bahmniInitialDiagnosisConcept) {
    Obs obs = findOrCreateObs(diagnosisObs, bahmniInitialDiagnosisConcept);
    String initialDiagnosisUuid =
        bahmniDiagnosis.getPreviousObs() == null && obs.getId() == null
            ? diagnosisObs.getUuid()
            : bahmniDiagnosis.getFirstDiagnosis().getExistingObs();

    obs.setValueText(initialDiagnosisUuid);
    addToObsGroup(diagnosisObs, obs);
  }
 private FHIRResource mapObservation(
     Obs openmrsObs, Encounter fhirEncounter, SystemProperties systemProperties) {
   FHIRResource fhirObservationResource =
       observationBuilder.buildObservationResource(
           fhirEncounter,
           systemProperties,
           openmrsObs.getUuid(),
           openmrsObs.getConcept().getName().getName());
   Observation fhirObservation = (Observation) fhirObservationResource.getResource();
   fhirObservation.setStatus(ObservationStatusEnum.PRELIMINARY);
   mapCode(openmrsObs, fhirObservation);
   mapValue(openmrsObs, fhirObservation);
   return fhirObservationResource;
 }
  @Test
  public void shouldUpdateObservations() throws Exception {
    executeDataSet("shouldUpdateObservations.xml");

    String json =
        "{ \"patientUuid\" : \"a76e8d23-0c38-408c-b2a8-ea5540f01b51\", "
            + "\"visitTypeUuid\" : \"b45ca846-c79a-11e2-b0c0-8e397087571c\", "
            + "\"encounterTypeUuid\": \"2b377dba-62c3-4e53-91ef-b51c68899890\","
            + "\"encounterDateTime\" : \"2013-01-01T00:00:00.000+0000\", "
            + "\"observations\":["
            + "{\"uuid\":\"z9fb7f47-e80a-4056-9285-bd798be13c63\", "
            + " \"groupMembers\" : [{\"uuid\":\"ze48cdcb-6a76-47e3-9f2e-2635032f3a9a\", \"value\":20, \"comment\":\"new gc\" }] }, "
            + "{\"uuid\":\"zf616900-5e7c-4667-9a7f-dcb260abf1de\", \"comment\" : \"new c\", \"value\":100 }"
            + "]}";

    EncounterTransaction response =
        deserialize(
            handle(newPostRequest("/rest/emrapi/encounter", json)), EncounterTransaction.class);

    Visit visit = visitService.getVisitByUuid(response.getVisitUuid());
    Encounter encounter = (Encounter) visit.getEncounters().toArray()[0];

    assertEquals(2, encounter.getObsAtTopLevel(false).size());
    Iterator<Obs> iterator = encounter.getObsAtTopLevel(false).iterator();

    Obs obs1 = iterator.next();
    assertEquals("zf616900-5e7c-4667-9a7f-dcb260abf1de", obs1.getUuid());
    assertEquals(new Double(100), obs1.getValueNumeric());
    assertEquals("new c", obs1.getComment());

    Obs obs2 = iterator.next();
    assertEquals("z9fb7f47-e80a-4056-9285-bd798be13c63", obs2.getUuid());
    assertEquals(1, obs2.getGroupMembers().size());
    Obs member = obs2.getGroupMembers().iterator().next();
    assertEquals(new Double(20), member.getValueNumeric());
    assertEquals("new gc", member.getComment());
  }
  @Test
  public void shouldDeleteObservationsOfPreviousEncounters() throws ParseException {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");

    List<Document> documents = new ArrayList<>();
    String testUuid = "3f596de5-5caa-11e3-a4c0-0800271c1b75";
    String obsUuid = "6d0ae386-707a-4629-9850-f15206e63j8s";
    String providerUuid = "331c6bf8-7846-11e3-a96a-0800271c1b75";
    documents.add(new Document("/patient_file", null, testUuid, obsUuid, encounterDate, true));

    visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            secondVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            firstEncounterTypeUUID,
            encounterDate,
            documents,
            providerUuid,
            null,
            null);
    visitDocumentService.upload(visitDocumentRequest);

    Context.flushSession();
    Context.clearSession();

    Encounter encounter = encounterService.getEncounterByUuid(firstEncounterUuid);
    for (Obs obs : encounter.getAllObs(true)) {
      if (obs.getUuid().equals(obsUuid)) {
        assertThat(obs.getVoided(), is(true));
        assertThat(obs.getGroupMembers(true).iterator().next().getVoided(), is(true));
      }
    }
  }
 public boolean isSameAs(Obs obs) {
   return this.getUuid().equals(obs.getUuid());
 }