/*
 * Copyright 2016 Liam Williams <liam.williams@zoho.com>.
 *
 * This file is part of yatspec-fluent.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.theangrydev.yatspecfluent;

import org.assertj.core.api.WithAssertions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import static java.lang.String.format;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.internal.verification.VerificationModeFactory.times;

public class FluentTestTest extends FluentTest<FluentTestTest.Request, FluentTestTest.Response> implements WithAssertions {

    private final ThenFactory<TestAssertions, Response> testAssertions = TestAssertions::new;
    private final TestSystem testSystem = mock(TestSystem.class);
    private final TestSystem nullRequestTestSystem = mock(TestSystem.class);
    private final TestSystem nullResponseTestSystem = mock(TestSystem.class);
    private final SomeDependency someDependency = mock(SomeDependency.class);
    private final SomeDependency someDependency2 = mock(SomeDependency.class);
    private final AnotherDependency anotherDependency = mock(AnotherDependency.class);
    private final Request request = new Request();
    private final Response response = new Response();

    private boolean given;
    private boolean when;
    private boolean then;

    private static class TestAssertions {

        final Response response;

        TestAssertions(Response response) {
            this.response = response;
        }

    }

    static class Response {}
    static class Request {}
    private interface TestSystem extends When<Request,Response> {}
    private interface SomeDependency extends Given {}
    private interface AnotherDependency extends Given {}

    @Before
    public void setUp() {
        Mockito.when(testSystem.request()).thenReturn(request);
        Mockito.when(nullResponseTestSystem.request()).thenReturn(request);
        Mockito.when(testSystem.response(request)).thenReturn(response);
    }

    @After
    public void putTestInValidState() {
        if (!given && !when) {
            given(someDependency);
        }
        if (!when) {
            when(testSystem);
        }
        if (!then) {
            then(testAssertions);
        }
    }

    @Override
    protected void given(Given given) {
        super.given(given);
        this.given = true;
    }

    @Override
    protected <T extends When<Request, Response>> void when(T when) {
        super.when(when);
        this.when = true;
    }

    @Override
    protected <Then> Then then(ThenFactory<Then, Response> thenFactory) {
        Then then = super.then(thenFactory);
        this.then = true;
        return then;
    }

    @Test
    public void hasTestState() {
        assertThat(testState().interestingGivens).isEqualTo(doNotUseTheInterestingGivens);
        assertThat(testState().capturedInputAndOutputs).isEqualTo(doNotUseTheCapturedInputAndOutputs);
    }

    @Test
    public void nullRequestIsReported() {
        assertThatThrownBy(() -> {
            when(nullRequestTestSystem);
        }).hasMessage(format("'%s' request was null", nullRequestTestSystem));
    }

    @Test
    public void nullResponseIsReported() {
        assertThatThrownBy(() -> {
            when(nullResponseTestSystem);
        }).hasMessage(format("'%s' response was null", nullResponseTestSystem));
    }

    @Test
    public void responseIsPassedToTheAssertions() {
        given(someDependency);
        when(testSystem);
        TestAssertions then = then(testAssertions);
        assertThat(then.response).isSameAs(response);
    }

    @Test
    public void firstGivenIsPrimedAfterGiven() {
        given(someDependency);
        verify(someDependency).prime();
    }

    @Test
    public void firstWhenAdaptedToGivenIsPrimedAfterGiven() {
        given(testSystem);
        verify(testSystem).response(request);
    }

    @Test
    public void subsequentGivensArePrimedAfterThem() {
        given(someDependency);
        and(anotherDependency);
        verify(anotherDependency).prime();
    }

    @Test
    public void subsequentWhenAdaptedToGivensArePrimedAfterThem() {
        given(someDependency);
        and(testSystem);
        verify(testSystem).response(request);
    }

    @Test
    public void whenIsPrimedAfterWhen() {
        given(someDependency);
        and(anotherDependency);
        when(testSystem);
        verify(testSystem).response(request);
    }

    @Test
    public void multipleGivensOfTheSameTypeAreAllowed() {
        given(someDependency);
        verify(someDependency).prime();
        and(someDependency2);
        verify(someDependency2).prime();
    }

    @Test
    public void callingSameGivenTwiceIsAllowed() {
        given(someDependency);
        verify(someDependency).prime();
        and(someDependency);
        verify(someDependency, times(2)).prime();
    }

    @Test
    public void callingGivenAfterWhenIsNotAllowed() {
        assertThatThrownBy(() -> {
            when(testSystem);
            given(someDependency);
        }).hasMessage("The 'given' steps must be specified before the 'when' and 'then' steps");
    }

    @Test
    public void multipleWhensAreNotAllowed() {
        assertThatThrownBy(() -> {
            when(testSystem);
            when(testSystem);
        }).hasMessage("There should only be one 'when', after the 'given' and before the 'then'");
    }

    @Test
    public void callingGivenAfterThenIsNotAllowed() {
        assertThatThrownBy(() -> {
            when(testSystem);
            then(testAssertions);
            given(someDependency);
        }).hasMessage("The 'given' steps must be specified before the 'when' and 'then' steps");
    }

    @Test
    public void callingThenBeforeWhenIsNotAllowed() {
        assertThatThrownBy(() -> then(testAssertions)).hasMessage("The 'then' steps should be after the 'when'");
    }
}

@@ -818,16 +818,58 @@
 t.Test;%0A
+import org.junit.runners.model.Statement;%0A
 import o
@@ -928,16 +928,66 @@
 format;%0A
+import static org.junit.runner.Description.EMPTY;%0A
 import s
@@ -1259,24 +1259,214 @@
 Assertions %7B
+%0A    private static final Statement SUCCESSFUL_STATEMENT = new Statement() %7B%0A        @Override%0A        public void evaluate() throws Throwable %7B%0A            // everything OK%0A        %7D%0A    %7D;
 %0A%0A    privat
@@ -6779,11 +6779,260 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void eachTestNeedsAtLeastAWhenAndAThen() %7B%0A        assertThatThrownBy(() -%3E makeSureThenIsUsed.apply(SUCCESSFUL_STATEMENT, EMPTY).evaluate())%0A                .hasMessage(%22Each test needs at least a 'when' and a 'then'%22);%0A    %7D
 %0A%7D%0A

Test coverage