@Test
  public void testUpdateMsisdnForSubscriberWithBothPacks() throws Exception {
    Long oldMsisdn = subscriptionHelper.makeNumber();
    Long newMsisdn = subscriptionHelper.makeNumber();

    Subscription childSubscription =
        subscriptionHelper.mksub(
            SubscriptionOrigin.MCTS_IMPORT, DateTime.now(), SubscriptionPackType.CHILD, oldMsisdn);
    String childId = "0123456789";
    childSubscription.getSubscriber().setChild(new MctsChild(childId));
    subscriberDataService.update(childSubscription.getSubscriber());

    Subscription pregnancySubscription =
        subscriptionHelper.mksub(
            SubscriptionOrigin.MCTS_IMPORT,
            DateTime.now().minusDays(150),
            SubscriptionPackType.PREGNANCY,
            oldMsisdn);
    String motherId = "9876543210";
    pregnancySubscription.getSubscriber().setMother(new MctsMother(motherId));
    subscriberDataService.update(pregnancySubscription.getSubscriber());

    assertEquals(
        2,
        subscriberDataService
            .findByCallingNumber(oldMsisdn)
            .getActiveAndPendingSubscriptions()
            .size());

    Reader reader = createUpdateReaderWithHeaders("1," + motherId + ",,,,,,,,,,,,," + newMsisdn);
    mctsBeneficiaryUpdateService.updateBeneficiaryData(reader);

    Subscriber pregnancySubscriber = subscriberDataService.findByCallingNumber(newMsisdn);
    Subscriber childSubscriber = subscriberDataService.findByCallingNumber(oldMsisdn);

    assertNotNull(pregnancySubscriber);
    assertNotNull(childSubscriber);
    assertNotEquals(childSubscriber, pregnancySubscriber);
    assertEquals(newMsisdn, pregnancySubscriber.getCallingNumber());
    assertEquals(oldMsisdn, childSubscriber.getCallingNumber());
    assertNull(pregnancySubscriber.getChild());
    assertNull(childSubscriber.getMother());
    assertEquals(1, pregnancySubscriber.getActiveAndPendingSubscriptions().size());
    assertEquals(1, childSubscriber.getActiveAndPendingSubscriptions().size());
  }
  @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());
  }