From 9df40a0d44deef19cc3f2e69f6bca45f59113022 Mon Sep 17 00:00:00 2001 From: Fabio Burzigotti Date: Wed, 13 Mar 2024 17:59:45 +0100 Subject: [PATCH] [issue-325] - Add TCK tests to verify that DeploymentException is thrown when HealthCheckResponse name is null or empty string --- .../tck/EmptyNameHealthCheckResponseTest.java | 67 +++++++++++++++++++ .../tck/NullNameHealthCheckResponseTest.java | 67 +++++++++++++++++++ .../health/tck/deployment/EmptyNameCheck.java | 37 ++++++++++ .../health/tck/deployment/NullNameCheck.java | 37 ++++++++++ 4 files changed, 208 insertions(+) create mode 100644 tck/src/main/java/org/eclipse/microprofile/health/tck/EmptyNameHealthCheckResponseTest.java create mode 100644 tck/src/main/java/org/eclipse/microprofile/health/tck/NullNameHealthCheckResponseTest.java create mode 100644 tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/EmptyNameCheck.java create mode 100644 tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/NullNameCheck.java diff --git a/tck/src/main/java/org/eclipse/microprofile/health/tck/EmptyNameHealthCheckResponseTest.java b/tck/src/main/java/org/eclipse/microprofile/health/tck/EmptyNameHealthCheckResponseTest.java new file mode 100644 index 0000000..774a0fb --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/health/tck/EmptyNameHealthCheckResponseTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 Contributors to the Eclipse Foundation + * + * See the NOTICES file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * + */ + +package org.eclipse.microprofile.health.tck; + +import org.eclipse.microprofile.health.tck.deployment.EmptyNameCheck; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.shrinkwrap.api.Archive; +import org.testng.Assert; +import org.testng.annotations.Test; + +import jakarta.enterprise.inject.spi.DeploymentException; +import jakarta.json.JsonArray; +import jakarta.json.JsonObject; + +/** + * @author Fabio Burzigotti + */ +public class EmptyNameHealthCheckResponseTest extends TCKBase { + + @Deployment + public static Archive getDeployment() { + return DeploymentUtils.createWarFileWithClasses(EmptyNameHealthCheckResponseTest.class.getSimpleName(), + EmptyNameCheck.class, TCKBase.class); + } + + /** + * Verifies that defining a HealthCheckResponse with an empty name will cause the runtime to throw a + * {@link jakarta.enterprise.inject.spi.DeploymentException} + */ + @Test + @RunAsClient + public void testStartupFailsWithException() { + Response response = getUrlStartedContents(); + + // status code + Assert.assertEquals(response.getStatus(), 503); + + JsonObject json = readJson(response); + + // response size + JsonArray checks = json.getJsonArray("checks"); + Assert.assertEquals(checks.size(), 1, "Expected a single check response"); + + assertOverallFailure(json); + } +} diff --git a/tck/src/main/java/org/eclipse/microprofile/health/tck/NullNameHealthCheckResponseTest.java b/tck/src/main/java/org/eclipse/microprofile/health/tck/NullNameHealthCheckResponseTest.java new file mode 100644 index 0000000..39ed214 --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/health/tck/NullNameHealthCheckResponseTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 Contributors to the Eclipse Foundation + * + * See the NOTICES file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * + */ + +package org.eclipse.microprofile.health.tck; + +import org.eclipse.microprofile.health.tck.deployment.NullNameCheck; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.shrinkwrap.api.Archive; +import org.testng.Assert; +import org.testng.annotations.Test; + +import jakarta.enterprise.inject.spi.DeploymentException; +import jakarta.json.JsonArray; +import jakarta.json.JsonObject; + +/** + * @author Fabio Burzigotti + */ +public class NullNameHealthCheckResponseTest extends TCKBase { + + @Deployment + public static Archive getDeployment() { + return DeploymentUtils.createWarFileWithClasses(NullNameHealthCheckResponseTest.class.getSimpleName(), + NullNameCheck.class, TCKBase.class); + } + + /** + * Verifies that defining a HealthCheckResponse with a null name will cause the runtime to throw a + * {@link DeploymentException} + */ + @Test + @RunAsClient + public void testStartupFailsWithException() { + Response response = getUrlStartedContents(); + + // status code + Assert.assertEquals(response.getStatus(), 503); + + JsonObject json = readJson(response); + + // response size + JsonArray checks = json.getJsonArray("checks"); + Assert.assertEquals(checks.size(), 1, "Expected a single check response"); + + assertOverallFailure(json); + } +} diff --git a/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/EmptyNameCheck.java b/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/EmptyNameCheck.java new file mode 100644 index 0000000..bc3fb3b --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/EmptyNameCheck.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 Contributors to the Eclipse Foundation + * + * See the NOTICES file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * + */ +package org.eclipse.microprofile.health.tck.deployment; + +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.Startup; + +import jakarta.enterprise.context.ApplicationScoped; + +@Startup +@ApplicationScoped +public class EmptyNameCheck implements HealthCheck { + @Override + public HealthCheckResponse call() { + return HealthCheckResponse.up(""); + } +} diff --git a/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/NullNameCheck.java b/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/NullNameCheck.java new file mode 100644 index 0000000..feed50b --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/health/tck/deployment/NullNameCheck.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 Contributors to the Eclipse Foundation + * + * See the NOTICES file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * + */ +package org.eclipse.microprofile.health.tck.deployment; + +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.Startup; + +import jakarta.enterprise.context.ApplicationScoped; + +@Startup +@ApplicationScoped +public class NullNameCheck implements HealthCheck { + @Override + public HealthCheckResponse call() { + return HealthCheckResponse.up(null); + } +}