Skip to content

Commit f843cb8

Browse files
authored
AJ-1452: get schema (#15)
add PfbReader.getPfbSchema method
1 parent eb64919 commit f843cb8

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

library/src/main/java/bio/terra/pfb/PfbReader.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ public class PfbReader {
2828

2929
public static String showSchema(String fileLocation) throws IOException {
3030
return convertEnum(
31-
readPfbSchema(fileLocation).getField("object").schema().getTypes().stream()
32-
.filter(t -> !t.getName().equals("Metadata"))
33-
.map(Schema::toString)
34-
.toList()
35-
.toString());
31+
getPfbSchema(fileLocation).stream().map(Schema::toString).toList().toString());
3632
}
3733

3834
public static String showNodes(String fileLocation) throws IOException {
@@ -97,6 +93,14 @@ public static Metadata getPfbMetadata(String fileLocation) throws IOException {
9793
throw new InvalidPfbException("Error reading PFB Metadata object");
9894
}
9995

96+
// note that this does not decode enum values via convertEnum. WDS does not need decoding;
97+
// if other clients do need it we should add it in here.
98+
public static List<Schema> getPfbSchema(String fileLocation) throws IOException {
99+
return readPfbSchema(fileLocation).getField("object").schema().getTypes().stream()
100+
.filter(t -> !t.getName().equals("Metadata"))
101+
.toList();
102+
}
103+
100104
static Schema readPfbSchema(String fileLocation) throws IOException {
101105
DatumReader<Entity> datumReader = new SpecificDatumReader<>(Entity.class);
102106
URL url = isValidUrl(fileLocation);

library/src/test/java/bio/terra/pfb/PfbReaderTest.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
import static bio.terra.pfb.utils.CompareOutputUtils.FileExtension.JSON;
44
import static bio.terra.pfb.utils.CompareOutputUtils.FileExtension.TXT;
55
import static bio.terra.pfb.utils.CompareOutputUtils.PfbCommandType.*;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
67
import static org.junit.jupiter.api.Assertions.assertThrows;
78

89
import bio.terra.pfb.exceptions.InvalidPfbException;
910
import bio.terra.pfb.utils.CompareOutputUtils;
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
import com.fasterxml.jackson.databind.ObjectMapper;
1013
import java.io.IOException;
1114
import java.util.List;
15+
import java.util.Spliterator;
16+
import java.util.Spliterators;
1217
import java.util.stream.Stream;
18+
import java.util.stream.StreamSupport;
19+
import org.apache.avro.Schema;
1320
import org.junit.jupiter.api.Disabled;
1421
import org.junit.jupiter.api.Test;
1522
import org.junit.jupiter.params.ParameterizedTest;
@@ -38,6 +45,39 @@ void showSchemaTest(String fileName) throws IOException {
3845
}
3946
}
4047

48+
// this test validates behavior of the PfbReader.getPfbSchema() method. This method is
49+
// used internally by PfbReader.showSchema, which is thoroughly tested by {@link
50+
// #showsSchemaTest()} therefore,
51+
// this test only performs cursory correctness checks.
52+
@ParameterizedTest
53+
@MethodSource("provideTestFiles")
54+
void getPfbSchemaTest(String fileName) throws IOException {
55+
// TODO - remove when fix is added for AJ-1288
56+
if (fileName.equals("empty")) {
57+
logger.error("Skipping test file: {} until fixed in AJ-1288\n", fileName);
58+
} else {
59+
// read the pypfb output for this file
60+
String expectedStr = CompareOutputUtils.getPyPfbOutput(fileName, SHOW_SCHEMA, JSON);
61+
// parse the pypfb output for this file
62+
ObjectMapper mapper = new ObjectMapper();
63+
JsonNode expected = mapper.readTree(expectedStr);
64+
// find the names of all top-level types from the pypfb output
65+
List<String> expectedNames =
66+
StreamSupport.stream(
67+
Spliterators.spliteratorUnknownSize(expected.elements(), Spliterator.ORDERED),
68+
false)
69+
.map(typeNode -> typeNode.get("name").asText())
70+
.toList();
71+
// get the PfbReader-calculated schema for this file from PfbReader
72+
List<Schema> actualSchema =
73+
PfbReader.getPfbSchema(CompareOutputUtils.getAvroFilePath(fileName, ""));
74+
// find the names of all top-level types in the actual schema
75+
List<String> actualNames = actualSchema.stream().map(Schema::getName).toList();
76+
77+
assertEquals(expectedNames, actualNames);
78+
}
79+
}
80+
4181
@ParameterizedTest
4282
@MethodSource("provideTestFiles")
4383
void showNodesTest(String fileName) throws IOException {
@@ -57,8 +97,7 @@ void getGenericRecordsStream(String fileName) throws IOException {
5797
}
5898

5999
@Test
60-
@MethodSource("provideTestFiles")
61-
void getGenericRecordsStreamError() throws IOException {
100+
void getGenericRecordsStreamError() {
62101
assertThrows(
63102
InvalidPfbException.class,
64103
() -> CompareOutputUtils.testDataStream("noFile.txt"),

library/src/test/java/bio/terra/pfb/utils/CompareOutputUtils.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ public class CompareOutputUtils {
2222
public static void assertJavaPfbIsPyPFB(
2323
String fileName, PfbCommandType commandType, String filePath, FileExtension fileExtension)
2424
throws IOException {
25-
String pythonOutput;
26-
pythonOutput =
27-
Files.readString(Paths.get(getPyPfbOutputFilePath(fileName, commandType, fileExtension)));
25+
String pythonOutput = getPyPfbOutput(fileName, commandType, fileExtension);
2826
String avroFilePath = getAvroFilePath(fileName, filePath);
2927
String javaPfbOutput =
3028
switch (commandType) {
@@ -91,14 +89,20 @@ public String toString() {
9189
}
9290
}
9391

94-
private static String getAvroFilePath(String fileName, String filePath) {
92+
public static String getPyPfbOutput(
93+
String fileName, PfbCommandType commandType, FileExtension fileExtension) throws IOException {
94+
return Files.readString(
95+
Paths.get(getPyPfbOutputFilePath(fileName, commandType, fileExtension)));
96+
}
97+
98+
public static String getAvroFilePath(String fileName, String filePath) {
9599
if (filePath.isEmpty()) {
96100
filePath = String.format("src/test/resources/avro/%s.avro", fileName);
97101
}
98102
return filePath;
99103
}
100104

101-
private static String getPyPfbOutputFilePath(
105+
public static String getPyPfbOutputFilePath(
102106
String fileName, PfbCommandType commandType, FileExtension fileExtension) {
103107
return String.format(
104108
"src/test/resources/pyPfbOutput/%s/%s.%s", commandType, fileName, fileExtension);

0 commit comments

Comments
 (0)