Search in sources :

Example 1 with Item

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);
            }
        }
    };
}
Also used : Item(org.apache.beam.sdk.transforms.display.DisplayData.Item) Description(org.hamcrest.Description) CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher)

Example 2 with Item

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);
}
Also used : Item(org.apache.beam.sdk.transforms.display.DisplayData.Item) Matchers.everyItem(org.hamcrest.Matchers.everyItem) Item(org.apache.beam.sdk.transforms.display.DisplayData.Item) DisplayDataMatchers.hasDisplayItem(org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Builder(org.apache.beam.sdk.transforms.display.DisplayData.Builder) Instant(org.joda.time.Instant) Test(org.junit.Test)

Example 3 with Item

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);
}
Also used : Item(org.apache.beam.sdk.transforms.display.DisplayData.Item) Matchers.everyItem(org.hamcrest.Matchers.everyItem) Item(org.apache.beam.sdk.transforms.display.DisplayData.Item) DisplayDataMatchers.hasDisplayItem(org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Builder(org.apache.beam.sdk.transforms.display.DisplayData.Builder) Instant(org.joda.time.Instant) Test(org.junit.Test)

Example 4 with Item

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());
}
Also used : Matchers.everyItem(org.hamcrest.Matchers.everyItem) Item(org.apache.beam.sdk.transforms.display.DisplayData.Item) DisplayDataMatchers.hasDisplayItem(org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Test(org.junit.Test)

Aggregations

Item (org.apache.beam.sdk.transforms.display.DisplayData.Item)4 DisplayDataMatchers.hasDisplayItem (org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem)3 Matchers.everyItem (org.hamcrest.Matchers.everyItem)3 Matchers.hasItem (org.hamcrest.Matchers.hasItem)3 Test (org.junit.Test)3 Builder (org.apache.beam.sdk.transforms.display.DisplayData.Builder)2 Instant (org.joda.time.Instant)2 CustomTypeSafeMatcher (org.hamcrest.CustomTypeSafeMatcher)1 Description (org.hamcrest.Description)1