Search in sources :

Example 11 with Matcher

use of org.hamcrest.Matcher in project pulsar by yahoo.

the class NamespacesTest method testUnloadNamespaces.

@Test
public void testUnloadNamespaces() throws Exception {
    final NamespaceName testNs = this.testLocalNamespaces.get(1);
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceName) {
                NamespaceName ns = (NamespaceName) item;
                return ns.equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(true).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceName) {
                NamespaceName ns = (NamespaceName) item;
                return ns.equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }));
    doNothing().when(nsSvc).unloadNamespace(testNs);
    NamespaceBundle bundle = nsSvc.getNamespaceBundleFactory().getFullBundle(testNs);
    doNothing().when(namespaces).validateBundleOwnership(bundle, false, true);
    try {
        namespaces.unloadNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
        fail("should have failed as bydefault bundle is activated and can't be unloaded");
    } catch (RestException re) {
    // ok
    }
    verify(nsSvc, times(0)).unloadNamespace(testNs);
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 12 with Matcher

use of org.hamcrest.Matcher in project pulsar by yahoo.

the class NamespacesTest method testNamespacesApiRedirects.

@Test
public void testNamespacesApiRedirects() throws Exception {
    // Redirect cases
    uriField.set(namespaces, uriInfo);
    doReturn(false).when(namespaces).isLeaderBroker();
    URI uri = URI.create("http://localhost" + ":" + BROKER_WEBSERVICE_PORT + "/admin/namespace/" + this.testLocalNamespaces.get(2).toString());
    doReturn(uri).when(uriInfo).getRequestUri();
    // Trick to force redirection
    conf.setAuthorizationEnabled(true);
    try {
        namespaces.deleteNamespace(this.testProperty, this.testOtherCluster, this.testLocalNamespaces.get(2).getLocalName(), false);
        fail("Should have raised exception to redirect request");
    } catch (WebApplicationException wae) {
        // OK
        assertEquals(wae.getResponse().getStatus(), Status.TEMPORARY_REDIRECT.getStatusCode());
        assertEquals(wae.getResponse().getLocation().toString(), UriBuilder.fromUri(uri).host("broker-usc.com").port(BROKER_WEBSERVICE_PORT).toString());
    }
    uri = URI.create("http://localhost" + ":" + BROKER_WEBSERVICE_PORT + "/admin/namespace/" + this.testLocalNamespaces.get(2).toString() + "/unload");
    doReturn(uri).when(uriInfo).getRequestUri();
    try {
        namespaces.unloadNamespaceBundle(this.testProperty, this.testOtherCluster, this.testLocalNamespaces.get(2).getLocalName(), "0x00000000_0xffffffff", false);
        fail("Should have raised exception to redirect request");
    } catch (WebApplicationException wae) {
        // OK
        assertEquals(wae.getResponse().getStatus(), Status.TEMPORARY_REDIRECT.getStatusCode());
        assertEquals(wae.getResponse().getLocation().toString(), UriBuilder.fromUri(uri).host("broker-usc.com").port(BROKER_WEBSERVICE_PORT).toString());
    }
    uri = URI.create("http://localhost" + ":" + BROKER_WEBSERVICE_PORT + "/admin/namespace/" + this.testGlobalNamespaces.get(0).toString() + "/configversion");
    doReturn(uri).when(uriInfo).getRequestUri();
    // setup to redirect to another broker in the same cluster
    doReturn(new URL("http://otherhost" + ":" + BROKER_WEBSERVICE_PORT)).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceName>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            NamespaceName nsname = (NamespaceName) item;
            return nsname.equals(NamespacesTest.this.testGlobalNamespaces.get(0));
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    admin.namespaces().setNamespaceReplicationClusters(testGlobalNamespaces.get(0).toString(), Lists.newArrayList("usw"));
    uri = URI.create("http://localhost" + ":" + BROKER_WEBSERVICE_PORT + "/admin/namespace/" + this.testLocalNamespaces.get(2).toString() + "?authoritative=false");
    doReturn(uri).when(uriInfo).getRequestUri();
    doReturn(true).when(namespaces).isLeaderBroker();
    try {
        namespaces.deleteNamespace(this.testLocalNamespaces.get(2).getProperty(), this.testLocalNamespaces.get(2).getCluster(), this.testLocalNamespaces.get(2).getLocalName(), false);
        fail("Should have raised exception to redirect request");
    } catch (WebApplicationException wae) {
        // OK
        assertEquals(wae.getResponse().getStatus(), Status.TEMPORARY_REDIRECT.getStatusCode());
        assertEquals(wae.getResponse().getLocation().toString(), UriBuilder.fromUri(uri).host("broker-usc.com").port(BROKER_WEBSERVICE_PORT).toString());
    }
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) WebApplicationException(javax.ws.rs.WebApplicationException) Matcher(org.hamcrest.Matcher) URI(java.net.URI) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 13 with Matcher

use of org.hamcrest.Matcher in project beam by apache.

the class GcpApiSurfaceTest method testGcpApiSurface.

