org.hamcrest.MatcherAssert Java Examples

The following examples show how to use org.hamcrest.MatcherAssert. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestUriOfTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void extractsUriFromDict() {
    final String base = "http://localhost:8080";
    final String path = "/example";
    MatcherAssert.assertThat(
        new RequestUri.Of(
            new HashDict(
                new KvpOf("uri", base),
                new KvpOf("path", path),
                new KvpOf("q.first", "10"),
                new KvpOf("q.second", "someStr")
            )
        ).uri().toString(),
        new IsEqual<>(
            String.format(
                "%s?first=10&second=someStr",
                URI.create(base + path).toString()
            )
        )
    );
}
 
Example #2
Source File: JsonPathResultMatchers.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private String getContent(MvcResult result) throws UnsupportedEncodingException {
	String content = result.getResponse().getContentAsString();
	if (StringUtils.hasLength(this.prefix)) {
		try {
			String reason = String.format("Expected a JSON payload prefixed with \"%s\" but found: %s",
					this.prefix, StringUtils.quote(content.substring(0, this.prefix.length())));
			MatcherAssert.assertThat(reason, content, StringStartsWith.startsWith(this.prefix));
			return content.substring(this.prefix.length());
		}
		catch (StringIndexOutOfBoundsException ex) {
			throw new AssertionError("JSON prefix \"" + this.prefix + "\" not found", ex);
		}
	}
	else {
		return content;
	}
}
 
Example #3
Source File: ExpandedWireTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void expandWireWithAdditionalData() throws Exception {
    final Dict response = new ExpandedWire(
        req -> req, new ContentType("application/json")
    ).send(new HashDict(new KvpOf("foo", "bar")));
    MatcherAssert.assertThat(
        new CollectionOf<>(response).size(),
        new IsEqual<>(2)
    );
    MatcherAssert.assertThat(
        response.get("h.Content-Type"),
        new IsEqual<>("application/json")
    );
    MatcherAssert.assertThat(
        response.get("foo"),
        new IsEqual<>("bar")
    );
}
 
Example #4
Source File: TestCrawlerSessionManagerValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testCrawlersSessionIdIsRemovedAfterSessionExpiry() throws IOException, ServletException {
    CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
    valve.setCrawlerIps("216\\.58\\.206\\.174");
    valve.setCrawlerUserAgents(valve.getCrawlerUserAgents());
    valve.setNext(EasyMock.createMock(Valve.class));
    valve.setSessionInactiveInterval(0);
    StandardSession session = new StandardSession(TEST_MANAGER);
    session.setId("id");
    session.setValid(true);

    Request request = createRequestExpectations("216.58.206.174", session, true);

    EasyMock.replay(request);

    valve.invoke(request, EasyMock.createMock(Response.class));

    EasyMock.verify(request);

    MatcherAssert.assertThat(valve.getClientIpSessionId().values(), CoreMatchers.hasItem("id"));

    session.expire();

    Assert.assertEquals(0, valve.getClientIpSessionId().values().size());
}
 
Example #5
Source File: JoinedDictTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void joinsKvpWithDict() {
    final Dict dict = new JoinedDict(
        new KvpOf("first", "first"),
        new HashDict(
            new KvpOf("first", "second"),
            new KvpOf("foo", "bar")
        )
    );
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        //@checkstyle MagicNumberCheck (1 lines)
        new IsEqual<>(3)
    );
    MatcherAssert.assertThat(
        dict.get("first"),
        new IsEqual<>("second")
    );
    MatcherAssert.assertThat(
        dict.get("foo"),
        new IsEqual<>("bar")
    );
}
 
Example #6
Source File: HeadersOfTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void extractHeadersFromDict() {
    final List<Kvp> kvps = new ListOf<>(
        new Headers.Of(
            new HashDict(
                new KvpOf("test", "test"),
                new KvpOf("h.kfirst", "first"),
                new KvpOf("h.ksecond", "second")
            )
        )
    );
    MatcherAssert.assertThat(
        kvps.size(),
        new IsEqual<>(2)
    );
    MatcherAssert.assertThat(
        kvps.get(0).key(),
        new IsEqual<>("kfirst")
    );
    MatcherAssert.assertThat(
        kvps.get(1).key(),
        new IsEqual<>("ksecond")
    );
}
 
Example #7
Source File: JoinedDictTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void joinsDictionaries() {
    final Dict dict = new JoinedDict(
        new HashDict(
            new KvpOf("first", "first")
        ),
        new HashDict(
            new KvpOf("first", "second"),
            new KvpOf("foo", "bar")
        )
    );
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        //@checkstyle MagicNumberCheck (1 lines)
        new IsEqual<>(3)
    );
    MatcherAssert.assertThat(
        dict.get("first"),
        new IsEqual<>("second")
    );
    MatcherAssert.assertThat(
        dict.get("foo"),
        new IsEqual<>("bar")
    );
}
 
