From 7df5cc49712f01f89e9fa90e3548369ce7a75801 Mon Sep 17 00:00:00 2001 From: Andrea Montemaggio Date: Tue, 1 Dec 2020 19:51:56 -0600 Subject: [PATCH] Failing tests: fixed assertions on indexed content. Fixed handling of String and Map payloads while building IndexRequest. The wrong overload of IndexRequest.source was called, which caused the message payload to be used as the key of the two-fields document {: "JSON"}. --- .../ElasticsearchConsumerConfiguration.java | 7 ++++-- ...ElasticsearchConsumerApplicationTests.java | 22 +++++++------------ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/functions/consumer/elasticsearch-consumer/src/main/java/org/springframework/cloud/fn/consumer/elasticsearch/ElasticsearchConsumerConfiguration.java b/functions/consumer/elasticsearch-consumer/src/main/java/org/springframework/cloud/fn/consumer/elasticsearch/ElasticsearchConsumerConfiguration.java index 1e010a5c..10d490ea 100644 --- a/functions/consumer/elasticsearch-consumer/src/main/java/org/springframework/cloud/fn/consumer/elasticsearch/ElasticsearchConsumerConfiguration.java +++ b/functions/consumer/elasticsearch-consumer/src/main/java/org/springframework/cloud/fn/consumer/elasticsearch/ElasticsearchConsumerConfiguration.java @@ -64,8 +64,11 @@ public class ElasticsearchConsumerConfiguration { } request.id(id); - if (message.getPayload() instanceof String || message.getPayload() instanceof Map) { - request.source(message.getPayload(), XContentType.JSON); + if (message.getPayload() instanceof String) { + request.source((String) message.getPayload(), XContentType.JSON); + } + else if (message.getPayload() instanceof Map) { + request.source((Map) message.getPayload(), XContentType.JSON); } else if (message.getPayload() instanceof XContentBuilder) { request.source((XContentBuilder) message.getPayload()); diff --git a/functions/consumer/elasticsearch-consumer/src/test/java/org/springframework/cloud/fn/consumer/elasticsearch/ElasticsearchConsumerApplicationTests.java b/functions/consumer/elasticsearch-consumer/src/test/java/org/springframework/cloud/fn/consumer/elasticsearch/ElasticsearchConsumerApplicationTests.java index 3e6af441..f415345c 100644 --- a/functions/consumer/elasticsearch-consumer/src/test/java/org/springframework/cloud/fn/consumer/elasticsearch/ElasticsearchConsumerApplicationTests.java +++ b/functions/consumer/elasticsearch-consumer/src/test/java/org/springframework/cloud/fn/consumer/elasticsearch/ElasticsearchConsumerApplicationTests.java @@ -19,7 +19,6 @@ package org.springframework.cloud.fn.consumer.elasticsearch; import java.time.Duration; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.function.Consumer; import org.awaitility.Awaitility; @@ -28,6 +27,7 @@ import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.junit.jupiter.api.Test; @@ -70,9 +70,9 @@ public class ElasticsearchConsumerApplicationTests { RestHighLevelClient restHighLevelClient = context.getBean(RestHighLevelClient.class); GetRequest getRequest = new GetRequest("foo").id("1"); final GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); - assertThat(response.isExists()).isTrue(); - assertThat(response.getSource().containsKey(jsonObject)).isTrue(); + assertThat(response.isExists()).isTrue(); + assertThat(response.getSourceAsString()).isEqualTo(jsonObject); }); } @@ -95,7 +95,7 @@ public class ElasticsearchConsumerApplicationTests { GetRequest getRequest = new GetRequest("foo").id("2"); final GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); assertThat(response.isExists()).isTrue(); - assertThat(response.getSource().containsKey(jsonObject)).isTrue(); + assertThat(response.getSourceAsString()).isEqualTo(jsonObject); assertThat(response.getId()).isEqualTo("2"); }); } @@ -118,13 +118,11 @@ public class ElasticsearchConsumerApplicationTests { RestHighLevelClient restHighLevelClient = context.getBean(RestHighLevelClient.class); GetRequest getRequest = new GetRequest("foo").id("3"); + final GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); + assertThat(response.isExists()).isTrue(); - final Optional data = response.getSource().keySet().stream().findFirst(); - assertThat(data.isPresent()).isTrue(); - assertThat(data.get().contains("age=10")).isTrue(); - assertThat(data.get().contains("dateOfBirth=1471466076564")).isTrue(); - assertThat(data.get().contains("fullName=John Doe")).isTrue(); + assertThat(response.getSource()).containsAllEntriesOf(jsonMap); assertThat(response.getId()).isEqualTo("3"); }); } @@ -153,11 +151,7 @@ public class ElasticsearchConsumerApplicationTests { final GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); assertThat(response.isExists()).isTrue(); - final Map source = response.getSource(); - assertThat(source.size()).isEqualTo(3); - assertThat(source.containsKey("user")).isTrue(); - assertThat(source.get("user")).isEqualTo("kimchy"); - + assertThat(response.getSourceAsString()).isEqualTo(Strings.toString(builder)); }); }