public SELF hasJsonBody(Object expected) throws UnsupportedEncodingException { JsonFluentAssert.assertThatJson(actual.getContentAsString()).isEqualTo(expected); return myself; }
private void assertJsonResponseBody(String body) { assertThatJson(body).isEqualTo("{" + "\"bookingId\":\"1\"," + "\"origin\":\"London\"," + "\"destination\":\"Singapore\"," + "\"time\":\"2011-09-01T12:30\"," + "\"_links\":{\"self\":{\"href\":\"http://localhost/api/bookings/1\"}}" + "}"); } }
@Test void testMoreNodes() { assertThatThrownBy(() -> assertThatJson("{\"test1\":2, \"test2\":1}").node("test1").isEqualTo(2).node("test2").isEqualTo(2)) .hasMessage("JSON documents are different:\nDifferent value found in node \"test2\", expected: <2> but was: <1>.\n"); }
@Test void shouldChainStringEquals() { assertThatJson("{ \"key1\": \"[value1]\", \"key2\": \"[value2]\" }") .node("key1").isStringEqualTo("[value1]") .node("key2").isStringEqualTo("[value2]"); }
@Test public void shouldBeAbleToGetABookingUsingHttps() { //Given hoverflyRule.simulate(classpath("test-service.json")); // When final ResponseEntity<String> getBookingResponse = restTemplate.getForEntity("https://www.my-test.com/api/bookings/1", String.class); // Then assertThat(getBookingResponse.getStatusCode()).isEqualTo(OK); assertThatJson(getBookingResponse.getBody()).isEqualTo("{" + "\"bookingId\":\"1\"," + "\"origin\":\"London\"," + "\"destination\":\"Singapore\"," + "\"time\":\"2011-09-01T12:30\"," + "\"_links\":{\"self\":{\"href\":\"http://localhost/api/bookings/1\"}}" + "}"); }
@Test void shouldImportSimulationFromDefaultLocation() throws IOException { final Request request = new Request.Builder() .url("http://www.my-test.com/api/bookings/1") .header("Content-Type", "text/plain; charset=utf-8") .build(); final Response response = client.newCall(request).execute(); String body = response.body().string(); assertThatJson(body).node("bookingId").isEqualTo("\"1\""); }
@Test void shouldImportSimulationFromCustomSource() throws IOException { final Request request = new Request.Builder().url("https://www.my-test.com/api/bookings/1") .build(); final Response response = client.newCall(request).execute(); assertThatJson(response.body().string()).node("bookingId").isStringEqualTo("1"); }
public SELF hasJsonAttribute(String attribute, Object object) throws UnsupportedEncodingException { JsonFluentAssert.assertThatJson(actual.getContentAsString()).node(attribute).isEqualTo(object); return myself; }
@Test public void shouldWorkWithJerseyClient() { // Given WebResource resource = jerseyClient.resource("https://www.my-test.com/api/bookings/1"); // When ClientResponse response = resource.get(ClientResponse.class); // Then assertThat(response.getStatus()).isEqualTo(OK.value()); assertThatJson(response.getEntity(String.class)).isEqualTo("{" + "\"bookingId\":\"1\"," + "\"origin\":\"London\"," + "\"destination\":\"Singapore\"," + "\"time\":\"2011-09-01T12:30\"," + "\"_links\":{\"self\":{\"href\":\"http://localhost/api/bookings/1\"}}" + "}"); }
@Test void shouldBeAbleToSimulateABookingQuery() throws Exception { final Request request = new Request.Builder() .url("http://www.my-test.com/api/bookings/1") .header("Content-Type", "text/plain; charset=utf-8") .build(); final Response response = client.newCall(request).execute(); assertThat(response.code()).isEqualTo(200); String body = response.body().string(); assertThatJson(body).node("id").isEqualTo(1); }
public SELF hasJsonMessage(String message) throws UnsupportedEncodingException { JsonFluentAssert.assertThatJson(actual.getContentAsString()).node("message").isEqualTo(message); return myself; }
@Test public void shouldSerializeToJson() throws JsonProcessingException { ObjectMapper objectMapper = getObjectMapper(); Tweet tweet = Tweet.builder() .author("@artem_zin") .content("Immutability for everybody!") .build(); assertThatJson(objectMapper.writeValueAsString(tweet)) .isEqualTo("{\"author\":\"@artem_zin\",\"content\":\"Immutability for everybody!\"}"); }
@Test public void shouldConstructValidationRequest() { HashMap<String, String> configuration = new HashMap<>(); configuration.put("key1", "value1"); configuration.put("key2", "value2"); configuration.put("key3", null); String requestBody = new ElasticAgentExtensionConverterV4().validateElasticProfileRequestBody(configuration); assertThatJson(requestBody).isEqualTo("{\"key3\":null,\"key2\":\"value2\",\"key1\":\"value1\"}"); }
@Test public void shouldBeAbleToQueryBookingsUsingHoverfly() { // When URI uri = UriComponentsBuilder.fromHttpUrl("http://www.other-anotherservice.com") .path("/api/bookings") .queryParam("class", "business", "premium") .queryParam("destination", "new york") .build() .toUri(); final ResponseEntity<String> getBookingResponse = restTemplate.getForEntity(uri, String.class); // Then assertThat(getBookingResponse.getStatusCode()).isEqualTo(OK); assertThatJson(getBookingResponse.getBody()).isEqualTo("{" + "\"bookingId\":\"2\"," + "\"origin\":\"London\"," + "\"destination\":\"New York\"," + "\"class\":\"BUSINESS\"," + "\"time\":\"2011-09-01T12:30\"," + "\"_links\":{\"self\":{\"href\":\"http://localhost/api/bookings/2\"}}" + "}"); }
@Test public void shouldConstructValidationRequest() { HashMap<String, String> configuration = new HashMap<>(); configuration.put("key1", "value1"); configuration.put("key2", "value2"); configuration.put("key3", null); String requestBody = new ElasticAgentExtensionConverterV3().validateElasticProfileRequestBody(configuration); assertThatJson(requestBody).isEqualTo("{\"key3\":null,\"key2\":\"value2\",\"key1\":\"value1\"}"); }
@Test public void shouldConvertToJsonWithoutMetadata() throws IOException { // given Topic topic = topic("group.topic").build(); AvroUser avroUser = new AvroUser("Bob", 18, "blue"); Message source = message() .withData(avroUser.asBytes()) .withSchema(new CompiledSchema<>(avroUser.getSchema(), SchemaVersion.valueOf(0))) .withExternalMetadata(of()) .build(); AvroToJsonMessageConverter converter = new AvroToJsonMessageConverter(); // when Message target = converter.convert(source, topic); // then assertThatJson(new String(target.getData())).isEqualTo("{\"name\": \"Bob\", \"age\": 18, \"favoriteColor\": \"blue\"}"); }
@Test public void shouldConstructAgentNotificationRequestMessage() throws Exception { Date transition_time = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN_FOR_V3); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); String time = simpleDateFormat.format(transition_time); AgentNotificationData agentNotificationData = new AgentNotificationData("agent_uuid", "agent_hostname", true, "127.0.0.1", "rh", "100", "enabled", "building", "building", transition_time); String expected = "{\n" + " \"agent_config_state\": \"enabled\",\n" + " \"agent_state\": \"building\",\n" + " \"build_state\": \"building\",\n" + " \"is_elastic\": true,\n" + " \"free_space\": \"100\",\n" + " \"host_name\": \"agent_hostname\",\n" + " \"ip_address\": \"127.0.0.1\",\n" + " \"operating_system\": \"rh\",\n" + " \"uuid\": \"agent_uuid\",\n" + " \"transition_time\": \""+ time + "\"\n" + "}\n"; String message = messageHandler.requestMessageForNotify(agentNotificationData); JsonFluentAssert.assertThatJson(expected).isEqualTo(message); }
@Test public void shouldConstructAgentNotificationRequestMessage() throws Exception { Date transition_time = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN_FOR_V3); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); String time = simpleDateFormat.format(transition_time); AgentNotificationData agentNotificationData = new AgentNotificationData("agent_uuid", "agent_hostname", true, "127.0.0.1", "rh", "100", "enabled", "building", "building", transition_time); String expected = "{\n" + " \"agent_config_state\": \"enabled\",\n" + " \"agent_state\": \"building\",\n" + " \"build_state\": \"building\",\n" + " \"is_elastic\": true,\n" + " \"free_space\": \"100\",\n" + " \"host_name\": \"agent_hostname\",\n" + " \"ip_address\": \"127.0.0.1\",\n" + " \"operating_system\": \"rh\",\n" + " \"uuid\": \"agent_uuid\",\n" + " \"transition_time\": \""+ time + "\"\n" + "}\n"; String message = messageHandler.requestMessageForNotify(agentNotificationData); JsonFluentAssert.assertThatJson(expected).isEqualTo(message); }
@Test public void validateConfigurationRequestBody_shouldSerializeConfigurationToJson() { final ArtifactMessageConverterV1 converter = new ArtifactMessageConverterV1(); final String requestBody = converter.validateConfigurationRequestBody(Collections.singletonMap("Foo", "Bar")); assertThatJson("{\"Foo\":\"Bar\"}").isEqualTo(requestBody); }
@Test public void shouldJSONizeElasticAgentStatusReportRequestBodyWhenElasticAgentIdIsProvided() throws Exception { String elasticAgentId = "my-fancy-elastic-agent-id"; String actual = new ElasticAgentExtensionConverterV3().getAgentStatusReportRequestBody(null, elasticAgentId); String expected = format("{" + " \"elastic_agent_id\": \"%s\"" + "}", elasticAgentId); assertThatJson(expected).isEqualTo(actual); }