/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
 */

package org.openlmis.referencedata.service;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.openlmis.referencedata.domain.GeographicZone;
import org.openlmis.referencedata.repository.GeographicZoneRepository;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

@RunWith(MockitoJUnitRunner.class)
public class GeographicZoneServiceTest {

  @Mock
  private GeographicZoneRepository geographicZoneRepository;

  @Mock
  private GeographicZone parent;

  @Mock
  private GeographicZone children;

  @Mock
  private GeographicZone secondChildren;

  @Mock
  private GeographicZone childrenOfChildren;

  @InjectMocks
  private GeographicZoneService geographicZoneService;

  @Test
  public void shouldRetrieveOneDescendant() {
    when(geographicZoneRepository.findByParent(parent))
        .thenReturn(Collections.singletonList(children));

    Collection<GeographicZone> allZonesInHierarchy =
        geographicZoneService.getAllZonesInHierarchy(parent);

    assertEquals(Collections.singletonList(children), allZonesInHierarchy);
  }

  @Test
  public void shouldRetrieveManyDescendantsWhenTheChildrenHasAChildren() {
    when(geographicZoneRepository.findByParent(parent))
        .thenReturn(Collections.singletonList(children));
    when(geographicZoneRepository.findByParent(children))
        .thenReturn(Collections.singletonList(childrenOfChildren));

    Collection<GeographicZone> allZonesInHierarchy =
        geographicZoneService.getAllZonesInHierarchy(parent);

    assertEquals(Arrays.asList(children, childrenOfChildren), allZonesInHierarchy);
  }

  @Test
  public void shouldRetrieveManyDescendantsWhenParentHasManyChildren() {
    when(geographicZoneRepository.findByParent(parent))
        .thenReturn(Arrays.asList(children, secondChildren));

    Collection<GeographicZone> allZonesInHierarchy =
        geographicZoneService.getAllZonesInHierarchy(parent);

    assertEquals(Arrays.asList(children, secondChildren), allZonesInHierarchy);
  }

  @Test
  public void shouldNotRetrieveAnyDescendantsWhenParentHasNoChildren() {
    when(geographicZoneRepository.findByParent(parent))
        .thenReturn(Collections.emptyList());

    Collection<GeographicZone> allZonesInHierarchy =
        geographicZoneService.getAllZonesInHierarchy(parent);

    assertEquals(Collections.emptyList(), allZonesInHierarchy);
  }
}
@@ -1576,27 +1576,24 @@
 icZone child
-ren
 ;%0A%0A  @Mock%0A 
@@ -1623,27 +1623,24 @@
  secondChild
-ren
 ;%0A%0A  @Mock%0A 
@@ -1668,29 +1668,23 @@
 ne child
-ren
 OfChild
-ren
 ;%0A%0A  @In
@@ -1798,16 +1798,37 @@
 scendant
+WhenParentHasOneChild
 () %7B%0A   
@@ -1927,27 +1927,24 @@
 onList(child
-ren
 ));%0A%0A    Col
@@ -2096,27 +2096,24 @@
 onList(child
-ren
 ), allZonesI
@@ -2193,19 +2193,16 @@
 TheChild
-ren
 HasAChil
@@ -2198,27 +2198,24 @@
 ildHasAChild
-ren
 () %7B%0A    whe
@@ -2310,27 +2310,24 @@
 onList(child
-ren
 ));%0A    when
@@ -2370,19 +2370,16 @@
 nt(child
-ren
 ))%0A     
@@ -2424,29 +2424,23 @@
 st(child
-ren
 OfChild
-ren
 ));%0A%0A   
@@ -2592,31 +2592,22 @@
 hild
-ren
 , child
-ren
 OfChild
-ren
 ), a
@@ -2800,35 +2800,32 @@
 ays.asList(child
-ren
 , secondChildren
@@ -2821,19 +2821,16 @@
 ondChild
-ren
 ));%0A%0A   
@@ -2978,19 +2978,16 @@
 st(child
-ren
 , second
@@ -2991,19 +2991,16 @@
 ondChild
-ren
 ), allZo

fix fields and test names