@Test
  public void testUpdateDOBAndCompleteActiveSubscription() throws Exception {
    createLocationData();

    Long msisdn = subscriptionHelper.makeNumber();
    String childId = "0123456789";
    DateTime originalDOB = DateTime.now().minusDays(100);
    DateTime updatedDOB = originalDOB.minusDays(400);

    MctsChild child = new MctsChild(childId);
    child.setState(stateDataService.findByCode(21L));
    child.setDistrict(districtService.findByStateAndCode(child.getState(), 3L));
    makeMctsSubscription(child, originalDOB, SubscriptionPackType.CHILD, msisdn);

    // verify that the subscription is active
    Subscriber subscriber = subscriberDataService.findByCallingNumber(msisdn);
    Subscription subscription = subscriber.getAllSubscriptions().iterator().next();
    assertEquals(SubscriptionStatus.ACTIVE, subscription.getStatus());

    // now, via CSV update, change the DOB to a past subscription date; subscription should be
    // marked completed
    Reader reader =
        createUpdateReaderWithHeaders(
            "1," + childId + ",," + getDateString(updatedDOB) + ",,,,,,,,,,,");
    mctsBeneficiaryUpdateService.updateBeneficiaryData(reader);

    Subscriber updatedSubscriber = subscriberDataService.findByCallingNumber(msisdn);
    assertEquals(getDateString(updatedDOB), getDateString(updatedSubscriber.getDateOfBirth()));
    Subscription updatedSubscription = updatedSubscriber.getAllSubscriptions().iterator().next();
    assertEquals(getDateString(updatedDOB), getDateString(updatedSubscription.getStartDate()));
    assertEquals(SubscriptionStatus.COMPLETED, updatedSubscription.getStatus());
  }
  @Test
  public void testUpdateLMPAndReactivateCompletedSubscription() throws Exception {
    createLocationData();

    Long msisdn = subscriptionHelper.makeNumber();
    String motherId = "0123456789";
    DateTime originalLMP = DateTime.now().minusDays(100);
    DateTime updatedLMP = originalLMP.minusDays(100);

    subscriptionHelper.mksub(
        SubscriptionOrigin.MCTS_IMPORT, originalLMP, SubscriptionPackType.PREGNANCY, msisdn);
    Subscriber subscriber = subscriberDataService.findByCallingNumber(msisdn);
    subscriber.setLastMenstrualPeriod(originalLMP);
    MctsMother mother = new MctsMother(motherId);
    mother.setState(stateDataService.findByCode(21L));
    mother.setDistrict(districtService.findByStateAndCode(mother.getState(), 3L));
    subscriber.setMother(mother);
    subscriberDataService.update(subscriber);

    // pre-date the LMP so that the subscription will be marked completed
    subscriber.setLastMenstrualPeriod(originalLMP.minusDays(600));
    subscriberService.update(subscriber);
    subscriber = subscriberDataService.findByCallingNumber(msisdn);
    Subscription subscription = subscriber.getAllSubscriptions().iterator().next();
    assertEquals(SubscriptionStatus.COMPLETED, subscription.getStatus());

    // now, via CSV update, change the LMP to a valid subscription date; subscription should get
    // reactivated
    Reader reader =
        createUpdateReaderWithHeaders(
            "1," + motherId + ",,," + getDateString(updatedLMP) + ",,,,,,,,,,");
    mctsBeneficiaryUpdateService.updateBeneficiaryData(reader);

    Subscriber updatedSubscriber = subscriberDataService.findByCallingNumber(msisdn);
    assertEquals(
        getDateString(updatedLMP), getDateString(updatedSubscriber.getLastMenstrualPeriod()));
    Subscription updatedSubscription =
        updatedSubscriber.getActiveAndPendingSubscriptions().iterator().next();
    assertEquals(
        getDateString(updatedLMP.plusDays(90)), getDateString(updatedSubscription.getStartDate()));
    assertEquals(SubscriptionStatus.ACTIVE, updatedSubscription.getStatus());
  }
  @Test
  public void testUpdateBeneficiariesFromFile() throws Exception {
    createLocationData();

    // ----Create 4 beneficiaries:----

    String child1id = "1234567890";
    MctsChild child1 = new MctsChild(child1id);
    child1.setState(stateDataService.findByCode(21L));
    child1.setDistrict(districtService.findByStateAndCode(child1.getState(), 3L));
    Long child1msisdn = subscriptionHelper.makeNumber();
    makeMctsSubscription(
        child1, DateTime.now().minusDays(100), SubscriptionPackType.CHILD, child1msisdn);

    String mother2id = "1234567899";
    MctsMother mother2 = new MctsMother(mother2id);
    mother2.setState(stateDataService.findByCode(21L));
    mother2.setDistrict(districtService.findByStateAndCode(mother2.getState(), 3L));
    Long mother2msisdn = subscriptionHelper.makeNumber();
    makeMctsSubscription(
        mother2, DateTime.now().minusDays(100), SubscriptionPackType.PREGNANCY, mother2msisdn);

    String mother3id = "9876543210";
    MctsMother mother3 = new MctsMother(mother3id);
    mother3.setState(stateDataService.findByCode(21L));
    mother3.setDistrict(districtService.findByStateAndCode(mother3.getState(), 3L));
    makeMctsSubscription(
        mother3,
        DateTime.now().minusDays(100),
        SubscriptionPackType.PREGNANCY,
        subscriptionHelper.makeNumber());

    Long child4msisdn = subscriptionHelper.makeNumber();
    MctsChild child4 = new MctsChild("9876543211");
    child4.setState(stateDataService.findByCode(21L));
    child4.setDistrict(districtService.findByStateAndCode(child4.getState(), 3L));
    makeMctsSubscription(
        child4, DateTime.now().minusDays(100), SubscriptionPackType.CHILD, child4msisdn);

    // ----Update all 4 via CSV:----

    mctsBeneficiaryUpdateService.updateBeneficiaryData(read("csv/mcts_beneficiary_update.csv"));

    // ----Validate updates to each:----

    // MSISDN update:
    Subscriber oldSubscriber1 = subscriberDataService.findByCallingNumber(child1msisdn);
    assertNull(oldSubscriber1.getChild());
    assertEquals(0, oldSubscriber1.getAllSubscriptions().size());

    Subscriber subscriber1 = subscriberDataService.findByCallingNumber(9439986187L);
    assertNotNull(subscriber1);
    assertEquals(child1id, subscriber1.getChild().getBeneficiaryId());

    // MSISDN update:
    Subscriber oldSubscriber2 = subscriberDataService.findByCallingNumber(mother2msisdn);
    assertNull(oldSubscriber2.getMother());
    assertEquals(0, oldSubscriber2.getAllSubscriptions().size());

    Subscriber subscriber2 = subscriberDataService.findByCallingNumber(9439986188L);
    assertNotNull(subscriber2);
    assertEquals(mother2id, subscriber2.getMother().getBeneficiaryId());

    // Location update:
    MctsMother updatedMother3 = mctsMotherDataService.findByBeneficiaryId(mother3id);
    assertEquals(21L, (long) updatedMother3.getState().getCode());
    assertEquals(3L, (long) updatedMother3.getDistrict().getCode());
    assertEquals("0026", updatedMother3.getTaluka().getCode());
    assertEquals(453L, (long) updatedMother3.getHealthBlock().getCode());

    // DOB update:
    String updatedDOB = "01-07-2015";
    Subscriber subscriber4 = subscriberDataService.findByCallingNumber(child4msisdn);
    assertEquals(updatedDOB, getDateString(subscriber4.getDateOfBirth()));
    Subscription updatedSubscription =
        subscriber4.getActiveAndPendingSubscriptions().iterator().next();
    assertEquals(updatedDOB, getDateString(updatedSubscription.getStartDate()));
    assertEquals(SubscriptionStatus.ACTIVE, updatedSubscription.getStatus());
  }