|
22 | 22 | import static org.hamcrest.Matchers.containsString; |
23 | 23 | import static org.junit.Assert.assertEquals; |
24 | 24 | import static org.junit.Assert.assertThrows; |
| 25 | +import static org.junit.Assert.assertTrue; |
25 | 26 |
|
26 | 27 | import java.io.IOException; |
27 | 28 | import java.io.InputStream; |
| 29 | +import java.util.Arrays; |
28 | 30 | import java.util.stream.Collectors; |
29 | 31 | import org.apache.beam.sdk.schemas.Schema.FieldType; |
30 | 32 | import org.apache.beam.sdk.schemas.utils.JsonUtils; |
| 33 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Iterables; |
31 | 34 | import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.ByteStreams; |
32 | 35 | import org.everit.json.schema.ValidationException; |
33 | 36 | import org.json.JSONArray; |
@@ -285,24 +288,38 @@ public void testArrayWithNestedRefsBeamSchema() throws IOException { |
285 | 288 | String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8"); |
286 | 289 | Schema parsedSchema = JsonUtils.beamSchemaFromJsonSchema(stringJsonSchema); |
287 | 290 |
|
288 | | - assertThat(parsedSchema.getFieldNames(), containsInAnyOrder("vegetables")); |
289 | | - assertThat( |
290 | | - parsedSchema.getFields().stream().map(Schema.Field::getType).collect(Collectors.toList()), |
291 | | - containsInAnyOrder( |
292 | | - Schema.FieldType.array( |
293 | | - Schema.FieldType.row( |
294 | | - Schema.of( |
295 | | - Schema.Field.of("veggieName", Schema.FieldType.STRING), |
296 | | - Schema.Field.of("veggieLike", Schema.FieldType.BOOLEAN), |
297 | | - Schema.Field.nullable( |
298 | | - "origin", |
299 | | - Schema.FieldType.row( |
300 | | - Schema.of( |
301 | | - Schema.Field.nullable("country", Schema.FieldType.STRING), |
302 | | - Schema.Field.nullable("town", Schema.FieldType.STRING), |
303 | | - Schema.Field.nullable( |
304 | | - "region", Schema.FieldType.STRING))))))) |
305 | | - .withNullable(true))); |
| 291 | + assertEquals("vegetables", Iterables.getOnlyElement(parsedSchema.getFieldNames())); |
| 292 | + Schema.Field field = parsedSchema.getField("vegetables"); |
| 293 | + |
| 294 | + // Top-level fields include only one nullable field, so ordering should be preserved on that |
| 295 | + // level. |
| 296 | + assertEquals( |
| 297 | + Arrays.asList("veggieName", "veggieLike", "origin"), |
| 298 | + field.getType().getCollectionElementType().getRowSchema().getFieldNames()); |
| 299 | + // Inner schema contains multiple nullable fields, which can be out of order. Test the |
| 300 | + // remaining using |
| 301 | + // equivalency instead. |
| 302 | + assertTrue( |
| 303 | + field |
| 304 | + .getType() |
| 305 | + .equivalent( |
| 306 | + Schema.FieldType.array( |
| 307 | + Schema.FieldType.row( |
| 308 | + Schema.of( |
| 309 | + Schema.Field.of("veggieName", Schema.FieldType.STRING), |
| 310 | + Schema.Field.of("veggieLike", Schema.FieldType.BOOLEAN), |
| 311 | + Schema.Field.nullable( |
| 312 | + "origin", |
| 313 | + Schema.FieldType.row( |
| 314 | + Schema.of( |
| 315 | + Schema.Field.nullable( |
| 316 | + "country", Schema.FieldType.STRING), |
| 317 | + Schema.Field.nullable( |
| 318 | + "town", Schema.FieldType.STRING), |
| 319 | + Schema.Field.nullable( |
| 320 | + "region", Schema.FieldType.STRING))))))) |
| 321 | + .withNullable(true), |
| 322 | + Schema.EquivalenceNullablePolicy.SAME)); |
306 | 323 | } |
307 | 324 | } |
308 | 325 |
|
|
0 commit comments