@Test
public void testGcpApiSurface() throws Exception {
    final Package thisPackage = getClass().getPackage();
    final ClassLoader thisClassLoader = getClass().getClassLoader();
    final ApiSurface apiSurface = ApiSurface.ofPackage(thisPackage, thisClassLoader).pruningPattern(BigqueryMatcher.class.getName()).pruningPattern("org[.]apache[.]beam[.].*Test.*").pruningPattern("org[.]apache[.]beam[.].*IT").pruningPattern("java[.]lang.*").pruningPattern("java[.]util.*");
    @SuppressWarnings("unchecked") final Set<Matcher<Class<?>>> allowedClasses = ImmutableSet.of(classesInPackage("com.google.api.client.googleapis"), classesInPackage("com.google.api.client.http"), classesInPackage("com.google.api.client.json"), classesInPackage("com.google.api.client.util"), classesInPackage("com.google.api.services.bigquery.model"), classesInPackage("com.google.auth"), classesInPackage("com.google.bigtable.v2"), classesInPackage("com.google.cloud.bigtable.config"), Matchers.<Class<?>>equalTo(com.google.cloud.bigtable.grpc.BigtableClusterName.class), Matchers.<Class<?>>equalTo(com.google.cloud.bigtable.grpc.BigtableInstanceName.class), Matchers.<Class<?>>equalTo(com.google.cloud.bigtable.grpc.BigtableTableName.class), Matchers.<Class<?>>equalTo(com.google.cloud.ByteArray.class), Matchers.<Class<?>>equalTo(com.google.cloud.Date.class), Matchers.<Class<?>>equalTo(com.google.cloud.Timestamp.class), classesInPackage("com.google.cloud.spanner"), classesInPackage("com.google.datastore.v1"), classesInPackage("com.google.protobuf"), classesInPackage("com.google.type"), classesInPackage("com.fasterxml.jackson.annotation"), classesInPackage("com.fasterxml.jackson.core"), classesInPackage("com.fasterxml.jackson.databind"), classesInPackage("io.grpc"), classesInPackage("java"), classesInPackage("javax"), classesInPackage("org.apache.beam"), classesInPackage("org.apache.commons.logging"), classesInPackage("org.joda.time"));
    assertThat(apiSurface, containsOnlyClassesMatching(allowedClasses));
}
Also used : BigqueryMatcher(org.apache.beam.sdk.io.gcp.testing.BigqueryMatcher) Matcher(org.hamcrest.Matcher) BigqueryMatcher(org.apache.beam.sdk.io.gcp.testing.BigqueryMatcher) ApiSurface(org.apache.beam.sdk.util.ApiSurface) ApiSurface.classesInPackage(org.apache.beam.sdk.util.ApiSurface.classesInPackage) Test(org.junit.Test)

Example 14 with Matcher

use of org.hamcrest.Matcher in project beam by apache.

the class ImmutableListBundleFactoryTest method afterCommitGetElementsShouldHaveAddedElements.

private <T> CommittedBundle<T> afterCommitGetElementsShouldHaveAddedElements(Iterable<WindowedValue<T>> elems) {
    UncommittedBundle<T> bundle = bundleFactory.createRootBundle();
    Collection<Matcher<? super WindowedValue<T>>> expectations = new ArrayList<>();
    Instant minElementTs = BoundedWindow.TIMESTAMP_MAX_VALUE;
    for (WindowedValue<T> elem : elems) {
        bundle.add(elem);
        expectations.add(equalTo(elem));
        if (elem.getTimestamp().isBefore(minElementTs)) {
            minElementTs = elem.getTimestamp();
        }
    }
    Matcher<Iterable<? extends WindowedValue<T>>> containsMatcher = Matchers.<WindowedValue<T>>containsInAnyOrder(expectations);
    Instant commitTime = Instant.now();
    CommittedBundle<T> committed = bundle.commit(commitTime);
    assertThat(committed.getElements(), containsMatcher);
    // Sanity check that the test is meaningful.
    assertThat(minElementTs, not(equalTo(commitTime)));
    assertThat(committed.getMinTimestamp(), equalTo(minElementTs));
    assertThat(committed.getSynchronizedProcessingOutputWatermark(), equalTo(commitTime));
    return committed;
}
Also used : Matcher(org.hamcrest.Matcher) WindowedValue(org.apache.beam.sdk.util.WindowedValue) Instant(org.joda.time.Instant) ArrayList(java.util.ArrayList)

Example 15 with Matcher

use of org.hamcrest.Matcher in project double-espresso by JakeWharton.

the class DrawerActions method checkDrawer.

/**
   * Returns true if the given matcher matches the drawer.
   */
private static boolean checkDrawer(int drawerLayoutId, final Matcher<View> matcher) {
    final AtomicBoolean matches = new AtomicBoolean(false);
    onView(withId(drawerLayoutId)).perform(new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(DrawerLayout.class);
        }

        @Override
        public String getDescription() {
            return "check drawer";
        }

        @Override
        public void perform(UiController uiController, View view) {
            matches.set(matcher.matches(view));
        }
    });
    return matches.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ViewAction(com.google.android.apps.common.testing.ui.espresso.ViewAction) Matcher(org.hamcrest.Matcher) UiController(com.google.android.apps.common.testing.ui.espresso.UiController) DrawerLayout(android.support.v4.widget.DrawerLayout) Espresso.onView(com.google.android.apps.common.testing.ui.espresso.Espresso.onView) View(android.view.View)

Aggregations

Matcher (org.hamcrest.Matcher)28 Description (org.hamcrest.Description)7 View (android.view.View)6 Test (org.junit.Test)6 Espresso.onView (android.support.test.espresso.Espresso.onView)4 UiController (android.support.test.espresso.UiController)4 ViewAction (android.support.test.espresso.ViewAction)4 TextView (android.widget.TextView)4 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)4 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)4 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)4 URL (java.net.URL)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.testng.annotations.Test)4 RestException (com.yahoo.pulsar.broker.web.RestException)3 ArrayList (java.util.ArrayList)3 Resources (android.content.res.Resources)2 ColorInt (android.support.annotation.ColorInt)2 MediumTest (android.support.test.filters.MediumTest)2 ViewGroup (android.view.ViewGroup)2