Example #8
Source File: PostTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsPostRequest() {
    final Dict dict = new Post("/items", new ContentType("text/html"));
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("POST")
    );
    MatcherAssert.assertThat(
        dict.get("path"),
        new IsEqual<>("/items")
    );
    MatcherAssert.assertThat(
        dict.get("h.Content-Type"),
        new IsEqual<>("text/html")
    );
}
 
Example #9
Source File: PostTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsPostRequestWithoutParameters() {
    final Dict dict = new Post("/items");
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(2)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("POST")
    );
    MatcherAssert.assertThat(
        dict.get("path"),
        new IsEqual<>("/items")
    );
}
 
Example #10
Source File: PostTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsEmptyPostRequest() {
    final Dict dict = new Post();
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(2)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("POST")
    );
    MatcherAssert.assertThat(
        dict.get("path").isEmpty(),
        new IsEqual<>(true)
    );
}
 
Example #11
Source File: PostTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsPostRequestWithEmptyPath() {
    final Dict dict = new Post(new ContentType("text/html"));
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(3)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("POST")
    );
    MatcherAssert.assertThat(
        dict.get("path").isEmpty(),
        new IsEqual<>(true)
    );
    MatcherAssert.assertThat(
        dict.get("h.Content-Type"),
        new IsEqual<>("text/html")
    );
}
 
Example #12
Source File: PutTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsPutRequestWithoutParameters() {
    final Dict dict = new Put("/items");
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(2)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("PUT")
    );
    MatcherAssert.assertThat(
        dict.get("path"),
        new IsEqual<>("/items")
    );
}
 
Example #13
Source File: TestSCMContainerPlacementRackAware.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoFallback() throws SCMException {
  assumeTrue(datanodeCount > (NODE_PER_RACK * 2) &&
      (datanodeCount <= NODE_PER_RACK * 3));
  // 5 replicas. there are only 3 racks. policy prohibit fallback should fail.
  int nodeNum = 5;
  try {
    policyNoFallback.chooseDatanodes(null, null, nodeNum, 15);
    fail("Fallback prohibited, this call should fail");
  } catch (Exception e) {
    assertEquals("SCMException", e.getClass().getSimpleName());
  }

  // get metrics
  long totalRequest = metrics.getDatanodeRequestCount();
  long successCount = metrics.getDatanodeChooseSuccessCount();
  long tryCount = metrics.getDatanodeChooseAttemptCount();
  long compromiseCount = metrics.getDatanodeChooseFallbackCount();

  Assert.assertEquals(totalRequest, nodeNum);
  MatcherAssert.assertThat("Not enough success count", successCount,
      greaterThanOrEqualTo(1L));
  MatcherAssert.assertThat("Not enough try count", tryCount,
      greaterThanOrEqualTo((long) 1L));
  Assert.assertEquals(compromiseCount, 0);
}
 
Example #14
Source File: GetTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsGetRequestWithEmptyPath() {
    final Dict dict = new Get(new ContentType("text/html"));
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(3)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("GET")
    );
    MatcherAssert.assertThat(
        dict.get("path").isEmpty(),
        new IsEqual<>(true)
    );
    MatcherAssert.assertThat(
        dict.get("h.Content-Type"),
        new IsEqual<>("text/html")
    );
}
 
Example #15
Source File: GetTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsGetRequestWithoutParameters() {
    final Dict dict = new Get("/items");
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(2)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("GET")
    );
    MatcherAssert.assertThat(
        dict.get("path"),
        new IsEqual<>("/items")
    );
}
 
Example #16
Source File: GetTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsGetRequest() {
    final Dict dict = new Get("/items", new ContentType("text/html"));
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("GET")
    );
    MatcherAssert.assertThat(
        dict.get("path"),
        new IsEqual<>("/items")
    );
    MatcherAssert.assertThat(
        dict.get("h.Content-Type"),
        new IsEqual<>("text/html")
    );
}
 
Example #17
Source File: PathTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void concatenatesPath() {
    final Dict dict = new Path("/items")
        .apply(
            new HashDict(
                new KvpOf("aaa", "test"),
                new KvpOf("path", "localhost")
            )
        );
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(3)
    );
    MatcherAssert.assertThat(
        dict.get("path"),
        new IsEqual<>("localhost/items")
    );
}
 
Example #18
Source File: DeleteTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsEmptyDeleteRequest() {
    final Dict dict = new Delete();
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(2)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("DELETE")
    );
    MatcherAssert.assertThat(
        dict.get("path").isEmpty(),
        new IsEqual<>(true)
    );
}
 
Example #19
Source File: PutTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsPutRequestWithEmptyPath() {
    final Dict dict = new Put(new ContentType("text/html"));
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(3)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("PUT")
    );
    MatcherAssert.assertThat(
        dict.get("path").isEmpty(),
        new IsEqual<>(true)
    );
    MatcherAssert.assertThat(
        dict.get("h.Content-Type"),
        new IsEqual<>("text/html")
    );
}
 
