package me.atam.atam4jsampleapp;

import me.atam.atam4j.dummytests.PassingTestsWithCategories;
import me.atam.atam4jdomain.IndividualTestResult;
import me.atam.atam4jdomain.TestsRunResult;
import me.atam.atam4jsampleapp.testsupport.AcceptanceTest;
import me.atam.atam4jsampleapp.testsupport.Atam4jApplicationStarter;
import org.junit.Test;

import javax.ws.rs.core.Response;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class PassingTestsByCategoryAcceptanceTest extends AcceptanceTest {

    @Test
    public void givenPassingTestsWithMultipleCategories_whenTestsByCategoryEndpointCalledAfterTestRun_thenOKMessageReceivedForOneTestOnly(){
        //given
        dropwizardTestSupportAppConfig = Atam4jApplicationStarter.startApplicationWith(0, PassingTestsWithCategories.class);
        //when
        Response response = getResponseFromTestsWithCategoryOnceTestRunHasCompleted("A");
        //then
        TestsRunResult testRunResult = response.readEntity(TestsRunResult.class);
        assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
        assertThat(testRunResult.getTests().size(), is(1));
        assertThat(
                testRunResult.getTests(),
                hasItem(new IndividualTestResult(PassingTestsWithCategories.class.getName(), "testThatPassesWithCategoryA", "A", true))
        );
    }


    @Test
    public void givenPassingTestsWithMultipleCategories_whenTestsByCategoryEndpointCalledAfterTestRunWithInvalidCategory_thenNotFoundMessageReceived(){
        //given
        dropwizardTestSupportAppConfig = Atam4jApplicationStarter.startApplicationWith(0, PassingTestsWithCategories.class);
        //when
        Response response = getResponseFromTestsWithCategoryOnceTestRunHasCompleted("non-existent-category");
        //then
        assertThat(response.getStatus(), is(Response.Status.NOT_FOUND.getStatusCode()));
    }

    @Test
    public void givenPassingTests_whenTestsEndpointCalledAfterTestRun_thenOKMessageReceivedForTwoTests(){
        //given
        dropwizardTestSupportAppConfig = Atam4jApplicationStarter.startApplicationWith(0, PassingTestsWithCategories.class);
        //when
        Response response = getResponseFromTestsEndpointOnceTestsRunHasCompleted();
        //then
        TestsRunResult testRunResult = response.readEntity(TestsRunResult.class);
        assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
        assertThat(testRunResult.getTests().size(), is(2));
        assertThat(testRunResult.getTests(),
                hasItem(new IndividualTestResult(PassingTestsWithCategories.class.getName(), "testThatPassesWithCategoryA", "A", true))
        );
        assertThat(testRunResult.getTests(),
                hasItem(new IndividualTestResult(PassingTestsWithCategories.class.getName(), "testThatPassesWithCategoryB", "B", true))
        );
    }


}

@@ -27,16 +27,76 @@
 leapp;%0A%0A
+import me.atam.atam4j.dummytests.PassingTestWithNoCategory;%0A
 import m
@@ -148,16 +148,16 @@
 gories;%0A
-
 import m
@@ -429,16 +429,112 @@
 ponse;%0A%0A
+import static me.atam.atam4jsampleapp.testsupport.AcceptanceTestTimeouts.TEN_SECONDS_IN_MILLIS;%0A
 import s
@@ -1428,32 +1428,32 @@
 ult.getTests(),%0A
-
                 
@@ -1590,16 +1590,731 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void givenPassingTests_withMultiplCategories_whenTestsByCategoryEndpointCalledBeforeTestRun_thenTooEarlyMessageReceived()%7B%0A        //given%0A        dropwizardTestSupportAppConfig = Atam4jApplicationStarter%0A                .startApplicationWith(TEN_SECONDS_IN_MILLIS, PassingTestWithNoCategory.class);%0A        //when%0A        Response testRunResultFromServer = getTestRunResultFromServer(getTestsURI()+%22/A%22);%0A        //then%0A        assertThat(testRunResultFromServer.getStatus(), is(Response.Status.OK.getStatusCode()));%0A        assertThat(%0A                testRunResultFromServer.readEntity(TestsRunResult.class).getStatus(),%0A                is(TestsRunResult.Status.TOO_EARLY)%0A        );%0A    %7D%0A%0A
 %0A    @Te

add test for too early response when querying by category