Search in sources :

Example 21 with Description

use of org.hamcrest.Description in project neo4j by neo4j.

the class TransactionMatchers method rowContainsDeletedEntitiesInPath.

public static Matcher<? super HTTP.Response> rowContainsDeletedEntitiesInPath(final int nodes, final int rels) {
    return new TypeSafeMatcher<HTTP.Response>() {

        @Override
        protected boolean matchesSafely(HTTP.Response response) {
            try {
                Iterator<JsonNode> meta = getJsonNodeWithName(response, "meta").iterator();
                int nodeCounter = 0;
                int relCounter = 0;
                assertTrue("Expected to find a JSON node, but there was none", meta.hasNext());
                JsonNode node = meta.next();
                assertTrue("Expected the node to be a list (for a path)", node.isArray());
                for (JsonNode inner : node) {
                    String type = inner.get("type").getTextValue();
                    switch(type) {
                        case "node":
                            if (inner.get("deleted").asBoolean()) {
                                ++nodeCounter;
                            }
                            break;
                        case "relationship":
                            if (inner.get("deleted").asBoolean()) {
                                ++relCounter;
                            }
                            break;
                        default:
                            fail("Unexpected type: " + type);
                            break;
                    }
                }
                assertEquals(nodes, nodeCounter);
                assertEquals(rels, relCounter);
                return true;
            } catch (JsonParseException e) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) HTTP(org.neo4j.test.server.HTTP) JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Example 22 with Description

use of org.hamcrest.Description in project neo4j by neo4j.

the class TransactionMatchers method graphContainsDeletedNodes.

public static Matcher<? super HTTP.Response> graphContainsDeletedNodes(final int amount) {
    return new TypeSafeMatcher<HTTP.Response>() {

        @Override
        protected boolean matchesSafely(HTTP.Response response) {
            try {
                Iterator<JsonNode> nodes = getJsonNodeWithName(response, "graph").get("nodes").iterator();
                for (int i = 0; i < amount; ++i) {
                    assertTrue(nodes.hasNext());
                    JsonNode node = nodes.next();
                    assertThat(node.get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                }
                while (nodes.hasNext()) {
                    JsonNode node = nodes.next();
                    assertNull(node.get("deleted"));
                }
                return true;
            } catch (JsonParseException e) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) HTTP(org.neo4j.test.server.HTTP) JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Example 23 with Description

use of org.hamcrest.Description in project druid by druid-io.

the class LookupCoordinatorManagerTest method testUpdateAllOnHostException.

@Test
public void testUpdateAllOnHostException() throws Exception {
    final HttpResponseHandler<InputStream, InputStream> responseHandler = EasyMock.createStrictMock(HttpResponseHandler.class);
    final URL url = LookupCoordinatorManager.getLookupsURL(HostAndPort.fromString("localhost"));
    final SettableFuture<InputStream> future = SettableFuture.create();
    future.set(new ByteArrayInputStream(new byte[0]));
    EasyMock.expect(client.go(EasyMock.<Request>anyObject(), EasyMock.<SequenceInputStreamResponseHandler>anyObject(), EasyMock.<Duration>anyObject())).andReturn(future).once();
    EasyMock.replay(client, responseHandler);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig) {

        @Override
        HttpResponseHandler<InputStream, InputStream> makeResponseHandler(final AtomicInteger returnCode, final AtomicReference<String> reasonString) {
            returnCode.set(500);
            reasonString.set("");
            return responseHandler;
        }
    };
    expectedException.expect(new BaseMatcher<Throwable>() {

        @Override
        public boolean matches(Object o) {
            return o instanceof IOException && ((IOException) o).getMessage().startsWith("Bad update request");
        }

        @Override
        public void describeTo(Description description) {
        }
    });
    try {
        manager.updateAllOnHost(url, SINGLE_LOOKUP_MAP);
    } finally {
        EasyMock.verify(client, responseHandler);
    }
}
Also used : Description(org.hamcrest.Description) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) Duration(org.joda.time.Duration) AtomicReference(java.util.concurrent.atomic.AtomicReference) SequenceInputStreamResponseHandler(com.metamx.http.client.response.SequenceInputStreamResponseHandler) IOException(java.io.IOException) URL(java.net.URL) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 24 with Description

use of org.hamcrest.Description in project druid by druid-io.

the class LookupCoordinatorManagerTest method testLookupDiscoverAllExceptional.

@Test
public void testLookupDiscoverAllExceptional() throws Exception {
    final IOException ex = new IOException("some exception");
    EasyMock.reset(discoverer);
    EasyMock.expect(discoverer.discoverChildren(LookupCoordinatorManager.LOOKUP_LISTEN_ANNOUNCE_KEY)).andThrow(ex).once();
    expectedException.expectCause(new BaseMatcher<Throwable>() {

        @Override
        public boolean matches(Object o) {
            return o == ex;
        }

        @Override
        public void describeTo(Description description) {
        }
    });
    EasyMock.replay(discoverer);
    final LookupCoordinatorManager manager = new LookupCoordinatorManager(client, discoverer, mapper, configManager, lookupCoordinatorManagerConfig);
    try {
        manager.discoverTiers();
    } finally {
        EasyMock.verify(discoverer);
    }
}
Also used : Description(org.hamcrest.Description) IOException(java.io.IOException) Test(org.junit.Test)

Example 25 with Description

use of org.hamcrest.Description in project neo4j by neo4j.

the class StreamMatchers method equalsStream.

public static Matcher<BoltResult> equalsStream(final String[] fieldNames, final Matcher... records) {
    return new TypeSafeMatcher<BoltResult>() {

        @Override
        protected boolean matchesSafely(BoltResult item) {
            if (!Arrays.equals(fieldNames, item.fieldNames())) {
                return false;
            }
            final Iterator<Matcher> expected = asList(records).iterator();
            final AtomicBoolean matched = new AtomicBoolean(true);
            try {
                item.accept(new BoltResult.Visitor() {

                    @Override
                    public void visit(Record record) {
                        if (!expected.hasNext() || !expected.next().matches(record)) {
                            matched.set(false);
                        }
                    }

                    @Override
                    public void addMetadata(String key, Object value) {
                    }
                });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            // All records matched, and there are no more expected records.
            return matched.get() && !expected.hasNext();
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Stream[").appendValueList(" fieldNames=[", ",", "]", fieldNames).appendList(", records=[", ",", "]", asList(records));
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

Description (org.hamcrest.Description)122 Test (org.junit.Test)38 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)35 StringDescription (org.hamcrest.StringDescription)29 BaseMatcher (org.hamcrest.BaseMatcher)27 View (android.view.View)22 ViewParent (android.view.ViewParent)11 TextView (android.widget.TextView)11 ViewGroup (android.view.ViewGroup)8 Expectations (org.jmock.Expectations)8 URL (java.net.URL)7 Matcher (org.hamcrest.Matcher)7 Invocation (org.jmock.api.Invocation)7 BoundedMatcher (android.support.test.espresso.matcher.BoundedMatcher)6 ImageView (android.widget.ImageView)6 File (java.io.File)6 IOException (java.io.IOException)6 URI (java.net.URI)6 List (java.util.List)6 JsonNode (org.codehaus.jackson.JsonNode)6