This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 50b2aab71a4264dd1e4bc18558a0c5137abf96eb Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Fri May 9 09:38:48 2025 +0200 CAMEL-22013 - camel-api - Remove all usage of component.extension and the component.extension package content itself - HTTP Signed-off-by: Andrea Cosentino <anco...@gmail.com> --- .../apache/camel/component/http/HttpComponent.java | 7 - .../http/HttpComponentVerifierExtension.java | 268 --------------------- .../http/CamelComponentVerifierExtensionTest.java | 232 ------------------ .../component/http/CamelComponentVerifierTest.java | 247 ------------------- .../http/rest/RestCamelComponentVerifierTest.java | 163 ------------- 5 files changed, 917 deletions(-) diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java index 76862f38d26..5f98369c650 100644 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java +++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java @@ -30,7 +30,6 @@ import org.apache.camel.CamelContextAware; import org.apache.camel.Endpoint; import org.apache.camel.Producer; import org.apache.camel.SSLContextParametersAware; -import org.apache.camel.component.extension.ComponentVerifierExtension; import org.apache.camel.http.base.HttpHelper; import org.apache.camel.http.common.HttpBinding; import org.apache.camel.http.common.HttpCommonComponent; @@ -210,7 +209,6 @@ public class HttpComponent extends HttpCommonComponent implements RestProducerFa protected boolean logHttpActivity; public HttpComponent() { - registerExtension(HttpComponentVerifierExtension::new); } /** @@ -1085,9 +1083,4 @@ public class HttpComponent extends HttpCommonComponent implements RestProducerFa super.doStop(); } - - public ComponentVerifierExtension getVerifier() { - return (scope, parameters) -> getExtension(ComponentVerifierExtension.class) - .orElseThrow(UnsupportedOperationException::new).verify(scope, parameters); - } } diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponentVerifierExtension.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponentVerifierExtension.java deleted file mode 100644 index ab7e6a5ebc6..00000000000 --- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponentVerifierExtension.java +++ /dev/null @@ -1,268 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.camel.component.http; - -import java.net.UnknownHostException; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -import org.apache.camel.component.extension.ComponentVerifierExtension; -import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension; -import org.apache.camel.component.extension.verifier.ResultBuilder; -import org.apache.camel.component.extension.verifier.ResultErrorBuilder; -import org.apache.camel.http.base.HttpHelper; -import org.apache.camel.util.FileUtil; -import org.apache.camel.util.ObjectHelper; -import org.apache.hc.client5.http.classic.methods.HttpGet; -import org.apache.hc.client5.http.config.RequestConfig; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; -import org.apache.hc.core5.http.ClassicHttpResponse; -import org.apache.hc.core5.http.io.HttpClientResponseHandler; - -final class HttpComponentVerifierExtension extends DefaultComponentVerifierExtension { - - HttpComponentVerifierExtension() { - super("http"); - } - - // ********************************* - // Parameters validation - // ********************************* - - @Override - protected Result verifyParameters(Map<String, Object> parameters) { - // Default is success - final ResultBuilder builder - = ResultBuilder.withStatusAndScope(Result.Status.OK, ComponentVerifierExtension.Scope.PARAMETERS); - // Make a copy to avoid clashing with parent validation - final HashMap<String, Object> verifyParams = new HashMap<>(parameters); - // Check if validation is rest-related - restRelatedSetup(parameters, verifyParams); - - // Validate using the catalog - super.verifyParametersAgainstCatalog(builder, verifyParams); - - return builder.build(); - } - - // ********************************* - // Connectivity validation - // ********************************* - - @Override - protected Result verifyConnectivity(Map<String, Object> parameters) { - // Default is success - final ResultBuilder builder - = ResultBuilder.withStatusAndScope(Result.Status.OK, ComponentVerifierExtension.Scope.CONNECTIVITY); - // Make a copy to avoid clashing with parent validation - final HashMap<String, Object> verifyParams = new HashMap<>(parameters); - final boolean isRest = restRelatedSetup(parameters, verifyParams); - - String httpUri = getOption(verifyParams, "httpUri", String.class).orElse(null); - if (ObjectHelper.isEmpty(httpUri)) { - builder.error( - ResultErrorBuilder.withMissingOption("httpUri") - .detail("rest", isRest) - .build()); - } - - try (CloseableHttpClient httpclient = createHttpClient(verifyParams)) { - httpclient.execute( - new HttpGet(httpUri), - createObjectHttpClientResponseHandler(verifyParams, builder)); - - } catch (UnknownHostException e) { - builder.error( - ResultErrorBuilder.withException(e) - .parameterKey("httpUri") - .build()); - } catch (Exception e) { - builder.error(ResultErrorBuilder.withException(e).build()); - } - - return builder.build(); - } - - private HttpClientResponseHandler<Object> createObjectHttpClientResponseHandler( - HashMap<String, Object> verifyParams, ResultBuilder builder) { - return response -> { - int code = response.getCode(); - String okCodes = getOption(verifyParams, "okStatusCodeRange", String.class).orElse("200-299"); - - if (!HttpHelper.isStatusCodeOk(code, okCodes)) { - if (code == 401) { - setupUnauthorizedRespose(builder, response, code); - } else if (code >= 300 && code < 400) { - setupRedirect(builder, response, code); - } else if (code >= 400) { - setupOtherHttpErrors(builder, response, code); - } - } - return null; - }; - } - - private static void setupOtherHttpErrors(ResultBuilder builder, ClassicHttpResponse response, int code) { - // generic http error - builder.error( - ResultErrorBuilder.withHttpCode(code) - .description(response.getReasonPhrase()) - .build()); - } - - private static void setupRedirect(ResultBuilder builder, ClassicHttpResponse response, int code) { - // redirect - builder.error( - ResultErrorBuilder.withHttpCode(code) - .description(response.getReasonPhrase()) - .parameterKey("httpUri") - .detail(VerificationError.HttpAttribute.HTTP_REDIRECT, - () -> HttpUtil.responseHeaderValue(response, "location")) - .build()); - } - - private static void setupUnauthorizedRespose(ResultBuilder builder, ClassicHttpResponse response, int code) { - // Unauthorized, add authUsername and authPassword to the list - // of parameters in error - builder.error( - ResultErrorBuilder.withHttpCode(code) - .description(response.getReasonPhrase()) - .parameterKey("authUsername") - .parameterKey("authPassword") - .build()); - } - - private boolean restRelatedSetup(Map<String, Object> parameters, HashMap<String, Object> verifyParams) { - // Check if validation is rest-related - final boolean isRest = verifyParams.entrySet().stream().anyMatch(HttpComponentVerifierExtension::isRest); - - if (isRest) { - // Build the httpUri from rest configuration - verifyParams.put("httpUri", buildHttpUriFromRestParameters(parameters)); - - // Cleanup parameters from rest related stuffs - verifyParams.entrySet().removeIf(HttpComponentVerifierExtension::isRest); - } - return isRest; - } - - private static boolean isRest(Map.Entry<String, Object> e) { - return e.getKey().startsWith("rest."); - } - - // ********************************* - // Helpers - // ********************************* - - private String buildHttpUriFromRestParameters(Map<String, Object> parameters) { - // We are doing rest endpoint validation but as today the endpoint - // can't do any param substitution so the validation is performed - // against the http uri - String httpUri = getOption(parameters, "rest.host", String.class).orElse(null); - String path = getOption(parameters, "rest.path", String.class).map(FileUtil::stripLeadingSeparator).orElse(null); - - if (ObjectHelper.isNotEmpty(httpUri) && ObjectHelper.isNotEmpty(path)) { - httpUri = httpUri + "/" + path; - } - - return httpUri; - } - - private Optional<HttpClientConfigurer> configureAuthentication( - Map<String, Object> parameters, - HttpCredentialsHelper credentialsHelper) { - Optional<String> authUsername = getOption(parameters, "authUsername", String.class); - Optional<String> authPassword = getOption(parameters, "authPassword", String.class); - - if (authUsername.isPresent() && authPassword.isPresent()) { - Optional<String> authDomain = getOption(parameters, "authDomain", String.class); - Optional<String> authHost = getOption(parameters, "authHost", String.class); - - return Optional.of( - new BasicAuthenticationHttpClientConfigurer( - authUsername.get(), - authPassword.get(), - authDomain.orElse(null), - authHost.orElse(null), - credentialsHelper)); - } - - return Optional.empty(); - } - - private Optional<HttpClientConfigurer> configureProxy( - Map<String, Object> parameters, - HttpCredentialsHelper credentialsHelper) { - Optional<String> uri = getOption(parameters, "httpUri", String.class); - Optional<String> proxyAuthHost = getOption(parameters, "proxyAuthHost", String.class); - Optional<Integer> proxyAuthPort = getOption(parameters, "proxyAuthPort", Integer.class); - - if (proxyAuthHost.isPresent() && proxyAuthPort.isPresent()) { - Optional<String> proxyAuthScheme = getOption(parameters, "proxyAuthScheme", String.class); - Optional<String> proxyAuthUsername = getOption(parameters, "proxyAuthUsername", String.class); - Optional<String> proxyAuthPassword = getOption(parameters, "proxyAuthPassword", String.class); - Optional<String> proxyAuthDomain = getOption(parameters, "proxyAuthDomain", String.class); - Optional<String> proxyAuthNtHost = getOption(parameters, "proxyAuthNtHost", String.class); - - if (proxyAuthScheme.isEmpty()) { - proxyAuthScheme = Optional.of(HttpHelper.isSecureConnection(uri.get()) ? "https" : "http"); - } - - if (proxyAuthUsername.isPresent() && proxyAuthPassword.isPresent()) { - return Optional.of( - new ProxyHttpClientConfigurer( - proxyAuthHost.get(), - proxyAuthPort.get(), - proxyAuthScheme.get(), - proxyAuthUsername.orElse(null), - proxyAuthPassword.orElse(null), - proxyAuthDomain.orElse(null), - proxyAuthNtHost.orElse(null), - credentialsHelper)); - } else { - return Optional.of( - new ProxyHttpClientConfigurer( - proxyAuthHost.get(), - proxyAuthPort.get(), - proxyAuthScheme.get())); - } - } - - return Optional.empty(); - } - - private CloseableHttpClient createHttpClient(Map<String, Object> parameters) throws Exception { - CompositeHttpConfigurer configurer = new CompositeHttpConfigurer(); - HttpCredentialsHelper credentialsHelper = new HttpCredentialsHelper(); - configureAuthentication(parameters, credentialsHelper).ifPresent(configurer::addConfigurer); - configureProxy(parameters, credentialsHelper).ifPresent(configurer::addConfigurer); - - HttpClientBuilder builder = HttpClientBuilder.create(); - configurer.configureHttpClient(builder); - - RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); - - // Apply custom http client properties like httpClient.redirectsEnabled - setProperties(builder, "httpClient.", parameters); - setProperties(requestConfigBuilder, "httpClient.", parameters); - - return builder.setDefaultRequestConfig(requestConfigBuilder.build()) - .build(); - } -} diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/CamelComponentVerifierExtensionTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/CamelComponentVerifierExtensionTest.java deleted file mode 100644 index b85f14784c6..00000000000 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/CamelComponentVerifierExtensionTest.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.camel.component.http; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import org.apache.camel.Component; -import org.apache.camel.component.extension.ComponentVerifierExtension; -import org.apache.camel.component.http.handler.AuthenticationValidationHandler; -import org.apache.camel.component.http.handler.BasicValidationHandler; -import org.apache.camel.component.http.interceptor.RequestBasicAuth; -import org.apache.camel.component.http.interceptor.ResponseBasicUnauthorized; -import org.apache.hc.core5.http.HttpStatus; -import org.apache.hc.core5.http.impl.bootstrap.HttpServer; -import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap; -import org.apache.hc.core5.http.io.HttpRequestHandler; -import org.apache.hc.core5.http.protocol.DefaultHttpProcessor; -import org.apache.hc.core5.http.protocol.HttpProcessor; -import org.apache.hc.core5.http.protocol.RequestValidateHost; -import org.apache.hc.core5.http.protocol.ResponseContent; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.apache.camel.component.http.HttpMethods.GET; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class CamelComponentVerifierExtensionTest extends BaseHttpTest { - private static final String AUTH_USERNAME = "camel"; - private static final String AUTH_PASSWORD = "password"; - - private HttpServer localServer; - private ComponentVerifierExtension verifier; - - @Override - public void setupResources() throws Exception { - localServer = ServerBootstrap.bootstrap() - .setCanonicalHostName("localhost").setHttpProcessor(getHttpProcessor()) - .register("/basic", new BasicValidationHandler(GET.name(), null, null, getExpectedContent())) - .register("/auth", - new AuthenticationValidationHandler( - GET.name(), null, null, getExpectedContent(), AUTH_USERNAME, AUTH_PASSWORD)) - .register("/redirect", redirectTo(HttpStatus.SC_MOVED_PERMANENTLY, "/redirected")) - .register("/redirected", new BasicValidationHandler(GET.name(), null, null, getExpectedContent())) - .create(); - - localServer.start(); - } - - @Override - public void cleanupResources() throws Exception { - if (localServer != null) { - localServer.stop(); - } - } - - @BeforeEach - void setupVerifier() { - Component component = context().getComponent("http"); - verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new); - } - - @Override - public boolean isUseRouteBuilder() { - return false; - } - - private HttpProcessor getHttpProcessor() { - return new DefaultHttpProcessor( - Arrays.asList( - new RequestValidateHost(), - new RequestBasicAuth()), - Arrays.asList( - new ResponseContent(), - new ResponseBasicUnauthorized())); - } - - // ************************************************* - // Helpers - // ************************************************* - - protected String getLocalServerUri(String contextPath) { - return "http://localhost:" - + localServer.getLocalPort() - + (contextPath != null - ? contextPath.startsWith("/") ? contextPath : "/" + contextPath - : ""); - } - - private HttpRequestHandler redirectTo(int code, String path) { - return (request, response, context) -> { - response.setHeader("location", getLocalServerUri(path)); - response.setCode(code); - }; - } - - // ************************************************* - // Tests (parameters) - // ************************************************* - - @Test - public void testParameters() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/basic")); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testMissingMandatoryParameters() { - Map<String, Object> parameters = new HashMap<>(); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - - ComponentVerifierExtension.VerificationError error = result.getErrors().get(0); - - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.MISSING_PARAMETER, error.getCode()); - assertTrue(error.getParameterKeys().contains("httpUri")); - } - - // ************************************************* - // Tests (connectivity) - // ************************************************* - - @Test - public void testConnectivity() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/basic")); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testConnectivityWithWrongUri() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", "http://www.not-existing-uri.unknown"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - - ComponentVerifierExtension.VerificationError error = result.getErrors().get(0); - - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.EXCEPTION, error.getCode()); - assertTrue(error.getParameterKeys().contains("httpUri")); - } - - @Test - public void testConnectivityWithAuthentication() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/auth")); - parameters.put("authUsername", AUTH_USERNAME); - parameters.put("authPassword", AUTH_PASSWORD); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testConnectivityWithWrongAuthenticationData() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/auth")); - parameters.put("authUsername", "unknown"); - parameters.put("authPassword", AUTH_PASSWORD); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - - ComponentVerifierExtension.VerificationError error = result.getErrors().get(0); - - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.AUTHENTICATION, error.getCode()); - assertEquals(401, error.getDetails().get(ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_CODE)); - assertTrue(error.getParameterKeys().contains("authUsername")); - assertTrue(error.getParameterKeys().contains("authPassword")); - } - - @Test - public void testConnectivityWithRedirect() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/redirect")); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testConnectivityWithRedirectDisabled() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/redirect")); - parameters.put("httpClient.redirectsEnabled", "false"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - - ComponentVerifierExtension.VerificationError error = result.getErrors().get(0); - - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.GENERIC, error.getCode()); - assertEquals(getLocalServerUri("/redirected"), - error.getDetails().get(ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_REDIRECT)); - assertTrue(error.getParameterKeys().contains("httpUri")); - } -} diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/CamelComponentVerifierTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/CamelComponentVerifierTest.java deleted file mode 100644 index a8fd99af222..00000000000 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/CamelComponentVerifierTest.java +++ /dev/null @@ -1,247 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.camel.component.http; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import org.apache.camel.component.extension.ComponentVerifierExtension; -import org.apache.camel.component.http.handler.AuthenticationValidationHandler; -import org.apache.camel.component.http.handler.BasicValidationHandler; -import org.apache.camel.component.http.interceptor.RequestBasicAuth; -import org.apache.camel.component.http.interceptor.ResponseBasicUnauthorized; -import org.apache.hc.core5.http.HttpStatus; -import org.apache.hc.core5.http.impl.bootstrap.HttpServer; -import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap; -import org.apache.hc.core5.http.io.HttpRequestHandler; -import org.apache.hc.core5.http.protocol.DefaultHttpProcessor; -import org.apache.hc.core5.http.protocol.HttpProcessor; -import org.apache.hc.core5.http.protocol.RequestValidateHost; -import org.apache.hc.core5.http.protocol.ResponseContent; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.apache.camel.component.http.HttpMethods.GET; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class CamelComponentVerifierTest extends BaseHttpTest { - private static final String AUTH_USERNAME = "camel"; - private static final String AUTH_PASSWORD = "password"; - - private HttpServer localServer; - private ComponentVerifierExtension verifier; - - @Override - public void setupResources() throws Exception { - localServer = ServerBootstrap.bootstrap() - .setHttpProcessor(getHttpProcessor()).setCanonicalHostName("localhost") - .register("/basic", new BasicValidationHandler(GET.name(), null, null, getExpectedContent())) - .register("/auth", - new AuthenticationValidationHandler( - GET.name(), null, null, getExpectedContent(), AUTH_USERNAME, AUTH_PASSWORD)) - .register("/redirect", redirectTo(HttpStatus.SC_MOVED_PERMANENTLY, "/redirected")) - .register("/redirected", new BasicValidationHandler(GET.name(), null, null, getExpectedContent())) - .create(); - - localServer.start(); - } - - @BeforeEach - void setupVerifier() { - HttpComponent component = context().getComponent("http", HttpComponent.class); - verifier = component.getVerifier(); - } - - @Override - public void cleanupResources() throws Exception { - - if (localServer != null) { - localServer.stop(); - } - } - - @Override - public boolean isUseRouteBuilder() { - return false; - } - - private HttpProcessor getHttpProcessor() { - return new DefaultHttpProcessor( - Arrays.asList( - new RequestValidateHost(), - new RequestBasicAuth()), - Arrays.asList( - new ResponseContent(), - new ResponseBasicUnauthorized())); - } - - // ************************************************* - // Helpers - // ************************************************* - - protected String getLocalServerUri(String contextPath) { - return "http://localhost:" - + localServer.getLocalPort() - + (contextPath != null - ? contextPath.startsWith("/") ? contextPath : "/" + contextPath - : ""); - } - - private HttpRequestHandler redirectTo(int code, String path) { - return (request, response, context) -> { - response.setHeader("location", getLocalServerUri(path)); - response.setCode(code); - }; - } - - // ************************************************* - // Tests (parameters) - // ************************************************* - - @Test - public void testParameters() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/basic")); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testMissingMandatoryParameters() { - Map<String, Object> parameters = new HashMap<>(); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - - ComponentVerifierExtension.VerificationError error = result.getErrors().get(0); - - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.MISSING_PARAMETER, error.getCode()); - assertTrue(error.getParameterKeys().contains("httpUri")); - } - - // ************************************************* - // Tests (connectivity) - // ************************************************* - - @Test - public void testConnectivity() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/basic")); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testConnectivityWithWrongUri() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", "http://www.not-existing-uri.unknown"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - - ComponentVerifierExtension.VerificationError error = result.getErrors().get(0); - - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.EXCEPTION, error.getCode()); - assertTrue(error.getParameterKeys().contains("httpUri")); - } - - @Test - public void testConnectivityWithAuthentication() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/auth")); - parameters.put("authUsername", AUTH_USERNAME); - parameters.put("authPassword", AUTH_PASSWORD); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testConnectivityWithWrongAuthenticationData() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/auth")); - parameters.put("authUsername", "unknown"); - parameters.put("authPassword", AUTH_PASSWORD); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - - ComponentVerifierExtension.VerificationError error = result.getErrors().get(0); - - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.AUTHENTICATION, error.getCode()); - assertEquals(401, error.getDetails().get(ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_CODE)); - assertTrue(error.getParameterKeys().contains("authUsername")); - assertTrue(error.getParameterKeys().contains("authPassword")); - } - - @Test - public void testConnectivityWithRedirect() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/redirect")); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testConnectivityWithRedirectDisabled() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/redirect")); - parameters.put("httpClient.redirectsEnabled", "false"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - - ComponentVerifierExtension.VerificationError error = result.getErrors().get(0); - - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.GENERIC, error.getCode()); - assertEquals(getLocalServerUri("/redirected"), - error.getDetails().get(ComponentVerifierExtension.VerificationError.HttpAttribute.HTTP_REDIRECT)); - assertTrue(error.getParameterKeys().contains("httpUri")); - } - - @Test - public void testConnectivityWithFollowRedirectEnabled() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("httpUri", getLocalServerUri("/redirect")); - parameters.put("httpMethod", "POST"); - parameters.put("followRedirects", "true"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - assertEquals(0, result.getErrors().size()); - - } - -} diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/rest/RestCamelComponentVerifierTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/rest/RestCamelComponentVerifierTest.java deleted file mode 100644 index 1a37cf99ef0..00000000000 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/rest/RestCamelComponentVerifierTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.camel.component.http.rest; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import org.apache.camel.component.extension.ComponentVerifierExtension; -import org.apache.camel.component.http.BaseHttpTest; -import org.apache.camel.component.http.handler.BasicValidationHandler; -import org.apache.camel.component.http.interceptor.RequestBasicAuth; -import org.apache.camel.component.http.interceptor.ResponseBasicUnauthorized; -import org.apache.camel.component.rest.RestComponent; -import org.apache.hc.core5.http.impl.bootstrap.HttpServer; -import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap; -import org.apache.hc.core5.http.protocol.DefaultHttpProcessor; -import org.apache.hc.core5.http.protocol.HttpProcessor; -import org.apache.hc.core5.http.protocol.RequestValidateHost; -import org.apache.hc.core5.http.protocol.ResponseContent; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.eclipse.jetty.http.HttpMethod.GET; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class RestCamelComponentVerifierTest extends BaseHttpTest { - - private HttpServer localServer; - private Map<String, Object> parameters; - private ComponentVerifierExtension verifier; - - @Override - public void setupResources() throws Exception { - localServer = ServerBootstrap.bootstrap() - .setCanonicalHostName("localhost") - .setHttpProcessor(getHttpProcessor()) - .register("/verify", new BasicValidationHandler(GET.name(), null, null, getExpectedContent())) - .create(); - - localServer.start(); - - parameters = new HashMap<>(); - parameters.put("producerComponentName", "http"); - parameters.put("host", "http://localhost:" + localServer.getLocalPort()); - parameters.put("path", "verify"); - } - - @BeforeEach - public void setupVerifier() { - RestComponent component = context().getComponent("rest", RestComponent.class); - verifier = component.getVerifier(); - } - - @Override - public void cleanupResources() throws Exception { - - if (localServer != null) { - localServer.stop(); - } - } - - @Override - public boolean isUseRouteBuilder() { - return false; - } - - private HttpProcessor getHttpProcessor() { - return new DefaultHttpProcessor( - Arrays.asList( - new RequestValidateHost(), - new RequestBasicAuth()), - Arrays.asList( - new ResponseContent(), - new ResponseBasicUnauthorized())); - } - - // ************************************************* - // Helpers - // ************************************************* - - @SuppressWarnings("unused") - protected String getLocalServerUri(String contextPath) { - return "http://localhost:" - + localServer.getLocalPort() - + (contextPath != null - ? contextPath.startsWith("/") ? contextPath : "/" + contextPath - : ""); - } - - // ************************************************* - // Tests - // ************************************************* - @Test - public void testParameters() { - - parameters.put("method", "get"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } - - @Test - public void testMissingRestParameters() { - // This parameter does not belong to the rest component and validation - // is delegated to the transport component - parameters.put("copyHeaders", false); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.MISSING_PARAMETER, - result.getErrors().get(0).getCode()); - assertEquals(1, result.getErrors().get(0).getParameterKeys().size()); - assertTrue(result.getErrors().get(0).getParameterKeys().contains("method")); - } - - @Test - public void testWrongComponentParameters() { - - parameters.put("method", "get"); - - // This parameter does not belong to the rest component and validation - // is delegated to the transport component - parameters.put("nonExistingOption", true); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); - assertEquals(1, result.getErrors().size()); - assertEquals(ComponentVerifierExtension.VerificationError.StandardCode.UNKNOWN_PARAMETER, - result.getErrors().get(0).getCode()); - assertEquals(1, result.getErrors().get(0).getParameterKeys().size()); - assertTrue(result.getErrors().get(0).getParameterKeys().contains("nonExistingOption")); - } - - @Test - public void testConnectivity() { - - parameters.put("method", "get"); - - ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); - - assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); - } -}