/** @see Encounter#getObs() */
  @Test
  @Verifies(value = "should get obs with two levels of hierarchy", method = "getObs()")
  public void getObs_shouldGetObsWithTwoLevelsOfHierarchy() throws Exception {
    Encounter enc = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    enc.addObs(parentObs);

    // add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // add the child obs directly to the encounter as well
    childObs.setEncounter(enc);
    enc.addObs(childObs);

    // make the obs two levels deep
    Obs grandChildObsOne = new Obs();
    Obs grandChildObsTwo = new Obs();
    childObs.addGroupMember(grandChildObsOne);
    childObs.addGroupMember(grandChildObsTwo);

    /// now getObs() should return the two leaf obs 2 levels down:
    assertEquals(2, enc.getObs().size());
    assertTrue(enc.getObs().contains(grandChildObsOne));
    assertTrue(enc.getObs().contains(grandChildObsTwo));

    // now grandChildObsOne isn't a leaf, it holds greatGrandChildObsOne, but the size of the set
    // returned shouldn't change:
    Obs greatGrandChildObsOne = new Obs();
    grandChildObsOne.addGroupMember(greatGrandChildObsOne);
    assertEquals(2, enc.getObs().size());
    assertTrue(enc.getObs().contains(greatGrandChildObsOne));
    assertTrue(enc.getObs().contains(grandChildObsTwo));

    // add a sibling to one of the leaves.  Add leaf to grandChildObsOne:
    Obs greatGrandChildObsTwo = new Obs();
    grandChildObsOne.addGroupMember(greatGrandChildObsTwo);

    // now getObs() should return a set of size 3:
    assertEquals(3, enc.getObs().size());

    // make sure none of them are grouping obs
    for (Obs oTmp : enc.getObs()) {
      assertFalse(oTmp.isObsGrouping());
    }
    assertTrue(enc.getObs().contains(greatGrandChildObsOne));
    assertTrue(enc.getObs().contains(greatGrandChildObsTwo));
    assertTrue(enc.getObs().contains(grandChildObsTwo));
  }
 private void addToObsGroup(Obs obsGroup, Obs member) {
   member.setPerson(obsGroup.getPerson());
   member.setObsDatetime(obsGroup.getObsDatetime());
   member.setLocation(obsGroup.getLocation());
   member.setEncounter(obsGroup.getEncounter());
   obsGroup.addGroupMember(member);
 }
  /** @see Encounter#getObsAtTopLevel(null) */
  @Test
  @Verifies(
      value = "should get both child and parent obs after removing child from parent grouping",
      method = "getObsAtTopLevel(null)")
  public void getObsAtTopLevel_shouldGetBothChildAndParentObsAfterRemovingChildFromParentGrouping()
      throws Exception {
    Encounter enc = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    enc.addObs(parentObs);

    // add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // add the child obs directly to the encounter as well
    childObs.setEncounter(enc);
    enc.addObs(childObs);

    // remove the obsGrouping, so that both obs are now just children of the Encounter
    parentObs.removeGroupMember(childObs);

    assertEquals(2, enc.getObsAtTopLevel(false).size());
  }
  /** @see Encounter#getAllObs(null) */
  @Test
  @Verifies(
      value = "should get both parent and child with child directly on encounter",
      method = "getAllObs(null)")
  public void getAllObs_shouldGetBothParentAndChildWithChildDirectlyOnEncounter() throws Exception {
    Encounter enc = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    enc.addObs(parentObs);

    // add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // add the child obs directly to the encounter as well
    childObs.setEncounter(enc);
    enc.addObs(childObs);

    // do the check
    assertEquals(2, enc.getAllObs(true).size());

    // this should return one of each -- the obsGroup and the child (because encounter_id is
    // populated in the child Obs):
    int numberOfChildObs = 0;
    int numberofParentObs = 0;
    for (Obs oTmp : enc.getAllObs(false)) {
      if (oTmp.isObsGrouping()) numberofParentObs++;
      else numberOfChildObs++;
    }
    assertEquals(1, numberOfChildObs);
    assertEquals(1, numberofParentObs);
  }
 public ObsBuilder addMember(Concept question, String valueText) {
   Obs child = new Obs();
   child.setPerson(obs.getPerson());
   child.setObsDatetime(obs.getObsDatetime());
   child.setConcept(question);
   child.setValueText(valueText);
   obs.addGroupMember(child);
   return this;
 }
  /** @see Encounter#getObs() */
  @Test
  @Verifies(value = "should not get voided obs with three layers of hierarchy", method = "getObs()")
  public void getObs_shouldNotGetVoidedObsWithThreeLayersOfHierarchy() throws Exception {
    Encounter enc = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    enc.addObs(parentObs);

    // add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // add the child obs directly to the encounter as well
    childObs.setEncounter(enc);
    enc.addObs(childObs);

    // make the obs two levels deep
    Obs grandChildObsOne = new Obs();
    Obs grandChildObsTwo = new Obs();
    Obs greatGrandChildObsOne = new Obs();
    Obs greatGrandChildObsTwo = new Obs();
    childObs.addGroupMember(grandChildObsOne);
    childObs.addGroupMember(grandChildObsTwo);
    grandChildObsOne.addGroupMember(greatGrandChildObsOne);
    grandChildObsOne.addGroupMember(greatGrandChildObsTwo);

    // set up the third level
    Obs greatGreatGrandChildObsOne = new Obs();
    Obs greatGreatGrandChildObsTwo = new Obs();
    greatGrandChildObsTwo.addGroupMember(greatGreatGrandChildObsOne);
    greatGrandChildObsTwo.addGroupMember(greatGreatGrandChildObsTwo);

    // make sure voided objects in the obsGroup hierarchy aren't getting returned:
    greatGrandChildObsTwo.setVoided(true);

    // now the set size should drop down to 2 because the obs we voided had two child leaves:
    assertEquals(2, enc.getObs().size());
    assertTrue(enc.getObs().contains(greatGrandChildObsOne));
    assertTrue(enc.getObs().contains(grandChildObsTwo));
    assertFalse(enc.getObs().contains(greatGreatGrandChildObsOne));
    assertFalse(enc.getObs().contains(greatGreatGrandChildObsTwo));
  }
  /** @see Encounter#addObs(Obs) */
  @Test
  @Verifies(
      value = "should add encounter attrs to obs if attributes are null",
      method = "addObs(Obs)")
  public void addObs_shouldAddEncounterAttrsToObsGroupMembersIfAttributesAreNull()
      throws Exception {
    /// an encounter that will hav the date/location/patient on it
    Encounter encounter = new Encounter();

    Date date = new Date();
    encounter.setEncounterDatetime(date);

    Location location = new Location(1);
    encounter.setLocation(location);

    Patient patient = new Patient(1);
    encounter.setPatient(patient);

    // add an obs that doesn't have date/location/patient set on it.
    Obs obs = new Obs(123);
    Obs childObs = new Obs(456);
    obs.addGroupMember(childObs);

    // check for infinite recursion
    // childObs-->childObs2   and childObs2-->childObs
    Obs childObs2 = new Obs(456);
    childObs.addGroupMember(childObs2);
    childObs2.addGroupMember(childObs);

    assertTrue(obs.getGroupMembers() != null && obs.getGroupMembers().size() == 1);

    encounter.addObs(obs);

    // check the values of the obs attrs to see if they were added
    assertTrue(childObs.getObsDatetime().equals(date));
    assertTrue(childObs.getLocation().equals(location));
    assertTrue(childObs.getPerson().equals(patient));
    assertTrue(childObs2.getObsDatetime().equals(date));
    assertTrue(childObs2.getLocation().equals(location));
    assertTrue(childObs2.getPerson().equals(patient));
  }
