use of org.apache.beam.sdk.transforms.display.DisplayData.Item in project beam by apache.
the class DisplayDataMatchers method includesDisplayDataFor.
/**
* Create a matcher that matches if the examined {@link DisplayData} contains all display data
* registered from the specified subcomponent and namespace.
*/
public static Matcher<DisplayData> includesDisplayDataFor(final String path, final HasDisplayData subComponent) {
return new CustomTypeSafeMatcher<DisplayData>("includes subcomponent") {
@Override
protected boolean matchesSafely(DisplayData displayData) {
DisplayData subComponentData = subComponentData(path);
if (subComponentData.items().size() == 0) {
throw new UnsupportedOperationException("subComponent contains no display data; " + "cannot verify whether it is included");
}
DisplayDataComparison comparison = checkSubset(displayData, subComponentData, path);
return comparison.missingItems.isEmpty();
}
@Override
protected void describeMismatchSafely(DisplayData displayData, Description mismatchDescription) {
DisplayData subComponentDisplayData = subComponentData(path);
DisplayDataComparison comparison = checkSubset(displayData, subComponentDisplayData, path);
mismatchDescription.appendText("did not include:\n").appendValue(comparison.missingItems).appendText("\nNon-matching items:\n").appendValue(comparison.unmatchedItems);
}
private DisplayData subComponentData(final String path) {
return DisplayData.from(new HasDisplayData() {
@Override
public void populateDisplayData(DisplayData.Builder builder) {
builder.include(path, subComponent);
}
});
}
private DisplayDataComparison checkSubset(DisplayData displayData, DisplayData included, String path) {
DisplayDataComparison comparison = new DisplayDataComparison(displayData.items());
for (Item item : included.items()) {
Item matchedItem = displayData.asMap().get(DisplayData.Identifier.of(DisplayData.Path.absolute(path), item.getNamespace(), item.getKey()));
if (matchedItem != null) {
comparison.matched(matchedItem);
} else {
comparison.missing(item);
}
}
return comparison;
}
class DisplayDataComparison {
Collection<Item> missingItems;
Collection<Item> unmatchedItems;
DisplayDataComparison(Collection<Item> superset) {
missingItems = Sets.newHashSet();
unmatchedItems = Sets.newHashSet(superset);
}
void matched(Item supersetItem) {
unmatchedItems.remove(supersetItem);
}
void missing(Item subsetItem) {
missingItems.add(subsetItem);
}
}
};
}
use of org.apache.beam.sdk.transforms.display.DisplayData.Item in project beam by apache.
the class DisplayDataTest method testStaticValueProviderDate.
@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 = Matchers.allOf(hasKey("foo"), hasType(DisplayData.Type.TIMESTAMP), hasValue(ISO_FORMATTER.print(value)));
assertThat(item, matchesAllOf);
}
use of org.apache.beam.sdk.transforms.display.DisplayData.Item in project beam by apache.
the class DisplayDataTest method testItemProperties.
@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 = Matchers.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);
}
use of org.apache.beam.sdk.transforms.display.DisplayData.Item in project beam by apache.
the class DisplayDataTest method testAutoValue.
@Test
public void testAutoValue() {
DisplayData data = DisplayData.from(new AutoValue_DisplayDataTest_Foo());
Item item = Iterables.getOnlyElement(data.asMap().values());
assertEquals(Foo.class, item.getNamespace());
}
Aggregations