Merge pull request #1882 from ahdis/oe_liquid_sheet_range
support for range in sheet for instance generation #1881
This commit is contained in:
commit
5ae6e990ba
|
@ -1,17 +1,14 @@
|
|||
package org.hl7.fhir.r5.testfactory.dataprovider;
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellReference;
|
||||
import org.hl7.fhir.exceptions.FHIRException;
|
||||
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
|
||||
import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
|
||||
import org.apache.poi.EncryptedDocumentException;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
|
@ -27,17 +24,21 @@ public class ExcelDataProvider extends TableDataProvider {
|
|||
private Map<String, Integer> columnIndexMap = new HashMap<>();
|
||||
private Row currentRow;
|
||||
private DataFormatter df = new DataFormatter();
|
||||
|
||||
private int startRow = 0;
|
||||
private int startCol = 0;
|
||||
private int endRow = -1;
|
||||
private int endCol = -1;
|
||||
|
||||
/**
|
||||
* Constructs an ExcelTableDataProvider.
|
||||
*
|
||||
* @param filename The path to the Excel file.
|
||||
* @param sheetName The name of the sheet to read.
|
||||
* @param sheetName The range of the sheet to read.
|
||||
* @throws IOException If an I/O error occurs.
|
||||
* @throws InvalidFormatException If the file format is invalid.
|
||||
*/
|
||||
public ExcelDataProvider(String filename, String sheetName) throws IOException, InvalidFormatException {
|
||||
public ExcelDataProvider(String filename, String sheetName, String range) throws IOException, InvalidFormatException {
|
||||
FileInputStream fis = new FileInputStream(ManagedFileAccess.file(filename));
|
||||
this.workbook = WorkbookFactory.create(fis);
|
||||
if (sheetName != null) {
|
||||
|
@ -55,7 +56,17 @@ public class ExcelDataProvider extends TableDataProvider {
|
|||
throw new IllegalArgumentException("Sheet '" + sheetName + "' does not exist in the file. Sheet Names = "+CommaSeparatedStringBuilder.join(",", names));
|
||||
}
|
||||
}
|
||||
|
||||
if (range != null ) {
|
||||
String[] parts = range.split(":");
|
||||
CellReference startCell = new CellReference(parts[0]);
|
||||
startRow = startCell.getRow();
|
||||
startCol = startCell.getCol();
|
||||
if (parts.length==2) {
|
||||
CellReference endCell = new CellReference(parts[1]);
|
||||
endRow = endCell.getRow();
|
||||
endCol = endCell.getCol();
|
||||
}
|
||||
}
|
||||
loadColumnHeaders();
|
||||
}
|
||||
|
||||
|
@ -74,12 +85,14 @@ public class ExcelDataProvider extends TableDataProvider {
|
|||
private void loadColumnHeaders() {
|
||||
columnHeaders = new ArrayList<>();
|
||||
columnHeaders.add("counter");
|
||||
Row headerRow = sheet.getRow(0);
|
||||
Row headerRow = sheet.getRow(startRow);
|
||||
if (headerRow != null) {
|
||||
for (Cell cell : headerRow) {
|
||||
String headerName = cell.getStringCellValue().trim();
|
||||
columnHeaders.add(headerName);
|
||||
columnIndexMap.put(headerName, cell.getColumnIndex());
|
||||
if (cell.getColumnIndex()>= startCol && (endCol==-1 || cell.getColumnIndex()<= endCol )) {
|
||||
String headerName = cell.getStringCellValue().trim();
|
||||
columnHeaders.add(headerName);
|
||||
columnIndexMap.put(headerName, cell.getColumnIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -92,7 +105,10 @@ public class ExcelDataProvider extends TableDataProvider {
|
|||
@Override
|
||||
public boolean nextRow() {
|
||||
currentRowIndex++;
|
||||
currentRow = sheet.getRow(currentRowIndex + 1); // Skip the header row
|
||||
currentRow = sheet.getRow(startRow + currentRowIndex + 1); // Skip the header row
|
||||
if (currentRow != null && endRow!=-1 && endRow == startRow + currentRowIndex) {
|
||||
currentRow = null;
|
||||
}
|
||||
return currentRow != null;
|
||||
}
|
||||
|
||||
|
@ -103,10 +119,12 @@ public class ExcelDataProvider extends TableDataProvider {
|
|||
if (currentRow != null) {
|
||||
for (Cell cell : currentRow) {
|
||||
int i = cell.getColumnIndex();
|
||||
while (cellValues.size() <= i) {
|
||||
cellValues.add("");
|
||||
if (i>= startCol && (endCol==-1 || i<= endCol )) {
|
||||
while (cellValues.size() <= i-startCol) {
|
||||
cellValues.add("");
|
||||
}
|
||||
cellValues.add(getCellValue(cell).trim());
|
||||
}
|
||||
cellValues.add(getCellValue(cell).trim());
|
||||
}
|
||||
}
|
||||
return cellValues;
|
||||
|
|
|
@ -19,7 +19,12 @@ public abstract class TableDataProvider {
|
|||
try {
|
||||
String filename = path;
|
||||
String sheetname = null;
|
||||
String range = null;
|
||||
|
||||
if (path.contains("!")) {
|
||||
range = path.substring(path.indexOf("!")+1);
|
||||
path = path.substring(0, path.indexOf("!"));
|
||||
}
|
||||
if (path.contains(";")) {
|
||||
filename = path.substring(0, path.indexOf(";"));
|
||||
sheetname = path.substring(path.indexOf(";")+1);
|
||||
|
@ -28,7 +33,7 @@ public abstract class TableDataProvider {
|
|||
if (Utilities.existsInList(extension, "csv", "txt")) {
|
||||
return new CSVDataProvider(filename);
|
||||
} else if (Utilities.existsInList(extension, "xlsx")) {
|
||||
return new ExcelDataProvider(filename, sheetname);
|
||||
return new ExcelDataProvider(filename, sheetname, range);
|
||||
} else if (Utilities.existsInList(extension, "db")) {
|
||||
return new SQLDataProvider(DriverManager.getConnection("jdbc:sqlite:"+filename), sheetname);
|
||||
} else {
|
||||
|
|
|
@ -76,7 +76,7 @@ public class TestInstanceGenerationTester {
|
|||
byte[] fsrc = TestingUtilities.loadTestResourceBytes("rX", "instance-generation", "factories", name);
|
||||
TextFile.bytesToFile(fsrc, Utilities.path(path, name));
|
||||
}
|
||||
for (String name : Utilities.strings("Patient-1.json", "Encounter-1.json", "MedicationStatement-1.json", "Observation-bp-1.json", "Observation-weight-1.json")) {
|
||||
for (String name : Utilities.strings("Patient-1.json","Patient2-1.json", "Encounter-1.json", "MedicationStatement-1.json", "Observation-bp-1.json", "Observation-weight-1.json")) {
|
||||
byte[] fsrc = TestingUtilities.loadTestResourceBytes("rX", "instance-generation", "expected", name);
|
||||
TextFile.bytesToFile(fsrc, Utilities.path(path, "expected", name));
|
||||
}
|
||||
|
@ -93,12 +93,12 @@ public class TestInstanceGenerationTester {
|
|||
for (String name : Utilities.strings("Bundle-patients.json", "Encounter-1.json", "Encounter-2.json", "Encounter-3.json", "Encounter-4.json", "MedicationStatement-1.json",
|
||||
"MedicationStatement-2.json", "MedicationStatement-4.json", "Observation-bp-1.json", "Observation-bp-2.json", "Observation-bp-3.json",
|
||||
"Observation-bp-4.json", "Observation-weight-1.json", "Observation-weight-2.json", "Observation-weight-3.json", "Observation-weight-4.json",
|
||||
"Patient-1.json", "Patient-2.json", "Patient-3.json", "Patient-4.json")) {
|
||||
"Patient-1.json", "Patient-2.json", "Patient-3.json", "Patient-4.json", "Patient2-1.json", "Patient2-2.json", "Patient2-3.json")) {
|
||||
File f = new File(Utilities.path(output, name));
|
||||
Assertions.assertTrue(f.exists());
|
||||
}
|
||||
|
||||
for (String name : Utilities.strings("Patient-1.json", "Encounter-1.json", "MedicationStatement-1.json", "Observation-bp-1.json", "Observation-weight-1.json")) {
|
||||
for (String name : Utilities.strings("Patient-1.json", "Patient2-1.json", "Encounter-1.json", "MedicationStatement-1.json", "Observation-bp-1.json", "Observation-weight-1.json")) {
|
||||
String diff = new CompareUtilities(null, null).checkJsonSrcIsSame(name, TextFile.fileToString(Utilities.path(expected, name)), TextFile.fileToString(Utilities.path(output, name)), false);
|
||||
Assertions.assertNull(diff, "unexpected difference for "+name);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue