/** * Creates a matcher that matches if the examined {@link DisplayData.Item} matches the specified * type. */ public static Matcher<DisplayData.Item> hasType(DisplayData.Type type) { return hasType(is(type)); }
@Test public void testTypeMappings() { DisplayData data = DisplayData.from( new HasDisplayData() { @Override public void populateDisplayData(DisplayData.Builder builder) { builder .add(DisplayData.item("string", "foobar")) .add(DisplayData.item("integer", 123)) .add(DisplayData.item("float", 2.34)) .add(DisplayData.item("boolean", true)) .add(DisplayData.item("java_class", DisplayDataTest.class)) .add(DisplayData.item("timestamp", Instant.now())) .add(DisplayData.item("duration", Duration.standardHours(1))); } }); Collection<Item> items = data.items(); assertThat(items, hasItem(allOf(hasKey("string"), hasType(DisplayData.Type.STRING)))); assertThat(items, hasItem(allOf(hasKey("integer"), hasType(DisplayData.Type.INTEGER)))); assertThat(items, hasItem(allOf(hasKey("float"), hasType(DisplayData.Type.FLOAT)))); assertThat(items, hasItem(allOf(hasKey("boolean"), hasType(DisplayData.Type.BOOLEAN)))); assertThat(items, hasItem(allOf(hasKey("java_class"), hasType(DisplayData.Type.JAVA_CLASS)))); assertThat(items, hasItem(allOf(hasKey("timestamp"), hasType(DisplayData.Type.TIMESTAMP)))); assertThat(items, hasItem(allOf(hasKey("duration"), hasType(DisplayData.Type.DURATION)))); }
private static Matcher<DisplayData> hasDisplayItem( String key, DisplayData.Type type, Object value) { DisplayData.FormattedItemValue formattedValue = type.format(value); return hasDisplayItem( allOf(hasKey(key), hasType(type), hasValue(formattedValue.getLongValue()))); }
@Test public void testStaticValueProviderDate() { final Instant value = Instant.now(); DisplayData data = DisplayData.from( new HasDisplayData() { @Override public void populateDisplayData(DisplayData.Builder builder) { builder.add(DisplayData.item("foo", StaticValueProvider.of(value))); } }); @SuppressWarnings("unchecked") DisplayData.Item item = (DisplayData.Item) data.items().toArray()[0]; @SuppressWarnings("unchecked") Matcher<Item> matchesAllOf = allOf( hasKey("foo"), hasType(DisplayData.Type.TIMESTAMP), hasValue(ISO_FORMATTER.print(value))); assertThat(item, matchesAllOf); }
@Test public void testHasType() { Matcher<DisplayData> matcher = hasDisplayItem(hasType(DisplayData.Type.JAVA_CLASS)); DisplayData data = DisplayData.from( new PTransform<PCollection<String>, PCollection<String>>() { @Override public PCollection<String> expand(PCollection<String> input) { throw new IllegalArgumentException("Should never be applied"); } @Override public void populateDisplayData(Builder builder) { builder.add(DisplayData.item("foo", DisplayDataMatchersTest.class)); } }); assertFalse(matcher.matches(createDisplayDataWithItem("fooz", "bar"))); assertThat(data, matcher); }
@Test public void testItemProperties() { final Instant value = Instant.now(); DisplayData data = DisplayData.from( new HasDisplayData() { @Override public void populateDisplayData(DisplayData.Builder builder) { builder.add( DisplayData.item("now", value) .withLabel("the current instant") .withLinkUrl("http://time.gov") .withNamespace(DisplayDataTest.class)); } }); @SuppressWarnings("unchecked") DisplayData.Item item = (DisplayData.Item) data.items().toArray()[0]; @SuppressWarnings("unchecked") Matcher<Item> matchesAllOf = allOf( hasNamespace(DisplayDataTest.class), hasKey("now"), hasType(DisplayData.Type.TIMESTAMP), hasValue(ISO_FORMATTER.print(value)), hasShortValue(nullValue(String.class)), hasLabel("the current instant"), hasUrl(is("http://time.gov"))); assertThat(item, matchesAllOf); }
@Test public void testDoFnDisplayData() { DoFn<String, String> fn = new DoFn<String, String>() { @ProcessElement public void processElement(ProcessContext c) {} @Override public void populateDisplayData(Builder builder) { builder.add(DisplayData.item("doFnMetadata", "bar")); } }; SingleOutput<String, String> parDo = ParDo.of(fn); DisplayData displayData = DisplayData.from(parDo); assertThat( displayData, hasDisplayItem( allOf( hasKey("fn"), hasType(DisplayData.Type.JAVA_CLASS), DisplayDataMatchers.hasValue(fn.getClass().getName())))); assertThat(displayData, includesDisplayDataFor("fn", fn)); }
Matchers.<Class<?>>isOneOf( transform.getClass(), subComponent1.getClass(), subComponent2.getClass())), hasType(notNullValue(DisplayData.Type.class)), hasValue(not(isEmptyOrNullString())))));
@Test public void testDisplayDataItemProperties() { PipelineOptions options = PipelineOptionsFactory.create(); options.setTempLocation("myTemp"); DisplayData displayData = DisplayData.from(options); assertThat( displayData, hasDisplayItem( allOf( hasKey("tempLocation"), hasType(DisplayData.Type.STRING), hasValue("myTemp"), hasNamespace(PipelineOptions.class)))); }