|
| 1 | +/* |
| 2 | + * Copyright 2017-2024 original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micronaut.http.server.tck.tests.endpoints.health; |
| 17 | + |
| 18 | +import io.micronaut.context.annotation.Requires; |
| 19 | +import io.micronaut.health.HealthStatus; |
| 20 | +import io.micronaut.http.HttpRequest; |
| 21 | +import io.micronaut.http.HttpStatus; |
| 22 | +import io.micronaut.http.annotation.Controller; |
| 23 | +import io.micronaut.http.annotation.Get; |
| 24 | +import io.micronaut.http.tck.AssertionUtils; |
| 25 | +import io.micronaut.http.tck.HttpResponseAssertion; |
| 26 | +import io.micronaut.json.JsonMapper; |
| 27 | +import io.micronaut.management.health.indicator.HealthResult; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Collections; |
| 32 | + |
| 33 | +import static io.micronaut.http.tck.TestScenario.asserts; |
| 34 | + |
| 35 | +@SuppressWarnings({ |
| 36 | + "java:S5960", // We're allowed assertions, as these are used in tests only |
| 37 | + "checkstyle:MissingJavadocType", |
| 38 | + "checkstyle:DesignForExtension" |
| 39 | +}) |
| 40 | +public class HealthResultTest { |
| 41 | + |
| 42 | + private static final String SPEC_NAME = "HealthResultTest"; |
| 43 | + |
| 44 | + /** |
| 45 | + * This test verifies the available JSON Mapper is able to serialize a {@link HealthResult}. |
| 46 | + * |
| 47 | + * @throws IOException Exception thrown while getting the server under test. |
| 48 | + */ |
| 49 | + @Test |
| 50 | + public void healthResultSerialization() throws IOException { |
| 51 | + // standard header name with mixed case |
| 52 | + asserts(SPEC_NAME, |
| 53 | + HttpRequest.GET("/healthresult"), |
| 54 | + (server, request) -> AssertionUtils.assertDoesNotThrow(server, request, HttpResponseAssertion.builder() |
| 55 | + .status(HttpStatus.OK) |
| 56 | + .body("{\"name\":\"db\",\"status\":\"DOWN\",\"details\":{\"foo\":\"bar\"}}") |
| 57 | + .build())); |
| 58 | + } |
| 59 | + |
| 60 | + @Controller("/healthresult") |
| 61 | + @Requires(property = "spec.name", value = SPEC_NAME) |
| 62 | + public static class QuestionController { |
| 63 | + private final JsonMapper jsonMapper; |
| 64 | + |
| 65 | + public QuestionController(JsonMapper jsonMapper) { |
| 66 | + this.jsonMapper = jsonMapper; |
| 67 | + } |
| 68 | + |
| 69 | + @Get |
| 70 | + String index() throws IOException { |
| 71 | + return jsonMapper.writeValueAsString(HealthResult.builder("db", HealthStatus.DOWN) |
| 72 | + .details(Collections.singletonMap("foo", "bar")) |
| 73 | + .build()); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments