@Test
  public void pregnancyVisitWithReferralWithMissingFieldShouldNotPublishEvent() {
    String formId = "formId";
    String referralCaseId = "referralCaseId";
    String motherCaseId = "motherCaseId";
    CommcareForm form = CommcareForm("Pregnancy Visit");
    form.getForm().setElementName("form");

    AddSingleValueFormField(form, Commcare.CREATE_REFERRAL, "yes");
    AddSingleValueFormField(form, Commcare.LAST_VISIT, "2013-01-03");

    FormValueElement aCase = AddSingleValueFormField(form, Commcare.CASE, "case");
    aCase.addAttribute(Commcare.CASE_ID, motherCaseId);

    CaseInfo referralCase = new CaseInfo();
    referralCase.setCaseType("referral");

    when(commcareCaseService.getCaseByCaseId(referralCaseId)).thenReturn(referralCase);

    CaseInfo motherCase = new CaseInfo();
    motherCase.setCaseType("mother");

    when(commcareCaseService.getCaseByCaseId(motherCaseId)).thenReturn(motherCase);

    when(commcareFormService.retrieveForm(formId)).thenReturn(form);

    List<String> list = Arrays.asList(motherCaseId, referralCaseId);
    commCareFormStubListener.handle(CommcareFormStubEvent(formId, list));

    verify(eventRelay).sendEventMessage(any(MotechEvent.class));
  }
  @Test
  public void shouldThrowExceptionIfCaseIdIsEmpty() {
    String instanceId = "myInstanceId";

    expectedException.expect(RuntimeException.class);
    expectedException.expectMessage(String.format("Empty case id found in form(%s)", instanceId));

    FormValueElement childCaseElement =
        new FormValueElementBuilder()
            .addAttribute("case_id", "")
            .addAttribute("date_modified", "2012-07-21T12:02:59.923+05:30")
            .build();

    FormValueElement childInfoElement = getFVE("case", childCaseElement);

    CommcareForm commcareForm =
        new CommcareFormBuilder().addSubElement("child_info", childInfoElement).build();
    commcareForm.setId(instanceId);

    when(infoParser.getCaseElement(childInfoElement)).thenReturn(childCaseElement);

    childInfoParser.parse(commcareForm);
    verify(infoParser, never()).parse(childCaseElement, true);
    verify(infoParser, never()).parse(childInfoElement, true);
  }
  private CommcareForm CommcareForm(String formName) {
    FormValueElement formValueElement = new FormValueElement();
    formValueElement.addAttribute(Commcare.NAME, formName);

    CommcareForm form = new CommcareForm();
    form.setForm(formValueElement);
    return form;
  }
  @Test
  public void postPartumVisitReferralShouldPublish() {
    String formId = "formId";
    String referralCaseId = "referralCaseId";
    String motherCaseId = "motherCaseId";
    CommcareForm form = CommcareForm("Post Partum Visit");
    form.getForm().setElementName("form");

    AddSingleValueFormField(form, Commcare.MOTHER_ALIVE, "yes");
    AddSingleValueFormField(form, Commcare.DELIVERED, "yes");
    AddSingleValueFormField(form, Commcare.CREATE_REFERRAL, "yes");
    AddSingleValueFormField(form, Commcare.REFERRAL_ID, "referralId");
    AddSingleValueFormField(form, Commcare.LAST_VISIT, "2013-01-03");

    FormValueElement aCase = AddSingleValueFormField(form, Commcare.CASE, "case");
    aCase.addAttribute(Commcare.CASE_ID, motherCaseId);

    FormValueElement postPartumVisit = new FormValueElement();
    postPartumVisit.setElementName(Commcare.POST_PARTUM_VISIT);

    AddSubelementValueFormField(postPartumVisit, Commcare.DATE_OF_BIRTH, "2013-01-03");
    AddSubelementValueFormField(postPartumVisit, Commcare.ATTENDED_PNC, "yes");
    AddSubelementValueFormField(postPartumVisit, Commcare.PLACE_OF_BIRTH, "home");

    form.getForm().addFormValueElement(Commcare.POST_PARTUM_VISIT, postPartumVisit);

    CaseInfo referralCase = new CaseInfo();
    referralCase.setCaseType("referral");

    when(commcareCaseService.getCaseByCaseId(referralCaseId)).thenReturn(referralCase);

    CaseInfo motherCase = new CaseInfo();
    motherCase.setCaseType("mother");

    when(commcareCaseService.getCaseByCaseId(motherCaseId)).thenReturn(motherCase);

    when(commcareFormService.retrieveForm(formId)).thenReturn(form);

    List<String> list = Arrays.asList(motherCaseId, referralCaseId);
    commCareFormStubListener.handle(CommcareFormStubEvent(formId, list));

    verify(eventRelay).sendEventMessage(any(MotechEvent.class));
  }
  @Test
  public void pregnancyVisitWithoutReferralShouldPublishNoEvent() {
    String formId = "formId";
    CommcareForm form = CommcareForm("Pregnancy Visit");
    form.getForm().setElementName("form");

    AddSingleValueFormField(form, Commcare.CREATE_REFERRAL, "no");
    AddSingleValueFormField(form, Commcare.REFERRAL_ID, null);
    AddSingleValueFormField(form, Commcare.LAST_VISIT, "2013-01-03");

    FormValueElement aCase = AddSingleValueFormField(form, Commcare.CASE, "case");
    aCase.addAttribute(Commcare.CASE_ID, "caseId");

    when(commcareFormService.retrieveForm(formId)).thenReturn(form);

    commCareFormStubListener.handle(CommcareFormStubEvent(formId));

    verify(eventRelay, never()).sendEventMessage(any(MotechEvent.class));
  }
  private FormValueElement AddSingleValueFormField(
      CommcareForm form, String elementName, String value) {
    FormValueElement formValueElement = new FormValueElement();

    formValueElement.setElementName(elementName);
    formValueElement.setValue(value);

    form.getForm().addFormValueElement(elementName, formValueElement);

    return formValueElement;
  }
  @Test
  public void
      shouldIgnoreChildrenIfCaseElementNotFoundWhilePopulatingCaseInformationAndNotLogError()
          throws Exception {
    TestAppender.clear();
    String instanceId = "myInstanceId";
    String caseId1 = "3e8998ce-b19f-4fa7-b1a1-721b6951e3cf";
    String caseId2 = "3e8998ce-b19f-4fa7-b1a1-721b6951e3cc";

    String dateModified1 = "2012-07-21T12:02:59.923+05:30";
    String dateModified2 = null;
    String receivedOn = DateTime.now().toString();

    FormValueElement childCaseElement1 =
        new FormValueElementBuilder()
            .addAttribute("case_id", caseId1)
            .addAttribute("date_modified", dateModified1)
            .build();

    FormValueElement childCaseElement2 =
        new FormValueElementBuilder()
            .addAttribute("case_id", caseId2)
            .addAttribute("date_modified", dateModified2)
            .build();

    FormValueElement childInfoElement1 = getFVE("case", childCaseElement1);
    FormValueElement childInfoElement2 = getFVE("case", childCaseElement2);

    CommcareForm commcareForm =
        new CommcareFormBuilder()
            .addSubElement("child_info", childInfoElement1)
            .addSubElement("child_info", childInfoElement2)
            .withReceivedOn(receivedOn)
            .build();

    commcareForm.setId(instanceId);

    when(infoParser.getCaseElement(childInfoElement2)).thenReturn(childCaseElement2);
    when(infoParser.shouldReportMissingCaseElement()).thenReturn(false);

    List<Map<String, String>> childrenMapList = childInfoParser.parse(commcareForm);

    assertEquals(1, childrenMapList.size());

    verify(infoParser, never()).parse(childInfoElement1, true);
    verify(infoParser, never()).parse(childInfoElement1, true);
    verify(infoParser).parse(childCaseElement2, true);
    verify(infoParser).parse(childInfoElement2, true);

    Collections.sort(
        childrenMapList,
        new Comparator<Map<String, String>>() {
          @Override
          public int compare(Map<String, String> o1, Map<String, String> o2) {
            return new CompareToBuilder().append(o1.get("caseId"), o2.get("caseId")).toComparison();
          }
        });

    ReflectionAssert.assertReflectionEquals(
        getExpectedChild(caseId2, dateModified2, receivedOn), childrenMapList.get(0));

    assertNull(TestAppender.findMatching(new IsEqual(Level.ERROR), new IsAnything<String>()));
  }