This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch camel-3.11.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.11.x by this push: new 4df7bf5 CAMEL-16764: Fix resolution of shared BoxAPIConnection 4df7bf5 is described below commit 4df7bf59ffd10e7608e2aedd270fc28cb73bd857 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Tue Jun 29 11:23:21 2021 +0100 CAMEL-16764: Fix resolution of shared BoxAPIConnection --- components/camel-box/camel-box-component/pom.xml | 20 ++++ .../camel/component/box/BoxConfiguration.java | 46 ++++++++ .../component/box/BoxSharedConnectionTest.java | 120 +++++++++++++++++++++ 3 files changed, 186 insertions(+) diff --git a/components/camel-box/camel-box-component/pom.xml b/components/camel-box/camel-box-component/pom.xml index 13219e1..4cf0a7a 100644 --- a/components/camel-box/camel-box-component/pom.xml +++ b/components/camel-box/camel-box-component/pom.xml @@ -87,6 +87,17 @@ <artifactId>camel-test-junit5</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-junit-jupiter</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-inline</artifactId> + <version>${mockito-version}</version> + <scope>test</scope> + </dependency> </dependencies> <build> @@ -105,6 +116,15 @@ </plugin> <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <!-- Avoid 'bootstrap classpath has been appended' error message caused by mockito-inline --> + <argLine>-Xshare:off</argLine> + </configuration> + </plugin> + + <plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-api-component-maven-plugin</artifactId> <executions> diff --git a/components/camel-box/camel-box-component/src/main/java/org/apache/camel/component/box/BoxConfiguration.java b/components/camel-box/camel-box-component/src/main/java/org/apache/camel/component/box/BoxConfiguration.java index 376cf90..2f980da 100644 --- a/components/camel-box/camel-box-component/src/main/java/org/apache/camel/component/box/BoxConfiguration.java +++ b/components/camel-box/camel-box-component/src/main/java/org/apache/camel/component/box/BoxConfiguration.java @@ -17,6 +17,7 @@ package org.apache.camel.component.box; import java.util.Map; +import java.util.Objects; import com.box.sdk.EncryptionAlgorithm; import com.box.sdk.IAccessTokenCache; @@ -459,4 +460,49 @@ public class BoxConfiguration { public void setAccessTokenCache(IAccessTokenCache accessTokenCache) { this.accessTokenCache = accessTokenCache; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + + BoxConfiguration that = (BoxConfiguration) o; + + return maxCacheEntries == that.maxCacheEntries + && Objects.equals(enterpriseId, that.enterpriseId) + && Objects.equals(userId, that.userId) + && Objects.equals(clientId, that.clientId) + && Objects.equals(publicKeyId, that.publicKeyId) + && Objects.equals(privateKeyFile, that.privateKeyFile) + && Objects.equals(privateKeyPassword, that.privateKeyPassword) + && Objects.equals(clientSecret, that.clientSecret) + && Objects.equals(userName, that.userName) + && Objects.equals(userPassword, that.userPassword) + && Objects.equals(accessTokenCache, that.accessTokenCache) + && encryptionAlgorithm == that.encryptionAlgorithm + && Objects.equals(authenticationType, that.authenticationType) + && Objects.equals(httpParams, that.httpParams) + && Objects.equals(sslContextParameters, that.sslContextParameters); + } + + @Override + public int hashCode() { + return Objects.hash( + enterpriseId, + userId, + clientId, + publicKeyId, + privateKeyFile, + privateKeyPassword, + clientSecret, + userName, + userPassword, + accessTokenCache, + maxCacheEntries, + encryptionAlgorithm, + authenticationType, + httpParams, + sslContextParameters); + } } diff --git a/components/camel-box/camel-box-component/src/test/java/org/apache/camel/component/box/BoxSharedConnectionTest.java b/components/camel-box/camel-box-component/src/test/java/org/apache/camel/component/box/BoxSharedConnectionTest.java new file mode 100644 index 0000000..a6b036c --- /dev/null +++ b/components/camel-box/camel-box-component/src/test/java/org/apache/camel/component/box/BoxSharedConnectionTest.java @@ -0,0 +1,120 @@ +/* + * 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.box; + +import com.box.sdk.BoxAPIConnection; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.box.internal.BoxApiCollection; +import org.apache.camel.component.box.internal.BoxConnectionHelper; +import org.apache.camel.component.box.internal.BoxFilesManagerApiMethod; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +public class BoxSharedConnectionTest { + + private static final String PATH_PREFIX + = BoxApiCollection.getCollection().getApiName(BoxFilesManagerApiMethod.class).getName(); + + @Test + public void testEndpointUsesSharedConnection() throws Exception { + final String boxUri = "box:" + PATH_PREFIX + "/getFileInfo"; + + BoxConfiguration configuration = new BoxConfiguration(); + configuration.setUserName("ca...@apache.org"); + configuration.setUserPassword("p4ssw0rd"); + configuration.setClientId("camel-client-id"); + configuration.setClientSecret("camel-client-secret"); + configuration.setAuthenticationType("STANDARD_AUTHENTICATION"); + + BoxComponent component = new BoxComponent(); + component.setConfiguration(configuration); + + CamelContext camelContext = new DefaultCamelContext(); + camelContext.addComponent("box", component); + camelContext.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to(boxUri); + } + }); + + BoxAPIConnection connection = Mockito.mock(BoxAPIConnection.class); + + try (MockedStatic<BoxConnectionHelper> helper = Mockito.mockStatic(BoxConnectionHelper.class)) { + helper.when(() -> BoxConnectionHelper.createConnection(configuration)).thenReturn(connection); + + camelContext.start(); + try { + BoxEndpoint endpoint = camelContext.getEndpoint(boxUri, BoxEndpoint.class); + + helper.verify(() -> BoxConnectionHelper.createConnection(configuration), Mockito.times(1)); + + Assertions.assertSame(component.getBoxConnection(), endpoint.getBoxConnection()); + } finally { + camelContext.stop(); + } + } + } + + @Test + public void testEndpointOverridesSharedConnection() throws Exception { + String boxUri = "box:" + PATH_PREFIX + "/getFileInfo?userPassword=0th3rP4ssw0rd"; + + BoxConfiguration configuration = new BoxConfiguration(); + configuration.setUserName("ca...@apache.org"); + configuration.setUserPassword("p4ssw0rd"); + configuration.setClientId("camel-client-id"); + configuration.setClientSecret("camel-client-secret"); + configuration.setAuthenticationType("STANDARD_AUTHENTICATION"); + + BoxComponent component = new BoxComponent(); + component.setConfiguration(configuration); + + CamelContext camelContext = new DefaultCamelContext(); + camelContext.addComponent("box", component); + camelContext.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:start").to(boxUri); + } + }); + + BoxAPIConnection componentConnection = Mockito.mock(BoxAPIConnection.class); + BoxAPIConnection endpointConnection = Mockito.mock(BoxAPIConnection.class); + + try (MockedStatic<BoxConnectionHelper> helper = Mockito.mockStatic(BoxConnectionHelper.class)) { + helper.when(() -> BoxConnectionHelper.createConnection(Mockito.isA(BoxConfiguration.class))) + .thenReturn(componentConnection, endpointConnection); + + camelContext.start(); + try { + BoxEndpoint endpoint = camelContext.getEndpoint(boxUri, BoxEndpoint.class); + + helper.verify(() -> BoxConnectionHelper.createConnection(Mockito.any()), Mockito.times(2)); + + Assertions.assertSame(componentConnection, component.getBoxConnection()); + Assertions.assertSame(endpointConnection, endpoint.getBoxConnection()); + } finally { + camelContext.stop(); + } + } + } +}