Skip to content

Commit

Permalink
Add test coverage for FTPS
Browse files Browse the repository at this point in the history
Fixes #2317
  • Loading branch information
jamesnetherton authored and aldettinger committed Mar 22, 2021
1 parent 397d6dc commit 98e28d3
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.quarkus.component.ftps.it;

import java.net.URI;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;

@Path("/ftps")
@ApplicationScoped
public class FtpsResource {

@Inject
ProducerTemplate producerTemplate;

@Inject
ConsumerTemplate consumerTemplate;

@Path("/get/{fileName}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getFile(@PathParam("fileName") String fileName) throws Exception {
return consumerTemplate.receiveBodyNoWait(
"ftps://admin@localhost:{{camel.ftps.test-port}}/ftp?password=admin&fileName=" + fileName,
String.class);
}

@Path("/create/{fileName}")
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response createFile(@PathParam("fileName") String fileName, String fileContent)
throws Exception {
producerTemplate.sendBodyAndHeader("ftps://admin@localhost:{{camel.ftps.test-port}}/ftp?password=admin", fileContent,
Exchange.FILE_NAME, fileName);
return Response
.created(new URI("https://camel.apache.org/"))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@
public class FtpTestResource implements QuarkusTestResourceLifecycleManager {
private static final Logger LOGGER = Logger.getLogger(FtpTestResource.class);

private final String componentName;
private FtpServer ftpServer;
private Path ftpRoot;
private Path usrFile;

public FtpTestResource() {
this("ftp");
}

public FtpTestResource(String componentName) {
this.componentName = componentName;
}

@Override
public Map<String, String> start() {
try {
Expand Down Expand Up @@ -78,8 +87,7 @@ public Map<String, String> start() {
user.setAuthorities(authorities);
userMgr.save(user);

ListenerFactory factory = new ListenerFactory();
factory.setPort(port);
ListenerFactory factory = createListenerFactory(port);

FtpServerFactory serverFactory = new FtpServerFactory();
serverFactory.setUserManager(userMgr);
Expand All @@ -92,9 +100,9 @@ public Map<String, String> start() {
ftpServer.start();

return CollectionHelper.mapOf(
"camel.ftp.test-port", Integer.toString(port),
"camel.ftp.test-root-dir", ftpRoot.toString(),
"camel.ftp.test-user-file", usrFile.toString());
"camel." + componentName + ".test-port", Integer.toString(port),
"camel." + componentName + ".test-root-dir", ftpRoot.toString(),
"camel." + componentName + ".test-user-file", usrFile.toString());
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -129,4 +137,10 @@ public void stop() {
LOGGER.warn("Failed delete usr file: {}, {}", usrFile, e);
}
}

protected ListenerFactory createListenerFactory(int port) {
ListenerFactory factory = new ListenerFactory();
factory.setPort(port);
return factory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.quarkus.component.ftps.it;

import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
class FtpsIT extends FtpsTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.quarkus.component.ftps.it;

import java.security.NoSuchAlgorithmException;

import io.quarkus.test.common.QuarkusTestResource;
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(FtpsTestResource.class)
class FtpsTest {
@Test
public void testFtpsComponent() throws InterruptedException, NoSuchAlgorithmException {
// Create a new file on the FTPS server
RestAssured.given()
.contentType(ContentType.TEXT)
.body("Hello Camel Quarkus FTPS")
.post("/ftps/create/hello.txt")
.then()
.statusCode(201);

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.quarkus.component.ftps.it;

import java.io.File;

import org.apache.camel.quarkus.component.ftp.it.FtpTestResource;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfigurationFactory;

public class FtpsTestResource extends FtpTestResource {

public FtpsTestResource() {
super("ftps");
}

@Override
protected ListenerFactory createListenerFactory(int port) {
SslConfigurationFactory sslConfigFactory = new SslConfigurationFactory();
sslConfigFactory.setKeystoreFile(new File("server.jks"));
sslConfigFactory.setKeystoreType("PKCS12");
sslConfigFactory.setKeystorePassword("password");
sslConfigFactory.setKeyPassword("password");

ListenerFactory factory = super.createListenerFactory(port);
factory.setSslConfiguration(sslConfigFactory.createSslConfiguration());
return factory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
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 {
/*
* Disabled due to SSL native integration failing in the Jenkins CI environment.
* https://github.com/apache/camel-quarkus/issues/468"
*/

@Test
@DisabledIfEnvironmentVariable(named = "JENKINS_ASF_CI", matches = "true")
public void testSftpComponent() throws InterruptedException {
// Create a new file on the SFTP server
RestAssured.given()
Expand Down
Binary file not shown.

0 comments on commit 98e28d3

Please sign in to comment.