@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));
  }
  @Test
  public void shouldSendSmsWhenTwoAppointmentsAreMissed() throws Exception {
    String motechCaseId = "motechCaseId";
    String phuId = "phuId";
    String phone = "phone";
    String userId = "userId";
    String firstName = "firstName";
    String lastName = "lastName";

    Map<String, String> motherCaseFieldValues = new HashMap<>();
    motherCaseFieldValues.put(Commcare.PHU_ID, phuId);

    MotechEvent event = new MotechEvent();
    event.getParameters().put(EventKeys.MOTHER_CASE_ID, motechCaseId);

    when(commcareCaseService.getCaseByCaseId(motechCaseId)).thenReturn(motherCase);
    when(motherCase.getFieldValues()).thenReturn(motherCaseFieldValues);
    when(motherCase.getUserId()).thenReturn(userId);

    when(fixtureIdMap.getPhoneForFixture(phuId)).thenReturn(phone);

    when(commcareUserService.getCommcareUserById(userId)).thenReturn(commcareUser);
    when(commcareUser.getFirstName()).thenReturn(firstName);
    when(commcareUser.getLastName()).thenReturn(lastName);

    when(settings.getLanguage()).thenReturn(LANGUAGE);
    when(cmsLiteService.getStringContent(LANGUAGE, POST_PARTUM_VISITS_MESSAGE_NAME))
        .thenReturn(new StringContent(LANGUAGE, POST_PARTUM_VISITS_MESSAGE_NAME, "%s"));

    listener.postPartumMissedVisitHandler(event);

    ArgumentCaptor<OutgoingSms> captor = ArgumentCaptor.forClass(OutgoingSms.class);

    verify(smsService).send(captor.capture());

    OutgoingSms value = captor.getValue();

    assertEquals(asList(phone), value.getRecipients());
    assertEquals(firstName + " " + lastName, value.getMessage());
  }
  @Test
  public void shouldNotSendSmsIfPhuIDIsNull() throws Exception {
    String motechCaseId = "motechCaseId";

    Map<String, String> motherCaseFieldValues = new HashMap<>();
    motherCaseFieldValues.put(Commcare.PHU_ID, null);

    MotechEvent event = new MotechEvent();
    event.getParameters().put(EventKeys.MOTHER_CASE_ID, motechCaseId);

    when(commcareCaseService.getCaseByCaseId(motechCaseId)).thenReturn(motherCase);
    when(motherCase.getFieldValues()).thenReturn(motherCaseFieldValues);

    listener.postPartumMissedVisitHandler(event);

    verify(fixtureIdMap, never()).getPhoneForFixture(anyString());
    verify(smsService, never()).send(any(OutgoingSms.class));
  }