This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit 050e313ffce8199599d3615f0515791bfa08b6ba Author: JiriOndrusek <ondrusek.j...@gmail.com> AuthorDate: Wed Oct 2 11:56:38 2024 +0200 Fixes #6576 - test coverage for smb producer --- integration-tests/smb/pom.xml | 27 ++++++ .../quarkus/component/smb/it/SmbResource.java | 74 +++++++++++++++ .../camel/quarkus/component/smb/it/SmbRoute.java | 58 +++++++++++- .../camel/quarkus/component/smb/it/SmbTest.java | 104 +++++++++++++++++++++ 4 files changed, 262 insertions(+), 1 deletion(-) diff --git a/integration-tests/smb/pom.xml b/integration-tests/smb/pom.xml index 4d268fc079..06db578c2e 100644 --- a/integration-tests/smb/pom.xml +++ b/integration-tests/smb/pom.xml @@ -39,6 +39,10 @@ <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-direct</artifactId> + </dependency> <dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-mock</artifactId> @@ -71,6 +75,16 @@ <artifactId>quarkus-junit4-mock</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.awaitility</groupId> + <artifactId>awaitility</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-integration-test-support</artifactId> @@ -115,6 +129,19 @@ </activation> <dependencies> <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory --> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-direct-deployment</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>*</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> <dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-mock-deployment</artifactId> diff --git a/integration-tests/smb/src/main/java/org/apache/camel/quarkus/component/smb/it/SmbResource.java b/integration-tests/smb/src/main/java/org/apache/camel/quarkus/component/smb/it/SmbResource.java index a4a01cdaaf..241534698c 100644 --- a/integration-tests/smb/src/main/java/org/apache/camel/quarkus/component/smb/it/SmbResource.java +++ b/integration-tests/smb/src/main/java/org/apache/camel/quarkus/component/smb/it/SmbResource.java @@ -16,11 +16,26 @@ */ package org.apache.camel.quarkus.component.smb.it; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.hierynomus.smbj.share.File; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.inject.Named; import jakarta.ws.rs.GET; +import jakarta.ws.rs.POST; import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.QueryParam; +import org.apache.camel.ConsumerTemplate; import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.ProducerTemplate; import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.smb.SmbConstants; +import org.eclipse.microprofile.config.inject.ConfigProperty; @Path("/smb") @ApplicationScoped @@ -29,6 +44,65 @@ public class SmbResource { @EndpointInject("mock:result") private MockEndpoint mock; + @ConfigProperty(name = "smb.host") + String host; + + @ConfigProperty(name = "smb.port") + String port; + + @ConfigProperty(name = "smb.username") + String username; + + @ConfigProperty(name = "smb.password") + String password; + + @ConfigProperty(name = "smb.share") + String share; + + @Inject + ConsumerTemplate consumer; + + @Inject + ProducerTemplate producer; + + @Inject + @Named("smbReceivedMsgs") + List<Map<String, String>> receivedContents; + + @POST + @Path("/send/{fileName}") + public void send(String content, + @PathParam("fileName") String fileName, + @QueryParam("fileExist") String fileExist) throws Exception { + if (fileExist == null || fileExist.isEmpty()) { + producer.sendBodyAndHeader("direct:send", content, Exchange.FILE_NAME, fileName); + } else { + producer.sendBodyAndHeaders("direct:send", content, Map.of(SmbConstants.SMB_FILE_EXISTS, fileExist, + Exchange.FILE_NAME, fileName)); + } + } + + @POST + @Path("/receive") + public String receive(String fileName) throws Exception { + + String uri = String.format("smb:%s:%s/%s?username=%s&password=%s&searchPattern=%s&path=/", host, port, share, + username, password, fileName); + var shareFile = consumer.receiveBody(uri, File.class); + return new String(shareFile.getInputStream().readAllBytes(), "UTF-8"); + } + + @POST + @Path("/receivedMsgs") + public String receivedMsgs() throws Exception { + + return receivedContents.stream().map(m -> m.entrySet() + .stream() + .map(entry -> entry.getKey() + "=" + entry.getValue()) // Format each entry as "key=value" + .collect(Collectors.joining(","))) + .collect(Collectors.joining(";")); + } + @GET @Path("/validate") public void validateSmbResults() throws Exception { diff --git a/integration-tests/smb/src/main/java/org/apache/camel/quarkus/component/smb/it/SmbRoute.java b/integration-tests/smb/src/main/java/org/apache/camel/quarkus/component/smb/it/SmbRoute.java index 251c778daa..a101b0b809 100644 --- a/integration-tests/smb/src/main/java/org/apache/camel/quarkus/component/smb/it/SmbRoute.java +++ b/integration-tests/smb/src/main/java/org/apache/camel/quarkus/component/smb/it/SmbRoute.java @@ -16,13 +16,69 @@ */ package org.apache.camel.quarkus.component.smb.it; +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; + +import com.hierynomus.smbj.share.File; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Produces; +import jakarta.inject.Inject; +import jakarta.inject.Named; +import jakarta.inject.Singleton; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.smb.SmbConstants; +import org.eclipse.microprofile.config.inject.ConfigProperty; +@ApplicationScoped public class SmbRoute extends RouteBuilder { + @ConfigProperty(name = "smb.host") + String host; + + @ConfigProperty(name = "smb.port") + String port; + + @ConfigProperty(name = "smb.username") + String username; + + @ConfigProperty(name = "smb.password") + String password; + + @ConfigProperty(name = "smb.share") + String share; + + @Inject + @Named("smbReceivedMsgs") + List<Map<String, String>> receivedContents; + @Override public void configure() throws Exception { - from("smb:{{smb.host}}:{{smb.port}}/{{smb.share}}?username={{smb.username}}&password={{smb.password}}&path=/&repeatCount=1") + from("smb:{{smb.host}}:{{smb.port}}/{{smb.share}}?username={{smb.username}}&password={{smb.password}}&path=/&repeatCount=1&searchPattern=*.txt") .to("mock:result"); + + from("direct:send") + .toF("smb:%s:%s/%s?username=%s&password=%s&path=/", host, port, share, username, password); + + from("smb:{{smb.host}}:{{smb.port}}/{{smb.share}}?username={{smb.username}}&password={{smb.password}}&path=/&searchPattern=*.tx1") + .process(e -> { + receivedContents.add(Map.of( + "path", e.getIn().getBody(File.class).getPath(), + "content", new String(e.getIn().getBody(InputStream.class).readAllBytes(), "UTF-8"), + SmbConstants.SMB_FILE_PATH, e.getIn().getHeader(SmbConstants.SMB_FILE_PATH, String.class), + SmbConstants.SMB_UNC_PATH, e.getIn().getHeader(SmbConstants.SMB_UNC_PATH, String.class))); + }); + } + + static class Producers { + + @Singleton + @Produces + @Named("smbReceivedMsgs") + List<Map<String, String>> smbReceivedMsgs() { + return new CopyOnWriteArrayList<>(); + } } + } diff --git a/integration-tests/smb/src/test/java/org/apache/camel/quarkus/component/smb/it/SmbTest.java b/integration-tests/smb/src/test/java/org/apache/camel/quarkus/component/smb/it/SmbTest.java index 2d6d1bf0bc..46dcad6604 100644 --- a/integration-tests/smb/src/test/java/org/apache/camel/quarkus/component/smb/it/SmbTest.java +++ b/integration-tests/smb/src/test/java/org/apache/camel/quarkus/component/smb/it/SmbTest.java @@ -16,11 +16,20 @@ */ package org.apache.camel.quarkus.component.smb.it; +import java.util.Set; +import java.util.concurrent.TimeUnit; + import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; +import org.apache.camel.component.smb.SmbConstants; import org.apache.camel.quarkus.test.DisabledIfFipsMode; +import org.eclipse.microprofile.config.ConfigProvider; +import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.testcontainers.shaded.org.awaitility.Awaitility; + +import static org.assertj.core.api.Assertions.assertThat; @QuarkusTest @QuarkusTestResource(SmbTestResource.class) @@ -33,4 +42,99 @@ public class SmbTest { .then() .statusCode(204); } + + @Test + public void testSendReceive() { + + RestAssured.given() + .body("Hello") + .post("/smb/send/test.doc") + .then() + .statusCode(204); + + RestAssured.given() + .body("test.doc") + .post("/smb/receive") + .then() + .statusCode(200) + .body(Matchers.equalTo("Hello")); + } + + @Test + public void testFileExistsOverride() { + + RestAssured.given() + .body("Hello1") + .post("/smb/send/testOverride.doc") + .then() + .statusCode(204); + + RestAssured.given() + .body("Hello2") + .queryParam("fileExist", "Override") + .post("/smb/send/testOverride.doc") + .then() + .statusCode(204); + + RestAssured.given() + .body("testOverride.doc") + .post("/smb/receive") + .then() + .statusCode(200) + .body(Matchers.equalTo("Hello2")); + } + + @Test + public void testFileExistsAppend() { + + RestAssured.given() + .body("Hello1") + .post("/smb/send/testAppend.doc") + .then() + .statusCode(204); + + RestAssured.given() + .body("Hello2") + .queryParam("fileExist", "Append") + .post("/smb/send/testAppend.doc") + .then() + .statusCode(204); + + RestAssured.given() + .body("testAppend.doc") + .post("/smb/receive") + .then() + .statusCode(200) + .body(Matchers.equalTo("Hello1Hello2")); + } + + @Test + public void testHeadersAndAutoConvertToInputStream() { + + RestAssured.given() + .body("Hello1") + .post("/smb/send/msg1.tx1") + .then() + .statusCode(204); + + Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { + String body = RestAssured.given() + .post("/smb/receivedMsgs") + .then() + .statusCode(200) + .extract().asString(); + + Set<String> set = Set.of(body.split(",")); + + String host = ConfigProvider.getConfig().getValue("smb.host", String.class); + + assertThat(set) + .contains("path=msg1.tx1") + .contains("content=Hello1") + .contains(SmbConstants.SMB_FILE_PATH + "=msg1.tx1") + .contains(SmbConstants.SMB_UNC_PATH + "=\\\\%s\\data-rw\\msg1.tx1".formatted(host)); + }); + + } + }