package fr.mmarie;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import io.dropwizard.testing.ResourceHelpers;
import io.dropwizard.testing.junit.DropwizardAppRule;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.assertj.core.api.StrictAssertions.assertThat;

public class GitLabJiraApplicationTest {
    public static final int PORT = 1338;

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(PORT);

    @ClassRule
    public static final DropwizardAppRule<GitLabJiraConfiguration> RULE =
            new DropwizardAppRule<>(GitLabJiraApplication.class, ResourceHelpers.resourceFilePath("properties-test.yml"));

    @Test
    public void testHealthChecks() {
        wireMockRule.stubFor(get(urlEqualTo("/rest/api/2/serverInfo"))
                .withHeader("Authorization", matching("Basic .*"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody("{}")));

        Client client = ClientBuilder.newClient();

        Response healthcheck = client.target(String.format("http://localhost:%d/", RULE.getAdminPort()))
                .path("healthcheck")
                .request()
                .get();

        assertThat(healthcheck.getStatus())
                .isEqualTo(Response.Status.OK.getStatusCode());

        assertThat(healthcheck.readEntity(String.class))
                .contains("\"jira\":{\"healthy\":true,\"message\":\"Jira server has been contacted successfully\"");

        wireMockRule.verify(getRequestedFor(urlEqualTo("/rest/api/2/serverInfo"))
                .withHeader("Authorization", matching("Basic .*")));

    }
}
@@ -1193,16 +1193,27 @@
 stHealth
+yJiraHealth
 Checks()
@@ -2155,20 +2155,1062 @@
 sic .*%22)));%0A
+    %7D%0A%0A    @Test%0A    public void testUnhealthyJiraHealthChecks() %7B%0A        wireMockRule.stubFor(get(urlEqualTo(%22/rest/api/2/serverInfo%22))%0A                .withHeader(%22Authorization%22, matching(%22Basic .*%22))%0A                .willReturn(aResponse()%0A                        .withStatus(500)%0A                        .withBody(%22%7B%7D%22)));%0A%0A        Client client = ClientBuilder.newClient();%0A%0A        Response healthcheck = client.target(String.format(%22http://localhost:%25d/%22, RULE.getAdminPort()))%0A                .path(%22healthcheck%22)%0A                .request()%0A                .get();%0A%0A        assertThat(healthcheck.getStatus())%0A                .isEqualTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());%0A%0A        assertThat(healthcheck.readEntity(String.class))%0A                .contains(%22%5C%22jira%5C%22:%7B%5C%22healthy%5C%22:false,%5C%22message%5C%22:%5C%22Unable to contact JIRA server, HTTP code received %3C500%3E%5C%22%22);%0A%0A        wireMockRule.verify(getRequestedFor(urlEqualTo(%22/rest/api/2/serverInfo%22))%0A                .withHeader(%22Authorization%22, matching(%22Basic .*%22)));
 %0A    %7D%0A%7D

add test for unhealthy case