Skip to content

Commit

Permalink
Fixes #6576 - test coverage for smb producer
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Oct 17, 2024
1 parent f4981d1 commit f66644c
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 1 deletion.
39 changes: 39 additions & 0 deletions integration-tests/smb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -71,12 +75,34 @@
<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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
Expand Down Expand Up @@ -115,6 +141,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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@
*/
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.hamcrest.Matchers;
import org.apache.camel.quarkus.test.DisabledIfFipsMode;
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)
Expand All @@ -33,4 +41,97 @@ public void testSmbResultsValidation() {
.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(","));

assertThat(set)
.contains("path=msg1.tx1")
.contains("content=Hello1")
.contains(SmbConstants.SMB_FILE_PATH + "=msg1.tx1")
.contains(SmbConstants.SMB_UNC_PATH + "=\\\\localhost\\data-rw\\msg1.tx1");
});

}

}

0 comments on commit f66644c

Please sign in to comment.