@Test
  public void shouldGetAllFacilitiesByName() {
    final String facilityName = "name";
    final String country = "country";
    final String region = "region";
    final String district = "district";
    final String province = "province";
    final String phoneNumber = "0123456789";
    final String mrsFacilityId = "12";

    final MRSFacility mrsFacility =
        new MRSFacility(mrsFacilityId, facilityName, country, region, district, province);
    Facility facility = new Facility(mrsFacility).phoneNumber(phoneNumber);

    when(mockMrsFacilityAdaptor.saveFacility(mrsFacility))
        .thenReturn(
            new MRSFacility(
                mrsFacilityId, facilityName, "country", "region", "district", "province"));
    allFacilities.add(facility);
    when(mockMrsFacilityAdaptor.getFacilities(facilityName)).thenReturn(Arrays.asList(mrsFacility));

    final List<Facility> actualFacilities = allFacilities.facilitiesByName(facilityName);

    final Facility actualFacility = actualFacilities.iterator().next();
    assertFacility(
        actualFacility, mrsFacilityId, facilityName, country, region, district, province);
    assertThat(actualFacility.phoneNumber(), is(equalTo(phoneNumber)));
  }
  @Test
  public void shouldUpdateFacility() {
    final String facilityId = "16";
    final String facilityName = "name";
    final String phoneNumber = "0123456789";
    String additionalPhone1 = "12345";
    String additionalPhone2 = "321234";
    String additionalPhone3 = "33344";

    final String updatedFacilityName = "name";
    final String updatedPhoneNumber = "0123456789";
    String updatedAdditionalPhone1 = "12345";
    String updatedAdditionalPhone2 = "321234";
    String updatedAdditionalPhone3 = "33344";

    final MRSFacility mrsFacility = new MRSFacility(facilityName);
    Facility facility =
        facility(phoneNumber, additionalPhone1, additionalPhone2, additionalPhone3, mrsFacility);

    when(mockMrsFacilityAdaptor.saveFacility(mrsFacility))
        .thenReturn(
            new MRSFacility(facilityId, facilityName, "country", "region", "district", "province"));
    allFacilities.add(facility);

    final MRSFacility updatedMRSFacility = new MRSFacility(updatedFacilityName);
    facility
        .phoneNumber(updatedPhoneNumber)
        .additionalPhoneNumber1(updatedAdditionalPhone1)
        .additionalPhoneNumber2(updatedAdditionalPhone2)
        .additionalPhoneNumber3(updatedAdditionalPhone3)
        .mrsFacility(updatedMRSFacility);

    allFacilities.update(facility);
  }
  @Test
  public void shouldFetchFacilitiesGivenAListOfMRSFacilityIds() {
    final MRSFacility anyFacility1 =
        new MRSFacility("1", "name", "country", "region", "district", "province");
    final MRSFacility anyFacility2 =
        new MRSFacility("2", "name", "country", "region", "district", "province");
    final MRSFacility anyFacility3 =
        new MRSFacility("3", "name", "country", "region", "district", "province");
    final MRSFacility anyFacility4 =
        new MRSFacility("4", "name", "country", "region", "district", "province");
    final Facility facility1 =
        new Facility(new MRSFacility("1", "name1", "country", "region", "district", "province"));
    final Facility facility2 =
        new Facility(new MRSFacility("2", "name2", "country", "region", "district", "province"));
    final Facility facility3 =
        new Facility(new MRSFacility("3", "name3", "country", "region", "district", "province"));
    final Facility facility4 =
        new Facility(new MRSFacility("4", "name4", "country", "region", "district", "province"));
    when(mockMrsFacilityAdaptor.saveFacility(facility1.mrsFacility())).thenReturn(anyFacility1);
    when(mockMrsFacilityAdaptor.saveFacility(facility2.mrsFacility())).thenReturn(anyFacility2);
    when(mockMrsFacilityAdaptor.saveFacility(facility3.mrsFacility())).thenReturn(anyFacility3);
    when(mockMrsFacilityAdaptor.saveFacility(facility4.mrsFacility())).thenReturn(anyFacility4);
    allFacilities.add(facility1);
    allFacilities.add(facility2);
    allFacilities.add(facility3);
    allFacilities.add(facility4);

    final List<Facility> byFacilityIds = allFacilities.findByFacilityIds(Arrays.asList("1", "2"));
    assertThat(byFacilityIds.size(), is(2));
  }
  @Test
  public void shouldGetFacilityByMotechFacilityId() {
    AllFacilities allFacilitiesSpy = spy(allFacilities);
    String motechFacilityId = "10000";
    String mrsFacilityId = "10";
    String phone_number = "0123456789";
    Facility motechFacility =
        facility(phone_number, null, null, null, null)
            .mrsFacilityId(mrsFacilityId)
            .motechId(motechFacilityId);
    MRSFacility mrsFacility = mock(MRSFacility.class);

    doReturn(motechFacility).when(allFacilitiesSpy).findByMotechFacilityId(motechFacilityId);
    when(mockMrsFacilityAdaptor.getFacility(mrsFacilityId)).thenReturn(mrsFacility);

    Facility returnedFacility = allFacilitiesSpy.getFacilityByMotechId(motechFacilityId);
    assertThat(returnedFacility.motechId(), is(equalTo(motechFacilityId)));
    assertThat(returnedFacility.mrsFacilityId(), is(equalTo(mrsFacilityId)));
    assertThat(returnedFacility.phoneNumber(), is(equalTo(phone_number)));
    assertThat(returnedFacility.mrsFacility(), is(equalTo(mrsFacility)));
  }
  @Test
  public void shouldSaveAFacility() throws Exception {
    final String facilityName = "name";
    final String phoneNumber = "0123456789";
    String additionalPhone1 = "12345";
    String additionalPhone2 = "321234";
    String additionalPhone3 = "33344";
    final MRSFacility mrsFacility = new MRSFacility(facilityName);
    Facility facility =
        facility(phoneNumber, additionalPhone1, additionalPhone2, additionalPhone3, mrsFacility);
    final int initialSize = allFacilities.getAll().size();

    final String facilityId = "12";
    when(mockMrsFacilityAdaptor.saveFacility(mrsFacility))
        .thenReturn(
            new MRSFacility(facilityId, facilityName, "country", "region", "district", "province"));

    allFacilities.add(facility);

    final List<Facility> facilities = allFacilities.getAll();
    final int actualSize = facilities.size();
    final Facility actualFacility = facilities.iterator().next();
    assertThat(actualSize, is(equalTo(initialSize + 1)));
    assertThat(actualFacility.phoneNumber(), is(equalTo(phoneNumber)));
    assertThat(actualFacility.additionalPhoneNumber1(), is(equalTo(additionalPhone1)));
    assertThat(actualFacility.additionalPhoneNumber2(), is(equalTo(additionalPhone2)));
    assertThat(actualFacility.additionalPhoneNumber3(), is(equalTo(additionalPhone3)));
    assertThat(actualFacility.mrsFacilityId(), is(equalTo(facilityId)));
  }
 private void assertFacility(
     Facility actualFacility,
     String facilityId,
     String facilityName,
     String country,
     String region,
     String district,
     String province) {
   assertThat(actualFacility.name(), is(equalTo(facilityName)));
   assertThat(actualFacility.country(), is(equalTo(country)));
   assertThat(actualFacility.region(), is(equalTo(region)));
   assertThat(actualFacility.province(), is(equalTo(province)));
   assertThat(actualFacility.district(), is(equalTo(district)));
   assertThat(actualFacility.mrsFacility().getId(), is(equalTo(facilityId)));
 }
  @Test
  public void shouldReturnListOfFacilitiesByNameEvenIfTheRecordsAreMissingInCouchDb() {
    final String facilityName = "name";
    final String country = "country";
    final String region = "region";
    final String district = "district";
    final String province = "province";
    final String mrsFacilityId = "13";

    final MRSFacility mrsFacility =
        new MRSFacility(mrsFacilityId, facilityName, country, region, district, province);
    when(mockMrsFacilityAdaptor.getFacilities(facilityName)).thenReturn(Arrays.asList(mrsFacility));

    final List<Facility> actualFacilities = allFacilities.facilitiesByName(facilityName);

    final Facility actualFacility = actualFacilities.iterator().next();
    assertThat(actualFacility.name(), is(equalTo(facilityName)));
    assertThat(actualFacility.country(), is(equalTo(country)));
    assertThat(actualFacility.region(), is(equalTo(region)));
    assertThat(actualFacility.province(), is(equalTo(province)));
    assertThat(actualFacility.district(), is(equalTo(district)));
  }