@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 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));
  }
  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 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));
  }