Example #8
0
  /**
   * This is an equivalent to a copy constructor. Creates a new copy of the given <code>obsToCopy
   * </code> with a null obs id
   *
   * @param obsToCopy The Obs that is going to be copied
   * @return a new Obs object with all the same attributes as the given obs
   */
  public static Obs newInstance(Obs obsToCopy) {
    Obs newObs =
        new Obs(
            obsToCopy.getPerson(),
            obsToCopy.getConcept(),
            obsToCopy.getObsDatetime(),
            obsToCopy.getLocation());

    newObs.setObsGroup(obsToCopy.getObsGroup());
    newObs.setAccessionNumber(obsToCopy.getAccessionNumber());
    newObs.setValueCoded(obsToCopy.getValueCoded());
    newObs.setValueDrug(obsToCopy.getValueDrug());
    newObs.setValueGroupId(obsToCopy.getValueGroupId());
    newObs.setValueDatetime(obsToCopy.getValueDatetime());
    newObs.setValueNumeric(obsToCopy.getValueNumeric());
    newObs.setValueModifier(obsToCopy.getValueModifier());
    newObs.setValueText(obsToCopy.getValueText());
    newObs.setComment(obsToCopy.getComment());
    newObs.setOrder(obsToCopy.getOrder());
    newObs.setEncounter(obsToCopy.getEncounter());
    newObs.setDateStarted(obsToCopy.getDateStarted());
    newObs.setDateStopped(obsToCopy.getDateStopped());
    newObs.setCreator(obsToCopy.getCreator());
    newObs.setDateCreated(obsToCopy.getDateCreated());
    newObs.setVoided(obsToCopy.getVoided());
    newObs.setVoidedBy(obsToCopy.getVoidedBy());
    newObs.setDateVoided(obsToCopy.getDateVoided());
    newObs.setVoidReason(obsToCopy.getVoidReason());

    newObs.setValueComplex(obsToCopy.getValueComplex());
    newObs.setComplexData(obsToCopy.getComplexData());

    if (obsToCopy.getGroupMembers() != null)
      for (Obs member : obsToCopy.getGroupMembers()) {
        // if the obs hasn't been saved yet, no need to duplicate it
        if (member.getObsId() == null) newObs.addGroupMember(member);
        else newObs.addGroupMember(Obs.newInstance(member));
      }

    return newObs;
  }
  /** @see Encounter#getObs() */
  @Test
  @Verifies(value = "should get obs with three levels of hierarchy", method = "getObs()")
  public void getObs_shouldGetObsWithThreeLevelsOfHierarchy() throws Exception {
    Encounter enc = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    enc.addObs(parentObs);

    // add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // add the child obs directly to the encounter as well
    childObs.setEncounter(enc);
    enc.addObs(childObs);

    // make the obs two levels deep
    Obs grandChildObsOne = new Obs();
    Obs grandChildObsTwo = new Obs();
    Obs greatGrandChildObsOne = new Obs();
    Obs greatGrandChildObsTwo = new Obs();
    childObs.addGroupMember(grandChildObsOne);
    childObs.addGroupMember(grandChildObsTwo);
    grandChildObsOne.addGroupMember(greatGrandChildObsOne);
    grandChildObsOne.addGroupMember(greatGrandChildObsTwo);

    // test the third level
    Obs greatGreatGrandChildObsOne = new Obs();
    Obs greatGreatGrandChildObsTwo = new Obs();
    greatGrandChildObsTwo.addGroupMember(greatGreatGrandChildObsOne);
    greatGrandChildObsTwo.addGroupMember(greatGreatGrandChildObsTwo);

    assertEquals(4, enc.getObs().size());
    assertTrue(enc.getObs().contains(greatGrandChildObsOne));
    assertTrue(enc.getObs().contains(greatGreatGrandChildObsOne));
    assertTrue(enc.getObs().contains(greatGreatGrandChildObsTwo));
    assertTrue(enc.getObs().contains(grandChildObsTwo));
  }
  @Test
  public void testParsingDispositionWithTransferLocation() throws Exception {
    Concept admit =
        new ConceptBuilder(
                null,
                conceptService.getConceptDatatypeByName("N/A"),
                conceptService.getConceptClassByName("Misc"))
            .addName("Transfer")
            .get();
    when(emrConceptService.getConcept("test:transfer")).thenReturn(admit);

    Obs dispositionObs =
        dispositionDescriptor.buildObsGroup(
            new Disposition(
                "emrapi.transfer",
                "Transfer",
                "test:transfer",
                Collections.<String>emptyList(),
                Collections.<DispositionObs>emptyList()),
            emrConceptService);

    Obs transferLocationObs = new Obs();
    transferLocationObs.setObsId(100);
    transferLocationObs.setConcept(dispositionDescriptor.getInternalTransferLocationConcept());
    transferLocationObs.setValueText("3");
    dispositionObs.addGroupMember(transferLocationObs);

    Location transferLocation = new Location();
    transferLocation.setName("Outpatient clinic");
    when(locationService.getLocation(3)).thenReturn(transferLocation);

    encounter.addObs(doNotGoToServiceToFormatMembers(dispositionObs));
    ParsedObs parsed = parser.parseObservations(Locale.ENGLISH);

    SimpleObject expectedTransferLocationObject =
        SimpleObject.create("obsId", transferLocationObs.getObsId());
    expectedTransferLocationObject.put("question", "Transfer location");
    expectedTransferLocationObject.put("answer", "Outpatient clinic");

    List<SimpleObject> expectedAdditionalObsList = new ArrayList<SimpleObject>();
    expectedAdditionalObsList.add(expectedTransferLocationObject);

    assertThat(parsed.getDiagnoses().size(), is(0));
    assertThat(parsed.getDispositions().size(), is(1));
    assertThat(parsed.getObs().size(), is(0));
    assertThat(path(parsed.getDispositions(), 0, "disposition"), is((Object) "Transfer"));
    assertThat(
        path(parsed.getDispositions(), 0, "additionalObs"), is((Object) expectedAdditionalObsList));
  }
  @Test
  public void testParsingDispositionWithDateOfDeath() throws Exception {
    Concept admit =
        new ConceptBuilder(
                null,
                conceptService.getConceptDatatypeByName("N/A"),
                conceptService.getConceptClassByName("Misc"))
            .addName("Death")
            .get();
    when(emrConceptService.getConcept("test:death")).thenReturn(admit);

    Obs dispositionObs =
        dispositionDescriptor.buildObsGroup(
            new Disposition(
                "emrapi.death",
                "Death",
                "test:death",
                Collections.<String>emptyList(),
                Collections.<DispositionObs>emptyList()),
            emrConceptService);

    Date dateOfDeath = new DateTime(2012, 2, 20, 10, 10, 10).toDate();
    Obs dateOfDeathObs = new Obs();
    dateOfDeathObs.setObsId(100);
    dateOfDeathObs.setConcept(dispositionDescriptor.getDateOfDeathConcept());
    dateOfDeathObs.setValueDate(dateOfDeath);
    dispositionObs.addGroupMember(dateOfDeathObs);

    encounter.addObs(doNotGoToServiceToFormatMembers(dispositionObs));
    ParsedObs parsed = parser.parseObservations(Locale.ENGLISH);

    SimpleObject expectedAdmissionLocationObject =
        SimpleObject.create("obsId", dateOfDeathObs.getObsId());
    expectedAdmissionLocationObject.put("question", "Date of death");
    expectedAdmissionLocationObject.put("answer", "20 Feb 2012 10:10 AM");

    List<SimpleObject> expectedAdditionalObsList = new ArrayList<SimpleObject>();
    expectedAdditionalObsList.add(expectedAdmissionLocationObject);

    assertThat(parsed.getDiagnoses().size(), is(0));
    assertThat(parsed.getDispositions().size(), is(1));
    assertThat(parsed.getObs().size(), is(0));
    assertThat(path(parsed.getDispositions(), 0, "disposition"), is((Object) "Death"));
    assertThat(
        path(parsed.getDispositions(), 0, "additionalObs"), is((Object) expectedAdditionalObsList));
  }
  /** @see Encounter#getObs() */
  @Test
  @Verifies(value = "should only get child obs", method = "getObs()")
  public void getObs_shouldOnlyGetChildObs() throws Exception {
    Encounter encounter = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    encounter.addObs(parentObs);

    // add a child to the obs and make sure that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // obsGroup should recurse and ONLY the child obs should be picked up:
    assertEquals(1, encounter.getObs().size());
    // make sure that the obs is the oChild
    Obs obsInEncounter = (Obs) encounter.getObs().toArray()[0];
    assertTrue(childObs.equals(obsInEncounter));
    assertFalse(obsInEncounter.isObsGrouping());
  }
  /** @see Encounter#getAllObs(null) */
  @Test
  @Verifies(value = "should get both parent and child obs", method = "getAllObs(null)")
  public void getAllObs_shouldGetBothParentAndChildObs() throws Exception {
    Encounter encounter = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    encounter.addObs(parentObs);

    // add a child to the obs and make sure that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // assert that the parent obs is returned
    assertNotNull(encounter.getAllObs(true));
    assertEquals(1, encounter.getAllObs(true).size());
    assertNotNull(encounter.getAllObs(false));
    assertEquals(1, encounter.getAllObs(false).size());
    Obs obsInEncounter = (Obs) encounter.getAllObs(false).toArray()[0];
    assertTrue(obsInEncounter.isObsGrouping());
  }
  /** @see Encounter#getObs() */
  @Test
  @Verifies(value = "should not get child obs if child also on encounter", method = "getObs()")
  public void getObs_shouldNotGetChildObsIfChildAlsoOnEncounter() throws Exception {
    Encounter encounter = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    encounter.addObs(parentObs);

    // add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // add the child obs directly to the encounter as well
    childObs.setEncounter(encounter);
    encounter.addObs(childObs);

    // do the check
    assertEquals(1, encounter.getObs().size());
    Obs obsInEncounter = (Obs) encounter.getObs().toArray()[0];
    assertFalse(obsInEncounter.isObsGrouping());
  }
  /** @see Encounter#getObsAtTopLevel(null) */
  @Test
  @Verifies(value = "should only get parents obs", method = "getObsAtTopLevel(null)")
  public void getObsAtTopLevel_shouldOnlyGetParentsObs() throws Exception {
    Encounter encounter = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    encounter.addObs(parentObs);

    // add a child to the obs and make sure that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // assert that the parent obs is returned by getObsAtTopLevel()
    assertNotNull(encounter.getObsAtTopLevel(true));
    assertEquals(1, encounter.getObsAtTopLevel(true).size());
    assertNotNull(encounter.getObsAtTopLevel(false));
    assertEquals(1, encounter.getObsAtTopLevel(false).size());

    // make sure that the obs is the parent obs
    Obs obsInEncounter = (Obs) encounter.getObsAtTopLevel(false).toArray()[0];
    assertTrue(obsInEncounter.isObsGrouping());
  }
  /** @see Encounter#getObsAtTopLevel(null) */
  @Test
  @Verifies(
      value = "should only return the grouped top level obs",
      method = "getObsAtTopLevel(null)")
  public void getObsAtTopLevel_shouldOnlyReturnTheGroupedTopLevelObs() throws Exception {
    Encounter encounter = new Encounter();

    // create and add an Obs
    Obs parentObs = new Obs();
    encounter.addObs(parentObs);

    // add a child to the obs and make sure that now that the Obs is an ObsGroup with one child:
    Obs childObs = new Obs();
    parentObs.addGroupMember(childObs);

    // add the child obs directly to the encounter as well
    childObs.setEncounter(encounter);
    encounter.addObs(childObs);

    // do the check
    assertEquals(1, encounter.getObsAtTopLevel(false).size());
    Obs obsInEncounter = (Obs) encounter.getObsAtTopLevel(false).toArray()[0];
    assertTrue(obsInEncounter.isObsGrouping());
  }