@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());
  }
  @Test
  public void shouldPreferVoidOverUpdateWhenEditingADocument() 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<>();
    String obsUuid = "6d0ae386-707a-4629-9850-f15206e63kj0";
    documents.add(
        new Document(
            "/radiology/foo.jpg",
            null,
            "3f596de5-5caa-11e3-a4c0-0800271c1b75",
            obsUuid,
            encounterDate,
            true));

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

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

    Encounter encounter = encounterService.getEncounterByUuid(secondEncounterUuid);

    Obs savedObs = obsService.getObsByUuid(obsUuid);
    assertTrue("Observation is not voided", savedObs.getVoided());
    assertTrue(
        "Observation is not voided", savedObs.getGroupMembers(true).iterator().next().getVoided());

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

    assertNull(savedDoc);
  }
  @Test
  public void shouldUseVisitStartTimeAsEncounterDateTimeForPreviousVisits() throws Exception {
    Date visitStartDate = getDateFromString("2010-09-22 00:00:00");

    List<Document> documents = new ArrayList<>();
    documents.add(
        new Document(
            "/radiology/foo-lalala.jpg",
            null,
            "3f596de5-5caa-11e3-a4c0-0800271c1b75",
            null,
            null,
            false));

    VisitDocumentRequest visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            firstVisitUuid,
            visitTypeUUID,
            null,
            null,
            firstEncounterTypeUUID,
            null,
            documents,
            secondProviderUuid,
            null,
            null);
    executeDataSet("visitDocumentData.xml");

    //        Date currentDate = new Date(System.currentTimeMillis() - 1000);
    visitDocumentService.upload(visitDocumentRequest);
    Context.flushSession();
    Context.clearSession();

    Visit visit = visitService.getVisit(1);
    Set<Encounter> encounters = visit.getEncounters();
    Encounter encounter = getEncounterByTypeUuid(encounters, firstEncounterTypeUUID);
    boolean condition = encounter.getEncounterDatetime().compareTo(visitStartDate) >= 0;
    assertTrue(condition);

    Set<Obs> allObs = encounter.getAllObs();
    Obs savedDocument = getSavedDocument(allObs, "3f596de5-5caa-11e3-a4c0-0800271c1b75");
    assertTrue(savedDocument.getObsDatetime().compareTo(visitStartDate) == 0);
  }
  @Test
  public void shouldNotChangeObservationsIfSameDetailsProvidedOnceAgain() 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,
            conceptUuid,
            "6d0ae386-707a-4629-9850-f15206e63kj0",
            encounterDate,
            false));

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

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

    Encounter encounter = encounterService.getEncounterByUuid(secondEncounterUuid);

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), conceptUuid);

    assertNotNull(savedDoc);
    Set<Obs> groupMembers = savedDoc.getGroupMembers();
    assertThat(groupMembers.size(), is(equalTo(1)));
    assertThat(groupMembers.iterator().next().getValueText(), is("/radiology/foo.jpg"));
    assertThat(
        groupMembers.iterator().next().getUuid(), is("6d0ae386-707a-4629-9850-f15606e63666"));
  }
  @Test
  public void shouldUseNewDateAsEncounterDateTimeForActiveVisits() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");
    Date obsDate = getDateFromString("2014-06-24 00:10:00");

    List<Document> documents = new ArrayList<>();
    documents.add(new Document("/radiology/fooo-bar.jpg", null, conceptUuid, null, obsDate, false));

    visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            secondVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            secondEncounterTypeUUID,
            encounterDate,
            documents,
            firstProviderUuid,
            null,
            null);

    Date currentDate = new Date(System.currentTimeMillis() - 1000);
    visitDocumentService.upload(visitDocumentRequest);
    Context.flushSession();
    Context.clearSession();

    Visit visit = visitService.getVisit(2);
    Set<Encounter> encounters = visit.getEncounters();
    Encounter encounter = getEncounterByTypeUuid(encounters, secondEncounterTypeUUID);
    boolean condition = encounter.getEncounterDatetime().compareTo(currentDate) >= 0;
    assertTrue(condition);

    Set<Obs> allObs = encounter.getAllObs();
    Obs savedDocument = getSavedDocument(allObs, conceptUuid);
    assertTrue(savedDocument.getObsDatetime().compareTo(currentDate) >= 0);
  }
  @Test
  public void shouldUploadImagesInOrderOfRequest() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");
    Date obsDate = getDateFromString("2014-06-24 00:10:00");

    List<Document> documents = new ArrayList<>();
    documents.add(new Document("/radiology/1.jpg", null, conceptUuid, null, obsDate, false));
    documents.add(new Document("/radiology/2.jpg", null, conceptUuid, null, obsDate, false));
    documents.add(new Document("/radiology/3.jpg", null, conceptUuid, null, obsDate, false));
    documents.add(new Document("/radiology/4.jpg", null, conceptUuid, null, obsDate, false));

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

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

    Encounter encounter = encounterService.getEncounterByUuid(firstEncounterUuid);
    List<Obs> savedDocuments = getSavedDocuments(encounter.getAllObs(), conceptUuid);
    assertEquals(4, savedDocuments.size());
    assertEquals("/radiology/1.jpg", getImageName(savedDocuments.get(0)));
    assertEquals("/radiology/2.jpg", getImageName(savedDocuments.get(1)));
    assertEquals("/radiology/3.jpg", getImageName(savedDocuments.get(2)));
    assertEquals("/radiology/4.jpg", getImageName(savedDocuments.get(3)));
  }
  @Test
  public void shouldCreateObservations() throws Exception {
    Date visitStartDate = getDateFromString("2014-06-22 00:00:00");
    Date encounterDate = getDateFromString("2014-06-23 00:00:00");
    Date obsDate = getDateFromString("2014-06-24 00:10:00");

    List<Document> documents = new ArrayList<>();
    documents.add(new Document("/radiology/fooo-bar.jpg", null, conceptUuid, null, obsDate, false));

    visitDocumentRequest =
        new VisitDocumentRequest(
            patientUUID,
            secondVisitUuid,
            visitTypeUUID,
            visitStartDate,
            null,
            firstEncounterTypeUUID,
            encounterDate,
            documents,
            firstProviderUuid,
            FIRST_LOCATION_UUID,
            null);

    visitDocumentService.upload(visitDocumentRequest);

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

    Encounter encounter = encounterService.getEncounterByUuid(firstEncounterUuid);

    Obs savedDoc = getSavedDocument(encounter.getAllObs(), conceptUuid);

    assertNotNull(savedDoc);
    assertThat(savedDoc.getConcept().getId(), is(222));
    assertThat(
        savedDoc.getGroupMembers().iterator().next().getValueText(), is("/radiology/fooo-bar.jpg"));
    assertEquals(FIRST_LOCATION_UUID, encounter.getLocation().getUuid());
  }
  @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));
      }
    }
  }