Repository: jenkinsci/jenkins
File: test/src/test/java/jenkins/health/HealthCheckActionTest.java (Java)

Code before:
                    "name": "completedInitialization",
                    "result": true
                  }
                ]
            }
            """), JSONObject.fromObject(page.getWebResponse().getContentAsString()));
        }
    }

    @Test
    public void healthCheckSuccessExtension() throws Exception {
        try (var webClient = r.createWebClient()) {
            var page = webClient.goTo("healthCheck", "application/json");
            assertThat(page.getWebResponse().getStatusCode(), is(200));
            assertEquals(JSONObject.fromObject("""
            {
                "status": true,
                "data": [
                  {
                    "name": "completedInitialization",
                    "result": true
                  },
                  {
                    "name": "success",
                    "result": true
                  }
                ]
            }
            """), JSONObject.fromObject(page.getWebResponse().getContentAsString()));
        }
    }

    @TestExtension({"healthCheckSuccessExtension", "healthCheckFailingExtension"})
    public static class SuccessHealthCheck implements HealthCheck {

        @Override
        public String getName() {
            return "success";
        }

        @Override
        public boolean check() {
            return true;
        }
    }

    @Test
    public void healthCheckFailingExtension() throws Exception {
        try (var webClient = r.createWebClient()) {
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setPrintContentOnFailingStatusCode(false);

Review comment (suggestion):
if all are true I would consider whether we any need to include this information.

(healthchecks can get hit a lot)

Code after:
            """), JSONObject.fromObject(page.getWebResponse().getContentAsString()));
        }
    }

    private static String healthUrl() {
        return ExtensionList.lookupSingleton(HealthCheckAction.class).getUrlName();
    }

    @Test
    public void healthCheckSuccessExtension() throws Exception {
        try (var webClient = r.createWebClient()) {
            var page = webClient.goTo(healthUrl(), "application/json");
            assertThat(page.getWebResponse().getStatusCode(), is(200));
            assertEquals(JSONObject.fromObject("""
            {
                "status": true
            }
            """), JSONObject.fromObject(page.getWebResponse().getContentAsString()));
        }
    }

    @TestExtension({"healthCheckSuccessExtension", "healthCheckFailingExtension"})
    public static class SuccessHealthCheck implements HealthCheck {

        @Override
        public String getName() {
            return "success";
        }

        @Override
        public boolean check() {
            return true;
        }
    }

    @Test
    public void healthCheckFailingExtension() throws Exception {
        try (var webClient = r.createWebClient()) {
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setPrintContentOnFailingStatusCode(false);
            var page = webClient.goTo(healthUrl(), "application/json");
            assertThat(page.getWebResponse().getStatusCode(), is(503));
            assertEquals(JSONObject.fromObject("""
            {
                "status": false,
                "failures": ["failing"]
            }
            """), JSONObject.fromObject(page.getWebResponse().getContentAsString()));
        }
    }