prot.writeMessageEnd(); assertThatJson(new String(outputStream.toByteArray(), StandardCharsets.UTF_8)).isEqualTo(request);
@Test public void testMessageConverterService() { AggregatedHttpMessage res; String body; // JSON for (final String path : Arrays.asList("/messageConverter/node/node", "/messageConverter/node/obj", "/messageConverter/obj/obj", "/messageConverter/obj/future")) { res = client.execute(HttpHeaders.of(HttpMethod.POST, path) .contentType(MediaType.JSON_UTF_8), "{\"name\":\"armeria\"}").aggregate().join(); assertThat(res.status()).isEqualTo(HttpStatus.OK); assertThat(res.headers().contentType().is(MediaType.JSON_UTF_8)).isTrue(); body = res.content().toStringUtf8(); assertThatJson(body).node("result").isStringEqualTo("success"); assertThatJson(body).node("from").isStringEqualTo("armeria"); } // custom(text protocol) res = client.execute(HttpHeaders.of(HttpMethod.POST, "/messageConverter/custom") .contentType(MediaType.PLAIN_TEXT_UTF_8), "armeria").aggregate().join(); assertThat(res.status()).isEqualTo(HttpStatus.OK); assertThat(res.headers().contentType().is(MediaType.PLAIN_TEXT_UTF_8)).isTrue(); assertThat(res.content().toStringUtf8()).isEqualTo("success:armeria"); }
@Test public void testInjectionService() { AggregatedHttpMessage res; res = client.get("/injection/param/armeria/1?gender=male").aggregate().join(); assertThat(res.status()).isEqualTo(HttpStatus.OK); assertThatJson(res.content().toStringUtf8()).isArray() .ofLength(3) .thatContains("armeria") .thatContains(1) .thatContains("MALE"); final HttpHeaders headers = HttpHeaders.of(HttpMethod.GET, "/injection/header") .add(HttpHeaderNames.of("x-armeria-text"), "armeria") .add(HttpHeaderNames.of("x-armeria-sequence"), "1") .add(HttpHeaderNames.of("x-armeria-sequence"), "2") .add(HttpHeaderNames.COOKIE, "a=1") .add(HttpHeaderNames.COOKIE, "b=1"); res = client.execute(headers).aggregate().join(); assertThat(res.status()).isEqualTo(HttpStatus.OK); assertThatJson(res.content().toStringUtf8()).isArray() .ofLength(3) .thatContains("armeria") .thatContains(Arrays.asList(1, 2)) .thatContains(Arrays.asList("a", "b")); }
@Test void testContains() { assertThatJson("[\"foo\", \"bar\"]").isArray().ofLength(2).thatContains("foo").thatContains("bar"); }
@Test void testAssertToleranceDirect() { assertThatJson("{\"test\":1.00001}").withTolerance(0.001).isEqualTo("{\"test\":1}"); }
@Test void isStringEqualToShouldPass() { assertThatJson("{\"test\":\"1\"}").node("test").isStringEqualTo("1"); }
@Test void shoulEscapeDot() { assertThatJson("{\"name.with.dot\": \"value\"}").node("name\\.with\\.dot").isStringEqualTo("value"); }
@Test void testAssertTolerance() { assertThatJson("{\"test\":1.00001}").node("test").withTolerance(0.001).isEqualTo(1); }
@Test void testAssertToleranceDifferentOrder() { assertThatJson("{\"test\":1.00001}").withTolerance(0.001).node("test").isEqualTo(1); }
/** * Sets the tolerance for floating number comparison. If set to null, requires exact match of the values. * For example, if set to 0.01, ignores all differences lower than 0.01, so 1 and 0.9999 are considered equal. * * @param tolerance */ public ConfigurableJsonFluentAssert withTolerance(double tolerance) { return withTolerance(BigDecimal.valueOf(tolerance)); }
/** * Sets the tolerance for floating number comparison. If set to null, requires exact match of the values. * For example, if set to 0.01, ignores all differences lower than 0.01, so 1 and 0.9999 are considered equal. * * @param tolerance */ public ConfigurableJsonFluentAssert withTolerance(double tolerance) { return withTolerance(BigDecimal.valueOf(tolerance)); }
@Test void testLogging() { assertThatJson("[\"foo\", \"bar\"]") .isArray() .ofLength(2) .thatContains("foo") .thatContains("bar"); }
@Test void testNotEqualsToToArray() { assertThatThrownBy(() -> assertThatJson("{\"test\":[1,2,3]}").node("test").isNotEqualTo(new int[]{1, 2, 3})) .hasMessage("JSON is equal."); }