Skip to content

Commit

Permalink
Reproduce apache#2318
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Mar 5, 2021
1 parent f2d852e commit 6db3809
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 11 deletions.
13 changes: 13 additions & 0 deletions integration-tests/ftp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-azure-storage-blob</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-seda</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand All @@ -54,6 +62,11 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

<!-- test dependencies - camel-quarkus -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.apache.camel.quarkus.component.sftp.it;

import java.util.Iterator;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;

import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.common.StorageSharedKeyCredential;
import org.apache.camel.LoggingLevel;
import org.apache.camel.Message;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.azure.storage.blob.BlobConstants;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class SftpAzureRoutes extends RouteBuilder {

@ConfigProperty(name = "azure.storage.account-name")
String azureAccountName;
@ConfigProperty(name = "azure.storage.account-key")
String azureAccessKey;
@ConfigProperty(name = "azure.storage.container-name")
String azureContainer;

@Produces
@Named("azureBlobServiceClient")
BlobServiceClient azureBlobServiceClient() {
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(azureAccountName, azureAccessKey);
String uri = String.format("https://%s.blob.core.windows.net", azureAccountName);
return new BlobServiceClientBuilder()
.endpoint(uri)
.credential(credential)
.buildClient();
}

@Override
public void configure() throws Exception {

from("sftp:admin@localhost:{{camel.sftp.test-port}}/sftp?password=admin&localWorkDirectory=target")
.routeId("sftpToAzure")
.errorHandler(deadLetterChannel("seda:error"))
.convertBodyTo(byte[].class)
.log(LoggingLevel.INFO, "File ${file:name} (${file:size} bytes) is fetched from SFTP.")
.process(exchange -> {
Message message = exchange.getIn();

Map<String, Object> headers = message.getHeaders();
Iterator<Map.Entry<String, Object>> iterator = headers.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> pair = iterator.next();
if (pair.getKey().equals("CamelFileNameOnly")) {
message.setHeader(BlobConstants.BLOB_NAME, pair.getValue().toString());
break;
}
}
})
.to(String.format(
"azure-storage-blob://%s/%s?blobName=blob&operation=uploadBlockBlob&serviceClient=#azureBlobServiceClient",
azureAccountName,
azureContainer))
.log(LoggingLevel.INFO, "Uploaded to Azure.");

from("seda:error")
.routeId("mailError")
.log(LoggingLevel.ERROR, exceptionMessage().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.quarkus.component.sftp.it;

import java.net.URI;
import java.nio.charset.StandardCharsets;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -32,6 +33,7 @@
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/sftp")
@ApplicationScoped
Expand All @@ -43,6 +45,13 @@ public class SftpResource {
@Inject
ConsumerTemplate consumerTemplate;

@ConfigProperty(name = "azure.storage.account-name")
String azureStorageAccountName;
@ConfigProperty(name = "azure.storage.account-key")
String azureAccessKey;
@ConfigProperty(name = "azure.storage.container-name")
String azureBlobContainerName;

@Path("/get/{fileName}")
@GET
@Produces(MediaType.TEXT_PLAIN)
Expand All @@ -64,4 +73,17 @@ public Response createFile(@PathParam("fileName") String fileName, String fileCo
.created(new URI("https://camel.apache.org/"))
.build();
}

@Path("/azure-blob/read/{blobName}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String readBlob(@PathParam("blobName") String blobName) throws Exception {
return producerTemplate.requestBodyAndHeader(
String.format(
"azure-storage-blob://%s/%s?blobServiceClient=#azureBlobServiceClient&operation=getBlob&blobName=%s",
azureStorageAccountName, azureBlobContainerName,
blobName),
null, Exchange.CHARSET_NAME, StandardCharsets.UTF_8.name(), String.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
azure.storage.account-name = ${AZURE_STORAGE_ACCOUNT_NAME}
azure.storage.account-key = ${AZURE_STORAGE_ACCOUNT_KEY}
azure.storage.container-name = ${AZURE_BLOB_CONTAINER_NAME}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;

@QuarkusTest
@QuarkusTestResource(FtpTestResource.class)
class FtpTest {
@Test
//@Test
public void testFtpComponent() throws InterruptedException {
// Create a new file on the FTP server
RestAssured.given()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
*/
package org.apache.camel.quarkus.component.sftp.it;

import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;

import static org.hamcrest.core.Is.is;

@QuarkusTest
@QuarkusTestResource(SftpTestResource.class)
class SftpTest {
Expand All @@ -36,18 +41,29 @@ class SftpTest {
@DisabledIfEnvironmentVariable(named = "JENKINS_ASF_CI", matches = "true")
public void testSftpComponent() throws InterruptedException {
// Create a new file on the SFTP server
final String fileName = "from-sftp-" + UUID.randomUUID().toString().toLowerCase(Locale.ROOT) + ".txt";
final String blobContent = "Hello " + fileName;
RestAssured.given()
.contentType(ContentType.TEXT)
.body("Hello Camel Quarkus SFTP")
.post("/sftp/create/hello.txt")
.body(blobContent)
.post("/sftp/create/" + fileName)
.then()
.statusCode(201);

// Read file back from the SFTP server
RestAssured.get("/sftp/get/hello.txt")
.then()
.statusCode(200)
.body(is("Hello Camel Quarkus SFTP"));
// // Read file back from the SFTP server
// RestAssured.get("/sftp/get/hello.txt")
// .then()
// .statusCode(200)
// .body(is("Hello Camel Quarkus SFTP"));

Awaitility.await().pollInterval(1, TimeUnit.SECONDS).atMost(120, TimeUnit.SECONDS).until(() -> {
ExtractableResponse<Response> response = RestAssured.get("/sftp/azure-blob/read/" + fileName)
.then()
.extract();
System.out.println("=== got status " + response.statusCode());
return response.statusCode() == 200 && blobContent.equals(response.body().asString());
});

}

}

0 comments on commit 6db3809

Please sign in to comment.