Merge remote-tracking branch 'origin/master'

This commit is contained in:
Grahame Grieve 2023-01-11 12:53:19 +11:00
commit cbac920c3e
6 changed files with 231 additions and 15 deletions

View File

@ -80,9 +80,8 @@ public class AuditEvent40_50Test {
org.hl7.fhir.r5.model.Resource r5_streamed = (org.hl7.fhir.r5.model.AuditEvent) new org.hl7.fhir.r5.formats.XmlParser().parse(new ByteArrayInputStream(stream.toByteArray()));
System.out.println(((org.hl7.fhir.r5.model.AuditEvent)r5_conv).getEntity().get(0).getQueryElement().getValueAsString());
//FIXME we should not be even getting this far.
assertArrayEquals(((org.hl7.fhir.r5.model.AuditEvent)r5_conv).getEntity().get(0).getQuery(), INVALID_BASE_64_BINARY_BYTE_ARRAY);
assertArrayEquals(((org.hl7.fhir.r5.model.AuditEvent)r5_streamed).getEntity().get(0).getQuery(), INVALID_BASE_64_BINARY_BYTE_ARRAY);

View File

@ -9,10 +9,12 @@ import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.text.StringEscapeUtils;
import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@ -22,8 +24,8 @@ public class StructureDefinition40_50Test {
@Test
@DisplayName("Test r5 -> r4 AuditEvent conversion.")
public void testR5_R4() throws IOException {
byte[] r4_input = TextFile.streamToBytes(this.getClass().getResourceAsStream("/sd-xpath-constraint-r4.xml"));
byte[] r5_input = TextFile.streamToBytes(this.getClass().getResourceAsStream("/sd-xpath-constraint-r5.xml"));
byte[] r4_input = getLineSeparatorNormalizedBytes("/sd-xpath-constraint-r4.xml");
byte[] r5_input = getLineSeparatorNormalizedBytes("/sd-xpath-constraint-r5.xml");
org.hl7.fhir.r5.model.StructureDefinition r5_actual = (org.hl7.fhir.r5.model.StructureDefinition) new org.hl7.fhir.r5.formats.XmlParser().parse(r5_input);
org.hl7.fhir.r4.model.StructureDefinition r4_conv = (org.hl7.fhir.r4.model.StructureDefinition) VersionConvertorFactory_40_50.convertResource(r5_actual);
@ -36,12 +38,17 @@ public class StructureDefinition40_50Test {
assertArrayEquals(r4_input, r4_output);
}
@Nullable
private byte[] getLineSeparatorNormalizedBytes(String fileName) throws IOException {
return new String(TextFile.streamToBytes(this.getClass().getResourceAsStream(fileName))).replace(System.lineSeparator(), "\n").getBytes();
}
@Test
@DisplayName("Test r4 -> r5 AuditEvent conversion.")
public void testR4_R5() throws IOException {
byte[] r4_input = TextFile.streamToBytes(this.getClass().getResourceAsStream("/sd-xpath-constraint-r4.xml"));
byte[] r5_input = TextFile.streamToBytes(this.getClass().getResourceAsStream("/sd-xpath-constraint-r5.xml"));
byte[] r4_input = getLineSeparatorNormalizedBytes("/sd-xpath-constraint-r4.xml");
byte[] r5_input = getLineSeparatorNormalizedBytes("/sd-xpath-constraint-r5.xml");
org.hl7.fhir.r4.model.StructureDefinition r4_actual = (org.hl7.fhir.r4.model.StructureDefinition) new org.hl7.fhir.r4.formats.XmlParser().parse(r4_input);
org.hl7.fhir.r5.model.StructureDefinition r5_conv = (org.hl7.fhir.r5.model.StructureDefinition) VersionConvertorFactory_40_50.convertResource(r4_actual);

View File

@ -68,7 +68,6 @@
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xpp3</groupId>
@ -87,6 +86,12 @@
<version>72.1</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!-- JUnit Jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
@ -143,7 +148,12 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mockftpserver</groupId>
<artifactId>MockFtpServer</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<!--
<dependency>

View File

@ -1,21 +1,67 @@
package org.hl7.fhir.utilities;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FTPClient {
private final org.apache.commons.net.ftp.FTPClient clientImpl;
final String server;
final String path;
final String user;
final String password;
final int port;
/**
* Connect to an FTP server
* @param server - the server to connect to (uusally just an IP address). It's up to the system to figure out access (VPN etc)
* @param server - the server to connect to (usually just an IP address). It's up to the system to figure out access (VPN etc)
* @param path - the path on the FTP server to treat all the operations as relative to
* @param user - username for the FTP server
* @param password - password for the FTP server
*/
public FTPClient(String server, String path, String user, String password) {
this (server, -1, path, user, password);
}
protected FTPClient(String server, int port, String path, String user, String password) {
this.server = server;
this.port = port;
this.path = path;
this.user = user;
this.password = password;
clientImpl = new org.apache.commons.net.ftp.FTPClient();
}
/**
* Connect to the server, throw an exception if it fails
*/
public void connect() {
public void connect() throws IOException {
if (port != -1) {
clientImpl.connect(server, port);
}
else {
clientImpl.connect(server);
}
clientImpl.login(user, password);
clientImpl.getSystemType();
int reply = clientImpl.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
clientImpl.disconnect();
throw new IOException("FTP server refused connection.");
}
}
/**
@ -23,7 +69,13 @@ public class FTPClient {
*
* @param path - relative to the path provided in the constructor
*/
public void delete(String path) {
public void delete(String path) throws IOException {
String resolvedPath = resolveRemotePath(path);
clientImpl.deleteFile(resolvedPath);
}
private String resolveRemotePath(String path) {
return String.join("", this.path, path);
}
/**
@ -31,9 +83,10 @@ public class FTPClient {
* @param source - absolute path on local system
* @param path - relative to the path provided in the constructor
*/
public void upload(String source, String path) {
public void upload(String source, String path) throws IOException {
String resolvedPath = resolveRemotePath(path);
FileInputStream localStream = new FileInputStream(new File(source));
clientImpl.storeFile( resolvedPath, localStream);
}
}

View File

@ -0,0 +1,140 @@
package org.hl7.fhir.utilities;
import org.hl7.fhir.utilities.tests.ResourceLoaderTests;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.mockftpserver.fake.FakeFtpServer;
import org.mockftpserver.fake.UserAccount;
import org.mockftpserver.fake.filesystem.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class FTPClientTest implements ResourceLoaderTests {
public static final String DUMMY_PASSWORD = "dummyPassword123";
public static final String DUMMY_USER = "dummyUser";
public static final String RELATIVE_PATH_1 = "relativePath1";
public static final String RELATIVE_PATH_2 = "relativePath2";
public static final String DUMMY_FILE_TO_DELETE = "dummyFileToDelete";
public static final String DUMMY_FILE_TO_UPLOAD = "dummyFileToUpload";
public static final int FAKE_FTP_PORT = 8021;
FakeFtpServer fakeFtpServer;
Path fakeFtpDirectory;
Path relativePath1;
Path relativePath2;
Path dummyFileToDeletePath;
Path dummyFileToUploadPath;
Path dummyUploadedFilePath;
@BeforeAll
public void setup() throws IOException {
setupDummyFileToUpload();
setupFakeFtpDirectory();
setupFakeFtpServer();
}
private void setupDummyFileToUpload() throws IOException {
dummyFileToUploadPath = Files.createTempFile("dummyFtpFileToUpload", "dummy");
}
public void setupFakeFtpServer() throws IOException {
fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setServerControlPort(FAKE_FTP_PORT);
fakeFtpServer.addUserAccount(new UserAccount(DUMMY_USER, DUMMY_PASSWORD, fakeFtpDirectory.toFile().getAbsolutePath()));
FileSystem fileSystem = useWindowsFileSystem()
? new WindowsFakeFileSystem()
: new UnixFakeFileSystem();
fileSystem.add(new DirectoryEntry(fakeFtpDirectory.toFile().getAbsolutePath()));
fileSystem.add(new DirectoryEntry(relativePath1.toFile().getAbsolutePath()));
fileSystem.add(new DirectoryEntry(relativePath2.toFile().getAbsolutePath()));
fileSystem.add(new FileEntry(dummyFileToDeletePath.toFile().getAbsolutePath()));
//fileSystem.add(new FileEntry("c:\\data\\run.exe"));
fakeFtpServer.setFileSystem(fileSystem);
fakeFtpServer.start();
}
private static boolean useWindowsFileSystem() {
return System.getProperty("os.name") != null && System.getProperty("os.name").startsWith("Windows");
}
private void setupFakeFtpDirectory() throws IOException {
fakeFtpDirectory = Files.createTempDirectory("fakeFtp");
relativePath1 = fakeFtpDirectory.resolve(RELATIVE_PATH_1);
relativePath2 = relativePath1.resolve(RELATIVE_PATH_2);
Files.createDirectory(relativePath1);
Files.createDirectory(relativePath2);
dummyFileToDeletePath = Files.createFile(relativePath2.resolve(DUMMY_FILE_TO_DELETE));
dummyUploadedFilePath = relativePath2.resolve(DUMMY_FILE_TO_UPLOAD);
}
@AfterAll
public void tearDownFakeFtpServer() {
fakeFtpServer.stop();
}
@Test
public void testDelete() throws IOException {
FTPClient client = connectToFTPClient();
String deleteFilePath = dummyFileToDeletePath.toFile().getAbsolutePath();
assertTrue(fakeFtpServer.getFileSystem().exists(deleteFilePath));
client.delete( RELATIVE_PATH_2 + "/" + DUMMY_FILE_TO_DELETE);
assertFalse(fakeFtpServer.getFileSystem().exists(deleteFilePath));
}
@NotNull
private static FTPClient connectToFTPClient() throws IOException {
FTPClient client = new FTPClient("localhost", FAKE_FTP_PORT, RELATIVE_PATH_1 + "/", DUMMY_USER, DUMMY_PASSWORD);
client.connect();
return client;
}
@Test
public void testUpload() throws IOException {
FTPClient client = connectToFTPClient();
String uploadFilePath = dummyUploadedFilePath.toFile().getAbsolutePath();
assertFalse(fakeFtpServer.getFileSystem().exists(uploadFilePath));
client.upload(dummyFileToUploadPath.toFile().getAbsolutePath(), RELATIVE_PATH_2 + "/" + DUMMY_FILE_TO_UPLOAD);
assertTrue(fakeFtpServer.getFileSystem().exists(uploadFilePath));
}
}

View File

@ -85,6 +85,13 @@
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<!-- Use saxon for xml processing -->
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>${saxon_he_version}</version>
</dependency>
</dependencies>
<dependencyManagement>
@ -158,7 +165,7 @@
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>${saxon_he_version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>