Example #20
Source File: PutTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsEmptyPutRequest() {
    final Dict dict = new Put();
    MatcherAssert.assertThat(
        new CollectionOf<>(dict).size(),
        new IsEqual<>(2)
    );
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("PUT")
    );
    MatcherAssert.assertThat(
        dict.get("path").isEmpty(),
        new IsEqual<>(true)
    );
}
 
Example #21
Source File: PutTest.java    From verano-http with MIT License 6 votes vote down vote up
@Test
public void buildsPutRequest() {
    final Dict dict = new Put("/items", new ContentType("text/html"));
    MatcherAssert.assertThat(
        dict.get("method"),
        new IsEqual<>("PUT")
    );
    MatcherAssert.assertThat(
        dict.get("path"),
        new IsEqual<>("/items")
    );
    MatcherAssert.assertThat(
        dict.get("h.Content-Type"),
        new IsEqual<>("text/html")
    );
}
 
Example #22
Source File: SingleThreadEventLoopTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000)
public void testOnEventLoopIteration() throws Exception {
    CountingRunnable onIteration = new CountingRunnable();
    loopC.executeAfterEventLoopIteration(onIteration);
    CountingRunnable noopTask = new CountingRunnable();
    loopC.submit(noopTask).sync();
    loopC.iterationEndSignal.take();
    MatcherAssert.assertThat("Unexpected invocation count for regular task.",
                             noopTask.getInvocationCount(), is(1));
    MatcherAssert.assertThat("Unexpected invocation count for on every eventloop iteration task.",
                             onIteration.getInvocationCount(), is(1));
}
 
Example #23
Source File: DefaultWebTestClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<R> matcher) {
	this.result.assertWithDiagnostics(() -> {
		B body = this.result.getResponseBody();
		MatcherAssert.assertThat(bodyMapper.apply(body), matcher);
	});
	return self();
}
 
Example #24
Source File: HeaderOfTest.java    From verano-http with MIT License 5 votes vote down vote up
@Test
public void extractsHeaderFromDict() {
    final String name = "John";
    MatcherAssert.assertThat(
        new Header.Of(
            "name",
            new HashDict(
                new KvpOf("h.Name", name),
                new KvpOf("surname", "Smith")
            )
        ).asString(),
        new IsEqual<>(name)
    );
}
 
Example #25
Source File: ResponseEntityTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void entityListWithConsumer() {

	this.client.get()
			.exchange()
			.expectStatus().isOk()
			.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
			.expectBodyList(Person.class).value(people -> {
				MatcherAssert.assertThat(people, hasItem(new Person("Jason")));
			});
}
 
Example #26
Source File: MethodTest.java    From verano-http with MIT License 5 votes vote down vote up
@Test
public void appliesMethodToRequest() {
    final String method = "GET";
    MatcherAssert.assertThat(
        new Method(method).apply(new HashDict()).get("method"),
        new IsEqual<>(method)
    );
}
 
Example #27
Source File: PersonControllerTest.java    From acelera-dev-brasil-2019-01 with Apache License 2.0 5 votes vote down vote up
@Test
public void deveRetornarABuscaPeloNome() {
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(getUrl())
            .queryParam("nome", "Val")
            .queryParam("altura", 190)
            .queryParam("foto", "url");

    ResponseEntity<List<Person>> entity = template.exchange(builder.toUriString(),
            HttpMethod.GET,
            new HttpEntity<>(headers),
            new ParameterizedTypeReference<List<Person>>() {
            });

    Assertions.assertEquals(HttpStatus.OK, entity.getStatusCode());

    List<Person> pessoas = entity.getBody();
    Assertions.assertEquals(1, pessoas.size());
    List<String> nomes = pessoas.stream().map(Person::getNome).collect(Collectors.toList());

    MatcherAssert.assertThat(nomes, Matchers.contains("Val"));


}
 
Example #28
Source File: AuthorizationTest.java    From verano-http with MIT License 5 votes vote down vote up
@Test
public void addsAuthorizationHeaderToRequest() {
    final String value = "basic";
    MatcherAssert.assertThat(
        new Authorization(value)
            .apply(new HashDict())
            .get("h.Authorization"),
        new IsEqual<>(value)
    );
}
 
Example #29
Source File: JsonBodyOfTest.java    From verano-http with MIT License 5 votes vote down vote up
@Test
public void extractsJsonReaderFromDict() {
    MatcherAssert.assertThat(
        new JsonBody.Of(
            new HashDict(
                new KvpOf("body", "{}"),
                new KvpOf("unknown", "")
            )
        ).reader(),
        new IsNot<>(new IsNull<>())
    );
}
 
Example #30
Source File: DefaultWebTestClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<R> matcher) {
	this.result.assertWithDiagnostics(() -> {
		B body = this.result.getResponseBody();
		MatcherAssert.assertThat(bodyMapper.apply(body), matcher);
	});
	return self();
}