package uk.ac.ebi.quickgo.rest.period;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

/**
 * Test RemainingTimeSupplier.
 *
 * @author Tony Wardell
 * Date: 12/04/2017
 * Time: 13:57
 * Created with IntelliJ IDEA.
 */
@RunWith(MockitoJUnitRunner.class)
public class RemainingTimeSupplierTest {

    @Mock private
    Period allowedPeriod;
    private static final Duration ONE_HOUR = Duration.ofHours(1);

    @Test
    public void durationReturnedFromSinglePeriod() {
        final Duration ONE_HOUR = Duration.ofHours(1);
        when(allowedPeriod.remainingTime(any(LocalDateTime.class))).thenReturn(ONE_HOUR);

        RemainingTimeSupplier remainingTimeSupplier =
                new RemainingTimeSupplier(Collections.singletonList(allowedPeriod));

        assertThat(remainingTimeSupplier.getDuration(), is(ONE_HOUR));
    }

    @Test
    public void durationReturnedFromThirdPeriod() {
        when(allowedPeriod.remainingTime(any(LocalDateTime.class))).thenReturn(Duration.ZERO);
        when(allowedPeriod.remainingTime(any(LocalDateTime.class))).thenReturn(Duration.ZERO);
        when(allowedPeriod.remainingTime(any(LocalDateTime.class))).thenReturn(ONE_HOUR);

        RemainingTimeSupplier remainingTimeSupplier = new RemainingTimeSupplier(Arrays.asList(allowedPeriod,
                                                                                              allowedPeriod,
                                                                                              allowedPeriod));

        assertThat(remainingTimeSupplier.getDuration(), is(ONE_HOUR));
    }

    @Test
    public void noActivePeriodSoDurationIsZero() {
        when(allowedPeriod.remainingTime(any(LocalDateTime.class))).thenReturn(Duration.ZERO);
        when(allowedPeriod.remainingTime(any(LocalDateTime.class))).thenReturn(Duration.ZERO);
        when(allowedPeriod.remainingTime(any(LocalDateTime.class))).thenReturn(Duration.ZERO);

        RemainingTimeSupplier remainingTimeSupplier = new RemainingTimeSupplier(Arrays.asList(allowedPeriod,
                                                                                              allowedPeriod,
                                                                                              allowedPeriod));

        assertThat(remainingTimeSupplier.getDuration(), is(Duration.ZERO));
    }

    @Test
    public void noDurationFoundFromEmptyPeriodList() {
        RemainingTimeSupplier remainingTimeSupplier = new RemainingTimeSupplier(new ArrayList<>());

        final Duration timeLeft = remainingTimeSupplier.getDuration();

        assertThat(timeLeft, is(Duration.ZERO));
    }

    @Test(expected = IllegalArgumentException.class)
    public void collectionOfPeriodsWhenNullThrowsException() {
        new RemainingTimeSupplier(null);
    }
}

@@ -695,24 +695,28 @@
    @Mock
+%0A   
  private
 %0A    Per
@@ -707,20 +707,16 @@
  private
-%0A   
  Period 
@@ -795,16 +795,82 @@
 ours(1);
+%0A    private static final Duration TWO_HOUR = Duration.ofHours(2);
 %0A%0A    @T
@@ -930,63 +930,8 @@
 ) %7B%0A
-        final Duration ONE_HOUR = Duration.ofHours(1);%0A
     
@@ -1285,13 +1285,20 @@
 From
-Third
+FirstNonZero
 Peri
@@ -1389,33 +1389,32 @@
 n(Duration.ZERO)
-;
 %0A        when(al
@@ -1410,67 +1410,67 @@
     
-when(allowedPeriod.remainingTime(any(LocalDateTime.class)))
+                                                           
 .the
@@ -1483,33 +1483,32 @@
 n(Duration.ZERO)
-;
 %0A        when(al
@@ -1504,67 +1504,156 @@
     
-when(allowedPeriod.remainingTime(any(LocalDateTime.class)))
+                                                           .thenReturn(ONE_HOUR)%0A                                                                   
 .the
@@ -1652,35 +1652,35 @@
     .thenReturn(
-ONE
+TWO
 _HOUR);%0A%0A       
@@ -2226,33 +2226,32 @@
 n(Duration.ZERO)
-;
 %0A        when(al
@@ -2247,67 +2247,67 @@
     
-when(allowedPeriod.remainingTime(any(LocalDateTime.class)))
+                                                           
 .the
@@ -2328,17 +2328,16 @@
 on.ZERO)
-;
 %0A       
@@ -2341,67 +2341,67 @@
     
-when(allowedPeriod.remainingTime(any(LocalDateTime.class)))
+                                                           
 .the

Reformat code, make test little clearer.