From b9f9e8a56c02d49d82e54c22642149be07043625 Mon Sep 17 00:00:00 2001 From: lkonno Date: Tue, 21 Nov 2023 09:35:42 -0300 Subject: [PATCH 1/2] overwrite the IllegalArgumentException http code --- .../util/IllegalArgumentExceptionMapper.java | 28 +++++++++++++++++++ src/test/java/itest/RulesPostJsonIT.java | 12 ++------ 2 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/main/java/io/cryostat/util/IllegalArgumentExceptionMapper.java diff --git a/src/main/java/io/cryostat/util/IllegalArgumentExceptionMapper.java b/src/main/java/io/cryostat/util/IllegalArgumentExceptionMapper.java new file mode 100644 index 000000000..6d5a619e9 --- /dev/null +++ b/src/main/java/io/cryostat/util/IllegalArgumentExceptionMapper.java @@ -0,0 +1,28 @@ +/* + * Copyright The Cryostat Authors. + * + * 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. + */ +package io.cryostat.util; + +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.ext.ExceptionMapper; +import jakarta.ws.rs.ext.Provider; + +@Provider +public class IllegalArgumentExceptionMapper implements ExceptionMapper { + @Override + public Response toResponse(IllegalArgumentException exception) { + return Response.status(400).build(); + } +} diff --git a/src/test/java/itest/RulesPostJsonIT.java b/src/test/java/itest/RulesPostJsonIT.java index 003777bda..838ff1f6b 100644 --- a/src/test/java/itest/RulesPostJsonIT.java +++ b/src/test/java/itest/RulesPostJsonIT.java @@ -37,7 +37,6 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; -import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; @QuarkusIntegrationTest @TestMethodOrder(OrderAnnotation.class) @@ -107,12 +106,6 @@ void testAddRuleThrowsWhenMimeUnsupported() throws Exception { ex.getCause().getMessage(), Matchers.equalTo("Unsupported Media Type")); } - @DisabledIfEnvironmentVariable( - named = "CI", - matches = "true", - disabledReason = - "The server 500 seems to cause issues for the next test in the suite, ex. HTTP" - + " connection closed when attempting to POST the next rule definition") @Test @Order(3) void testAddRuleThrowsWhenMimeInvalid() throws Exception { @@ -131,9 +124,8 @@ void testAddRuleThrowsWhenMimeInvalid() throws Exception { Assertions.assertThrows( ExecutionException.class, () -> response.get(10, TimeUnit.SECONDS)); MatcherAssert.assertThat( - ((HttpException) ex.getCause()).getStatusCode(), Matchers.equalTo(500)); - MatcherAssert.assertThat( - ex.getCause().getMessage(), Matchers.equalTo("Internal Server Error")); + ((HttpException) ex.getCause()).getStatusCode(), Matchers.equalTo(400)); + MatcherAssert.assertThat(ex.getCause().getMessage(), Matchers.equalTo("Bad Request")); } @Test From fec7b3ee65b6008bb555137a5f25524b75c2b6bb Mon Sep 17 00:00:00 2001 From: lkonno Date: Tue, 21 Nov 2023 12:28:30 -0300 Subject: [PATCH 2/2] moved the map to ExceptionMappers class --- .../java/io/cryostat/ExceptionMappers.java | 5 ++++ .../util/IllegalArgumentExceptionMapper.java | 28 ------------------- 2 files changed, 5 insertions(+), 28 deletions(-) delete mode 100644 src/main/java/io/cryostat/util/IllegalArgumentExceptionMapper.java diff --git a/src/main/java/io/cryostat/ExceptionMappers.java b/src/main/java/io/cryostat/ExceptionMappers.java index a1895beb1..f03dc89fd 100644 --- a/src/main/java/io/cryostat/ExceptionMappers.java +++ b/src/main/java/io/cryostat/ExceptionMappers.java @@ -48,4 +48,9 @@ public RestResponse mapScriptException(ScriptException ex) { public RestResponse mapNoSuchKeyException(NoSuchKeyException ex) { return RestResponse.status(HttpResponseStatus.NOT_FOUND.code()); } + + @ServerExceptionMapper + public RestResponse mapIllegalArgumentException(IllegalArgumentException exception) { + return RestResponse.status(HttpResponseStatus.BAD_REQUEST.code()); + } } diff --git a/src/main/java/io/cryostat/util/IllegalArgumentExceptionMapper.java b/src/main/java/io/cryostat/util/IllegalArgumentExceptionMapper.java deleted file mode 100644 index 6d5a619e9..000000000 --- a/src/main/java/io/cryostat/util/IllegalArgumentExceptionMapper.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright The Cryostat Authors. - * - * 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. - */ -package io.cryostat.util; - -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.ext.ExceptionMapper; -import jakarta.ws.rs.ext.Provider; - -@Provider -public class IllegalArgumentExceptionMapper implements ExceptionMapper { - @Override - public Response toResponse(IllegalArgumentException exception) { - return Response.status(400).build(); - } -}